Vijava 学习笔记之(模板生成虚拟机)解析

你猜 阅读:233 2021-03-31 22:36:30 评论:0

vijava 源代码:

package com.vmware.template; 
 
 
import com.vmware.util.Session; 
import com.vmware.vim25.VirtualDeviceConfigSpec; 
import com.vmware.vim25.VirtualMachineCloneSpec; 
import com.vmware.vim25.VirtualMachineConfigSpec; 
import com.vmware.vim25.VirtualMachineRelocateSpec; 
import com.vmware.vim25.mo.*; 
 
import java.rmi.RemoteException; 
 
/** 
 * Created by vixuan-008 on 2015/5/4. 
 */ 
public class VirtualMachineTemplate { 
    public static void main(String[] args){ 
 
        try{ 
            String poolName=""; 
            String hostName="192.168.0.10"; 
            com.vmware.vim25.mo.VirtualMachine templateVM = null; 
            com.vmware.vim25.mo.ResourcePool pool = null; 
            ComputeResource computerResource = null; 
            InventoryNavigator inventoryNavigator = null; 
            Task task = null; 
 
            ServiceInstance instance = Session.getInstance("***", "***", "**"); 
            Folder rootFolder = instance.getRootFolder(); 
            inventoryNavigator = new InventoryNavigator(rootFolder); 
            try { 
                templateVM = (com.vmware.vim25.mo.VirtualMachine) inventoryNavigator.searchManagedEntity( 
                        "VirtualMachine", "wintem"); 
            } catch (RemoteException e) { 
                e.printStackTrace(); 
            } 
            VirtualMachineRelocateSpec virtualMachineRelocateSpec = new VirtualMachineRelocateSpec(); 
            if (null != poolName && !"".equals(poolName)) { 
                try { 
                    pool = (ResourcePool) inventoryNavigator.searchManagedEntity("ResourcePool", poolName); 
                    virtualMachineRelocateSpec.setPool(pool.getMOR()); 
                } catch (RemoteException e) { 
                    e.printStackTrace(); 
                } 
 
 
            } else { 
 
                try { 
                    computerResource = (ComputeResource) inventoryNavigator.searchManagedEntity("ComputeResource", hostName); 
 
                    virtualMachineRelocateSpec.setPool(computerResource.getResourcePool().getMOR()); 
                    virtualMachineRelocateSpec.setHost(computerResource.getHosts()[0].getMOR()); 
 
                } catch (RemoteException e) { 
                    e.printStackTrace(); 
                } 
 
            } 
 
            VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); 
 
 
            configSpec.setNumCPUs(Integer.parseInt("4")); 
            configSpec.setMemoryMB(Long.parseLong("1024")); 
 
 
 
 
 
            VirtualMachineCloneSpec cloneSpec = 
                    new VirtualMachineCloneSpec(); 
            VirtualDeviceConfigSpec virtualDeviceConfigSpec = new VirtualDeviceConfigSpec(); 
 
 
            cloneSpec.setLocation(virtualMachineRelocateSpec); 
 
            cloneSpec.setPowerOn(true); 
            cloneSpec.setTemplate(false); 
            cloneSpec.setConfig(configSpec); 
 
 
 
            try { 
 
 
                task = templateVM.cloneVM_Task((Folder) templateVM.getParent(), 
                        "vm6", cloneSpec); 
                String result=task.waitForTask(); 
                if(result.equals(Task.SUCCESS)){ 
                    System.out.println("success"); 
                }else{ 
                    System.out.println("faile"); 
                } 
 
 
            } catch (RemoteException e) { 
                e.printStackTrace(); 
 
            } 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
} 

相关截图:


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们