use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getAllVms.
private HashMap<String, State> getAllVms() {
final HashMap<String, State> vmStates = new HashMap<String, State>();
String[] vms = null;
int[] ids = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
s_logger.debug("Failed to get connection: " + e.getMessage());
return vmStates;
}
try {
ids = conn.listDomains();
} catch (final LibvirtException e) {
s_logger.warn("Unable to listDomains", e);
return null;
}
try {
vms = conn.listDefinedDomains();
} catch (final LibvirtException e) {
s_logger.warn("Unable to listDomains", e);
return null;
}
Domain dm = null;
for (int i = 0; i < ids.length; i++) {
try {
dm = conn.domainLookupByID(ids[i]);
DomainInfo.DomainState ps = dm.getInfo().state;
final State state = convertToState(ps);
s_logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
String vmName = dm.getName();
vmStates.put(vmName, state);
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
}
for (int i = 0; i < vms.length; i++) {
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vms[i].getBytes()));
DomainInfo.DomainState ps = dm.getInfo().state;
final State state = convertToState(ps);
String vmName = dm.getName();
s_logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
vmStates.put(vmName, state);
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} catch (Exception e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
}
return vmStates;
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method initialize.
@Override
public StartupCommand[] initialize() {
Map<String, State> changes = null;
synchronized (_vms) {
_vms.clear();
changes = sync();
}
final List<Object> info = getHostInfo();
final StartupRoutingCommand cmd = new StartupRoutingCommand((Integer) info.get(0), (Long) info.get(1), (Long) info.get(2), (Long) info.get(4), (String) info.get(3), HypervisorType.KVM, RouterPrivateIpStrategy.HostLocal);
cmd.setStateChanges(changes);
fillNetworkInformation(cmd);
_privateIp = cmd.getPrivateIpAddress();
cmd.getHostDetails().putAll(getVersionStrings());
cmd.setPool(_pool);
cmd.setCluster(_clusterId);
cmd.setGatewayIpAddress(_localGateway);
StartupStorageCommand sscmd = null;
try {
KVMStoragePool localStoragePool = _storagePoolMgr.createStoragePool(_localStorageUUID, "localhost", _localStoragePath, StoragePoolType.Filesystem);
com.cloud.agent.api.StoragePoolInfo pi = new com.cloud.agent.api.StoragePoolInfo(localStoragePool.getUuid(), cmd.getPrivateIpAddress(), _localStoragePath, _localStoragePath, StoragePoolType.Filesystem, localStoragePool.getCapacity(), localStoragePool.getUsed());
sscmd = new StartupStorageCommand();
sscmd.setPoolInfo(pi);
sscmd.setGuid(pi.getUuid());
sscmd.setDataCenter(_dcId);
sscmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
} catch (CloudRuntimeException e) {
}
if (sscmd != null) {
return new StartupCommand[] { cmd, sscmd };
} else {
return new StartupCommand[] { cmd };
}
}
use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getVmState.
protected State getVmState(Connect conn, final String vmName) {
int retry = 3;
Domain vms = null;
while (retry-- > 0) {
try {
vms = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
State s = convertToState(vms.getInfo().state);
return s;
} catch (final LibvirtException e) {
s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
} catch (Exception e) {
s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
} finally {
try {
if (vms != null) {
vms.free();
}
} catch (final LibvirtException e) {
}
}
}
return State.Stopped;
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class AffinityGroupServiceImpl method postStateTransitionEvent.
@Override
public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) {
if (!status) {
return false;
}
State newState = transition.getToState();
if ((newState == State.Expunging) || (newState == State.Error)) {
// cleanup all affinity groups associations of the Expunged VM
SearchCriteria<AffinityGroupVMMapVO> sc = _affinityGroupVMMapDao.createSearchCriteria();
sc.addAnd("instanceId", SearchCriteria.Op.EQ, vo.getId());
_affinityGroupVMMapDao.expunge(sc);
}
return true;
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class Ovm3HypervisorSupport method getAllVmStates.
/**
* getAllVmStates: Get the state of all the VMs
*
* @return
* @throws Ovm3ResourceException
*/
private Map<String, State> getAllVmStates(Map<String, State> vmStateMap) throws Ovm3ResourceException {
Map<String, Xen.Vm> vms = getAllVms();
final Map<String, State> states = new HashMap<String, State>();
for (final Map.Entry<String, Xen.Vm> entry : vms.entrySet()) {
Xen.Vm vm = entry.getValue();
State ns = State.Running;
String as = vm.getVmState();
if (vm.isControlDomain() || as == null) {
continue;
}
/* need a more exact match! */
if (as.contains("r")) {
ns = State.Running;
/* The domain is blocked, and not running or runnable. */
} else if (as.contains("b")) {
ns = State.Running;
/* The domain has been paused */
} else if (as.contains("p")) {
ns = State.Running;
/* The guest has requested to be shutdown, still migrating... */
} else if (as.contains("s")) {
if (vmStateMap.get(vm.getVmName()) == State.Migrating) {
ns = State.Migrating;
} else {
ns = State.Stopped;
}
/* The domain has crashed */
} else if (as.contains("c")) {
ns = State.Error;
/*
* The domain is in process of dying (if we see this twice we
* have a problem ?)
*/
} else if (as.contains("d")) {
ns = State.Stopping;
} else {
ns = State.Unknown;
}
LOGGER.trace("state " + ns + " for " + vm.getVmName() + " based on " + as);
states.put(vm.getVmName(), ns);
}
return states;
}
Aggregations