vSphere for Java类库:vijava   

 

虚拟机配置类

package com.vmware.vcenter_event.VirtualMachine;

import com.vmware.vim25.CustomizationIPSettings;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.Folder;
import lombok.Data;


@Data
public class VirtualMachineConfig {

    private String vmName;

    private String hostName;

    private String domain;

    private Integer cpuNums;

    private Long memoryGB;

    private Integer diskGB;

    private Folder folder;

    private Datastore datastore;

    private ClusterComputeResource clusterComputeResource;

    private CustomizationIPSettings ipSettings;

}

VirtualMachine服务类

package com.vmware.vcenter_event.VirtualMachine;


import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

@Service
public class VirtualMachineService {


    private ServiceInstance vCenterServiceInstance;

    private InventoryNavigator inventoryNavigator;

    public VirtualMachineService() {
        vCenterServiceInstance = this.vcInit();
        inventoryNavigator = new InventoryNavigator(vCenterServiceInstance.getRootFolder());
    }

    public ServiceInstance vcInit(){
        try {
            return new ServiceInstance(new URL("https://vc.vmware.com/sdk"), "administrator@vsphere.local", "VMware1!", true);
        } catch (MalformedURLException | RemoteException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Datastore getDataStoreByName(String dataStoreName) throws RemoteException {
         return (Datastore) inventoryNavigator.searchManagedEntity("Datastore",dataStoreName);
    }

    private ClusterComputeResource getClusterComputeResourceByName(String clusterName) throws RemoteException {
        return  (ClusterComputeResource) inventoryNavigator.searchManagedEntity("ClusterComputeResource", clusterName);
    }

    private CustomizationSpec getCustomizationSpecByName(String customizationSpecName) throws RemoteException {
        return vCenterServiceInstance.getCustomizationSpecManager().getCustomizationSpec(customizationSpecName).getSpec();
    }

    private VirtualMachine getVirtualMachineTemplateByName(String virtualMachineTemplateName) throws RemoteException {
        VirtualMachine virtualMachine = (VirtualMachine) inventoryNavigator.searchManagedEntity("VirtualMachine", virtualMachineTemplateName);
        if (virtualMachine.getConfig().isTemplate()){
            return virtualMachine;
        }
        return null;
    }

    private Folder getFolderByName(String folderName) throws RemoteException {
        return  (Folder) inventoryNavigator.searchManagedEntity("Folder", folderName);
    }

    private void cloneVirtualMachineFromTemplateWithCustomization(
            VirtualMachine virtualMachineTemplate,
            VirtualMachineConfig virtualMachineConfig,
            CustomizationSpec customizationSpec)
    {

        // vm location
        VirtualMachineRelocateSpec virtualMachineRelocateSpec = new VirtualMachineRelocateSpec();
        virtualMachineRelocateSpec.setDatastore(virtualMachineConfig.getDatastore().getMOR());
        virtualMachineRelocateSpec.setPool(virtualMachineConfig.getClusterComputeResource().getResourcePool().getMOR());

        // vm configSpec

        VirtualMachineConfigSpec virtualMachineConfigSpec = new VirtualMachineConfigSpec();
        virtualMachineConfigSpec.setNumCPUs(virtualMachineConfig.getCpuNums());

        // configSpec.setNumCoresPerSocket(1);
        virtualMachineConfigSpec.setMemoryMB(virtualMachineConfig.getMemoryGB() * 1024);

        VirtualMachineCloneSpec virtualMachineCloneSpec = new VirtualMachineCloneSpec();
        virtualMachineCloneSpec.setLocation(virtualMachineRelocateSpec);
        virtualMachineCloneSpec.setPowerOn(true);
        virtualMachineCloneSpec.setTemplate(false);
        virtualMachineCloneSpec.setCustomization(customizationSpec);
        virtualMachineCloneSpec.setConfig(virtualMachineConfigSpec);

        try {
            Task task = virtualMachineTemplate.cloneVM_Task(
                    virtualMachineConfig.getFolder(),
                    virtualMachineConfig.getVmName(),
                    virtualMachineCloneSpec);
            String result = task.waitForTask();

            if (result.equals(Task.SUCCESS)) {
                System.out.println("模板生成虚拟机成功");

            } else {
                System.out.println("模板生成虚拟机失败,请查询Vcenter 上相关日志信息");
            }

        } catch (RemoteException | InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
        VirtualMachineService virtualMachineService = new VirtualMachineService();

        VirtualMachine virtualMachineTemplate = virtualMachineService.getVirtualMachineTemplateByName("Centos-7-temp");
        Folder folder = virtualMachineService.getFolderByName("VCH");
        ClusterComputeResource clusterComputeResource = virtualMachineService.getClusterComputeResourceByName("Cluster");
        Datastore datastore = virtualMachineService.getDataStoreByName("VMFS02");
        CustomizationSpec customizationSpec = virtualMachineService.getCustomizationSpecByName("Linux");


        VirtualMachineConfig virtualMachineConfig = new VirtualMachineConfig();
        virtualMachineConfig.setCpuNums(2);
        virtualMachineConfig.setMemoryGB(2L);
        virtualMachineConfig.setVmName("VM-Test04");
        virtualMachineConfig.setHostName("VM04-Test");
        virtualMachineConfig.setDomain("vmware.com");
        virtualMachineConfig.setFolder(folder);
        virtualMachineConfig.setClusterComputeResource(clusterComputeResource);
        virtualMachineConfig.setDatastore(datastore);

        CustomizationFixedIp fixedIp = new CustomizationFixedIp();
        fixedIp.setIpAddress("1.1.1.1");
        CustomizationIPSettings customizationIPSettings = new CustomizationIPSettings();
        customizationIPSettings.setIp(fixedIp);
        customizationIPSettings.setSubnetMask("255.255.255.0");
        customizationIPSettings.setGateway(new String[]{"1.1.1.254"});
        customizationIPSettings.setDnsDomain(virtualMachineConfig.getDomain());
        customizationIPSettings.setDnsServerList(new String[]{"8.8.8.8", "114.114.114.114"});
        virtualMachineConfig.setIpSettings(customizationIPSettings);


        CustomizationAdapterMapping customizationAdapterMappings = new CustomizationAdapterMapping();
        customizationAdapterMappings.setAdapter(virtualMachineConfig.getIpSettings());
        customizationSpec.setNicSettingMap(new CustomizationAdapterMapping[]{customizationAdapterMappings});
        CustomizationLinuxPrep linuxPrep = new CustomizationLinuxPrep();
        linuxPrep.setDomain(virtualMachineConfig.getDomain());
        CustomizationFixedName fixedName = new CustomizationFixedName();
        fixedName.setName(virtualMachineConfig.getHostName());
        linuxPrep.setHostName(fixedName);
        customizationSpec.setIdentity(linuxPrep);

        if (virtualMachineTemplate != null){
            virtualMachineService.cloneVirtualMachineFromTemplateWithCustomization(
                    virtualMachineTemplate,
                    virtualMachineConfig,
                    customizationSpec);
        }else {
            throw new Exception("Can't find the virtual machine template");
        }

    }

}