Search in sources :

Example 1 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class UserVmManagerImpl method rebootVirtualMachine.

private UserVm rebootVirtualMachine(long userId, long vmId) throws InsufficientCapacityException, ResourceUnavailableException {
    UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null || vm.getState() == State.Destroyed || vm.getState() == State.Expunging || vm.getRemoved() != null) {
        s_logger.warn("Vm id=" + vmId + " doesn't exist");
        return null;
    }
    if (vm.getState() == State.Running && vm.getHostId() != null) {
        collectVmDiskStatistics(vm);
        DataCenterVO dc = _dcDao.findById(vm.getDataCenterId());
        try {
            if (dc.getNetworkType() == DataCenter.NetworkType.Advanced) {
                //List all networks of vm
                List<Long> vmNetworks = _vmNetworkMapDao.getNetworks(vmId);
                List<DomainRouterVO> routers = new ArrayList<DomainRouterVO>();
                //List the stopped routers
                for (long vmNetworkId : vmNetworks) {
                    List<DomainRouterVO> router = _routerDao.listStopped(vmNetworkId);
                    routers.addAll(router);
                }
                //and routers are started serially ,may revisit to make this process parallel
                for (DomainRouterVO routerToStart : routers) {
                    s_logger.warn("Trying to start router " + routerToStart.getInstanceName() + " as part of vm: " + vm.getInstanceName() + " reboot");
                    _virtualNetAppliance.startRouter(routerToStart.getId(), true);
                }
            }
        } catch (ConcurrentOperationException e) {
            throw new CloudRuntimeException("Concurrent operations on starting router. " + e);
        } catch (Exception ex) {
            throw new CloudRuntimeException("Router start failed due to" + ex);
        } finally {
            s_logger.info("Rebooting vm " + vm.getInstanceName());
            _itMgr.reboot(vm.getUuid(), null);
        }
        return _vmDao.findById(vmId);
    } else {
        s_logger.error("Vm id=" + vmId + " is not in Running state, failed to reboot");
        return null;
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException)

Example 2 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class UserVmManagerImpl method updateVirtualMachine.

@Override
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData, Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName, List<Long> securityGroupIdList) throws ResourceUnavailableException, InsufficientCapacityException {
    UserVmVO vm = _vmDao.findById(id);
    if (vm == null) {
        throw new CloudRuntimeException("Unable to find virtual machine with id " + id);
    }
    if (instanceName != null) {
        VMInstanceVO vmInstance = _vmInstanceDao.findVMByInstanceName(instanceName);
        if (vmInstance != null && vmInstance.getId() != id) {
            throw new CloudRuntimeException("Instance name : " + instanceName + " is not unique");
        }
    }
    if (vm.getState() == State.Error || vm.getState() == State.Expunging) {
        s_logger.error("vm is not in the right state: " + id);
        throw new InvalidParameterValueException("Vm with id " + id + " is not in the right state");
    }
    if (displayName == null) {
        displayName = vm.getDisplayName();
    }
    if (ha == null) {
        ha = vm.isHaEnabled();
    }
    ServiceOffering offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
    if (!offering.getOfferHA() && ha) {
        throw new InvalidParameterValueException("Can't enable ha for the vm as it's created from the Service offering having HA disabled");
    }
    if (isDisplayVmEnabled == null) {
        isDisplayVmEnabled = vm.isDisplayVm();
    }
    boolean updateUserdata = false;
    if (userData != null) {
        // check and replace newlines
        userData = userData.replace("\\n", "");
        validateUserData(userData, httpMethod);
        // update userData on domain router.
        updateUserdata = true;
    } else {
        userData = vm.getUserData();
    }
    if (isDynamicallyScalable == null) {
        isDynamicallyScalable = vm.isDynamicallyScalable();
    }
    if (osTypeId == null) {
        osTypeId = vm.getGuestOSId();
    }
    if (group != null) {
        addInstanceToGroup(id, group);
    }
    if (isDynamicallyScalable == null) {
        isDynamicallyScalable = vm.isDynamicallyScalable();
    }
    boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
    if (securityGroupIdList != null && isVMware) {
        throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
    } else {
        // Get default guest network in Basic zone
        Network defaultNetwork = null;
        try {
            DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
            if (zone.getNetworkType() == NetworkType.Basic) {
                // Get default guest network in Basic zone
                defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
            } else if (zone.isSecurityGroupEnabled()) {
                NicVO defaultNic = _nicDao.findDefaultNicForVM(vm.getId());
                if (defaultNic != null) {
                    defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
                }
            }
        } catch (InvalidParameterValueException e) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(e.getMessage(), e);
            }
            defaultNetwork = _networkModel.getDefaultNetworkForVm(id);
        }
        if (securityGroupIdList != null && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
            if (vm.getState() == State.Stopped) {
                // Remove instance from security groups
                _securityGroupMgr.removeInstanceFromGroups(id);
                // Add instance in provided groups
                _securityGroupMgr.addInstanceToGroups(id, securityGroupIdList);
            } else {
                throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
            }
        }
    }
    if (hostName != null) {
        // Check is hostName is RFC compliant
        checkNameForRFCCompliance(hostName);
        if (vm.getHostName().equalsIgnoreCase(hostName)) {
            s_logger.debug("Vm " + vm + " is already set with the hostName specified: " + hostName);
            hostName = null;
        }
        // Verify that vm's hostName is unique
        List<NetworkVO> vmNtwks = new ArrayList<NetworkVO>();
        List<? extends Nic> nics = _nicDao.listByVmId(vm.getId());
        for (Nic nic : nics) {
            vmNtwks.add(_networkDao.findById(nic.getNetworkId()));
        }
        checkIfHostNameUniqueInNtwkDomain(hostName, vmNtwks);
    }
    _vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName);
    if (updateUserdata) {
        boolean result = updateUserDataInternal(_vmDao.findById(id));
        if (result) {
            s_logger.debug("User data successfully updated for vm id=" + id);
        } else {
            throw new CloudRuntimeException("Failed to reset userdata for the virtual machine ");
        }
    }
    return _vmDao.findById(id);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) ServiceOffering(com.cloud.offering.ServiceOffering) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork)

Example 3 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class NuageVspGuestNetworkGuruTest method testReserve.

@Test
public void testReserve() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, URISyntaxException {
    final NetworkVO network = mock(NetworkVO.class);
    when(network.getId()).thenReturn(NETWORK_ID);
    when(network.getUuid()).thenReturn("aaaaaa");
    when(network.getDataCenterId()).thenReturn(NETWORK_ID);
    when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    when(network.getDomainId()).thenReturn(NETWORK_ID);
    when(network.getAccountId()).thenReturn(NETWORK_ID);
    when(network.getVpcId()).thenReturn(null);
    when(network.getBroadcastUri()).thenReturn(new URI("vsp://aaaaaa-aavvv/10.1.1.1"));
    final DataCenterVO dataCenter = mock(DataCenterVO.class);
    when(_dataCenterDao.findById(NETWORK_ID)).thenReturn(dataCenter);
    final AccountVO networksAccount = mock(AccountVO.class);
    when(networksAccount.getId()).thenReturn(NETWORK_ID);
    when(networksAccount.getUuid()).thenReturn("aaaa-abbbb");
    when(networksAccount.getType()).thenReturn(Account.ACCOUNT_TYPE_NORMAL);
    when(_accountDao.findById(NETWORK_ID)).thenReturn(networksAccount);
    final DomainVO networksDomain = mock(DomainVO.class);
    when(networksDomain.getId()).thenReturn(NETWORK_ID);
    when(networksDomain.getUuid()).thenReturn("aaaaa-bbbbb");
    when(_domainDao.findById(NETWORK_ID)).thenReturn(networksDomain);
    final NicVO nicvo = mock(NicVO.class);
    when(nicvo.getId()).thenReturn(NETWORK_ID);
    when(nicvo.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
    when(nicvo.getUuid()).thenReturn("aaaa-fffff");
    when(nicvo.getNetworkId()).thenReturn(NETWORK_ID);
    when(nicvo.getInstanceId()).thenReturn(NETWORK_ID);
    when(_nicDao.findById(NETWORK_ID)).thenReturn(nicvo);
    when(_nicDao.findDefaultNicForVM(NETWORK_ID)).thenReturn(nicvo);
    final VirtualMachine vm = mock(VirtualMachine.class);
    when(vm.getId()).thenReturn(NETWORK_ID);
    when(vm.getType()).thenReturn(VirtualMachine.Type.User);
    final VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
    when(vmProfile.getType()).thenReturn(VirtualMachine.Type.User);
    when(vmProfile.getInstanceName()).thenReturn("");
    when(vmProfile.getUuid()).thenReturn("aaaa-bbbbb");
    when(vmProfile.getVirtualMachine()).thenReturn(vm);
    NicProfile nicProfile = mock(NicProfile.class);
    when(nicProfile.getUuid()).thenReturn("aaa-bbbb");
    when(nicProfile.getId()).thenReturn(NETWORK_ID);
    when(nicProfile.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
    final NetworkOfferingVO ntwkoffering = mock(NetworkOfferingVO.class);
    when(ntwkoffering.getId()).thenReturn(NETWORK_ID);
    when(_networkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffering);
    when(_networkDao.acquireInLockTable(NETWORK_ID, 1200)).thenReturn(network);
    when(_ipAddressDao.findByVmIdAndNetworkId(NETWORK_ID, NETWORK_ID)).thenReturn(null);
    when(_domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
    final Answer answer = mock(Answer.class);
    when(answer.getResult()).thenReturn(true);
    when(_agentManager.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
    final ReservationContext reservationContext = mock(ReservationContext.class);
    when(reservationContext.getAccount()).thenReturn(networksAccount);
    when(reservationContext.getDomain()).thenReturn(networksDomain);
    _nuageVspGuestNetworkGuru.reserve(nicProfile, network, vmProfile, mock(DeployDestination.class), reservationContext);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NicProfile(com.cloud.vm.NicProfile) URI(java.net.URI) AccountVO(com.cloud.user.AccountVO) ReservationContext(com.cloud.vm.ReservationContext) DomainVO(com.cloud.domain.DomainVO) Answer(com.cloud.agent.api.Answer) DeployDestination(com.cloud.deploy.DeployDestination) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicVO(com.cloud.vm.NicVO) VirtualMachine(com.cloud.vm.VirtualMachine) NuageTest(com.cloud.NuageTest) Test(org.junit.Test)

Example 4 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cosmic by MissionCriticalCloud.

the class ResourceCheckerTest method test_checkIfDataCenterIsUsable_whenDataCenterIsEnabledAndAccountIsRoot.

@Test
public void test_checkIfDataCenterIsUsable_whenDataCenterIsEnabledAndAccountIsRoot() throws Exception {
    final DataCenterVO dataCenter = new DataCenterVO();
    dataCenter.setAllocationState(AllocationState.Enabled);
    final AccountVO account = new AccountVO(1L);
    when(accountManager.isRootAdmin(1L)).thenReturn(true);
    final ResourceChecker resourceChecker = buildResourceChecker();
    try {
        resourceChecker.checkIfDataCenterIsUsable(dataCenter, account);
    } catch (final PermissionDeniedException e) {
        fail("No PermissionDeniedException should have be generated");
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) AccountVO(com.cloud.user.AccountVO) Test(org.junit.Test)

Example 5 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cosmic by MissionCriticalCloud.

the class ResourceCheckerTest method test_checkIfPodIsUsable_whenPodDoesNotBelongToDataCenter.

@Test(expected = InvalidParameterValueException.class)
public void test_checkIfPodIsUsable_whenPodDoesNotBelongToDataCenter() throws Exception {
    final DataCenterVO dataCenter = new DataCenterVO();
    dataCenter.setId(1L);
    dataCenter.setUuid("DataCenterUUID");
    final HostPodVO hostPod = new HostPodVO();
    hostPod.setDataCenterId(2L);
    hostPod.setUuid("HostPodUUID");
    final ResourceChecker resourceChecker = buildResourceChecker();
    resourceChecker.checkIfPodIsUsable(dataCenter, hostPod);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) HostPodVO(com.cloud.dc.HostPodVO) Test(org.junit.Test)

Aggregations

DataCenterVO (com.cloud.dc.DataCenterVO)305 ArrayList (java.util.ArrayList)91 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)86 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)65 Account (com.cloud.user.Account)61 HostVO (com.cloud.host.HostVO)51 NetworkVO (com.cloud.network.dao.NetworkVO)48 HashMap (java.util.HashMap)45 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)44 DB (com.cloud.utils.db.DB)42 HostPodVO (com.cloud.dc.HostPodVO)40 ClusterVO (com.cloud.dc.ClusterVO)38 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)37 Network (com.cloud.network.Network)37 DomainRouterVO (com.cloud.vm.DomainRouterVO)36 ConfigurationException (javax.naming.ConfigurationException)35 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)29 NetworkTopology (org.apache.cloudstack.network.topology.NetworkTopology)29 Test (org.junit.Test)28 ActionEvent (com.cloud.event.ActionEvent)27