Search in sources :

Example 76 with HostVO

use of com.cloud.host.HostVO in project cloudstack by apache.

the class VMSnapshotStrategyTest method testRevertSnapshot.

@Test
public void testRevertSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    Long hostId = 1L;
    Long vmId = 1L;
    Long guestOsId = 1L;
    HypervisorType hypervisorType = HypervisorType.Any;
    String hypervisorVersion = "default";
    String guestOsName = "Other";
    List<VolumeObjectTO> volumeObjectTOs = new ArrayList<VolumeObjectTO>();
    VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    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);
    GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    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);
    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);
    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 (CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    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);
    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(org.apache.cloudstack.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 77 with HostVO

use of com.cloud.host.HostVO in project cloudstack by apache.

the class VMSnapshotStrategyTest method testCreateVMSnapshot.

@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException {
    Long hostId = 1L;
    Long vmId = 1L;
    Long guestOsId = 1L;
    HypervisorType hypervisorType = HypervisorType.Any;
    String hypervisorVersion = "default";
    String guestOsName = "Other";
    List<VolumeObjectTO> volumeObjectTOs = new ArrayList<VolumeObjectTO>();
    VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
    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);
    GuestOSVO guestOSVO = Mockito.mock(GuestOSVO.class);
    Mockito.when(guestOSDao.findById(Matchers.anyLong())).thenReturn(guestOSVO);
    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);
    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 (CloudRuntimeException e1) {
        e = e1;
    }
    assertNotNull(e);
    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(org.apache.cloudstack.storage.to.VolumeObjectTO) Test(org.junit.Test)

Example 78 with HostVO

use of com.cloud.host.HostVO in project cloudstack by apache.

the class DefaultEndPointSelector method select.

@Override
public EndPoint select(DataStore store, String downloadUrl) {
    HostVO host = null;
    try {
        URI uri = new URI(downloadUrl);
        String scheme = uri.getScheme();
        String publicIp = uri.getHost();
        // If its https then public ip will be of the form xxx-xxx-xxx-xxx.mydomain.com
        if (scheme.equalsIgnoreCase("https")) {
            // We want xxx-xxx-xxx-xxx
            publicIp = publicIp.split("\\.")[0];
            // We not want the IP -  xxx.xxx.xxx.xxx
            publicIp = publicIp.replace("-", ".");
        }
        host = hostDao.findByPublicIp(publicIp);
        if (host != null) {
            return RemoteHostEndPoint.getHypervisorHostEndPoint(host);
        }
    } catch (URISyntaxException e) {
        s_logger.debug("Received URISyntaxException for url" + downloadUrl);
    }
    // If ssvm doesnt exist then find any ssvm in the zone.
    s_logger.debug("Coudn't find ssvm for url" + downloadUrl);
    return findEndpointForImageStorage(store);
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HostVO(com.cloud.host.HostVO)

Example 79 with HostVO

use of com.cloud.host.HostVO in project cloudstack by apache.

the class HypervisorHelperImpl method quiesceVm.

@Override
public VMSnapshotTO quiesceVm(VirtualMachine virtualMachine) {
    String value = configurationDao.getValue("vmsnapshot.create.wait");
    int wait = NumbersUtil.parseInt(value, 1800);
    Long hostId = vmSnapshotHelper.pickRunningHost(virtualMachine.getId());
    VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(1L, UUID.randomUUID().toString(), VMSnapshot.Type.Disk, null, null, false, null, true);
    GuestOSVO guestOS = guestOSDao.findById(virtualMachine.getGuestOSId());
    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());
    ccmd.setPlatformEmulator(guestOsMapping.getGuestOsName());
    ccmd.setWait(wait);
    try {
        Answer answer = agentMgr.send(hostId, ccmd);
        if (answer != null && answer.getResult()) {
            CreateVMSnapshotAnswer snapshotAnswer = (CreateVMSnapshotAnswer) answer;
            vmSnapshotTO.setVolumes(snapshotAnswer.getVolumeTOs());
        } else {
            String errMsg = (answer != null) ? answer.getDetails() : null;
            throw new CloudRuntimeException("Failed to quiesce vm, due to " + errMsg);
        }
    } catch (AgentUnavailableException e) {
        throw new CloudRuntimeException("Failed to quiesce vm", e);
    } catch (OperationTimedoutException e) {
        throw new CloudRuntimeException("Failed to quiesce vm", e);
    }
    return vmSnapshotTO;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) GuestOSVO(com.cloud.storage.GuestOSVO) CreateVMSnapshotCommand(com.cloud.agent.api.CreateVMSnapshotCommand) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) Answer(com.cloud.agent.api.Answer) IntroduceObjectAnswer(org.apache.cloudstack.storage.command.IntroduceObjectAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 80 with HostVO

use of com.cloud.host.HostVO in project cloudstack by apache.

the class VMSnapshotHelperImpl method pickRunningHost.

@Override
public Long pickRunningHost(Long vmId) {
    UserVmVO vm = userVmDao.findById(vmId);
    // use VM's host if VM is running
    if (vm.getState() == VirtualMachine.State.Running)
        return vm.getHostId();
    // check if lastHostId is available
    if (vm.getLastHostId() != null) {
        HostVO lastHost = hostDao.findByIdIncludingRemoved(vm.getLastHostId());
        if (lastHost.getStatus() == com.cloud.host.Status.Up && !lastHost.isInMaintenanceStates())
            return lastHost.getId();
    }
    List<VolumeVO> listVolumes = volumeDao.findByInstance(vmId);
    if (listVolumes == null || listVolumes.size() == 0) {
        throw new InvalidParameterValueException("vmInstance has no volumes");
    }
    VolumeVO volume = listVolumes.get(0);
    Long poolId = volume.getPoolId();
    if (poolId == null) {
        throw new InvalidParameterValueException("pool id is not found");
    }
    StoragePoolVO storagePool = primaryDataStoreDao.findById(poolId);
    if (storagePool == null) {
        throw new InvalidParameterValueException("storage pool is not found");
    }
    List<HostVO> listHost = hostDao.listAllUpAndEnabledNonHAHosts(Host.Type.Routing, storagePool.getClusterId(), storagePool.getPodId(), storagePool.getDataCenterId(), null);
    if (listHost == null || listHost.size() == 0) {
        throw new InvalidParameterValueException("no host in up state is found");
    }
    return listHost.get(0).getId();
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) HostVO(com.cloud.host.HostVO)

Aggregations

HostVO (com.cloud.host.HostVO)631 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)223 ArrayList (java.util.ArrayList)178 Answer (com.cloud.agent.api.Answer)105 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)95 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)91 Test (org.junit.Test)81 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)75 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)74 ClusterVO (com.cloud.dc.ClusterVO)72 Account (com.cloud.user.Account)67 HashMap (java.util.HashMap)67 VMInstanceVO (com.cloud.vm.VMInstanceVO)60 ConfigurationException (javax.naming.ConfigurationException)60 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)58 DataCenterVO (com.cloud.dc.DataCenterVO)50 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)48 HostPodVO (com.cloud.dc.HostPodVO)47 DB (com.cloud.utils.db.DB)47 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)46