use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class Ovm3HypervisorSupport method execute.
/* check "the" virtual machine */
public CheckVirtualMachineAnswer execute(final CheckVirtualMachineCommand cmd) {
LOGGER.debug("CheckVirtualMachineCommand: " + cmd.getVmName());
String vmName = cmd.getVmName();
try {
CloudstackPlugin plug = new CloudstackPlugin(c);
Integer vncPort = Integer.valueOf(plug.getVncPort(vmName));
if (vncPort == 0) {
LOGGER.warn("No VNC port for " + vmName);
}
/* we already have the state ftw */
Map<String, State> states = getAllVmStates(vmStateMap);
State vmState = states.get(vmName);
if (vmState == null) {
LOGGER.warn("Check state of " + vmName + " return null in CheckVirtualMachineCommand");
vmState = State.Stopped;
}
synchronized (vmStateMap) {
vmStateMap.put(vmName, State.Running);
}
return new CheckVirtualMachineAnswer(cmd, convertStateToPower(vmState), vncPort);
} catch (Ovm3ResourceException e) {
LOGGER.debug("Check migration for " + vmName + " failed", e);
return new CheckVirtualMachineAnswer(cmd, convertStateToPower(State.Stopped), null);
}
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class Ovm3HypervisorSupport method hostVmStateReport.
/**
* hostVmStateReport: Get all the VM states.
*
* @return
* @throws Ovm3ResourceException
*/
public Map<String, HostVmStateReportEntry> hostVmStateReport() throws Ovm3ResourceException {
final Map<String, HostVmStateReportEntry> vmStates = new HashMap<String, HostVmStateReportEntry>();
for (final Map.Entry<String, State> vm : vmStateMap.entrySet()) {
LOGGER.debug("VM " + vm.getKey() + " state: " + vm.getValue() + ":" + convertStateToPower(vm.getValue()));
vmStates.put(vm.getKey(), new HostVmStateReportEntry(convertStateToPower(vm.getValue()), c.getIp()));
}
return vmStates;
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class Ovm3HypervisorSupport method syncState.
private Map<String, State> syncState(Map<String, State> vmStateMap) throws Ovm3ResourceException {
Map<String, State> newStates;
Map<String, State> oldStates = null;
final Map<String, State> changes = new HashMap<String, State>();
try {
newStates = getAllVmStates(vmStateMap);
} catch (Ovm3ResourceException e) {
LOGGER.error("Ovm3 full sync failed: ", e);
throw e;
}
synchronized (vmStateMap) {
oldStates = new HashMap<String, State>(vmStateMap.size());
oldStates.putAll(vmStateMap);
for (final Map.Entry<String, State> entry : newStates.entrySet()) {
final String vmName = entry.getKey();
State newState = entry.getValue();
final State oldState = oldStates.remove(vmName);
LOGGER.trace("state for " + vmName + ", old: " + oldState + ", new: " + newState);
/* eurh ? */
if (newState == State.Stopped && oldState != State.Stopping && oldState != null && oldState != State.Stopped) {
LOGGER.trace("Getting power state....");
newState = State.Running;
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("VM " + vmName + ": ovm has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null"));
}
if (newState == State.Migrating) {
LOGGER.trace(vmName + " is migrating, skipping state check");
continue;
}
if (oldState == null) {
vmStateMap.put(vmName, newState);
LOGGER.debug("New state without old state: " + vmName);
changes.put(vmName, newState);
} else if (oldState == State.Starting) {
if (newState == State.Running) {
vmStateMap.put(vmName, newState);
} else if (newState == State.Stopped) {
LOGGER.debug("Ignoring vm " + vmName + " because of a lag in starting the vm.");
}
} else if (oldState == State.Migrating) {
if (newState == State.Running) {
LOGGER.debug("Detected that a migrating VM is now running: " + vmName);
vmStateMap.put(vmName, newState);
}
} else if (oldState == State.Stopping) {
if (newState == State.Stopped) {
vmStateMap.put(vmName, newState);
} else if (newState == State.Running) {
LOGGER.debug("Ignoring vm " + vmName + " because of a lag in stopping the vm. ");
/* should kill it hard perhaps ? */
}
} else if (oldState != newState) {
vmStateMap.put(vmName, newState);
if (newState == State.Stopped) {
// For now leave it be.
}
changes.put(vmName, newState);
}
}
for (final Map.Entry<String, State> entry : oldStates.entrySet()) {
final String vmName = entry.getKey();
final State oldState = entry.getValue();
if (oldState == State.Stopping) {
LOGGER.debug("Removing VM " + vmName + " in transition state stopping.");
vmStateMap.remove(vmName);
} else if (oldState == State.Starting) {
LOGGER.debug("Removing VM " + vmName + " in transition state starting.");
vmStateMap.remove(vmName);
} else if (oldState == State.Stopped) {
LOGGER.debug("Stopped VM " + vmName + " removing.");
vmStateMap.remove(vmName);
} else if (oldState == State.Migrating) {
/*
* do something smarter here.. newstate should say stopping
* already
*/
LOGGER.debug("Ignoring VM " + vmName + " in migrating state.");
} else {
/* if it's not there name it stopping */
State state = State.Stopping;
LOGGER.debug("VM " + vmName + " is now missing from ovm3 server so removing it");
changes.put(vmName, state);
vmStateMap.remove(vmName);
vmStateMap.put(vmName, state);
}
}
}
return changes;
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class UserVmStateListener method postStateTransitionEvent.
@Override
public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) {
if (!status) {
return false;
}
Event event = transition.getEvent();
State oldState = transition.getCurrentState();
State newState = transition.getToState();
pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
if (vo.getType() != VirtualMachine.Type.User) {
return true;
}
if (transition.isImpacted(StateMachine2.Transition.Impact.USAGE)) {
if (oldState == State.Destroyed && newState == State.Stopped) {
generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_CREATE);
} else if (newState == State.Running) {
generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_START);
} else if (newState == State.Stopped) {
generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_STOP);
List<NicVO> nics = _nicDao.listByVmId(vo.getId());
for (NicVO nic : nics) {
NetworkVO network = _networkDao.findById(nic.getNetworkId());
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vo.getAccountId(), vo.getDataCenterId(), vo.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, 0L, vo.getClass().getName(), vo.getUuid(), vo.isDisplay());
}
} else if (newState == State.Destroyed || newState == State.Error || newState == State.Expunging) {
generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_DESTROY);
}
}
return true;
}
use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.
the class NetworkHelperImpl method startRouters.
@Override
public List<DomainRouterVO> startRouters(final RouterDeploymentDefinition routerDeploymentDefinition) throws StorageUnavailableException, InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
final List<DomainRouterVO> runningRouters = new ArrayList<DomainRouterVO>();
for (DomainRouterVO router : routerDeploymentDefinition.getRouters()) {
boolean skip = false;
final State state = router.getState();
if (router.getHostId() != null && state != State.Running) {
final HostVO host = _hostDao.findById(router.getHostId());
if (host == null || host.getState() != Status.Up) {
skip = true;
}
}
if (!skip) {
if (state != State.Running) {
router = startVirtualRouter(router, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount(), routerDeploymentDefinition.getParams());
}
if (router != null) {
runningRouters.add(router);
}
}
}
return runningRouters;
}
Aggregations