Java 类com.vmware.vim25.OvfFile 实例源码

项目:cloudstack    文件:HypervisorHostHelper.java   
public static void createOvfFile(VmwareHypervisorHost host, String diskFileName, String ovfName, String datastorePath, String templatePath, long diskCapacity, long fileSize,
        ManagedObjectReference morDs) throws Exception {
    VmwareContext context = host.getContext();
    ManagedObjectReference morOvf = context.getServiceContent().getOvfManager();
    VirtualMachineMO workerVmMo = HypervisorHostHelper.createWorkerVM(host, new DatastoreMO(context, morDs), ovfName);
    if (workerVmMo == null)
        throw new Exception("Unable to find just-created worker VM");

    String[] disks = {datastorePath + File.separator + diskFileName};
    try {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();

        // Reconfigure worker VM with datadisk
        VirtualDevice device = VmwareHelper.prepareDiskDevice(workerVmMo, null, -1, disks, morDs, -1, 1);
        deviceConfigSpec.setDevice(device);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        workerVmMo.configureVm(vmConfigSpec);

        // Write OVF descriptor file
        OvfCreateDescriptorParams ovfDescParams = new OvfCreateDescriptorParams();
        String deviceId = File.separator + workerVmMo.getMor().getValue() + File.separator + "VirtualIDEController0:0";
        OvfFile ovfFile = new OvfFile();
        ovfFile.setPath(diskFileName);
        ovfFile.setDeviceId(deviceId);
        ovfFile.setSize(fileSize);
        ovfFile.setCapacity(diskCapacity);
        ovfDescParams.getOvfFiles().add(ovfFile);
        OvfCreateDescriptorResult ovfCreateDescriptorResult = context.getService().createDescriptor(morOvf, workerVmMo.getMor(), ovfDescParams);

        String ovfPath = templatePath + File.separator + ovfName + ".ovf";
        try {
            FileWriter out = new FileWriter(ovfPath);
            out.write(ovfCreateDescriptorResult.getOvfDescriptor());
            out.close();
        } catch (Exception e) {
            throw e;
        }
    } finally {
        workerVmMo.detachAllDisks();
        workerVmMo.destroy();
    }
}