Search in sources :

Example 31 with VMInstanceVO

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

the class VmwareStorageMotionStrategy method copyAsync.

@Override
public void copyAsync(Map<VolumeInfo, DataStore> volumeMap, VirtualMachineTO vmTo, Host srcHost, Host destHost, AsyncCompletionCallback<CopyCommandResult> callback) {
    Answer answer = null;
    String errMsg = null;
    try {
        VMInstanceVO instance = instanceDao.findById(vmTo.getId());
        if (instance != null) {
            if (srcHost.getClusterId().equals(destHost.getClusterId())) {
                answer = migrateVmWithVolumesWithinCluster(instance, vmTo, srcHost, destHost, volumeMap);
            } else {
                answer = migrateVmWithVolumesAcrossCluster(instance, vmTo, srcHost, destHost, volumeMap);
            }
        } else {
            throw new CloudRuntimeException("Unsupported operation requested for moving data.");
        }
    } catch (Exception e) {
        s_logger.error("copy failed", e);
        errMsg = e.toString();
    }
    CopyCommandResult result = new CopyCommandResult(null, answer);
    result.setResult(errMsg);
    callback.complete(result);
}
Also used : Answer(com.cloud.agent.api.Answer) MigrateWithStorageAnswer(com.cloud.agent.api.MigrateWithStorageAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMInstanceVO(com.cloud.vm.VMInstanceVO) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException)

Example 32 with VMInstanceVO

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

the class VirtualMachineModelTest method testCreateVirtualMachine.

@Test
public void testCreateVirtualMachine() throws IOException {
    String uuid = UUID.randomUUID().toString();
    ContrailManagerImpl contrailMgr = mock(ContrailManagerImpl.class);
    ModelController controller = mock(ModelController.class);
    ApiConnector api = new ApiConnectorMock(null, 0);
    when(controller.getManager()).thenReturn(contrailMgr);
    when(controller.getApiAccessor()).thenReturn(api);
    // Create Virtual-Network (VN)
    NetworkVO network = mock(NetworkVO.class);
    when(network.getName()).thenReturn("testnetwork");
    when(network.getState()).thenReturn(Network.State.Allocated);
    when(network.getGateway()).thenReturn("10.1.1.1");
    when(network.getCidr()).thenReturn("10.1.1.0/24");
    when(network.getPhysicalNetworkId()).thenReturn(42L);
    when(network.getDomainId()).thenReturn(10L);
    when(network.getAccountId()).thenReturn(42L);
    when(contrailMgr.getCanonicalName(network)).thenReturn("testnetwork");
    when(contrailMgr.getProjectId(network.getDomainId(), network.getAccountId())).thenReturn("testProjectId");
    // Create Virtual-Machine (VM)
    VMInstanceVO vm = mock(VMInstanceVO.class);
    when(vm.getInstanceName()).thenReturn("testVM1");
    when(vm.getState()).thenReturn(VirtualMachine.State.Starting);
    when(vm.getDomainId()).thenReturn(10L);
    when(vm.getAccountId()).thenReturn(42L);
    UserVmDao VmDao = mock(UserVmDao.class);
    when(VmDao.findById(anyLong())).thenReturn(null);
    when(controller.getVmDao()).thenReturn(VmDao);
    VirtualMachineModel vmModel = new VirtualMachineModel(vm, uuid);
    assertEquals(vmModel.getInstanceName(), "testVM1");
    assertEquals(vmModel.getUuid(), uuid);
    vmModel.build(controller, vm);
    try {
        vmModel.update(controller);
    } catch (Exception ex) {
        fail("virtual-network update failed ");
    }
    //verify
    assertTrue(vmModel.verify(controller));
}
Also used : ContrailManagerImpl(org.apache.cloudstack.network.contrail.management.ContrailManagerImpl) NetworkVO(com.cloud.network.dao.NetworkVO) ApiConnector(net.juniper.contrail.api.ApiConnector) VMInstanceVO(com.cloud.vm.VMInstanceVO) UserVmDao(com.cloud.vm.dao.UserVmDao) ApiConnectorMock(net.juniper.contrail.api.ApiConnectorMock) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with VMInstanceVO

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

the class ConsoleProxyManagerImpl method doAssignProxy.

public ConsoleProxyVO doAssignProxy(long dataCenterId, long vmId) {
    ConsoleProxyVO proxy = null;
    VMInstanceVO vm = _instanceDao.findById(vmId);
    if (vm == null) {
        s_logger.warn("VM " + vmId + " no longer exists, return a null proxy for vm:" + vmId);
        return null;
    }
    if (vm != null && vm.getState() != State.Running) {
        if (s_logger.isInfoEnabled()) {
            s_logger.info("Detected that vm : " + vmId + " is not currently at running state, we will fail the proxy assignment for it");
        }
        return null;
    }
    if (_allocProxyLock.lock(ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_SYNC)) {
        try {
            if (vm.getProxyId() != null) {
                proxy = _consoleProxyDao.findById(vm.getProxyId());
                if (proxy != null) {
                    if (!isInAssignableState(proxy)) {
                        if (s_logger.isInfoEnabled()) {
                            s_logger.info("A previous assigned proxy is not assignable now, reassign console proxy for user vm : " + vmId);
                        }
                        proxy = null;
                    } else {
                        if (_consoleProxyDao.getProxyActiveLoad(proxy.getId()) < _capacityPerProxy || hasPreviousSession(proxy, vm)) {
                            if (s_logger.isTraceEnabled()) {
                                s_logger.trace("Assign previous allocated console proxy for user vm : " + vmId);
                            }
                            if (proxy.getActiveSession() >= _capacityPerProxy) {
                                s_logger.warn("Assign overloaded proxy to user VM as previous session exists, user vm : " + vmId);
                            }
                        } else {
                            proxy = null;
                        }
                    }
                }
            }
            if (proxy == null) {
                proxy = assignProxyFromRunningPool(dataCenterId);
            }
        } finally {
            _allocProxyLock.unlock();
        }
    } else {
        s_logger.error("Unable to acquire synchronization lock to get/allocate proxy resource for vm :" + vmId + ". Previous console proxy allocation is taking too long");
    }
    if (proxy == null) {
        s_logger.warn("Unable to find or allocate console proxy resource");
        return null;
    }
    // if it is a new assignment or a changed assignment, update the record
    if (vm.getProxyId() == null || vm.getProxyId().longValue() != proxy.getId()) {
        _instanceDao.updateProxyId(vmId, proxy.getId(), DateUtil.currentGMTTime());
    }
    proxy.setSslEnabled(_sslEnabled);
    if (_sslEnabled) {
        proxy.setPort(443);
    } else {
        proxy.setPort(80);
    }
    return proxy;
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) ConsoleProxyVO(com.cloud.vm.ConsoleProxyVO)

Example 34 with VMInstanceVO

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

the class RulesManagerImpl method isIpReadyForStaticNat.

protected void isIpReadyForStaticNat(long vmId, IPAddressVO ipAddress, String vmIp, Account caller, long callerUserId) throws NetworkRuleConflictException, ResourceUnavailableException {
    if (ipAddress.isSourceNat()) {
        throw new InvalidParameterValueException("Can't enable static, ip address " + ipAddress + " is a sourceNat ip address");
    }
    if (!ipAddress.isOneToOneNat()) {
        // Dont allow to enable static nat if PF/LB rules exist for the IP
        List<FirewallRuleVO> portForwardingRules = _firewallDao.listByIpAndPurposeAndNotRevoked(ipAddress.getId(), Purpose.PortForwarding);
        if (portForwardingRules != null && !portForwardingRules.isEmpty()) {
            throw new NetworkRuleConflictException("Failed to enable static nat for the ip address " + ipAddress + " as it already has PortForwarding rules assigned");
        }
        List<FirewallRuleVO> loadBalancingRules = _firewallDao.listByIpAndPurposeAndNotRevoked(ipAddress.getId(), Purpose.LoadBalancing);
        if (loadBalancingRules != null && !loadBalancingRules.isEmpty()) {
            throw new NetworkRuleConflictException("Failed to enable static nat for the ip address " + ipAddress + " as it already has LoadBalancing rules assigned");
        }
    } else if (ipAddress.getAssociatedWithVmId() != null && ipAddress.getAssociatedWithVmId().longValue() != vmId) {
        throw new NetworkRuleConflictException("Failed to enable static for the ip address " + ipAddress + " and vm id=" + vmId + " as it's already assigned to antoher vm");
    }
    //check wether the vm ip is alreday associated with any public ip address
    IPAddressVO oldIP = _ipAddressDao.findByAssociatedVmIdAndVmIp(vmId, vmIp);
    if (oldIP != null) {
        // If elasticIP functionality is supported in the network, we always have to disable static nat on the old
        // ip in order to re-enable it on the new one
        Long networkId = oldIP.getAssociatedWithNetworkId();
        VMInstanceVO vm = _vmInstanceDao.findById(vmId);
        boolean reassignStaticNat = false;
        if (networkId != null) {
            Network guestNetwork = _networkModel.getNetwork(networkId);
            NetworkOffering offering = _entityMgr.findById(NetworkOffering.class, guestNetwork.getNetworkOfferingId());
            if (offering.getElasticIp()) {
                reassignStaticNat = true;
            }
        }
        // If there is public ip address already associated with the vm, throw an exception
        if (!reassignStaticNat) {
            throw new InvalidParameterValueException("Failed to enable static nat on the  ip " + ipAddress.getAddress() + " with Id " + ipAddress.getUuid() + " as the vm " + vm.getInstanceName() + " with Id " + vm.getUuid() + " is already associated with another public ip " + oldIP.getAddress() + " with id " + oldIP.getUuid());
        }
        // unassign old static nat rule
        s_logger.debug("Disassociating static nat for ip " + oldIP);
        if (!disableStaticNat(oldIP.getId(), caller, callerUserId, true)) {
            throw new CloudRuntimeException("Failed to disable old static nat rule for vm " + vm.getInstanceName() + " with id " + vm.getUuid() + "  and public ip " + oldIP);
        }
    }
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkOffering(com.cloud.offering.NetworkOffering) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) VMInstanceVO(com.cloud.vm.VMInstanceVO) IPAddressVO(com.cloud.network.dao.IPAddressVO) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException)

Example 35 with VMInstanceVO

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

the class RulesManagerImpl method listAssociatedRulesForGuestNic.

@Override
public List<FirewallRuleVO> listAssociatedRulesForGuestNic(Nic nic) {
    s_logger.debug("Checking if PF/StaticNat/LoadBalancer rules are configured for nic " + nic.getId());
    List<FirewallRuleVO> result = new ArrayList<FirewallRuleVO>();
    // add PF rules
    result.addAll(_portForwardingDao.listByNetworkAndDestIpAddr(nic.getIPv4Address(), nic.getNetworkId()));
    if (result.size() > 0) {
        s_logger.debug("Found " + result.size() + " portforwarding rule configured for the nic in the network " + nic.getNetworkId());
    }
    // add static NAT rules
    List<FirewallRuleVO> staticNatRules = _firewallDao.listStaticNatByVmId(nic.getInstanceId());
    for (FirewallRuleVO rule : staticNatRules) {
        if (rule.getNetworkId() == nic.getNetworkId()) {
            result.add(rule);
            s_logger.debug("Found rule " + rule.getId() + " " + rule.getPurpose() + " configured");
        }
    }
    List<? extends IpAddress> staticNatIps = _ipAddressDao.listStaticNatPublicIps(nic.getNetworkId());
    for (IpAddress ip : staticNatIps) {
        if (ip.getVmIp() != null && ip.getVmIp().equals(nic.getIPv4Address())) {
            VMInstanceVO vm = _vmInstanceDao.findById(nic.getInstanceId());
            // generate a static Nat rule on the fly because staticNATrule does not persist into db anymore
            // FIX ME
            FirewallRuleVO staticNatRule = new FirewallRuleVO(null, ip.getId(), 0, 65535, NetUtils.ALL_PROTO.toString(), nic.getNetworkId(), vm.getAccountId(), vm.getDomainId(), Purpose.StaticNat, null, null, null, null, null);
            result.add(staticNatRule);
            s_logger.debug("Found rule " + staticNatRule.getId() + " " + staticNatRule.getPurpose() + " configured");
        }
    }
    // add LB rules
    List<LoadBalancerVMMapVO> lbMapList = _loadBalancerVMMapDao.listByInstanceId(nic.getInstanceId());
    for (LoadBalancerVMMapVO lb : lbMapList) {
        FirewallRuleVO lbRule = _firewallDao.findById(lb.getLoadBalancerId());
        if (lbRule.getNetworkId() == nic.getNetworkId()) {
            result.add(lbRule);
            s_logger.debug("Found rule " + lbRule.getId() + " " + lbRule.getPurpose() + " configured");
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) IpAddress(com.cloud.network.IpAddress) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO)

Aggregations

VMInstanceVO (com.cloud.vm.VMInstanceVO)131 ArrayList (java.util.ArrayList)40 HostVO (com.cloud.host.HostVO)34 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)28 Account (com.cloud.user.Account)28 VolumeVO (com.cloud.storage.VolumeVO)24 Test (org.junit.Test)24 HostPodVO (com.cloud.dc.HostPodVO)15 HashMap (java.util.HashMap)14 NetworkVO (com.cloud.network.dao.NetworkVO)13 User (com.cloud.user.User)13 NicVO (com.cloud.vm.NicVO)13 VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)13 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)12 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)12 ActionEvent (com.cloud.event.ActionEvent)11 Random (java.util.Random)11 Answer (com.cloud.agent.api.Answer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10