Search in sources :

Example 21 with ManagedObjectReference

use of com.vmware.vim25.ManagedObjectReference 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();
        }
    }
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 22 with ManagedObjectReference

use of com.vmware.vim25.ManagedObjectReference in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method attachIso.

// isoDatastorePath: [datastore name] isoFilePath
public void attachIso(String isoDatastorePath, ManagedObjectReference morDs, boolean connect, boolean connectAtBoot) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachIso(). target MOR: " + _mor.get_value() + ", isoDatastorePath: " + isoDatastorePath + ", datastore: " + morDs.get_value() + ", connect: " + connect + ", connectAtBoot: " + connectAtBoot);
    assert (isoDatastorePath != null);
    assert (morDs != null);
    boolean newCdRom = false;
    VirtualCdrom cdRom = (VirtualCdrom) getIsoDevice();
    if (cdRom == null) {
        newCdRom = true;
        cdRom = new VirtualCdrom();
        cdRom.setControllerKey(getIDEDeviceControllerKey());
        int deviceNumber = getNextIDEDeviceNumber();
        cdRom.setUnitNumber(deviceNumber);
        cdRom.setKey(-deviceNumber);
    }
    VirtualDeviceConnectInfo cInfo = new VirtualDeviceConnectInfo();
    cInfo.setConnected(connect);
    cInfo.setStartConnected(connectAtBoot);
    cdRom.setConnectable(cInfo);
    VirtualCdromIsoBackingInfo backingInfo = new VirtualCdromIsoBackingInfo();
    backingInfo.setFileName(isoDatastorePath);
    backingInfo.setDatastore(morDs);
    cdRom.setBacking(backingInfo);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(cdRom);
    if (newCdRom) {
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    } else {
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
    }
    deviceConfigSpecArray[0] = deviceConfigSpec;
    reConfigSpec.setDeviceChange(deviceConfigSpecArray);
    ManagedObjectReference morTask = _context.getService().reconfigVM_Task(_mor, reConfigSpec);
    String result = _context.getServiceUtil().waitForTask(morTask);
    if (!result.equals("sucess")) {
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - detachIso() done(failed)");
        throw new Exception("Failed to attach ISO due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    _context.waitForTaskProgressDone(morTask);
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachIso() done(successfully)");
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualCdromIsoBackingInfo(com.vmware.vim25.VirtualCdromIsoBackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualCdrom(com.vmware.vim25.VirtualCdrom) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 23 with ManagedObjectReference

use of com.vmware.vim25.ManagedObjectReference 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;
}
Also used : ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 24 with ManagedObjectReference

use of com.vmware.vim25.ManagedObjectReference 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);
}
Also used : Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) VirtualMachineSnapshotInfo(com.vmware.vim25.VirtualMachineSnapshotInfo) SnapshotInfo(com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Example 25 with ManagedObjectReference

use of com.vmware.vim25.ManagedObjectReference in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method createSnapshot.

public boolean createSnapshot(String snapshotName, String snapshotDescription, boolean dumpMemory, boolean quiesce) throws Exception {
    ManagedObjectReference morTask = _context.getService().createSnapshot_Task(_mor, snapshotName, snapshotDescription, dumpMemory, quiesce);
    String result = _context.getServiceUtil().waitForTask(morTask);
    if (result.equals("sucess")) {
        _context.waitForTaskProgressDone(morTask);
        ManagedObjectReference morSnapshot = null;
        // We still need to wait until the object appear in vCenter
        long startTick = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTick < 10000) {
            morSnapshot = getSnapshotMor(snapshotName);
            if (morSnapshot != null) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        if (morSnapshot == null)
            s_logger.error("We've been waiting for over 10 seconds for snapshot MOR to be appearing in vCenter after CreateSnapshot task is done, but it is still not there?!");
        return true;
    } else {
        s_logger.error("VMware createSnapshot_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)236 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)62 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)57 Pair (com.cloud.utils.Pair)49 RemoteException (java.rmi.RemoteException)48 ArrayList (java.util.ArrayList)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)39 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)35 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)32 IOException (java.io.IOException)31 CloudException (com.cloud.exception.CloudException)30 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)30 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)29 ObjectContent (com.vmware.vim25.ObjectContent)28 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)27 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)25 ConfigurationException (javax.naming.ConfigurationException)24 InternalErrorException (com.cloud.exception.InternalErrorException)23 VirtualDisk (com.vmware.vim25.VirtualDisk)23 ConnectException (java.net.ConnectException)23