public StopCommand(VirtualMachine vm, boolean executeInSequence, boolean checkBeforeCleanup) { super(vm.getInstanceName(), executeInSequence); this.checkBeforeCleanup = checkBeforeCleanup; }
public StopCommand(VirtualMachine vm, boolean isProxy, String urlPort, String publicConsoleProxyIpAddress, boolean executeInSequence, boolean checkBeforeCleanup) { super(vm.getInstanceName(), executeInSequence); this.isProxy = isProxy; this.urlPort = urlPort; this.publicConsoleProxyIpAddress = publicConsoleProxyIpAddress; this.checkBeforeCleanup = checkBeforeCleanup; }
public StopCommand(VirtualMachine vm, boolean executeInSequence, boolean checkBeforeCleanup, boolean forceStop) { super(vm.getInstanceName(), executeInSequence); this.checkBeforeCleanup = checkBeforeCleanup; this.forceStop = forceStop; }
@Override public String getInstanceName() { return _vm.getInstanceName(); }
private void setupAgentSecurity(final Host vmHost, final Map<String, String> sshAccessDetails, final VirtualMachine vm) throws AgentUnavailableException, OperationTimedoutException { final String csr = caManager.generateKeyStoreAndCsr(vmHost, sshAccessDetails); if (!Strings.isNullOrEmpty(csr)) { final Map<String, String> ipAddressDetails = new HashMap<>(sshAccessDetails); ipAddressDetails.remove(NetworkElementCommand.ROUTER_NAME); final Certificate certificate = caManager.issueCertificate(csr, Arrays.asList(vm.getHostName(), vm.getInstanceName()), new ArrayList<>(ipAddressDetails.values()), CAManager.CertValidityPeriod.value(), null); final boolean result = caManager.deployCertificate(vmHost, certificate, false, sshAccessDetails); if (!result) { s_logger.error("Failed to setup certificate for system vm: " + vm.getInstanceName()); } } else { s_logger.error("Failed to setup keystore and generate CSR for system vm: " + vm.getInstanceName()); } }
public FenceCommand(VirtualMachine vm, Host host) { super(); vmName = vm.getInstanceName(); hostGuid = host.getGuid(); hostIp = host.getPrivateIpAddress(); inSeq = false; }
@Override public String getVmNameOnVolume(Volume volume) { Long vmId = volume.getInstanceId(); if (vmId != null) { VirtualMachine vm = _entityMgr.findById(VirtualMachine.class, vmId); if (vm == null) { return null; } return vm.getInstanceName(); } return null; }
@Override public boolean postStateTransitionEvent(final StateMachine2.Transition<VirtualMachine.State, VirtualMachine.Event> transition, final VirtualMachine vo, final boolean status, final Object opaque) { final VirtualMachine.State newState = transition.getToState(); final VirtualMachine.Event event = transition.getEvent(); if (vo.getType() == VirtualMachine.Type.DomainRouter && event == VirtualMachine.Event.FollowAgentPowerOnReport && newState == VirtualMachine.State.Running && isOutOfBandMigrated(opaque)) { s_logger.debug("Virtual router " + vo.getInstanceName() + " is powered-on out-of-band"); } return true; }
@Override public Map<String, String> getSystemVMAccessDetails(final VirtualMachine vm) { final Map<String, String> accessDetails = new HashMap<>(); accessDetails.put(NetworkElementCommand.ROUTER_NAME, vm.getInstanceName()); String privateIpAddress = null; for (final NicProfile profile : getNicProfiles(vm)) {
@Override public boolean isVmAlive(VirtualMachine vm, Host host) throws UnknownVM { CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName()); try { CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer)_agentMgr.send(vm.getHostId(), cmd); if (!answer.getResult()) { s_logger.debug("Unable to get vm state on " + vm.toString()); throw new UnknownVM(); } s_logger.debug("Agent responded with state " + answer.getState().toString()); return answer.getState() == PowerState.PowerOn; } catch (AgentUnavailableException e) { s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage()); throw new UnknownVM(); } catch (OperationTimedoutException e) { s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage()); throw new UnknownVM(); } } }
private boolean deleteConfigDriveIso(final VirtualMachine vm) throws ResourceUnavailableException { DataStore dataStore = _dataStoreMgr.getImageStore(vm.getDataCenterId()); Long agentId = findAgentIdForImageStore(dataStore); if (VirtualMachineManager.VmConfigDriveOnPrimaryPool.value()) { List<VolumeVO> volumes = _volumeDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT); if (volumes != null && volumes.size() > 0) { dataStore = _dataStoreMgr.getDataStore(volumes.get(0).getPoolId(), DataStoreRole.Primary); } agentId = (vm.getHostId() != null) ? vm.getHostId() : vm.getLastHostId(); } if (agentId == null || dataStore == null) { throw new ResourceUnavailableException("Config drive iso creation failed, agent or datastore not available", ConfigDriveNetworkElement.class, 0L); } LOG.debug("Deleting config drive ISO for vm: " + vm.getInstanceName()); final String isoPath = ConfigDrive.createConfigDrivePath(vm.getInstanceName()); final HandleConfigDriveIsoCommand configDriveIsoCommand = new HandleConfigDriveIsoCommand(isoPath, null, dataStore.getTO(), false); final Answer answer = agentManager.easySend(agentId, configDriveIsoCommand); if (!answer.getResult()) { LOG.error("Failed to remove config drive for instance: " + vm.getInstanceName()); return false; } return true; }
@Override public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) { State newState = transition.getToState(); State oldState = transition.getCurrentState(); if (newState != State.Starting && newState != State.Error && newState != State.Expunging) { return true; } if (vo.getHypervisorType() != HypervisorType.BareMetal) { return true; } HostVO host = _hostDao.findById(vo.getHostId()); if (host == null) { s_logger.debug("Skip oldState " + oldState + " to " + "newState " + newState + " transimtion"); return true; } _hostDao.loadDetails(host); if (newState == State.Starting) { host.setDetail("vmName", vo.getInstanceName()); s_logger.debug("Add vmName " + host.getDetail("vmName") + " to host " + host.getId() + " details"); } else { if (host.getDetail("vmName") != null && host.getDetail("vmName").equalsIgnoreCase(vo.getInstanceName())) { s_logger.debug("Remove vmName " + host.getDetail("vmName") + " from host " + host.getId() + " details"); host.getDetails().remove("vmName"); } } _hostDao.saveDetails(host); return true; }
@Override public Pair<String, Integer> getVncPort(final VirtualMachine vm) { if (vm.getHostId() == null) { s_logger.warn("VM " + vm.getHostName() + " does not have host, return -1 for its VNC port"); return new Pair<String, Integer>(null, -1); } if (s_logger.isTraceEnabled()) { s_logger.trace("Trying to retrieve VNC port from agent about VM " + vm.getHostName()); } final GetVncPortAnswer answer = (GetVncPortAnswer)_agentMgr.easySend(vm.getHostId(), new GetVncPortCommand(vm.getId(), vm.getInstanceName())); if (answer != null && answer.getResult()) { return new Pair<String, Integer>(answer.getAddress(), answer.getPort()); } return new Pair<String, Integer>(null, -1); }
public VspVm buildVspVm(VirtualMachine vm, Network network) { VspVm.Builder vspVmBuilder = new VspVm.Builder() .uuid(vm.getUuid()) .name(vm.getInstanceName()) .state(getEnumValue(vm.getState(), VspVm.State.Unknown)); boolean isDomainRouter = vm.getType().equals(VirtualMachine.Type.DomainRouter); vspVmBuilder.domainRouter(isDomainRouter); if (network.getBroadcastUri() != null) { String domainRouterIp = network.getBroadcastUri().getPath().substring(1); vspVmBuilder.domainRouterIp(domainRouterIp); } return vspVmBuilder.build(); }
protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) throws AgentUnavailableException, OperationTimedoutException { final Answer answer = _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName())); if (answer == null || !answer.getResult()) { return false; } if (answer instanceof CheckVirtualMachineAnswer) { final CheckVirtualMachineAnswer vmAnswer = (CheckVirtualMachineAnswer)answer; if (vmAnswer.getState() == PowerState.PowerOff) { return false; } } UserVmVO userVm = _userVmDao.findById(vm.getId()); if (userVm != null) { List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vm.getId()); RestoreVMSnapshotCommand command = _vmSnapshotMgr.createRestoreCommand(userVm, vmSnapshots); if (command != null) { RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) _agentMgr.send(hostId, command); if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) { s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer.getDetails()); } } } return true; }
@Override public List<Command> finalizeExpungeVolumes(VirtualMachine vm) { List<Command> commands = new ArrayList<Command>(); List<VolumeVO> volumes = volumeDao.findByInstance(vm.getId()); // 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 if (volumes != null) { for (VolumeVO volume : volumes) { StoragePoolVO storagePool = storagePoolDao.findById(volume.getPoolId()); // storagePool should be null if we are expunging a volume that was never // attached to a VM that was started (the "trick" for storagePool to be null // is that none of the VMs this volume may have been attached to were ever started, // so the volume was never assigned to a storage pool) if (storagePool != null && storagePool.isManaged()) { DataTO volTO = volFactory.getVolume(volume.getId()).getTO(); DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType()); DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName()); cmd.setManaged(true); cmd.setStorageHost(storagePool.getHostAddress()); cmd.setStoragePort(storagePool.getPort()); cmd.set_iScsiName(volume.get_iScsiName()); commands.add(cmd); } } } return commands; }
@Override public boolean unquiesceVM(VirtualMachine virtualMachine, VMSnapshotTO vmSnapshotTO) { Long hostId = vmSnapshotHelper.pickRunningHost(virtualMachine.getId()); List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(virtualMachine.getId()); GuestOSVO guestOS = guestOSDao.findById(virtualMachine.getGuestOSId()); DeleteVMSnapshotCommand deleteSnapshotCommand = new DeleteVMSnapshotCommand(virtualMachine.getInstanceName(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName()); try { Answer answer = agentMgr.send(hostId, deleteSnapshotCommand); if (answer != null && answer.getResult()) { return true; } else { String errMsg = (answer != null) ? answer.getDetails() : null; throw new CloudRuntimeException("Failed to unquiesce vm, due to " + errMsg); } } catch (AgentUnavailableException e) { throw new CloudRuntimeException("Failed to unquiesce vm", e); } catch (OperationTimedoutException e) { throw new CloudRuntimeException("Failed to unquiesce vm", e); } } }
s_logger.error("Invalid answer received in response to a StopCommand for " + vm.getInstanceName()); return false;
List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(virtualMachine.getId()); CreateVMSnapshotCommand ccmd = new CreateVMSnapshotCommand(virtualMachine.getInstanceName(), virtualMachine.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName()); HostVO host = hostDao.findById(hostId); GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
assert tpl != null : "How can a null template get here!!!"; PreparePxeServerCommand cmd = new PreparePxeServerCommand(ip, mac, mask, gateway, dns, tpl, profile.getVirtualMachine().getInstanceName(), dest.getHost().getName()); PreparePxeServerAnswer ans = (PreparePxeServerAnswer)_agentMgr.send(pxeServerId, cmd); if (!ans.getResult()) {