use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class LibvirtComputingResourceTest method testCreateVMFromSpecLegacy.
/**
This test tests if the Agent can handle a vmSpec coming
from a <=4.1 management server.
The overcommit feature has not been merged in there and thus
only 'speed' is set.
*/
@Test
public void testCreateVMFromSpecLegacy() {
final int id = random.nextInt(65534);
final String name = "test-instance-1";
final int cpus = random.nextInt(2) + 1;
final int speed = 1024;
final int minRam = 256 * 1024;
final int maxRam = 512 * 1024;
final String os = "Ubuntu";
final String vncAddr = "";
final String vncPassword = "mySuperSecretPassword";
final LibvirtComputingResource lcr = new LibvirtComputingResource();
final VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, speed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword);
to.setVncAddr(vncAddr);
to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
final LibvirtVMDef vm = lcr.createVMFromSpec(to);
vm.setHvsType(hyperVisorType);
verifyVm(to, vm);
}
use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class LibvirtComputingResourceTest method testCreateVMFromSpec.
/**
This test tests if the Agent can handle a vmSpec coming
from a >4.1 management server.
It tests if the Agent can handle a vmSpec with overcommit
data like minSpeed and maxSpeed in there
*/
@Test
public void testCreateVMFromSpec() {
final int id = random.nextInt(65534);
final String name = "test-instance-1";
final int cpus = random.nextInt(2) + 1;
final int minSpeed = 1024;
final int maxSpeed = 2048;
final int minRam = 256 * 1024;
final int maxRam = 512 * 1024;
final String os = "Ubuntu";
final String vncAddr = "";
final String vncPassword = "mySuperSecretPassword";
final LibvirtComputingResource lcr = new LibvirtComputingResource();
final VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, minSpeed, maxSpeed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword);
to.setVncAddr(vncAddr);
to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
final LibvirtVMDef vm = lcr.createVMFromSpec(to);
vm.setHvsType(hyperVisorType);
verifyVm(to, vm);
}
use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class VmwareResource method execute.
protected ScaleVmAnswer execute(ScaleVmCommand cmd) {
VmwareContext context = getServiceContext();
VirtualMachineTO vmSpec = cmd.getVirtualMachine();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
int ramMb = getReservedMemoryMb(vmSpec);
long hotaddIncrementSizeInMb;
long hotaddMemoryLimitInMb;
long requestedMaxMemoryInMb = vmSpec.getMaxRam() / (1024 * 1024);
// Check if VM is really running on hypervisor host
if (getVmPowerState(vmMo) != PowerState.PowerOn) {
throw new CloudRuntimeException("Found that the VM " + vmMo.getVmName() + " is not running. Unable to scale-up this VM");
}
// Check max hot add limit
hotaddIncrementSizeInMb = vmMo.getHotAddMemoryIncrementSizeInMb();
hotaddMemoryLimitInMb = vmMo.getHotAddMemoryLimitInMb();
if (requestedMaxMemoryInMb > hotaddMemoryLimitInMb) {
throw new CloudRuntimeException("Memory of VM " + vmMo.getVmName() + " cannot be scaled to " + requestedMaxMemoryInMb + "MB." + " Requested memory limit is beyond the hotadd memory limit for this VM at the moment is " + hotaddMemoryLimitInMb + "MB.");
}
// Check increment is multiple of increment size
long reminder = requestedMaxMemoryInMb % hotaddIncrementSizeInMb;
if (reminder != 0) {
requestedMaxMemoryInMb = requestedMaxMemoryInMb + hotaddIncrementSizeInMb - reminder;
}
// Check if license supports the feature
VmwareHelper.isFeatureLicensed(hyperHost, FeatureKeyConstants.HOTPLUG);
VmwareHelper.setVmScaleUpConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), vmSpec.getMinSpeed(), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Unable to execute ScaleVmCommand");
}
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new ScaleVmAnswer(cmd, false, "Unable to execute ScaleVmCommand due to " + e.toString());
}
return new ScaleVmAnswer(cmd, true, null);
}
use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.
the class UserVmManagerImpl method finalizeStart.
@Override
public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) {
UserVmVO vm = _vmDao.findById(profile.getId());
Answer[] answersToCmds = cmds.getAnswers();
if (answersToCmds == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Returning from finalizeStart() since there are no answers to read");
}
return true;
}
Answer startAnswer = cmds.getAnswer(StartAnswer.class);
String returnedIp = null;
String originalIp = null;
if (startAnswer != null) {
StartAnswer startAns = (StartAnswer) startAnswer;
VirtualMachineTO vmTO = startAns.getVirtualMachine();
for (NicTO nicTO : vmTO.getNics()) {
if (nicTO.getType() == TrafficType.Guest) {
returnedIp = nicTO.getIp();
}
}
}
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
NicVO guestNic = null;
NetworkVO guestNetwork = null;
for (NicVO nic : nics) {
NetworkVO network = _networkDao.findById(nic.getNetworkId());
long isDefault = (nic.isDefaultNic()) ? 1 : 0;
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, isDefault, VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplay());
if (network.getTrafficType() == TrafficType.Guest) {
originalIp = nic.getIPv4Address();
guestNic = nic;
guestNetwork = network;
// In vmware, we will be effecting pvlan settings in portgroups in StartCommand.
if (profile.getHypervisorType() != HypervisorType.VMware) {
if (nic.getBroadcastUri().getScheme().equals("pvlan")) {
NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, false, "pvlan-nic");
if (!setupVmForPvlan(true, hostId, nicProfile)) {
return false;
}
}
}
}
}
boolean ipChanged = false;
if (originalIp != null && !originalIp.equalsIgnoreCase(returnedIp)) {
if (returnedIp != null && guestNic != null) {
guestNic.setIPv4Address(returnedIp);
ipChanged = true;
}
}
if (returnedIp != null && !returnedIp.equalsIgnoreCase(originalIp)) {
if (guestNic != null) {
guestNic.setIPv4Address(returnedIp);
ipChanged = true;
}
}
if (ipChanged) {
_dcDao.findById(vm.getDataCenterId());
UserVmVO userVm = _vmDao.findById(profile.getId());
// dc.getDhcpProvider().equalsIgnoreCase(Provider.ExternalDhcpServer.getName())
if (_ntwkSrvcDao.canProviderSupportServiceInNetwork(guestNetwork.getId(), Service.Dhcp, Provider.ExternalDhcpServer)) {
_nicDao.update(guestNic.getId(), guestNic);
userVm.setPrivateIpAddress(guestNic.getIPv4Address());
_vmDao.update(userVm.getId(), userVm);
s_logger.info("Detected that ip changed in the answer, updated nic in the db with new ip " + returnedIp);
}
}
// get system ip and create static nat rule for the vm
try {
_rulesMgr.getSystemIpAndEnableStaticNatForVm(profile.getVirtualMachine(), false);
} catch (Exception ex) {
s_logger.warn("Failed to get system ip and enable static nat for the vm " + profile.getVirtualMachine() + " due to exception ", ex);
return false;
}
Answer answer = cmds.getAnswer("restoreVMSnapshot");
if (answer != null && answer instanceof RestoreVMSnapshotAnswer) {
RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) answer;
if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) {
s_logger.warn("Unable to restore the vm snapshot from image file to the VM: " + restoreVMSnapshotAnswer.getDetails());
}
}
final VirtualMachineProfile vmProfile = profile;
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
final UserVmVO vm = _vmDao.findById(vmProfile.getId());
final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
for (NicVO nic : nics) {
Network network = _networkModel.getNetwork(nic.getNetworkId());
if (_networkModel.isSharedNetworkWithoutServices(network.getId())) {
vmIdCountMap.put(nic.getId(), new VmAndCountDetails(nic.getInstanceId(), VmIpFetchTrialMax.value()));
}
}
}
});
return true;
}
Aggregations