199 lines
5.4 KiB
Java
199 lines
5.4 KiB
Java
|
package org.gcube.vremanagement.vremodeler.impl.deploy;
|
||
|
|
||
|
import java.sql.ResultSet;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.Collections;
|
||
|
import java.util.List;
|
||
|
import net.java.dev.jaxb.array.StringArray;
|
||
|
import org.apache.axis.client.Call;
|
||
|
import org.apache.axis.client.Stub;
|
||
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
||
|
import org.gcube.vremanagement.vremodeler.db.DBInterface;
|
||
|
import org.gcube.vremanagement.vremodeler.impl.ServiceContext;
|
||
|
import org.gcube.vremanagement.vremodeler.stubs.deployreport.GHNonCloudReport;
|
||
|
import org.gcube.vremanagement.vremodeler.stubs.deployreport.State;
|
||
|
import org.uoa.eolus.Eolus;
|
||
|
import org.uoa.eolus.EolusServiceLocator;
|
||
|
|
||
|
|
||
|
public class DeployGHNsOnCloud{
|
||
|
|
||
|
private static GCUBELog logger= new GCUBELog(DeployGHNsOnCloud.class);
|
||
|
|
||
|
int numberOfVMs;
|
||
|
private String vreName;
|
||
|
private GHNonCloudReport report;
|
||
|
|
||
|
public DeployGHNsOnCloud(int numberOfVMs, String vreName) {
|
||
|
super();
|
||
|
this.numberOfVMs = numberOfVMs;
|
||
|
this.vreName= vreName;
|
||
|
this.report= new GHNonCloudReport();
|
||
|
this.report.setState(State.Running);
|
||
|
}
|
||
|
|
||
|
public GHNonCloudReport getReport() {
|
||
|
return report;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public GHNstoUse run() throws Exception{
|
||
|
|
||
|
String username = "gcube";
|
||
|
String password = "gcube.cloud.2010";
|
||
|
|
||
|
EolusServiceLocator servicelocator = new EolusServiceLocator();
|
||
|
Eolus eolus = servicelocator.getEolusPort();
|
||
|
|
||
|
((Stub) eolus)._setProperty(Call.USERNAME_PROPERTY, username);
|
||
|
((Stub) eolus)._setProperty(Call.PASSWORD_PROPERTY, password);
|
||
|
|
||
|
//filling the report
|
||
|
List<State> ghnsStates=new ArrayList<State>(numberOfVMs);
|
||
|
Collections.fill(ghnsStates, State.Running);
|
||
|
this.report.setDeployingState(ghnsStates);
|
||
|
|
||
|
VM[] hostnames=createVMs(eolus);
|
||
|
configureAndStartContainers(eolus, hostnames);
|
||
|
|
||
|
//waiting few seconds for GHN registration
|
||
|
Thread.sleep(30000);
|
||
|
checkGHNAvailability(hostnames);
|
||
|
|
||
|
//now we can be sure that the GHNs are registered on the IS
|
||
|
//and the return type can be created
|
||
|
GHNstoUse ghnToUse= new GHNstoUse();
|
||
|
ghnToUse.setCandidateForRM(hostnames[0].getGhnId());
|
||
|
List<String> ghns= new ArrayList<String>(hostnames.length-1);
|
||
|
for (int i=1; i<hostnames.length; i++)
|
||
|
ghns.add(hostnames[i].getGhnId());
|
||
|
ghnToUse.setGhns(ghns);
|
||
|
return ghnToUse;
|
||
|
}
|
||
|
|
||
|
private VM[] createVMs(Eolus eolus) throws Exception{
|
||
|
String[] nets = {"public"};
|
||
|
StringArray vnets = new StringArray();
|
||
|
vnets.setItem(nets);
|
||
|
|
||
|
//TODO: change this code
|
||
|
String template=eolus.getTemplates().getItem()[0];
|
||
|
VM[] vmsNames= new VM[numberOfVMs];
|
||
|
//the first VM will be set with limited resource (1Gb of ram) for the RM
|
||
|
eolus.createVM(template, vreName+"ResourceManager", 2, 1024, vnets);
|
||
|
vmsNames[0]= new VM(vreName+"ResourceManager");
|
||
|
|
||
|
//the others VMs will be created with 2 GB of ram
|
||
|
for (int i=1; i<numberOfVMs; i++){
|
||
|
eolus.createVM(template, vreName+i, 2, 2048, vnets);
|
||
|
vmsNames[i]= new VM(vreName+"ResourceManager");
|
||
|
}
|
||
|
|
||
|
//waiting few seconds
|
||
|
Thread.sleep(10000);
|
||
|
|
||
|
//check if the VMs are ready
|
||
|
boolean[] arrayCheck= new boolean[numberOfVMs];
|
||
|
Arrays.fill(arrayCheck, false);
|
||
|
while (!and(arrayCheck)){
|
||
|
//TODO: this cycle cannot continue forever
|
||
|
for (int i=0; i<numberOfVMs; i++){
|
||
|
try{
|
||
|
if (arrayCheck[i]) continue;
|
||
|
if(eolus.getVMStatus(vmsNames[i].getName()).equalsIgnoreCase("running")){
|
||
|
arrayCheck[i]=true;
|
||
|
vmsNames[i].setIp(eolus.getVMIP(vmsNames[i].getName()));
|
||
|
}
|
||
|
}catch (Exception e) {
|
||
|
//if one fails i cannot continue
|
||
|
logger.error("error deploying "+vmsNames[i].getName());
|
||
|
report.getDeployingState().set(i, State.Failed);
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return vmsNames;
|
||
|
}
|
||
|
|
||
|
private void configureAndStartContainers(Eolus eolus, VM[] hostnames) throws Exception {
|
||
|
String[] scopes=ServiceContext.getContext().getScope().toString().split("/");
|
||
|
String cmdtorun = "configureGHN.sh "+scopes[1]+" "+scopes[2];
|
||
|
String[] res;
|
||
|
for (int i=1; i<hostnames.length; i++){
|
||
|
res = eolus.execCMD(cmdtorun, hostnames[i].getName()).getItem();
|
||
|
if (res.length > 2)
|
||
|
this.report.getDeployingState().set(i, State.Failed);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void checkGHNAvailability(VM[] hostnames) throws Exception{
|
||
|
boolean[] arrayCheck= new boolean[hostnames.length];
|
||
|
Arrays.fill(arrayCheck, false);
|
||
|
while (!and(arrayCheck)){
|
||
|
ResultSet queryRes;
|
||
|
for (int i=0; i<hostnames.length; i++){
|
||
|
if (arrayCheck[i]) continue;
|
||
|
queryRes= DBInterface.queryDB("select id from ghn where host='"+hostnames[i].getIp()+"'");
|
||
|
if (queryRes.next()) {
|
||
|
hostnames[i].setGhnId(queryRes.getString(1));
|
||
|
arrayCheck[i]=true;
|
||
|
this.report.getDeployingState().set(i, State.Finished);
|
||
|
DBInterface.ExecuteUpdate("update GHN set isoncloud='true' where id='"+hostnames[i].getGhnId()+"'");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private boolean and(boolean ... array){
|
||
|
for (int i=0; i<array.length; i++)
|
||
|
if(!array[i]) return false;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @author lucio
|
||
|
*
|
||
|
*/
|
||
|
public class VM{
|
||
|
private String ip;
|
||
|
private String name;
|
||
|
private String ghnId;
|
||
|
|
||
|
public VM(String name) {
|
||
|
super();
|
||
|
this.ip=null;
|
||
|
this.ghnId=null;
|
||
|
this.name = name;
|
||
|
}
|
||
|
public String getIp() {
|
||
|
return ip;
|
||
|
}
|
||
|
public void setIp(String ip) {
|
||
|
this.ip = ip;
|
||
|
}
|
||
|
public String getName() {
|
||
|
return name;
|
||
|
}
|
||
|
public void setName(String name) {
|
||
|
this.name = name;
|
||
|
}
|
||
|
public String getGhnId() {
|
||
|
return ghnId;
|
||
|
}
|
||
|
public void setGhnId(String ghnId) {
|
||
|
this.ghnId = ghnId;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|