use of com.cloud.legacymodel.vm.VirtualMachine.PowerState in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method getHostVmStateReport.
private HashMap<String, HostVmStateReportEntry> getHostVmStateReport(final Connect conn) {
final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<>();
String[] vms = null;
int[] ids = null;
try {
ids = conn.listDomains();
} catch (final LibvirtException e) {
logger.warn("Unable to listDomains", e);
return null;
}
try {
vms = conn.listDefinedDomains();
} catch (final LibvirtException e) {
logger.warn("Unable to listDomains", e);
return null;
}
Domain dm = null;
for (final int id : ids) {
try {
dm = conn.domainLookupByID(id);
final DomainState ps = dm.getInfo().state;
final PowerState state = convertToPowerState(ps);
logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
final String vmName = dm.getName();
vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
} catch (final LibvirtException e) {
logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
logger.trace("Ignoring libvirt error.", e);
}
}
}
for (final String vm : vms) {
try {
dm = conn.domainLookupByName(vm);
final DomainState ps = dm.getInfo().state;
final PowerState state = convertToPowerState(ps);
final String vmName = dm.getName();
logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
} catch (final LibvirtException e) {
logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
logger.trace("Ignoring libvirt error.", e);
}
}
}
return vmStates;
}
use of com.cloud.legacymodel.vm.VirtualMachine.PowerState in project cosmic by MissionCriticalCloud.
the class LibvirtRevertToVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
final PowerState vmState;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
}
final DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
if (snapshot == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
}
dm.revertToSnapshot(snapshot, VIR_DOMAIN_SNAPSHOT_REVERT_FORCE | VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING);
snapshot.free();
if (!snapshotMemory) {
dm.destroy();
if (dm.isPersistent() == 1) {
dm.undefine();
}
vmState = PowerState.PowerOff;
} else {
vmState = PowerState.PowerOn;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (final LibvirtException e) {
final String msg = " Revert to VM snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of com.cloud.legacymodel.vm.VirtualMachine.PowerState in project cosmic by MissionCriticalCloud.
the class CitrixCheckVirtualMachineCommandWrapper method execute.
@Override
public Answer execute(final CheckVirtualMachineCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final String vmName = command.getVmName();
final PowerState powerState = citrixResourceBase.getVmState(conn, vmName);
final Integer vncPort = null;
if (powerState == PowerState.PowerOn) {
s_logger.debug("3. The VM " + vmName + " is in Running state");
}
return new CheckVirtualMachineAnswer(command, powerState, vncPort);
}
use of com.cloud.legacymodel.vm.VirtualMachine.PowerState in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method getVmState.
public PowerState getVmState(final Connect conn, final String vmName) {
int retry = 3;
Domain vms = null;
while (retry-- > 0) {
try {
vms = conn.domainLookupByName(vmName);
final PowerState s = convertToPowerState(vms.getInfo().state);
return s;
} catch (final LibvirtException e) {
logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
} finally {
try {
if (vms != null) {
vms.free();
}
} catch (final LibvirtException l) {
logger.trace("Ignoring libvirt error.", l);
}
}
}
return PowerState.PowerOff;
}
use of com.cloud.legacymodel.vm.VirtualMachine.PowerState in project cosmic by MissionCriticalCloud.
the class LibvirtCheckVirtualMachineCommandWrapper method execute.
@Override
public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) {
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName());
Integer vncPort = null;
if (state == PowerState.PowerOn) {
vncPort = libvirtComputingResource.getVncPort(conn, command.getVmName());
}
return new CheckVirtualMachineAnswer(command, state, vncPort);
} catch (final LibvirtException e) {
return new CheckVirtualMachineAnswer(command, e.getMessage());
}
}
Aggregations