use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method createVifs.
public void createVifs(final VirtualMachineTO vmSpec, final LibvirtVmDef vm) throws InternalErrorException, LibvirtException {
final Map<String, String> params = vmSpec.getDetails();
String nicAdapter = "";
if (params != null && params.get("nicAdapter") != null && !params.get("nicAdapter").isEmpty()) {
nicAdapter = params.get("nicAdapter");
}
for (final NicTO nic : vmSpec.getNics()) {
createVif(vm, nic, nicAdapter);
}
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPrepareForMigrationCommandInternalErrorException.
@Test
public void testPrepareForMigrationCommandInternalErrorException() {
final Connect conn = Mockito.mock(Connect.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final VirtualMachineTO vm = Mockito.mock(VirtualMachineTO.class);
final KvmStoragePoolManager storagePoolManager = Mockito.mock(KvmStoragePoolManager.class);
final NicTO nicTO = Mockito.mock(NicTO.class);
final DiskTO volume = Mockito.mock(DiskTO.class);
final PrepareForMigrationCommand command = new PrepareForMigrationCommand(vm);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vm.getName())).thenReturn(conn);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(vm.getNics()).thenReturn(new NicTO[] { nicTO });
when(nicTO.getType()).thenReturn(TrafficType.Guest);
when(this.libvirtComputingResource.getVifDriver(nicTO.getType())).thenThrow(InternalErrorException.class);
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolManager);
try {
when(this.libvirtComputingResource.getVolumePath(conn, volume)).thenReturn("/path");
} catch (final LibvirtException e) {
fail(e.getMessage());
} catch (final URISyntaxException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vm.getName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(vm, times(1)).getNics();
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testPrepareForMigrationCommandLibvirtException.
@Test
public void testPrepareForMigrationCommandLibvirtException() {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final VirtualMachineTO vm = Mockito.mock(VirtualMachineTO.class);
final KvmStoragePoolManager storagePoolManager = Mockito.mock(KvmStoragePoolManager.class);
final NicTO nicTO = Mockito.mock(NicTO.class);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final PrepareForMigrationCommand command = new PrepareForMigrationCommand(vm);
when(this.libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vm.getName())).thenThrow(LibvirtException.class);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(vm.getNics()).thenReturn(new NicTO[] { nicTO });
when(nicTO.getType()).thenReturn(TrafficType.Guest);
when(this.libvirtComputingResource.getVifDriver(nicTO.getType())).thenReturn(vifDriver);
when(this.libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolManager);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, this.libvirtComputingResource);
assertFalse(answer.getResult());
verify(this.libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vm.getName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
verify(this.libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(vm, times(1)).getNics();
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class LibvirtStartCommandWrapper method execute.
@Override
public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) {
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final String vmName = vmSpec.getName();
LibvirtVmDef vm = null;
DomainState state = DomainState.VIR_DOMAIN_SHUTOFF;
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = null;
try {
vm = libvirtComputingResource.createVmFromSpec(vmSpec);
conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
final Long remainingMem = getFreeMemory(conn, libvirtComputingResource);
if (remainingMem == null) {
return new StartAnswer(command, "Failed to get free memory");
} else if (remainingMem < vmSpec.getMinRam()) {
return new StartAnswer(command, "Not enough memory on the host, remaining: " + remainingMem + ", asking: " + vmSpec.getMinRam());
}
final NicTO[] nics = vmSpec.getNics();
libvirtComputingResource.createVbd(conn, vmSpec, vmName, vm);
if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) {
return new StartAnswer(command, "Failed to connect physical disks to host");
}
libvirtComputingResource.createVifs(vmSpec, vm);
s_logger.debug("starting " + vmName + ": " + vm.toString());
libvirtComputingResource.startVm(conn, vmName, vm.toString());
// system vms
if (vmSpec.getType() != VirtualMachineType.User) {
// pass cmdline with config for the systemvm to configure itself
if (libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())) {
s_logger.debug("Passing cmdline succeeded");
} else {
final String errorMessage = "Passing cmdline failed, aborting.";
s_logger.debug(errorMessage);
return new StartAnswer(command, errorMessage);
}
String controlIp = null;
for (final NicTO nic : nics) {
if (nic.getType() == TrafficType.Control) {
controlIp = nic.getIp();
break;
}
}
// connect to the router by using its linklocal address (that should now be configured)
s_logger.debug("Starting ssh attempts to " + controlIp);
final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
if (!virtRouterResource.connect(controlIp, 30, 5000)) {
final String errorMessage = "Unable to login to router via linklocal address " + controlIp + " after 30 tries, aborting.";
s_logger.debug(errorMessage);
return new StartAnswer(command, errorMessage);
}
s_logger.debug("Successfully completed ssh attempts to " + controlIp);
}
state = DomainState.VIR_DOMAIN_RUNNING;
return new StartAnswer(command);
} catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
s_logger.warn("Exception while starting VM: " + ExceptionUtils.getRootCauseMessage(e));
if (conn != null) {
libvirtComputingResource.handleVmStartFailure(vm);
}
return new StartAnswer(command, e.getMessage());
} finally {
if (state != DomainState.VIR_DOMAIN_RUNNING) {
storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec);
}
}
}
use of com.cloud.legacymodel.to.NicTO in project cosmic by MissionCriticalCloud.
the class VirtualMachineManagerImpl method orchestrateRemoveNicFromVm.
private boolean orchestrateRemoveNicFromVm(final VirtualMachine vm, final Nic nic) throws ConcurrentOperationException, ResourceUnavailableException {
final CallContext cctx = CallContext.current();
final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
final NetworkVO network = _networkDao.findById(nic.getNetworkId());
final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
final Zone zone = _zoneRepository.findById(network.getDataCenterId()).orElse(null);
final Host host = _hostDao.findById(vm.getHostId());
final DeployDestination dest = new DeployDestination(zone, null, null, host);
final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
// 1) Unplug the nic
if (vm.getState() == State.Running) {
final NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType());
s_logger.debug("Un-plugging nic " + nic + " for vm " + vm + " from network " + network);
final boolean result = unplugNic(network, nicTO, vmTO, context, dest);
if (result) {
s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network);
final long isDefault = nic.isDefaultNic() ? 1 : 0;
} else {
s_logger.warn("Failed to unplug nic for the vm " + vm + " from network " + network);
return false;
}
} else if (vm.getState() != State.Stopped) {
s_logger.warn("Unable to remove vm " + vm + " from network " + network);
throw new ResourceUnavailableException("Unable to remove vm " + vm + " from network, is not in the right state", DataCenter.class, vm.getDataCenterId());
}
// 2) Release the nic
_networkMgr.releaseNic(vmProfile, nic);
s_logger.debug("Successfully released nic " + nic + "for vm " + vm);
// 3) Remove the nic
_networkMgr.removeNic(vmProfile, nic);
_nicsDao.expunge(nic.getId());
return true;
}
Aggregations