use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareResource method execute.
private ReplugNicAnswer execute(ReplugNicCommand cmd) {
getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
String vmName = cmd.getVmName();
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(vmName);
}
}
if (vmMo == null) {
String msg = "Router " + vmName + " no longer exists to execute ReplugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
/*
if(!isVMWareToolsInstalled(vmMo)){
String errMsg = "vmware tools is not installed or not running, cannot add nic to vm " + vmName;
s_logger.debug(errMsg);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
}
*/
// Fallback to E1000 if no specific nicAdapter is passed
VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
Map<String, String> details = cmd.getDetails();
if (details != null) {
nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
}
NicTO nicTo = cmd.getNic();
VirtualDevice nic = findVirtualNicDevice(vmMo, nicTo.getMac());
if (nic == null) {
return new ReplugNicAnswer(cmd, false, "Nic to replug not found");
}
Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
String dvSwitchUuid = null;
if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
VmwareHelper.updateDvNicDevice(nic, networkInfo.first(), dvSwitchUuid);
} else {
s_logger.info("Preparing NIC device on network " + networkInfo.second());
VmwareHelper.updateNicDevice(nic, networkInfo.first(), networkInfo.second());
}
configureNicDevice(vmMo, nic, VirtualDeviceConfigSpecOperation.EDIT, "ReplugNicCommand");
return new ReplugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new ReplugNicAnswer(cmd, false, "Unable to execute ReplugNicCommand due to " + e.toString());
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareResource method mapSpecDisksToClonedDisksAndTearDownDatadisks.
/**
* Modify the specDisks information to match the cloned VM's disks (from vmMo VM)
*/
private void mapSpecDisksToClonedDisksAndTearDownDatadisks(VirtualMachineMO vmMo, String vmInternalCSName, DiskTO[] specDisks) {
try {
s_logger.debug("Mapping spec disks information to cloned VM disks for VM " + vmInternalCSName);
if (vmMo != null && ArrayUtils.isNotEmpty(specDisks)) {
List<VirtualDisk> vmDisks = vmMo.getVirtualDisksOrderedByKey();
List<VirtualDisk> rootDisks = new ArrayList<>();
List<DiskTO> sortedRootDisksFromSpec = Arrays.asList(sortVolumesByDeviceId(specDisks)).stream().filter(x -> x.getType() == Volume.Type.ROOT).collect(Collectors.toList());
for (int i = 0; i < sortedRootDisksFromSpec.size(); i++) {
DiskTO specDisk = sortedRootDisksFromSpec.get(i);
VirtualDisk vmDisk = vmDisks.get(i);
DataTO dataVolume = specDisk.getData();
if (dataVolume instanceof VolumeObjectTO) {
VolumeObjectTO volumeObjectTO = (VolumeObjectTO) dataVolume;
if (!volumeObjectTO.getSize().equals(vmDisk.getCapacityInBytes())) {
s_logger.info("Mapped disk size is not the same as the cloned VM disk size: " + volumeObjectTO.getSize() + " - " + vmDisk.getCapacityInBytes());
}
VirtualDeviceBackingInfo backingInfo = vmDisk.getBacking();
if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo backing = (VirtualDiskFlatVer2BackingInfo) backingInfo;
String fileName = backing.getFileName();
if (StringUtils.isNotBlank(fileName)) {
String[] fileNameParts = fileName.split(" ");
String datastoreUuid = fileNameParts[0].replace("[", "").replace("]", "");
String relativePath = fileNameParts[1].split("/")[1].replace(".vmdk", "");
String vmSpecDatastoreUuid = volumeObjectTO.getDataStore().getUuid().replaceAll("-", "");
if (!datastoreUuid.equals(vmSpecDatastoreUuid)) {
s_logger.info("Mapped disk datastore UUID is not the same as the cloned VM datastore UUID: " + datastoreUuid + " - " + vmSpecDatastoreUuid);
}
volumeObjectTO.setPath(relativePath);
specDisk.setPath(relativePath);
rootDisks.add(vmDisk);
} else {
s_logger.error("Empty backing filename for volume " + volumeObjectTO.getName());
}
} else {
s_logger.error("Could not get volume backing info for volume " + volumeObjectTO.getName());
}
}
}
vmDisks.removeAll(rootDisks);
if (CollectionUtils.isNotEmpty(vmDisks)) {
s_logger.info("Tearing down datadisks for deploy-as-is VM");
tearDownVMDisks(vmMo, vmDisks);
}
}
} catch (Exception e) {
String msg = "Error mapping deploy-as-is VM disks from cloned VM " + vmInternalCSName;
s_logger.error(msg, e);
throw new CloudRuntimeException(e);
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareResource method execute.
private Answer execute(PrepareUnmanageVMInstanceCommand cmd) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
String instanceName = cmd.getInstanceName();
try {
s_logger.debug(String.format("Verify if VMware instance: [%s] is available before unmanaging VM.", cmd.getInstanceName()));
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(getServiceContext(), dcMor);
VirtualMachineMO vm = dataCenterMo.findVm(instanceName);
if (vm == null) {
return new PrepareUnmanageVMInstanceAnswer(cmd, false, String.format("Cannot find VM with name [%s] in datacenter [%s].", instanceName, dataCenterMo.getName()));
}
} catch (Exception e) {
s_logger.error("Error trying to verify if VM to unmanage exists", e);
return new PrepareUnmanageVMInstanceAnswer(cmd, false, "Error: " + e.getMessage());
}
return new PrepareUnmanageVMInstanceAnswer(cmd, true, "OK");
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(UnregisterVMCommand cmd) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
DatacenterMO dataCenterMo = new DatacenterMO(getServiceContext(), hyperHost.getHyperHostDatacenter());
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
if (vmMo != null) {
try {
VirtualMachineFileLayoutEx vmFileLayout = vmMo.getFileLayout();
context.getService().unregisterVM(vmMo.getMor());
if (cmd.getCleanupVmFiles()) {
deleteUnregisteredVmFiles(vmFileLayout, dataCenterMo, false, null);
}
return new Answer(cmd, true, "unregister succeeded");
} catch (Exception e) {
s_logger.warn("We are not able to unregister VM " + VmwareHelper.getExceptionMessage(e));
}
String msg = "Expunge failed in vSphere. vm: " + cmd.getVmName();
s_logger.warn(msg);
return new Answer(cmd, false, msg);
} else {
String msg = "Unable to find the VM in vSphere to unregister, assume it is already removed. VM: " + cmd.getVmName();
s_logger.warn(msg);
return new Answer(cmd, true, msg);
}
} catch (Exception e) {
return new Answer(cmd, false, createLogMessageException(e, cmd));
}
}
use of com.cloud.hypervisor.vmware.mo.VirtualMachineMO in project cloudstack by apache.
the class VmwareStorageManagerImpl method execute.
@Override
public RevertToVMSnapshotAnswer execute(VmwareHostService hostService, RevertToVMSnapshotCommand cmd) {
String snapshotName = cmd.getTarget().getSnapshotName();
String vmName = cmd.getVmName();
Boolean snapshotMemory = cmd.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
VirtualMachineMO vmMo = null;
VmwareContext context = hostService.getServiceContext(cmd);
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
// wait if there are already VM revert task running
ManagedObjectReference taskmgr = context.getServiceContent().getTaskManager();
List<ManagedObjectReference> tasks = context.getVimClient().getDynamicProperty(taskmgr, "recentTask");
for (ManagedObjectReference taskMor : tasks) {
TaskInfo info = (TaskInfo) (context.getVimClient().getDynamicProperty(taskMor, "info"));
if (info.getEntityName().equals(cmd.getVmName()) && org.apache.commons.lang3.StringUtils.isNotBlank(info.getName()) && info.getName().equalsIgnoreCase("RevertToSnapshot_Task")) {
s_logger.debug("There is already a VM snapshot task running, wait for it");
context.getVimClient().waitForTask(taskMor);
}
}
HostMO hostMo = (HostMO) hyperHost;
vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
}
if (vmMo == null) {
String msg = "Unable to find VM for RevertToVMSnapshotCommand";
s_logger.debug(msg);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} else {
if (cmd.isReloadVm()) {
vmMo.reload();
}
boolean result = false;
if (snapshotName != null) {
ManagedObjectReference morSnapshot = vmMo.getSnapshotMor(snapshotName);
result = hostMo.revertToSnapshot(morSnapshot);
} else {
return new RevertToVMSnapshotAnswer(cmd, false, "Unable to find the snapshot by name " + snapshotName);
}
if (result) {
Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
setVolumeToPathAndSize(listVolumeTo, mapNewDisk, context, hyperHost, cmd.getVmName());
if (!snapshotMemory) {
vmState = VirtualMachine.PowerState.PowerOff;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} else {
return new RevertToVMSnapshotAnswer(cmd, false, "Error while reverting to snapshot due to execute in ESXi");
}
}
} catch (Exception e) {
String msg = "revert vm " + vmName + " to snapshot " + snapshotName + " failed due to " + e.getMessage();
s_logger.error(msg);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
}
}
Aggregations