use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method redoRegistration.
public void redoRegistration(ManagedObjectReference morHost) throws Exception {
String vmName = getVmName();
VirtualMachineFileInfo vmFileInfo = getFileInfo();
boolean isTemplate = isTemplate();
HostMO hostMo;
if (morHost != null)
hostMo = new HostMO(getContext(), morHost);
else
hostMo = getRunningHost();
ManagedObjectReference morFolder = getParentMor();
ManagedObjectReference morPool = hostMo.getHyperHostOwnerResourcePool();
_context.getService().unregisterVM(_mor);
ManagedObjectReference morTask = _context.getService().registerVM_Task(morFolder, vmFileInfo.getVmPathName(), vmName, false, morPool, hostMo.getMor());
String result = _context.getServiceUtil().waitForTask(morTask);
if (!result.equalsIgnoreCase("Sucess")) {
throw new Exception("Unable to register template due to " + TaskMO.getTaskFailureInfo(_context, morTask));
} else {
_context.waitForTaskProgressDone(morTask);
if (isTemplate) {
VirtualMachineMO vmNewRegistration = hostMo.findVmOnHyperHost(vmName);
assert (vmNewRegistration != null);
vmNewRegistration.markAsTemplate();
}
}
}
use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method getVmxHttpAccessUrl.
public String getVmxHttpAccessUrl() throws Exception {
Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
VirtualMachineFileInfo fileInfo = getFileInfo();
String vmxFilePath = fileInfo.getVmPathName();
String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
StringBuffer sb = new StringBuffer("https://" + _context.getServerAddress() + "/folder/");
sb.append(URLEncoder.encode(vmxPathTokens[2].trim()));
sb.append("/");
sb.append(URLEncoder.encode(vmxPathTokens[3].trim()));
sb.append("?dcPath=");
sb.append(URLEncoder.encode(dcInfo.second()));
sb.append("&dsName=");
sb.append(URLEncoder.encode(vmxPathTokens[1].trim()));
return sb.toString();
}
use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method getDiskDatastorePathChain.
@Deprecated
public List<Pair<String, ManagedObjectReference>> getDiskDatastorePathChain(VirtualDisk disk, boolean followChain) throws Exception {
VirtualDeviceBackingInfo backingInfo = disk.getBacking();
if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
throw new Exception("Unsupported VirtualDeviceBackingInfo");
}
List<Pair<String, ManagedObjectReference>> pathList = new ArrayList<Pair<String, ManagedObjectReference>>();
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
if (!followChain) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
return pathList;
}
Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
VirtualMachineFileInfo vmFilesInfo = getFileInfo();
DatastoreFile snapshotDirFile = new DatastoreFile(vmFilesInfo.getSnapshotDirectory());
DatastoreFile vmxDirFile = new DatastoreFile(vmFilesInfo.getVmPathName());
do {
if (diskBackingInfo.getParent() != null) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
diskBackingInfo = diskBackingInfo.getParent();
} else {
// try getting parent info from VMDK file itself
byte[] content = null;
try {
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), diskBackingInfo.getFileName());
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
} catch (Exception e) {
// if snapshot directory has been changed to place other than default. VMware has a bug
// that its corresponding disk backing info is not updated correctly. therefore, we will try search
// in snapshot directory one more time
DatastoreFile currentFile = new DatastoreFile(diskBackingInfo.getFileName());
String vmdkFullDsPath = snapshotDirFile.getCompanionPath(currentFile.getFileName());
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), vmdkFullDsPath);
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(vmdkFullDsPath, diskBackingInfo.getDatastore()));
}
VmdkFileDescriptor descriptor = new VmdkFileDescriptor();
descriptor.parse(content);
if (descriptor.getParentFileName() != null && !descriptor.getParentFileName().isEmpty()) {
// create a fake one
VirtualDiskFlatVer2BackingInfo parentDiskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
parentDiskBackingInfo.setDatastore(diskBackingInfo.getDatastore());
String parentFileName = descriptor.getParentFileName();
if (parentFileName.startsWith("/")) {
int fileNameStartPos = parentFileName.lastIndexOf("/");
parentFileName = parentFileName.substring(fileNameStartPos + 1);
parentDiskBackingInfo.setFileName(vmxDirFile.getCompanionPath(parentFileName));
} else {
parentDiskBackingInfo.setFileName(snapshotDirFile.getCompanionPath(parentFileName));
}
diskBackingInfo = parentDiskBackingInfo;
} else {
break;
}
}
} while (diskBackingInfo != null);
return pathList;
}
use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method backupCurrentSnapshot.
// destName does not contain extension name
public void backupCurrentSnapshot(String deviceName, ManagedObjectReference morDestDs, String destDsDirectory, String destName, boolean includeBase) throws Exception {
SnapshotDescriptor descriptor = getSnapshotDescriptor();
SnapshotInfo[] snapshotInfo = descriptor.getCurrentDiskChain();
if (snapshotInfo.length == 0) {
String msg = "No snapshot found in this VM";
throw new Exception(msg);
}
HostMO hostMo = getRunningHost();
DatacenterMO dcMo = getOwnerDatacenter().first();
List<Pair<ManagedObjectReference, String>> mounts = hostMo.getDatastoreMountsOnHost();
VirtualMachineFileInfo vmFileInfo = getFileInfo();
List<Ternary<String, String, String>> backupInfo = new ArrayList<Ternary<String, String, String>>();
for (int i = 0; i < snapshotInfo.length; i++) {
if (!includeBase && i == snapshotInfo.length - 1) {
break;
}
SnapshotDescriptor.DiskInfo[] disks = snapshotInfo[i].getDisks();
if (disks != null) {
String destBaseFileName;
String destFileName;
String destParentFileName;
for (SnapshotDescriptor.DiskInfo disk : disks) {
if (deviceName == null || deviceName.equals(disk.getDeviceName())) {
String srcVmdkFullDsPath = getSnapshotDiskFileDatastorePath(vmFileInfo, mounts, disk.getDiskFileName());
Pair<DatastoreMO, String> srcDsInfo = getOwnerDatastore(srcVmdkFullDsPath);
Pair<VmdkFileDescriptor, byte[]> vmdkInfo = getVmdkFileInfo(srcVmdkFullDsPath);
String srcVmdkBaseFilePath = DatastoreFile.getCompanionDatastorePath(srcVmdkFullDsPath, vmdkInfo.first().getBaseFileName());
destFileName = destName + (snapshotInfo.length - i - 1) + ".vmdk";
if (vmdkInfo.first().getParentFileName() != null) {
destBaseFileName = destName + (snapshotInfo.length - i - 1) + "-delta.vmdk";
destParentFileName = destName + (snapshotInfo.length - i - 2) + ".vmdk";
} else {
destBaseFileName = destName + (snapshotInfo.length - i - 1) + "-flat.vmdk";
destParentFileName = null;
}
s_logger.info("Copy VMDK base file " + srcVmdkBaseFilePath + " to " + destDsDirectory + "/" + destBaseFileName);
srcDsInfo.first().copyDatastoreFile(srcVmdkBaseFilePath, dcMo.getMor(), morDestDs, destDsDirectory + "/" + destBaseFileName, dcMo.getMor(), true);
byte[] newVmdkContent = VmdkFileDescriptor.changeVmdkContentBaseInfo(vmdkInfo.second(), destBaseFileName, destParentFileName);
String vmdkUploadUrl = getContext().composeDatastoreBrowseUrl(dcMo.getName(), destDsDirectory + "/" + destFileName);
s_logger.info("Upload VMDK content file to " + destDsDirectory + "/" + destFileName);
getContext().uploadResourceContent(vmdkUploadUrl, newVmdkContent);
backupInfo.add(new Ternary<String, String, String>(destFileName, destBaseFileName, destParentFileName));
}
}
}
}
byte[] vdiskInfo = VmwareHelper.composeDiskInfo(backupInfo, snapshotInfo.length, includeBase);
String vdiskUploadUrl = getContext().composeDatastoreBrowseUrl(dcMo.getName(), destDsDirectory + "/" + destName + ".vdisk");
getContext().uploadResourceContent(vdiskUploadUrl, vdiskInfo);
}
use of com.vmware.vim25.VirtualMachineFileInfo in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method getHttpAccessPathInfo.
/**
* Retrieve path info to access VM files via vSphere web interface
* @return [0] vm-name, [1] data-center-name, [2] datastore-name
* @throws Exception
*/
public String[] getHttpAccessPathInfo() throws Exception {
String[] pathInfo = new String[3];
Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
VirtualMachineFileInfo fileInfo = getFileInfo();
String vmxFilePath = fileInfo.getVmPathName();
String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
assert (vmxPathTokens.length == 4);
// vSphere vm name
pathInfo[1] = vmxPathTokens[1].trim();
// vSphere datacenter name
pathInfo[2] = dcInfo.second();
// vSphere datastore name
pathInfo[3] = vmxPathTokens[0].trim();
return pathInfo;
}
Aggregations