use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class LibvirtComputingResourceTest method testPrepareForMigrationCommandMigration.
@Test
public void testPrepareForMigrationCommandMigration() {
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 diskTO = Mockito.mock(DiskTO.class);
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final PrepareForMigrationCommand command = new PrepareForMigrationCommand(vm);
when(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(vm.getDisks()).thenReturn(new DiskTO[] { diskTO });
when(nicTO.getType()).thenReturn(TrafficType.Guest);
when(diskTO.getType()).thenReturn(Volume.Type.ISO);
when(libvirtComputingResource.getVifDriver(nicTO.getType())).thenReturn(vifDriver);
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolManager);
when(storagePoolManager.connectPhysicalDisksViaVmSpec(vm)).thenReturn(true);
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vm.getName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(vm, times(1)).getNics();
verify(vm, times(1)).getDisks();
verify(diskTO, times(1)).getType();
}
use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class SimulatorGuru method implement.
@Override
public VirtualMachineTO implement(VirtualMachineProfile vm) {
VirtualMachineTO to = toVirtualMachineTO(vm);
// Determine the VM's OS description
GuestOSVO guestOS = _guestOsDao.findById(vm.getVirtualMachine().getGuestOSId());
to.setOs(guestOS.getDisplayName());
return to;
}
use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class AgentRoutingResource method execute.
protected synchronized Answer execute(StartCommand cmd) throws IllegalArgumentException {
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
String vmName = vmSpec.getName();
if (this.totalCpu < (vmSpec.getCpus() * vmSpec.getMaxSpeed() + this.usedCpu) || this.totalMem < (vmSpec.getMaxRam() + this.usedMem)) {
return new StartAnswer(cmd, "Not enough resource to start the vm");
}
Answer result = _simMgr.simulate(cmd, hostGuid);
if (!result.getResult()) {
return new StartAnswer(cmd, result.getDetails());
}
this.usedCpu += vmSpec.getCpus() * vmSpec.getMaxSpeed();
this.usedMem += vmSpec.getMaxRam();
_runningVms.put(vmName, new Pair<Long, Long>(Long.valueOf(vmSpec.getCpus() * vmSpec.getMaxSpeed()), vmSpec.getMaxRam()));
return new StartAnswer(cmd);
}
use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class XenServer610MigrateWithStorageReceiveCommandWrapper method execute.
@Override
public Answer execute(final MigrateWithStorageReceiveCommand command, final XenServer610Resource xenServer610Resource) {
final Connection connection = xenServer610Resource.getConnection();
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final List<Pair<VolumeTO, String>> volumeToStorageUuid = command.getVolumeToStorageUuid();
try {
// In a cluster management server setup, the migrate with storage receive and send
// commands and answers may have to be forwarded to another management server. This
// happens when the host/resource on which the command has to be executed is owned
// by the second management server. The serialization/deserialization of the command
// and answers fails as the xapi SR and Network class type isn't understand by the
// agent attache. Seriliaze the SR and Network objects here to a string and pass in
// the answer object. It'll be deserialzed and object created in migrate with
// storage send command execution.
Gson gson = new Gson();
// Get a map of all the SRs to which the vdis will be migrated.
final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<>();
for (final Pair<VolumeTO, String> entry : volumeToStorageUuid) {
final String storageUuid = entry.second();
final SR sr = xenServer610Resource.getStorageRepository(connection, storageUuid);
volumeToSr.add(new Pair<VolumeTO, Object>(entry.first(), sr));
}
// Get the list of networks to which the vifs will attach.
final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<Pair<NicTO, Object>>();
for (final NicTO nicTo : vmSpec.getNics()) {
final Network network = xenServer610Resource.getNetwork(connection, nicTo);
nicToNetwork.add(new Pair<NicTO, Object>(nicTo, network));
}
final XsLocalNetwork nativeNetworkForTraffic = xenServer610Resource.getNativeNetworkForTraffic(connection, TrafficType.Storage, null);
final Network network = nativeNetworkForTraffic.getNetwork();
final XsHost xsHost = xenServer610Resource.getHost();
final String uuid = xsHost.getUuid();
final Map<String, String> other = new HashMap<String, String>();
other.put("live", "true");
final Host host = Host.getByUuid(connection, uuid);
final Map<String, String> token = host.migrateReceive(connection, network, other);
return new MigrateWithStorageReceiveAnswer(command, volumeToSr, nicToNetwork, token);
} catch (final CloudRuntimeException e) {
s_logger.error("Migration of vm " + vmSpec.getName() + " with storage failed due to " + e.toString(), e);
return new MigrateWithStorageReceiveAnswer(command, e);
} catch (final Exception e) {
s_logger.error("Migration of vm " + vmSpec.getName() + " with storage failed due to " + e.toString(), e);
return new MigrateWithStorageReceiveAnswer(command, e);
}
}
use of com.cloud.agent.api.to.VirtualMachineTO in project CloudStack-archive by CloudStack-extras.
the class CloudZonesComputingResource method execute.
@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
String vmName = vmSpec.getName();
LibvirtVMDef vm = null;
State state = State.Stopped;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
synchronized (_vms) {
_vms.put(vmName, State.Starting);
}
vm = createVMFromSpec(vmSpec);
createVbd(conn, vmSpec, vmName, vm);
createVifs(conn, vmSpec, vm);
s_logger.debug("starting " + vmName + ": " + vm.toString());
startDomain(conn, vmName, vm.toString());
NicTO[] nics = vmSpec.getNics();
for (NicTO nic : nics) {
if (nic.isSecurityGroupEnabled()) {
if (vmSpec.getType() != VirtualMachine.Type.User) {
default_network_rules_for_systemvm(conn, vmName);
} else {
nic.setIp(null);
default_network_rules(conn, vmName, nic, vmSpec.getId());
}
}
}
// attached disk
for (DiskDef disk : vm.getDevices().getDisks()) {
if (disk.isAttachDeferred()) {
attachOrDetachDevice(conn, true, vmName, disk.toString());
}
}
if (vmSpec.getType() == VirtualMachine.Type.User) {
for (NicTO nic : nics) {
if (nic.getType() == TrafficType.Guest) {
InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
if (ipAddr == null) {
s_logger.debug("Failed to get guest DHCP ip, stop it");
StopCommand stpCmd = new StopCommand(vmName);
execute(stpCmd);
return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
}
s_logger.debug(ipAddr);
nic.setIp(ipAddr.getHostAddress());
post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
_vmDataServer.handleVmStarted(cmd.getVirtualMachine());
}
}
}
state = State.Running;
return new StartAnswer(cmd);
} catch (Exception e) {
s_logger.warn("Exception ", e);
if (conn != null) {
handleVmStartFailure(conn, vmName, vm);
}
return new StartAnswer(cmd, e.getMessage());
} finally {
synchronized (_vms) {
if (state != State.Stopped) {
_vms.put(vmName, state);
} else {
_vms.remove(vmName);
}
}
}
}
Aggregations