Search in sources :

Example 6 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class OvsNetworkTopologyGuruImpl method getActiveVmsInNetworkOnHost.

/**
     * get the list of all Vm id's in the network that are running on the host
     */
@Override
public List<Long> getActiveVmsInNetworkOnHost(long networkId, long hostId, boolean includeVr) {
    List<Long> vmIds = new ArrayList<>();
    List<UserVmVO> vms = _userVmDao.listByNetworkIdAndStates(networkId, VirtualMachine.State.Running, VirtualMachine.State.Migrating);
    // Find routers for the network
    List<DomainRouterVO> routers = _routerDao.findByNetwork(networkId);
    if (vms != null) {
        for (UserVmVO vm : vms) {
            if (vm.getHostId() == hostId)
                vmIds.add(vm.getId());
        }
    }
    if (routers.size() != 0 && includeVr) {
        for (DomainRouterVO router : routers) {
            if (router.getHostId() == hostId)
                vmIds.add(router.getId());
        }
    }
    return vmIds;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 7 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class OvsNetworkTopologyGuruImpl method getAllActiveVmsInNetwork.

/**
     * get the list of all active Vm id's in a network
     */
@Override
public List<Long> getAllActiveVmsInNetwork(long networkId) {
    List<Long> vmIds = new ArrayList<>();
    List<UserVmVO> vms = _userVmDao.listByNetworkIdAndStates(networkId, VirtualMachine.State.Running, VirtualMachine.State.Starting, VirtualMachine.State.Stopping, VirtualMachine.State.Unknown, VirtualMachine.State.Migrating);
    // Find routers for the network
    List<DomainRouterVO> routers = _routerDao.findByNetwork(networkId);
    if (vms != null) {
        for (UserVmVO vm : vms) {
            vmIds.add(vm.getId());
        }
    }
    if (routers.size() != 0) {
        for (DomainRouterVO router : routers) {
            vmIds.add(router.getId());
        }
    }
    return vmIds;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 8 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class OvsNetworkTopologyGuruImpl method getNetworkSpanedHosts.

/**
     * get the list of hypervisor hosts on which VM's belonging to a network currently spans
     */
public List<Long> getNetworkSpanedHosts(long networkId) {
    List<Long> hostIds = new ArrayList<Long>();
    // Find active VMs with a NIC on the target network
    List<UserVmVO> vms = _userVmDao.listByNetworkIdAndStates(networkId, VirtualMachine.State.Running, VirtualMachine.State.Starting, VirtualMachine.State.Stopping, VirtualMachine.State.Unknown, VirtualMachine.State.Migrating);
    // Find routers for the network
    List<DomainRouterVO> routers = _routerDao.findByNetwork(networkId);
    List<VMInstanceVO> ins = new ArrayList<VMInstanceVO>();
    if (vms != null) {
        ins.addAll(vms);
    }
    if (routers.size() != 0) {
        ins.addAll(routers);
    }
    for (VMInstanceVO v : ins) {
        Long rh = v.getHostId();
        if (rh == null) {
            continue;
        }
        if (!hostIds.contains(rh)) {
            hostIds.add(rh);
        }
    }
    return hostIds;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 9 with UserVmVO

use of com.cloud.vm.UserVmVO in project cloudstack by apache.

the class CloudZonesNetworkElement method addPasswordAndUserdata.

@Override
public boolean addPasswordAndUserdata(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
    if (canHandle(dest, network.getTrafficType())) {
        if (vm.getType() != VirtualMachine.Type.User) {
            return false;
        }
        @SuppressWarnings("unchecked") UserVmVO uservm = _userVmDao.findById(vm.getId());
        _userVmDao.loadDetails(uservm);
        String password = (String) vm.getParameter(VirtualMachineProfile.Param.VmPassword);
        String userData = uservm.getUserData();
        String sshPublicKey = uservm.getDetail("SSH.PublicKey");
        Commands cmds = new Commands(Command.OnError.Continue);
        if (password != null && nic.isDefaultNic()) {
            SavePasswordCommand cmd = new SavePasswordCommand(password, nic.getIPv4Address(), uservm.getHostName(), _networkMgr.getExecuteInSeqNtwkElmtCmd());
            cmds.addCommand("password", cmd);
        }
        String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(uservm.getServiceOfferingId()).getDisplayText();
        String zoneName = _dcDao.findById(network.getDataCenterId()).getName();
        cmds.addCommand("vmdata", generateVmDataCommand(nic.getIPv4Address(), userData, serviceOffering, zoneName, nic.getIPv4Address(), uservm.getHostName(), uservm.getInstanceName(), uservm.getId(), uservm.getUuid(), sshPublicKey));
        try {
            _agentManager.send(dest.getHost().getId(), cmds);
        } catch (OperationTimedoutException e) {
            s_logger.debug("Unable to send vm data command to host " + dest.getHost());
            return false;
        }
        Answer dataAnswer = cmds.getAnswer("vmdata");
        if (dataAnswer != null && dataAnswer.getResult()) {
            s_logger.info("Sent vm data successfully to vm " + uservm.getInstanceName());
            return true;
        }
        s_logger.info("Failed to send vm data to vm " + uservm.getInstanceName());
        return false;
    }
    return false;
}
Also used : Answer(com.cloud.agent.api.Answer) UserVmVO(com.cloud.vm.UserVmVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) SavePasswordCommand(com.cloud.agent.api.routing.SavePasswordCommand) Commands(com.cloud.agent.manager.Commands)

Example 10 with UserVmVO

use of com.cloud.vm.UserVmVO 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)

Aggregations

UserVmVO (com.cloud.vm.UserVmVO)88 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)32 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)30 Account (com.cloud.user.Account)23 ArrayList (java.util.ArrayList)21 HostVO (com.cloud.host.HostVO)14 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)13 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)13 DataCenterVO (com.cloud.dc.DataCenterVO)12 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)12 VolumeVO (com.cloud.storage.VolumeVO)10 NicVO (com.cloud.vm.NicVO)10 VMInstanceVO (com.cloud.vm.VMInstanceVO)10 ActionEvent (com.cloud.event.ActionEvent)9 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)9 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)9 DomainRouterVO (com.cloud.vm.DomainRouterVO)9 Commands (com.cloud.agent.manager.Commands)8 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)8 VirtualRouter (com.cloud.network.router.VirtualRouter)8