use of com.vmware.vim25.mo.Folder in project vsphere-cloud-plugin by jenkinsci.
the class VSphere method createRelocateSpec.
private VirtualMachineRelocateSpec createRelocateSpec(PrintStream jLogger, boolean linkedClone, String resourcePoolName, String cluster, String datastoreName, boolean isResourcePoolRequired) throws RemoteException, MalformedURLException, VSphereException {
VirtualMachineRelocateSpec rel = new VirtualMachineRelocateSpec();
if (linkedClone) {
rel.setDiskMoveType("createNewChildDiskBacking");
} else {
rel.setDiskMoveType("moveAllDiskBackingsAndDisallowSharing");
}
ClusterComputeResource clusterResource = getClusterByName(cluster);
// probably only of interest if someone actually entered a cluster name
if (clusterResource == null && StringUtils.isNotBlank(cluster)) {
logMessage(jLogger, "Cluster resource " + cluster + " does not exist, root folder will be used for getting resource pool and datastore");
}
if (resourcePoolName != null && !resourcePoolName.isEmpty()) {
ResourcePool resourcePool = getResourcePoolByName(resourcePoolName, clusterResource);
if (resourcePool == null) {
throw new VSphereNotFoundException("Resource pool", resourcePoolName);
}
rel.setPool(resourcePool.getMOR());
} else if (isResourcePoolRequired) {
throw new VSphereException("You must specify a resource pool when using a template");
}
if (datastoreName != null && !datastoreName.isEmpty()) {
Datastore datastore = getDatastoreByName(datastoreName, clusterResource);
if (datastore == null) {
throw new VSphereNotFoundException("Datastore", datastoreName);
}
rel.setDatastore(datastore.getMOR());
}
return rel;
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VmwareStorageProcessor method deleteVolume.
@Override
public Answer deleteVolume(DeleteCommand cmd) {
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
DataStoreTO store = vol.getDataStore();
PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO) store;
Map<String, String> details = primaryDataStoreTO.getDetails();
boolean isManaged = false;
String managedDatastoreName = null;
if (details != null) {
isManaged = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
if (isManaged) {
managedDatastoreName = getManagedDatastoreNameFromPath(vol.getPath());
}
}
ManagedObjectReference morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, isManaged ? managedDatastoreName : store.getUuid());
if (morDs == null) {
String msg = "Unable to find datastore based on volume mount point " + store.getUuid();
s_logger.error(msg);
throw new Exception(msg);
}
DatastoreMO dsMo = new DatastoreMO(context, morDs);
ManagedObjectReference morDc = hyperHost.getHyperHostDatacenter();
DatacenterMO dcMo = new DatacenterMO(context, morDc);
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ClusterMO clusterMo = new ClusterMO(context, morCluster);
if (vol.getVolumeType() == Volume.Type.ROOT) {
String vmName = vol.getVmName();
if (vmName != null) {
VirtualMachineMO vmMo = clusterMo.findVmOnHyperHost(vmName);
if (vmMo == null) {
// Volume might be on a zone-wide storage pool, look for VM in datacenter
vmMo = dcMo.findVm(vmName);
}
List<Map<String, String>> dynamicTargetsToRemove = null;
boolean deployAsIs = vol.isDeployAsIs();
if (vmMo != null) {
if (s_logger.isInfoEnabled()) {
if (deployAsIs) {
s_logger.info("Destroying root volume " + vol.getPath() + " of deploy-as-is VM " + vmName);
} else {
s_logger.info("Destroy root volume and VM itself. vmName " + vmName);
}
}
VirtualMachineDiskInfo diskInfo = null;
if (vol.getChainInfo() != null)
diskInfo = _gson.fromJson(vol.getChainInfo(), VirtualMachineDiskInfo.class);
HostMO hostMo = vmMo.getRunningHost();
List<NetworkDetails> networks = vmMo.getNetworksWithDetails();
// tear down all devices first before we destroy the VM to avoid accidently delete disk backing files
if (VmwareResource.getVmState(vmMo) != PowerState.PowerOff) {
vmMo.safePowerOff(_shutdownWaitMs);
}
// call this before calling detachAllDisksExcept
// when expunging a VM, we need to see if any of its disks are serviced by managed storage
// if there is one or more disk serviced by managed storage, remove the iSCSI connection(s)
// don't remove the iSCSI connection(s) until the supported disk(s) is/are removed from the VM
// (removeManagedTargetsFromCluster should be called after detachAllDisksExcept and vm.destroy)
List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
List<String> managedDatastoreNames = getManagedDatastoreNamesFromVirtualDisks(virtualDisks);
// Preserve other disks of the VM
List<String> detachedDisks = vmMo.detachAllDisksExcept(vol.getPath(), diskInfo != null ? diskInfo.getDiskDeviceBusName() : null);
VmwareStorageLayoutHelper.moveVolumeToRootFolder(new DatacenterMO(context, morDc), detachedDisks);
// vmMo.tearDownDevices(new Class<?>[] { VirtualDisk.class, VirtualEthernetCard.class });
if (isManaged) {
vmMo.unregisterVm();
} else {
vmMo.destroy();
}
// this.hostService.handleDatastoreAndVmdkDetach(iScsiName, storageHost, storagePort);
if (managedDatastoreNames != null && !managedDatastoreNames.isEmpty()) {
removeManagedTargetsFromCluster(managedDatastoreNames);
}
for (NetworkDetails netDetails : networks) {
if (netDetails.getGCTag() != null && netDetails.getGCTag().equalsIgnoreCase("true")) {
if (netDetails.getVMMorsOnNetwork() == null || netDetails.getVMMorsOnNetwork().length == 1) {
resource.cleanupNetwork(dcMo, netDetails);
}
}
}
} else if (deployAsIs) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroying root volume " + vol.getPath() + " of already removed deploy-as-is VM " + vmName);
}
// The disks of the deploy-as-is VM have been detached from the VM and moved to root folder
String deployAsIsRootDiskPath = dsMo.searchFileInSubFolders(vol.getPath() + VmwareResource.VMDK_EXTENSION, true, null);
if (StringUtils.isNotBlank(deployAsIsRootDiskPath)) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Removing disk " + deployAsIsRootDiskPath);
}
dsMo.deleteFile(deployAsIsRootDiskPath, morDc, true);
String deltaFilePath = dsMo.searchFileInSubFolders(vol.getPath() + "-delta" + VmwareResource.VMDK_EXTENSION, true, null);
if (StringUtils.isNotBlank(deltaFilePath)) {
dsMo.deleteFile(deltaFilePath, morDc, true);
}
}
}
return new Answer(cmd, true, "");
}
if (s_logger.isInfoEnabled()) {
s_logger.info("Destroy root volume directly from datastore");
}
}
if (!isManaged) {
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vol.getPath(), new DatacenterMO(context, morDc), VmwareManager.s_vmwareSearchExcludeFolder.value());
}
return new Answer(cmd, true, "Success");
} catch (Throwable e) {
return new Answer(cmd, false, hostService.createLogMessageException(e, cmd));
}
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VmwareStorageProcessor method cleanUpDatastore.
/**
* 1) Possibly expand the datastore.
* 2) Possibly consolidate all relevant VMDK files into one VMDK file.
* 3) Possibly move the VMDK file to the root folder (may already be there).
* 4) If the VMDK file wasn't already in the root folder, then delete the folder the VMDK file was in.
* 5) Possibly rename the VMDK file (this will lead to there being a delta file with the new name and the
* original file with the original name).
*
* Note: If the underlying VMDK file was for a root disk, the 'vmdk' parameter's value might look, for example,
* like "i-2-32-VM/ROOT-32.vmdk".
*
* Note: If the underlying VMDK file was for a data disk, the 'vmdk' parameter's value might look, for example,
* like "-iqn.2010-01.com.solidfire:4nhe.data-32.79-0.vmdk".
*
* Returns the (potentially new) name of the VMDK file.
*/
private String cleanUpDatastore(Command cmd, HostDatastoreSystemMO hostDatastoreSystem, DatastoreMO dsMo, Map<String, String> details) throws Exception {
s_logger.debug(String.format("Executing clean up in DataStore: [%s].", dsMo.getName()));
boolean expandDatastore = Boolean.parseBoolean(details.get(DiskTO.EXPAND_DATASTORE));
// reserve value in use is set to the minimum for the cloned volume), but that's fine.
if (expandDatastore) {
expandDatastore(hostDatastoreSystem, dsMo);
}
String vmdk = details.get(DiskTO.VMDK);
String fullVmdkPath = new DatastoreFile(dsMo.getName(), vmdk).getPath();
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
String vmName = getVmName(vmdk);
// If vmName is not null, then move all VMDK files out of this folder to the root folder and then delete the folder named vmName.
if (vmName != null) {
String workerVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
VirtualMachineMO vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVmName, null);
if (vmMo == null) {
throw new Exception("Unable to create a worker VM for volume creation");
}
vmMo.attachDisk(new String[] { fullVmdkPath }, dsMo.getMor());
List<String> backingFiles = new ArrayList<>(1);
List<VirtualDisk> virtualDisks = vmMo.getVirtualDisks();
VirtualDisk virtualDisk = virtualDisks.get(0);
VirtualDeviceBackingInfo virtualDeviceBackingInfo = virtualDisk.getBacking();
while (virtualDeviceBackingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) virtualDeviceBackingInfo;
backingFiles.add(backingInfo.getFileName());
virtualDeviceBackingInfo = backingInfo.getParent();
}
vmMo.detachAllDisksAndDestroy();
VmwareStorageLayoutHelper.moveVolumeToRootFolder(dcMo, backingFiles);
vmdk = new DatastoreFile(vmdk).getFileName();
// Delete the folder the VMDK file was in.
DatastoreFile folderToDelete = new DatastoreFile(dsMo.getName(), vmName);
dsMo.deleteFolder(folderToDelete.getPath(), dcMo.getMor());
}
return vmdk;
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VmwareStorageProcessor method createVMAndFolderWithVMName.
private String createVMAndFolderWithVMName(VmwareContext context, VmwareHypervisorHost hyperHost, TemplateObjectTO template, VirtualMachineMO vmTemplate, VolumeObjectTO volume, DatacenterMO dcMo, DatastoreMO dsMo, String searchExcludedFolders) throws Exception {
String vmdkName = volume.getName();
try {
ManagedObjectReference morDatastore = dsMo.getMor();
ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
if (template.getSize() != null) {
_fullCloneFlag = volume.getSize() > template.getSize() ? true : _fullCloneFlag;
}
if (!_fullCloneFlag) {
if (_diskProvisioningStrictness && volume.getProvisioningType() != ProvisioningType.THIN) {
throw new CloudRuntimeException("Unable to create linked clones with strict disk provisioning enabled");
}
createVMLinkedClone(vmTemplate, dcMo, vmdkName, morDatastore, morPool);
} else {
createVMFullClone(vmTemplate, dcMo, dsMo, vmdkName, morDatastore, morPool, volume.getProvisioningType());
}
VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(vmdkName);
assert (vmMo != null);
String vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
s_logger.info("Move volume out of volume-wrapper VM " + vmdkFileBaseName);
String[] vmwareLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.VMWARE, !_fullCloneFlag);
String[] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, vmdkName, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, !_fullCloneFlag);
for (int i = 0; i < vmwareLayoutFilePair.length; i++) {
dsMo.moveDatastoreFile(vmwareLayoutFilePair[i], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[i], dcMo.getMor(), true);
}
s_logger.info("detach disks from volume-wrapper VM and destroy" + vmdkName);
vmMo.detachAllDisksAndDestroy();
String srcFile = dsMo.getDatastorePath(vmdkName, true);
dsMo.deleteFile(srcFile, dcMo.getMor(), true, searchExcludedFolders);
if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmdkName)) {
dsMo.deleteFolder(srcFile, dcMo.getMor());
}
// restoreVM - move the new ROOT disk into corresponding VM folder
VirtualMachineMO restoreVmMo = dcMo.findVm(volume.getVmName());
if (restoreVmMo != null) {
// VM folder name in datastore will be VM's name in vCenter.
String vmNameInVcenter = restoreVmMo.getName();
if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmNameInVcenter)) {
VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmNameInVcenter, dsMo, vmdkFileBaseName, searchExcludedFolders);
}
}
return vmdkFileBaseName;
} finally {
// check if volume wrapper VM is cleaned, if not cleanup
VirtualMachineMO vmdknamedVM = dcMo.findVm(vmdkName);
if (vmdknamedVM != null) {
vmdknamedVM.destroy();
}
}
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VmwareStorageProcessor method cloneVolumeFromBaseTemplate.
@Override
public Answer cloneVolumeFromBaseTemplate(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO();
TemplateObjectTO template = (TemplateObjectTO) srcData;
DataTO destData = cmd.getDestTO();
VolumeObjectTO volume = (VolumeObjectTO) destData;
DataStoreTO primaryStore = volume.getDataStore();
DataStoreTO srcStore = template.getDataStore();
String searchExcludedFolders = cmd.getContextParam("searchexludefolders");
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
VirtualMachineMO vmMo = null;
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
if (morDatastore == null) {
throw new Exception("Unable to find datastore in vSphere");
}
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
String vmdkName = volume.getName();
String vmName = volume.getVmName();
String vmdkFileBaseName = null;
if (template.isDeployAsIs() && volume.getVolumeType() == Volume.Type.ROOT) {
VirtualMachineMO existingVm = dcMo.findVm(vmName);
if (volume.getDeviceId().equals(0L)) {
if (existingVm != null) {
s_logger.info(String.format("Found existing VM wth name [%s] before cloning from template, destroying it", vmName));
existingVm.detachAllDisksAndDestroy();
}
s_logger.info("ROOT Volume from deploy-as-is template, cloning template");
cloneVMFromTemplate(hyperHost, template.getPath(), vmName, primaryStore.getUuid());
} else {
s_logger.info("ROOT Volume from deploy-as-is template, volume already created at this point");
}
} else {
if (srcStore == null) {
// create a root volume for blank VM (created from ISO)
String dummyVmName = hostService.getWorkerName(context, cmd, 0, dsMo);
try {
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName, null);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
vmdkFileBaseName = vmMo.getVmdkFileBaseNames().get(0);
// we only use the first file in the pair, linked or not will not matter
String[] vmdkFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, null, vmdkFileBaseName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, true);
String volumeDatastorePath = vmdkFilePair[0];
synchronized (this) {
s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo, searchExcludedFolders);
vmMo.createDisk(volumeDatastorePath, (long) (volume.getSize() / (1024L * 1024L)), morDatastore, -1, null);
vmMo.detachDisk(volumeDatastorePath, false);
}
} finally {
s_logger.info("Destroy dummy VM after volume creation");
if (vmMo != null) {
s_logger.warn("Unable to destroy a null VM ManagedObjectReference");
vmMo.detachAllDisksAndDestroy();
}
}
} else {
String templatePath = template.getPath();
VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templatePath), true);
if (vmTemplate == null) {
s_logger.warn("Template host in vSphere is not in connected state, request template reload");
return new CopyCmdAnswer("Template host in vSphere is not in connected state, request template reload");
}
if (dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
vmdkFileBaseName = cloneVMforVvols(context, hyperHost, template, vmTemplate, volume, dcMo, dsMo);
} else {
vmdkFileBaseName = createVMAndFolderWithVMName(context, hyperHost, template, vmTemplate, volume, dcMo, dsMo, searchExcludedFolders);
}
}
// restoreVM - move the new ROOT disk into corresponding VM folder
VirtualMachineMO restoreVmMo = dcMo.findVm(volume.getVmName());
if (restoreVmMo != null) {
if (!dsMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
// VM folder name in datastore will be VM's name in vCenter.
String vmNameInVcenter = restoreVmMo.getName();
if (dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmNameInVcenter)) {
VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmNameInVcenter, dsMo, vmdkFileBaseName, searchExcludedFolders);
}
}
}
}
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(vmdkFileBaseName);
if (template.isDeployAsIs()) {
newVol.setSize(volume.getSize());
} else if (template.getSize() != null) {
newVol.setSize(template.getSize());
} else {
newVol.setSize(volume.getSize());
}
return new CopyCmdAnswer(newVol);
} catch (Throwable e) {
return new CopyCmdAnswer(hostService.createLogMessageException(e, cmd));
}
}
Aggregations