use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
the class HypervisorHostHelper method createWorkerVM.
public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception {
// Allow worker VM to float within cluster so that we will have better chance to
// create it successfully
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
if (morCluster != null)
hyperHost = new ClusterMO(hyperHost.getContext(), morCluster);
VirtualMachineMO workingVM = null;
VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
vmConfig.setName(vmName);
vmConfig.setMemoryMB((long) 4);
vmConfig.setNumCPUs(1);
vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value());
VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
fileInfo.setVmPathName(dsMo.getDatastoreRootPath());
vmConfig.setFiles(fileInfo);
VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
scsiController.setBusNumber(0);
scsiController.setKey(1);
VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
scsiControllerSpec.setDevice(scsiController);
scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfig.getDeviceChange().add(scsiControllerSpec);
if (hyperHost.createVm(vmConfig)) {
// Ugly work-around, it takes time for newly created VM to appear
for (int i = 0; i < 10 && workingVM == null; i++) {
workingVM = hyperHost.findVmOnHyperHost(vmName);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
s_logger.debug("[ignored] interupted while waiting to config vm.");
}
}
}
if (workingVM != null) {
workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER, "true");
String workerTag = String.format("%d-%s", System.currentTimeMillis(), hyperHost.getContext().getStockObject("noderuninfo"));
workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER_TAG, workerTag);
}
return workingVM;
}
use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
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(), "UTF-8"));
sb.append("/");
sb.append(URLEncoder.encode(vmxPathTokens[3].trim(), "UTF-8"));
sb.append("?dcPath=");
sb.append(URLEncoder.encode(dcInfo.second(), "UTF-8"));
sb.append("&dsName=");
sb.append(URLEncoder.encode(vmxPathTokens[1].trim(), "UTF-8"));
return sb.toString();
}
use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
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().registerVMTask(morFolder, vmFileInfo.getVmPathName(), vmName, false, morPool, hostMo.getMor());
boolean result = _context.getVimClient().waitForTask(morTask);
if (!result) {
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 by apache.
the class VirtualMachineMO method setSnapshotDirectory.
// snapshot directory in format of: /vmfs/volumes/<datastore name>/<path>
@Deprecated
public void setSnapshotDirectory(String snapshotDir) throws Exception {
VirtualMachineFileInfo fileInfo = getFileInfo();
Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
String vmxUrl = _context.composeDatastoreBrowseUrl(dcInfo.second(), fileInfo.getVmPathName());
byte[] vmxContent = _context.getResourceContent(vmxUrl);
BufferedReader in = null;
BufferedWriter out = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean replaced = false;
try {
in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(vmxContent), "UTF-8"));
out = new BufferedWriter(new OutputStreamWriter(bos, "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith("workingDir")) {
replaced = true;
out.write(String.format("workingDir=\"%s\"", snapshotDir));
out.newLine();
} else {
out.write(line);
out.newLine();
}
}
if (!replaced) {
out.newLine();
out.write(String.format("workingDir=\"%s\"", snapshotDir));
out.newLine();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
_context.uploadResourceContent(vmxUrl, bos.toByteArray());
// It seems that I don't need to do re-registration. VMware has bug in writing the correct snapshot's VMDK path to
// its disk backing info anyway.
// redoRegistration();
}
use of com.vmware.vim25.VirtualMachineFileInfo in project cloudstack by apache.
the class VirtualMachineMO method getSnapshotDescriptorDatastorePath.
public String getSnapshotDescriptorDatastorePath() throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.getPathSet().add("name");
pSpec.getPathSet().add("config.files");
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.FALSE);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
assert (ocs != null);
String vmName = null;
VirtualMachineFileInfo fileInfo = null;
assert (ocs.size() == 1);
for (ObjectContent oc : ocs) {
List<DynamicProperty> props = oc.getPropSet();
if (props != null) {
assert (props.size() == 2);
for (DynamicProperty prop : props) {
if (prop.getName().equals("name")) {
vmName = prop.getVal().toString();
} else {
fileInfo = (VirtualMachineFileInfo) prop.getVal();
}
}
}
}
assert (vmName != null);
assert (fileInfo != null);
// .vmsd file exists at the same directory of .vmx file
DatastoreFile vmxFile = new DatastoreFile(fileInfo.getVmPathName());
return vmxFile.getCompanionPath(vmName + ".vmsd");
}
Aggregations