use of com.cloud.agent.manager.Commands in project cloudstack by apache.
the class VirtualMachineManagerImpl method ensureVmRunningContext.
private void ensureVmRunningContext(final long hostId, VMInstanceVO vm, final Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException, InsufficientAddressCapacityException {
final VirtualMachineGuru vmGuru = getVmGuru(vm);
s_logger.debug("VM state is starting on full sync so updating it to running");
vm = _vmDao.findById(vm.getId());
// grab outstanding work item if any
final ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState());
if (work != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found an outstanding work item for this vm " + vm + " in state:" + vm.getState() + ", work id:" + work.getId());
}
}
try {
stateTransitTo(vm, cause, hostId);
} catch (final NoTransitionException e1) {
s_logger.warn(e1.getMessage());
}
s_logger.debug("VM's " + vm + " state is starting on full sync so updating it to Running");
// this should ensure vm has the most
vm = _vmDao.findById(vm.getId());
// up to date info
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
final List<NicVO> nics = _nicsDao.listByVmId(profile.getId());
for (final NicVO nic : nics) {
final Network network = _networkModel.getNetwork(nic.getNetworkId());
final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(profile.getHypervisorType(), network));
profile.addNic(nicProfile);
}
final Commands cmds = new Commands(Command.OnError.Stop);
s_logger.debug("Finalizing commands that need to be send to complete Start process for the vm " + vm);
if (vmGuru.finalizeCommandsOnStart(cmds, profile)) {
if (cmds.size() != 0) {
_agentMgr.send(vm.getHostId(), cmds);
}
if (vmGuru.finalizeStart(profile, vm.getHostId(), cmds, null)) {
stateTransitTo(vm, cause, vm.getHostId());
} else {
s_logger.error("Unable to finish finialization for running vm: " + vm);
}
} else {
s_logger.error("Unable to finalize commands on start for vm: " + vm);
}
if (work != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Updating outstanding work item to Done, id:" + work.getId());
}
work.setStep(Step.Done);
_workDao.update(work.getId(), work);
}
}
use of com.cloud.agent.manager.Commands in project cloudstack by apache.
the class UserVmManagerImpl method handleManagedStorage.
private void handleManagedStorage(UserVmVO vm, VolumeVO root) {
if (Volume.State.Allocated.equals(root.getState())) {
return;
}
StoragePoolVO storagePool = _storagePoolDao.findById(root.getPoolId());
if (storagePool != null && storagePool.isManaged()) {
Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
if (hostId != null) {
VolumeInfo volumeInfo = volFactory.getVolume(root.getId());
Host host = _hostDao.findById(hostId);
final Command cmd;
if (host.getHypervisorType() == HypervisorType.XenServer) {
DiskTO disk = new DiskTO(volumeInfo.getTO(), root.getDeviceId(), root.getPath(), root.getVolumeType());
// it's OK in this case to send a detach command to the host for a root volume as this
// will simply lead to the SR that supports the root volume being removed
cmd = new DettachCommand(disk, vm.getInstanceName());
DettachCommand detachCommand = (DettachCommand) cmd;
detachCommand.setManaged(true);
detachCommand.setStorageHost(storagePool.getHostAddress());
detachCommand.setStoragePort(storagePool.getPort());
detachCommand.set_iScsiName(root.get_iScsiName());
} else if (host.getHypervisorType() == HypervisorType.VMware) {
PrimaryDataStore primaryDataStore = (PrimaryDataStore) volumeInfo.getDataStore();
Map<String, String> details = primaryDataStore.getDetails();
if (details == null) {
details = new HashMap<String, String>();
primaryDataStore.setDetails(details);
}
details.put(DiskTO.MANAGED, Boolean.TRUE.toString());
cmd = new DeleteCommand(volumeInfo.getTO());
} else {
throw new CloudRuntimeException("This hypervisor type is not supported on managed storage for this command.");
}
Commands cmds = new Commands(Command.OnError.Stop);
cmds.addCommand(cmd);
try {
_agentMgr.send(hostId, cmds);
} catch (Exception ex) {
throw new CloudRuntimeException(ex.getMessage());
}
if (!cmds.isSuccessful()) {
for (Answer answer : cmds.getAnswers()) {
if (!answer.getResult()) {
s_logger.warn("Failed to reset vm due to: " + answer.getDetails());
throw new CloudRuntimeException("Unable to reset " + vm + " due to " + answer.getDetails());
}
}
}
// root.getPoolId() should be null if the VM we are detaching the disk from has never been started before
DataStore dataStore = root.getPoolId() != null ? _dataStoreMgr.getDataStore(root.getPoolId(), DataStoreRole.Primary) : null;
volumeMgr.revokeAccess(volFactory.getVolume(root.getId()), host, dataStore);
}
}
}
use of com.cloud.agent.manager.Commands in project cloudstack by apache.
the class NetworkHelperImplTest method testSendCommandsToRouterWithTrueResult.
/**
* The only way result can be true is if each and every command receive a true result
*
* @throws AgentUnavailableException
* @throws OperationTimedoutException
*/
@Test
public void testSendCommandsToRouterWithTrueResult() throws AgentUnavailableException, OperationTimedoutException, ResourceUnavailableException {
// Prepare
NetworkHelperImpl nwHelperUT = spy(this.nwHelper);
VirtualRouter vr = mock(VirtualRouter.class);
when(vr.getHostId()).thenReturn(HOST_ID);
doReturn(true).when(nwHelperUT).checkRouterVersion(vr);
Commands commands = mock(Commands.class);
when(commands.size()).thenReturn(3);
Answer answer1 = mock(Answer.class);
Answer answer2 = mock(Answer.class);
Answer answer3 = mock(Answer.class);
// In the second iteration it should match and return, without invoking the third
Answer[] answers = { answer1, answer2, answer3 };
when(answer1.getResult()).thenReturn(true);
when(answer2.getResult()).thenReturn(true);
when(answer3.getResult()).thenReturn(true);
when(this.agentManager.send(HOST_ID, commands)).thenReturn(answers);
// Execute
final boolean result = nwHelperUT.sendCommandsToRouter(vr, commands);
// Assert
verify(this.agentManager, times(1)).send(HOST_ID, commands);
verify(answer1, times(1)).getResult();
verify(answer2, times(1)).getResult();
verify(answer3, times(1)).getResult();
assertTrue(result);
}
use of com.cloud.agent.manager.Commands in project cloudstack by apache.
the class NetworkHelperImplTest method testSendCommandsToRouterWithNoAnswers.
/**
* If the number of answers is different to the number of commands the result is false
*
* @throws AgentUnavailableException
* @throws OperationTimedoutException
*/
@Test
public void testSendCommandsToRouterWithNoAnswers() throws AgentUnavailableException, OperationTimedoutException, ResourceUnavailableException {
// Prepare
NetworkHelperImpl nwHelperUT = spy(this.nwHelper);
VirtualRouter vr = mock(VirtualRouter.class);
when(vr.getHostId()).thenReturn(HOST_ID);
doReturn(true).when(nwHelperUT).checkRouterVersion(vr);
Commands commands = mock(Commands.class);
when(commands.size()).thenReturn(3);
Answer answer1 = mock(Answer.class);
Answer answer2 = mock(Answer.class);
// In the second iteration it should match and return, without invoking the third
Answer[] answers = { answer1, answer2 };
when(this.agentManager.send(HOST_ID, commands)).thenReturn(answers);
// Execute
final boolean result = nwHelperUT.sendCommandsToRouter(vr, commands);
// Assert
verify(this.agentManager, times(1)).send(HOST_ID, commands);
verify(answer1, times(0)).getResult();
assertFalse(result);
}
use of com.cloud.agent.manager.Commands in project cloudstack by apache.
the class NetworkHelperImplTest method testSendCommandsToRouter.
@Test
public void testSendCommandsToRouter() throws AgentUnavailableException, OperationTimedoutException, ResourceUnavailableException {
// Prepare
NetworkHelperImpl nwHelperUT = spy(this.nwHelper);
VirtualRouter vr = mock(VirtualRouter.class);
when(vr.getHostId()).thenReturn(HOST_ID);
doReturn(true).when(nwHelperUT).checkRouterVersion(vr);
Commands commands = mock(Commands.class);
when(commands.size()).thenReturn(3);
Answer answer1 = mock(Answer.class);
Answer answer2 = mock(Answer.class);
Answer answer3 = mock(Answer.class);
// In the second iteration it should match and return, without invoking the third
Answer[] answers = { answer1, answer2, answer3 };
when(answer1.getResult()).thenReturn(true);
when(answer2.getResult()).thenReturn(false);
when(answer3.getResult()).thenReturn(false);
when(this.agentManager.send(HOST_ID, commands)).thenReturn(answers);
// Execute
final boolean result = nwHelperUT.sendCommandsToRouter(vr, commands);
// Assert
verify(this.agentManager, times(1)).send(HOST_ID, commands);
verify(answer1, times(1)).getResult();
verify(answer2, times(1)).getResult();
verify(answer3, times(0)).getResult();
assertFalse(result);
}
Aggregations