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);
}
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));
}
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;
}
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);
}
}
}
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;
}
Aggregations