/** * Given the map of volume to target storage pool entered by the user, we check for other volumes that the VM might have and were not configured. * This map can be then used by CloudStack to find new target storage pools according to the target host. */ protected List<Volume> findVolumesThatWereNotMappedByTheUser(VirtualMachineProfile profile, Map<Volume, StoragePool> volumeToStoragePoolObjectMap) { List<VolumeVO> allVolumes = _volsDao.findUsableVolumesForInstance(profile.getId()); List<Volume> volumesNotMapped = new ArrayList<>(); for (Volume volume : allVolumes) { if (!volumeToStoragePoolObjectMap.containsKey(volume)) { volumesNotMapped.add(volume); } } return volumesNotMapped; }
private String getSshKey(VirtualMachineProfile profile) { final UserVmDetailVO vmDetailSshKey = _userVmDetailsDao.findDetail(profile.getId(), "SSH.PublicKey"); return (vmDetailSshKey!=null ? vmDetailSshKey.getValue() : null); }
@Override public void expungeNics(final VirtualMachineProfile vm) { final List<NicVO> nics = _nicDao.listByVmIdIncludingRemoved(vm.getId()); for (final NicVO nic : nics) { _nicDao.expunge(nic.getId()); } }
/** * Store password in vm details so it can be picked up during VM start. */ private void storePasswordInVmDetails(VirtualMachineProfile vm) { final String password = (String) vm.getParameter(VirtualMachineProfile.Param.VmPassword); final String password_encrypted = DBEncryptionUtil.encrypt(password); final UserVmVO userVmVO = _userVmDao.findById(vm.getId()); _userVmDetailsDao.addDetail(vm.getId(), "password", password_encrypted, false); userVmVO.setUpdateParameters(true); _userVmDao.update(userVmVO.getId(), userVmVO); }
@Override @DB public void release(final VirtualMachineProfile vmProfile, final boolean forced) throws ConcurrentOperationException, ResourceUnavailableException { final List<NicVO> nics = _nicDao.listByVmId(vmProfile.getId()); for (final NicVO nic : nics) { releaseNic(vmProfile, nic.getId()); } }
@Override public void finalizeStop(final VirtualMachineProfile profile, final Answer answer) { super.finalizeStop(profile, answer); // Mark VPN connections as Disconnected final DomainRouterVO router = _routerDao.findById(profile.getId()); final Long vpcId = router.getVpcId(); if (vpcId != null) { _s2sVpnMgr.markDisconnectVpnConnByVpc(vpcId); } }
@Override public void prepareStop(VirtualMachineProfile profile) { UserVmVO vm = _vmDao.findById(profile.getId()); if (vm != null && vm.getState() == State.Stopping) { collectVmDiskStatistics(vm); collectVmNetworkStatistics(vm); } }
@Override public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile profile) { UserVmVO vm = _vmDao.findById(profile.getId()); List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vm.getId()); RestoreVMSnapshotCommand command = _vmSnapshotMgr.createRestoreCommand(vm, vmSnapshots); if (command != null) { cmds.addCommand("restoreVMSnapshot", command); } return true; }
private Pair<Boolean, Boolean> findVMStorageRequirements(VirtualMachineProfile vmProfile) { boolean requiresShared = false, requiresLocal = false; List<VolumeVO> volumesTobeCreated = _volsDao.findUsableVolumesForInstance(vmProfile.getId()); // for each volume find whether shared or local pool is required for (VolumeVO toBeCreated : volumesTobeCreated) { DiskOfferingVO diskOffering = _diskOfferingDao.findById(toBeCreated.getDiskOfferingId()); if (diskOffering != null) { if (diskOffering.isUseLocalStorage()) { requiresLocal = true; } else { requiresShared = true; } } } return new Pair<Boolean, Boolean>(requiresShared, requiresLocal); }
@Override public void finalizeStop(VirtualMachineProfile profile, Answer answer) { //release elastic IP here if assigned IPAddressVO ip = _ipAddressDao.findByAssociatedVmId(profile.getId()); if (ip != null && ip.getSystem()) { CallContext ctx = CallContext.current(); try { _rulesMgr.disableStaticNat(ip.getId(), ctx.getCallingAccount(), ctx.getCallingUserId(), true); } catch (Exception ex) { s_logger.warn("Failed to disable static nat and release system ip " + ip + " as a part of vm " + profile.getVirtualMachine() + " stop due to exception ", ex); } } }
@Override public VirtualMachineTO implement(VirtualMachineProfile vm) { VirtualMachineTO to = toVirtualMachineTO(vm); VMInstanceVO vo = _vmDao.findById(vm.getId()); if (vo.getLastHostId() == null) { to.setBootArgs(BaremetalManager.DO_PXE); } Map<String, String> details = new HashMap<String, String>(); details.put("template", vm.getTemplate().getUrl()); to.setDetails(details); // Determine the VM's OS description GuestOSVO guestOS = _guestOsDao.findById(vm.getVirtualMachine().getGuestOSId()); to.setOs(guestOS.getDisplayName()); return to; }
@Override public void doInTransactionWithoutResult(TransactionStatus status) { final UserVmVO vm = _vmDao.findById(vmProfile.getId()); final List<NicVO> nics = _nicDao.listByVmId(vm.getId()); for (NicVO nic : nics) { Network network = _networkModel.getNetwork(nic.getNetworkId()); if (_networkModel.isSharedNetworkWithoutServices(network.getId())) { vmIdCountMap.put(nic.getId(), new VmAndCountDetails(nic.getInstanceId(), VmIpFetchTrialMax.value())); } } } });
@Override public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException { _router = router; UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao(); _userVM = userVmDao.findById(_profile.getId()); userVmDao.loadDetails(_userVM); //for basic zone, send vm data/password information only to the router in the same pod NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao(); _nicVo = nicDao.findById(_nic.getId()); return visitor.visit(this); }
@Override public boolean accept(final NetworkTopologyVisitor visitor, final VirtualRouter router) throws ResourceUnavailableException { _router = router; UserVmDao userVmDao = visitor.getVirtualNetworkApplianceFactory().getUserVmDao(); _userVM = userVmDao.findById(_profile.getId()); userVmDao.loadDetails(_userVM); NicDao nicDao = visitor.getVirtualNetworkApplianceFactory().getNicDao(); _nicVo = nicDao.findById(_nic.getId()); return visitor.visit(this); }
protected boolean applyUserData(HypervisorType hyperVisorType, UserVm vm, Nic nic) throws ResourceUnavailableException, InsufficientCapacityException { Network network = _networkDao.findById(nic.getNetworkId()); NicProfile nicProfile = new NicProfile(nic, network, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag( hyperVisorType, network)); VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm); if (_networkModel.areServicesSupportedByNetworkOffering(network.getNetworkOfferingId(), Service.UserData)) { UserDataServiceProvider element = _networkModel.getUserDataUpdateProvider(network); if (element == null) { throw new CloudRuntimeException("Can't find network element for " + Service.UserData.getName() + " provider needed for UserData update"); } boolean result = element.saveUserData(network, nicProfile, vmProfile); if (!result) { s_logger.error("Failed to update userdata for vm " + vm + " and nic " + nic); } else { return true; } } else { s_logger.debug("Not applying userdata for nic id=" + nic.getId() + " in vm id=" + vmProfile.getId() + " because it is not supported in network id=" + network.getId()); } return false; }
@Override public boolean finalizeDeployment(final Commands cmds, final VirtualMachineProfile profile, final DeployDestination dest, final ReservationContext context) throws ResourceUnavailableException { final DomainRouterVO internalLbVm = _internalLbVmDao.findById(profile.getId()); final List<NicProfile> nics = profile.getNics(); for (final NicProfile nic : nics) { if (nic.getTrafficType() == TrafficType.Control) { internalLbVm.setPrivateIpAddress(nic.getIPv4Address()); internalLbVm.setPrivateMacAddress(nic.getMacAddress()); } } _internalLbVmDao.update(internalLbVm.getId(), internalLbVm); finalizeCommandsOnStart(cmds, profile); return true; }
protected NicProfile getControlNic(final VirtualMachineProfile profile) { final DomainRouterVO router = _routerDao.findById(profile.getId()); final DataCenterVO dcVo = _dcDao.findById(router.getDataCenterId()); NicProfile controlNic = null; if (profile.getHypervisorType() == HypervisorType.VMware && dcVo.getNetworkType() == NetworkType.Basic) { // TODO this is a ugly to test hypervisor type here // for basic network mode, we will use the guest NIC for control NIC for (final NicProfile nic : profile.getNics()) { if (nic.getTrafficType() == TrafficType.Guest && nic.getIPv4Address() != null) { controlNic = nic; } } } else { for (final NicProfile nic : profile.getNics()) { if (nic.getTrafficType() == TrafficType.Control && nic.getIPv4Address() != null) { controlNic = nic; } } } return controlNic; }
protected void finalizeSshAndVersionAndNetworkUsageOnStart(final Commands cmds, final VirtualMachineProfile profile, final DomainRouterVO router, final NicProfile controlNic) { final DomainRouterVO vr = _routerDao.findById(profile.getId()); cmds.addCommand("checkSsh", new CheckSshCommand(profile.getInstanceName(), controlNic.getIPv4Address(), 3922)); // Update router template/scripts version final GetDomRVersionCmd command = new GetDomRVersionCmd(); command.setAccessDetail(NetworkElementCommand.ROUTER_IP, controlNic.getIPv4Address()); command.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmds.addCommand("getDomRVersion", command); // Network usage command to create iptables rules final boolean forVpc = vr.getVpcId() != null; if (!forVpc) { cmds.addCommand("networkUsage", new NetworkUsageCommand(controlNic.getIPv4Address(), router.getHostName(), "create", forVpc)); } }
private boolean configureConfigDriveData(final VirtualMachineProfile profile, final NicProfile nic) { final UserVmVO vm = _userVmDao.findById(profile.getId()); if (vm.getType() != VirtualMachine.Type.User) { return false; } final Nic defaultNic = _networkModel.getDefaultNic(vm.getId()); if (defaultNic != null) { final String sshPublicKey = getSshKey(profile); final String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId()).getDisplayText(); boolean isWindows = _guestOSCategoryDao.findById(_guestOSDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows"); final List<String[]> vmData = _networkModel.generateVmData(vm.getUserData(), serviceOffering, vm.getDataCenterId(), vm.getInstanceName(), vm.getHostName(), vm.getId(), vm.getUuid(), nic.getIPv4Address(), sshPublicKey, (String) profile.getParameter(VirtualMachineProfile.Param.VmPassword), isWindows); profile.setVmData(vmData); profile.setConfigDriveLabel(VirtualMachineManager.VmConfigDriveLabel.value()); } return true; }