Search in sources :

Example 96 with UserVmVO

use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.

the class VirtualRouterElement method savePassword.

@Override
public boolean savePassword(final Network network, final NicProfile nic, final VirtualMachineProfile vm) throws ResourceUnavailableException {
    if (!canHandle(network, null)) {
        return false;
    }
    final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
    if (routers == null || routers.isEmpty()) {
        s_logger.debug("Can't find virtual router element in network " + network.getId());
        return true;
    }
    final VirtualMachineProfile uservm = vm;
    final Zone zone = zoneRepository.findOne(network.getDataCenterId());
    final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(zone);
    // save the password in DB
    for (final VirtualRouter router : routers) {
        if (router.getState() == State.Running) {
            return networkTopology.savePasswordToRouter(network, nic, uservm, router);
        }
    }
    final String password = (String) uservm.getParameter(VirtualMachineProfile.Param.VmPassword);
    final String password_encrypted = DBEncryptionUtil.encrypt(password);
    final UserVmVO userVmVO = _userVmDao.findById(vm.getId());
    _userVmDao.loadDetails(userVmVO);
    userVmVO.setDetail("password", password_encrypted);
    _userVmDao.saveDetails(userVmVO);
    userVmVO.setUpdateParameters(true);
    _userVmDao.update(userVmVO.getId(), userVmVO);
    return true;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) Zone(com.cloud.db.model.Zone) NetworkTopology(com.cloud.network.topology.NetworkTopology) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DomainRouterVO(com.cloud.vm.DomainRouterVO) VirtualRouter(com.cloud.network.router.VirtualRouter)

Example 97 with UserVmVO

use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.

the class XenServerGuru method implement.

@Override
public VirtualMachineTO implement(final VirtualMachineProfile vm) {
    BootloaderType bt = BootloaderType.PyGrub;
    if (vm.getBootLoaderType() == BootloaderType.CD) {
        bt = vm.getBootLoaderType();
    }
    final VirtualMachineTO to = toVirtualMachineTO(vm);
    final UserVmVO userVmVO = _userVmDao.findById(vm.getId());
    if (userVmVO != null) {
        final HostVO host = hostDao.findById(userVmVO.getHostId());
        if (host != null) {
            to.setVcpuMaxLimit(MaxNumberOfVCPUSPerVM.valueIn(host.getClusterId()));
        }
    }
    to.setBootloader(bt);
    // Determine the VM's OS description
    final GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId());
    to.setOs(guestOS.getDisplayName());
    final HostVO host = hostDao.findById(vm.getVirtualMachine().getHostId());
    GuestOSHypervisorVO guestOsMapping = null;
    if (host != null) {
        guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion());
    }
    if (guestOsMapping == null || host == null) {
        to.setPlatformEmulator(null);
    } else {
        to.setPlatformEmulator(guestOsMapping.getGuestOsName());
    }
    return to;
}
Also used : GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) UserVmVO(com.cloud.vm.UserVmVO) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) GuestOSVO(com.cloud.storage.GuestOSVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) HostVO(com.cloud.host.HostVO)

Example 98 with UserVmVO

use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testCreateVMSnapshot.

@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    final Long hostId = 1L;
    final Long vmId = 1L;
    final Long guestOsId = 1L;
    final HypervisorType hypervisorType = HypervisorType.Any;
    final String hypervisorVersion = "default";
    final String guestOsName = "Other";
    final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
    final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    final HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final CreateVMSnapshotAnswer answer = Mockito.mock(CreateVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(true);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    VMSnapshot snapshot = null;
    snapshot = vmSnapshotStrategy.takeVMSnapshot(vmSnapshot);
    assertNotNull(snapshot);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 99 with UserVmVO

use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.

the class VMSnapshotStrategyTest method testRevertSnapshot.

@Test
public void testRevertSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    final Long hostId = 1L;
    final Long vmId = 1L;
    final Long guestOsId = 1L;
    final HypervisorType hypervisorType = HypervisorType.Any;
    final String hypervisorVersion = "default";
    final String guestOsName = "Other";
    final List<VolumeObjectTO> volumeObjectTOs = new ArrayList<>();
    final VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    final UserVmVO userVmVO = Mockito.mock(UserVmVO.class);
    Mockito.when(userVmVO.getGuestOSId()).thenReturn(guestOsId);
    Mockito.when(vmSnapshot.getVmId()).thenReturn(vmId);
    Mockito.when(vmSnapshotHelper.pickRunningHost(Matchers.anyLong())).thenReturn(hostId);
    Mockito.when(vmSnapshotHelper.getVolumeTOList(Matchers.anyLong())).thenReturn(volumeObjectTOs);
    Mockito.when(userVmDao.findById(Matchers.anyLong())).thenReturn(userVmVO);
    final GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    final GuestOSHypervisorVO guestOSHypervisorVO = Mockito.mock(GuestOSHypervisorVO.class);
    Mockito.when(guestOSHypervisorVO.getGuestOsName()).thenReturn(guestOsName);
    Mockito.when(guestOsHypervisorDao.findById(Matchers.anyLong())).thenReturn(guestOSHypervisorVO);
    Mockito.when(guestOsHypervisorDao.findByOsIdAndHypervisor(Matchers.anyLong(), Matchers.anyString(), Matchers.anyString())).thenReturn(guestOSHypervisorVO);
    final VMSnapshotTO vmSnapshotTO = Mockito.mock(VMSnapshotTO.class);
    Mockito.when(vmSnapshotHelper.getSnapshotWithParents(Matchers.any(VMSnapshotVO.class))).thenReturn(vmSnapshotTO);
    Mockito.when(vmSnapshotDao.findById(Matchers.anyLong())).thenReturn(vmSnapshot);
    Mockito.when(vmSnapshot.getId()).thenReturn(1L);
    Mockito.when(vmSnapshot.getCreated()).thenReturn(new Date());
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(null);
    final HostVO hostVO = Mockito.mock(HostVO.class);
    Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(hostVO);
    Mockito.when(hostVO.getHypervisorType()).thenReturn(hypervisorType);
    Mockito.when(hostVO.getHypervisorVersion()).thenReturn(hypervisorVersion);
    Exception e = null;
    try {
        vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
    } catch (final CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    final RevertToVMSnapshotAnswer answer = Mockito.mock(RevertToVMSnapshotAnswer.class);
    Mockito.when(answer.getResult()).thenReturn(Boolean.TRUE);
    Mockito.when(agentMgr.send(Matchers.anyLong(), Matchers.any(Command.class))).thenReturn(answer);
    final boolean result = vmSnapshotStrategy.revertVMSnapshot(vmSnapshot);
    assertTrue(result);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) Date(java.util.Date) HostVO(com.cloud.host.HostVO) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) Command(com.cloud.agent.api.Command) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 100 with UserVmVO

use of com.cloud.vm.UserVmVO in project cosmic by MissionCriticalCloud.

the class AffinityGroupServiceImpl method updateVMAffinityGroups.

@Override
public UserVm updateVMAffinityGroups(final Long vmId, final List<Long> affinityGroupIds) {
    // Verify input parameters
    final UserVmVO vmInstance = _userVmDao.findById(vmId);
    if (vmInstance == null) {
        throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
    }
    final Account caller = CallContext.current().getCallingAccount();
    final Account owner = _accountMgr.getAccount(vmInstance.getAccountId());
    // check that the affinity groups exist
    for (final Long affinityGroupId : affinityGroupIds) {
        final AffinityGroupVO AffinityGroupVO = _affinityGroupDao.findById(affinityGroupId);
        if (AffinityGroupVO == null) {
            throw new InvalidParameterValueException("Unable to find affinity group by id " + affinityGroupId);
        } else {
            // verify permissions (same as when deploying VM)
            _accountMgr.checkAccess(caller, null, false, owner, AffinityGroupVO);
            // Only Explicit Dedication can be handled in non-Stopped state
            if ("ExplicitDedication".equals(AffinityGroupVO.getType())) {
                // Check if VM is currently running on host Explicitly Dedicated to this domain
                Boolean VmRunsOnDedicatedHost = false;
                final Pair<List<DedicatedResourceVO>, Integer> result = _dedicatedDao.searchDedicatedHosts(vmInstance.getHostId(), vmInstance.getDomainId(), null, affinityGroupId);
                final List<DedicatedResourceVO> DedicatedResourceList = result.first();
                for (final DedicatedResourceVO dedicatedResourceVO : DedicatedResourceList) {
                    if (dedicatedResourceVO.getHostId() != null && dedicatedResourceVO.getHostId().equals(vmInstance.getHostId())) {
                        VmRunsOnDedicatedHost = true;
                    }
                }
                if (!VmRunsOnDedicatedHost && !State.Stopped.equals(vmInstance.getState())) {
                    throw new InvalidParameterValueException("Unable update Explicit Dedication affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is either stopped or running on a host that is part of the " + "Explicit Dedication Affinity Group.");
                }
            } else {
                if (!State.Stopped.equals(vmInstance.getState())) {
                    throw new InvalidParameterValueException("Unable update affinity groups of the virtual machine " + vmInstance.toString() + " " + "in state " + vmInstance.getState() + "; make sure the virtual machine is stopped and not in an error state before updating.");
                }
            }
            // owner of these entities is same
            if (caller.getId() == Account.ACCOUNT_ID_SYSTEM || _accountMgr.isRootAdmin(caller.getId())) {
                if (!_affinityGroupService.isAffinityGroupAvailableInDomain(AffinityGroupVO.getId(), owner.getDomainId())) {
                    throw new PermissionDeniedException("Affinity Group " + AffinityGroupVO + " does not belong to the VM's domain");
                }
            }
        }
    }
    _affinityGroupVMMapDao.updateMap(vmId, affinityGroupIds);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Updated VM :" + vmId + " affinity groups to =" + affinityGroupIds);
    }
    // APIResponseHelper will pull out the updated affinitygroups.
    return vmInstance;
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) List(java.util.List) ArrayList(java.util.ArrayList) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO)

Aggregations

UserVmVO (com.cloud.vm.UserVmVO)190 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)79 Account (com.cloud.user.Account)50 ArrayList (java.util.ArrayList)45 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)39 HostVO (com.cloud.host.HostVO)34 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)33 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)31 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)30 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)28 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)25 VolumeVO (com.cloud.storage.VolumeVO)25 VMInstanceVO (com.cloud.vm.VMInstanceVO)24 ActionEvent (com.cloud.event.ActionEvent)23 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)22 GuestOSVO (com.cloud.storage.GuestOSVO)20 HashMap (java.util.HashMap)19 ConfigurationException (javax.naming.ConfigurationException)19 Test (org.junit.Test)19 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)18