use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected Answer execute(StopCommand cmd) {
final String vmName = cmd.getVmName();
Long bytesReceived = new Long(0);
Long bytesSent = new Long(0);
State state = null;
synchronized (_vms) {
state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
try {
Connect conn = LibvirtConnection.getConnection();
List<DiskDef> disks = getDisks(conn, vmName);
destroy_network_rules_for_vm(conn, vmName);
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
if (result == null) {
for (DiskDef disk : disks) {
if (disk.getDeviceType() == DiskDef.deviceType.CDROM && disk.getDiskPath() != null)
cleanupDisk(conn, disk);
}
}
final String result2 = cleanupVnet(conn, cmd.getVnet());
if (result != null && result2 != null) {
result = result2 + result;
}
state = State.Stopped;
return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
} catch (LibvirtException e) {
return new StopAnswer(cmd, e.getMessage());
} finally {
synchronized (_vms) {
if (state != null) {
_vms.put(vmName, state);
} else {
_vms.remove(vmName);
}
}
}
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
private Answer execute(CheckVirtualMachineCommand cmd) {
try {
Connect conn = LibvirtConnection.getConnection();
final State state = getVmState(conn, cmd.getVmName());
Integer vncPort = null;
if (state == State.Running) {
vncPort = getVncPort(conn, cmd.getVmName());
synchronized (_vms) {
_vms.put(cmd.getVmName(), State.Running);
}
}
return new CheckVirtualMachineAnswer(cmd, state, vncPort);
} catch (LibvirtException e) {
return new CheckVirtualMachineAnswer(cmd, e.getMessage());
}
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method sync.
protected HashMap<String, State> sync() {
HashMap<String, State> newStates;
HashMap<String, State> oldStates = null;
final HashMap<String, State> changes = new HashMap<String, State>();
synchronized (_vms) {
newStates = getAllVms();
if (newStates == null) {
s_logger.debug("Unable to get the vm states so no state sync at this point.");
return changes;
}
oldStates = new HashMap<String, State>(_vms.size());
oldStates.putAll(_vms);
for (final Map.Entry<String, State> entry : newStates.entrySet()) {
final String vm = entry.getKey();
State newState = entry.getValue();
final State oldState = oldStates.remove(vm);
if (newState == State.Stopped && oldState != State.Stopping && oldState != null && oldState != State.Stopped) {
newState = getRealPowerState(vm);
}
if (s_logger.isTraceEnabled()) {
s_logger.trace("VM " + vm + ": libvirt has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null"));
}
if (vm.startsWith("migrating")) {
s_logger.debug("Migration detected. Skipping");
continue;
}
if (oldState == null) {
_vms.put(vm, newState);
s_logger.debug("Detecting a new state but couldn't find a old state so adding it to the changes: " + vm);
changes.put(vm, newState);
} else if (oldState == State.Starting) {
if (newState == State.Running) {
_vms.put(vm, newState);
} else if (newState == State.Stopped) {
s_logger.debug("Ignoring vm " + vm + " because of a lag in starting the vm.");
}
} else if (oldState == State.Migrating) {
if (newState == State.Running) {
s_logger.debug("Detected that an migrating VM is now running: " + vm);
_vms.put(vm, newState);
}
} else if (oldState == State.Stopping) {
if (newState == State.Stopped) {
_vms.put(vm, newState);
} else if (newState == State.Running) {
s_logger.debug("Ignoring vm " + vm + " because of a lag in stopping the vm. ");
}
} else if (oldState != newState) {
_vms.put(vm, newState);
if (newState == State.Stopped) {
if (_vmsKilled.remove(vm)) {
s_logger.debug("VM " + vm + " has been killed for storage. ");
newState = State.Error;
}
}
changes.put(vm, newState);
}
}
for (final Map.Entry<String, State> entry : oldStates.entrySet()) {
final String vm = entry.getKey();
final State oldState = entry.getValue();
if (s_logger.isTraceEnabled()) {
s_logger.trace("VM " + vm + " is now missing from libvirt so reporting stopped");
}
if (oldState == State.Stopping) {
s_logger.debug("Ignoring VM " + vm + " in transition state stopping.");
_vms.remove(vm);
} else if (oldState == State.Starting) {
s_logger.debug("Ignoring VM " + vm + " in transition state starting.");
} else if (oldState == State.Stopped) {
_vms.remove(vm);
} else if (oldState == State.Migrating) {
s_logger.debug("Ignoring VM " + vm + " in migrating state.");
} else {
_vms.remove(vm);
State state = State.Stopped;
if (_vmsKilled.remove(entry.getKey())) {
s_logger.debug("VM " + vm + " has been killed by storage monitor");
state = State.Error;
}
changes.put(entry.getKey(), state);
}
}
}
return changes;
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class CloudZonesComputingResource method execute.
protected Answer execute(StopCommand cmd) {
final String vmName = cmd.getVmName();
Long bytesReceived = new Long(0);
Long bytesSent = new Long(0);
State state = null;
synchronized (_vms) {
state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
try {
Connect conn = LibvirtConnection.getConnection();
try {
Domain dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
} catch (LibvirtException e) {
state = State.Stopped;
return new StopAnswer(cmd, null, 0, bytesSent, bytesReceived);
}
String macAddress = null;
if (vmName.startsWith("i-")) {
List<InterfaceDef> nics = getInterfaces(conn, vmName);
if (!nics.isEmpty()) {
macAddress = nics.get(0).getMacAddress();
}
}
destroy_network_rules_for_vm(conn, vmName);
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
try {
cleanupVnet(conn, cmd.getVnet());
_dhcpSnooper.cleanup(macAddress, vmName);
_vmDataServer.handleVmStopped(cmd.getVmName());
} catch (Exception e) {
}
state = State.Stopped;
return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
} catch (LibvirtException e) {
return new StopAnswer(cmd, e.getMessage());
} finally {
synchronized (_vms) {
if (state != null) {
_vms.put(vmName, state);
} else {
_vms.remove(vmName);
}
}
}
}
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);
}
Aggregations