use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class FakeComputingResource method execute.
protected synchronized StartAnswer execute(StartCommand cmd) {
VmMgr vmMgr = getVmManager();
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
String vmName = vmSpec.getName();
State state = State.Stopped;
try {
if (!_vms.containsKey(vmName)) {
synchronized (_vms) {
_vms.put(vmName, State.Starting);
}
MockVm vm = vmMgr.createVmFromSpec(vmSpec);
vmMgr.createVbd(vmSpec, vmName, vm);
vmMgr.createVif(vmSpec, vmName, vm);
state = State.Running;
for (NicTO nic : cmd.getVirtualMachine().getNics()) {
if (nic.getType() == TrafficType.Guest) {
InetAddress addr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
nic.setIp(addr.getHostAddress());
}
}
_vmDataServer.handleVmStarted(cmd.getVirtualMachine());
return new StartAnswer(cmd);
} else {
String msg = "There is already a VM having the same name " + vmName;
s_logger.warn(msg);
return new StartAnswer(cmd, msg);
}
} catch (Exception ex) {
} finally {
synchronized (_vms) {
_vms.put(vmName, state);
}
}
return new StartAnswer(cmd);
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class FakeComputingResource method execute.
protected CheckVirtualMachineAnswer execute(final CheckVirtualMachineCommand cmd) {
VmMgr vmMgr = getVmManager();
final String vmName = cmd.getVmName();
final State state = vmMgr.checkVmState(vmName);
Integer vncPort = null;
if (state == State.Running) {
vncPort = vmMgr.getVncPort(vmName);
synchronized (_vms) {
_vms.put(vmName, State.Running);
}
}
return new CheckVirtualMachineAnswer(cmd, state, vncPort);
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected synchronized StartAnswer execute(StartCommand cmd) {
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
String vmName = vmSpec.getName();
LibvirtVMDef vm = null;
State state = State.Stopped;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
synchronized (_vms) {
_vms.put(vmName, State.Starting);
}
vm = createVMFromSpec(vmSpec);
createVbd(conn, vmSpec, vmName, vm);
createVifs(conn, vmSpec, vm);
s_logger.debug("starting " + vmName + ": " + vm.toString());
startDomain(conn, vmName, vm.toString());
Script.runSimpleBashScript("virsh schedinfo " + vmName + " --set cpu_shares=" + vmSpec.getCpus() * vmSpec.getSpeed());
NicTO[] nics = vmSpec.getNics();
for (NicTO nic : nics) {
if (nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
if (vmSpec.getType() != VirtualMachine.Type.User) {
default_network_rules_for_systemvm(conn, vmName);
break;
} else {
default_network_rules(conn, vmName, nic, vmSpec.getId());
}
}
}
state = State.Running;
return new StartAnswer(cmd);
} catch (Exception e) {
s_logger.warn("Exception ", e);
if (conn != null) {
handleVmStartFailure(conn, vmName, vm);
}
return new StartAnswer(cmd, e.getMessage());
} finally {
synchronized (_vms) {
if (state != State.Stopped) {
_vms.put(vmName, state);
} else {
_vms.remove(vmName);
}
}
}
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class Ovm3HypervisorResource method execute.
@Override
public synchronized StartAnswer execute(StartCommand cmd) {
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
String vmName = vmSpec.getName();
State state = State.Stopped;
Xen xen = new Xen(c);
try {
hypervisorsupport.setVmStateStarting(vmName);
Xen.Vm vm = xen.getVmConfig();
/* max and min ? */
vm.setVmCpus(vmSpec.getCpus());
/* in mb not in bytes */
vm.setVmMemory(vmSpec.getMinRam() / 1024 / 1024);
vm.setVmUuid(UUID.nameUUIDFromBytes(vmSpec.getName().getBytes(Charset.defaultCharset())).toString());
vm.setVmName(vmName);
String domType = guesttypes.getOvm3GuestType(vmSpec.getOs());
if (domType == null || domType.isEmpty()) {
domType = "default";
LOGGER.debug("VM Virt type missing setting to: " + domType);
} else {
LOGGER.debug("VM Virt type set to " + domType + " for " + vmSpec.getOs());
}
vm.setVmDomainType(domType);
if (vmSpec.getBootloader() == BootloaderType.CD) {
LOGGER.warn("CD booting is not supported");
}
/*
* officially CD boot is only supported on HVM, although there is a
* simple way around it..
*/
vmsupport.createVbds(vm, vmSpec);
if (vmSpec.getType() != VirtualMachine.Type.User) {
// double check control network if we run a non user VM
hypervisornetwork.configureNetworking();
vm.setVmExtra(vmSpec.getBootArgs().replace(" ", "%"));
String svmPath = configuration.getAgentOvmRepoPath() + "/" + ovmObject.deDash(vm.getPrimaryPoolUuid()) + "/ISOs";
String svmIso = svmPath + "/" + storagepool.getSystemVMPatchIsoFile().getName();
vm.addIso(svmIso);
}
/* OVS/Network stuff should go here! */
vmsupport.createVifs(vm, vmSpec);
vm.setupVifs();
vm.setVnc("0.0.0.0", vmSpec.getVncPassword());
xen.createVm(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid());
xen.startVm(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid());
state = State.Running;
if (vmSpec.getType() != VirtualMachine.Type.User) {
String controlIp = null;
for (NicTO nic : vmSpec.getNics()) {
if (nic.getType() == TrafficType.Control) {
controlIp = nic.getIp();
}
}
/* fix is in cloudstack.py for xend restart timer */
for (int count = 0; count < 60; count++) {
CloudstackPlugin cSp = new CloudstackPlugin(c);
/* skip a beat to make sure we didn't miss start */
if (hypervisorsupport.getVmState(vmName) == null && count > 1) {
String msg = "VM " + vmName + " went missing on " + configuration.getAgentHostname() + ", returning stopped";
LOGGER.debug(msg);
state = State.Stopped;
return new StartAnswer(cmd, msg);
}
/* creative fix? */
try {
Boolean res = cSp.domrCheckSsh(controlIp);
LOGGER.debug("connected to " + controlIp + " on attempt " + count + " result: " + res);
if (res) {
break;
}
} catch (Exception x) {
LOGGER.trace("unable to connect to " + controlIp + " on attempt " + count + " " + x.getMessage(), x);
}
Thread.sleep(5000);
}
}
/*
* Can't remember if HA worked if we were only a pool ?
*/
if (configuration.getAgentInOvm3Pool() && configuration.getAgentInOvm3Cluster()) {
xen.configureVmHa(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid(), true);
}
/* should be starting no ? */
state = State.Running;
return new StartAnswer(cmd);
} catch (Exception e) {
LOGGER.debug("Start vm " + vmName + " failed", e);
state = State.Stopped;
return new StartAnswer(cmd, e.getMessage());
} finally {
hypervisorsupport.setVmState(vmName, state);
}
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class Ovm3HypervisorResource method execute.
/**
* Removes the vm and its configuration from the hypervisor.
*/
@Override
public StopAnswer execute(StopCommand cmd) {
String vmName = cmd.getVmName();
State state = State.Error;
hypervisorsupport.setVmState(vmName, State.Stopping);
try {
Xen vms = new Xen(c);
Xen.Vm vm = null;
vm = vms.getRunningVmConfig(vmName);
if (vm == null) {
state = State.Stopping;
LOGGER.debug("Unable to get details of vm: " + vmName + ", treating it as Stopping");
return new StopAnswer(cmd, "success", true);
}
String repoId = ovmObject.deDash(vm.getVmRootDiskPoolId());
String vmId = vm.getVmUuid();
/* can we do without the poolId ? */
vms.stopVm(repoId, vmId);
int tries = 30;
while (vms.getRunningVmConfig(vmName) != null && tries > 0) {
String msg = "Waiting for " + vmName + " to stop";
LOGGER.debug(msg);
tries--;
Thread.sleep(10 * 1000);
}
vms.deleteVm(repoId, vmId);
vmsupport.cleanup(vm);
if (vms.getRunningVmConfig(vmName) != null) {
String msg = "Stop " + vmName + " failed ";
LOGGER.debug(msg);
return new StopAnswer(cmd, msg, false);
}
state = State.Stopped;
return new StopAnswer(cmd, "success", true);
} catch (Exception e) {
LOGGER.debug("Stop " + vmName + " failed ", e);
return new StopAnswer(cmd, e.getMessage(), false);
} finally {
if (state != null) {
hypervisorsupport.setVmState(vmName, state);
} else {
hypervisorsupport.revmoveVmState(vmName);
}
}
}
Aggregations