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");
} } }

vSphere SDK for Java - 从模板部署虚拟机并配置IP地址的更多相关文章

  1. VM虚拟机手动配置IP地址

    1.查看虚拟机的网关 编辑-->虚拟网络编辑器 VMnet8 NAT模式-->NAT设置-->网关IP 2.设置IP地址 系统-->首选项-->网络连接 system e ...

  2. 一、安装虚拟机,配置ip地址

    一.安装linux 注意点: 一.选择最小化安装的时候,要自定义安装软件,必须要安装下面的 如果没有安装上面的,需要用下面的命令来查询安装 如果没有安装就会出现各种问题 二.分区简单介绍 1.至少要一 ...

  3. 云服务和虚拟机的预留 IP 地址

    大家好! 我很高兴地向大家宣布,云服务和虚拟机的预留 IP 地址将自 2014年 5月 12日起正式发布.在这篇博客中,我们将演示如何管理预留 IP.将预留 IP 与云服务和虚拟机关联.定价模型和一些 ...

  4. [部署]CentOS配置IP地址

    环境 虚拟机:VMWare10.0.1 build-1379776 操作系统:CentOS7 64位 简介 CentOS7最小化安装(Minimal)时,是不带ifconfig指令的,该指令在net- ...

  5. JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)

    1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...

  6. Java 获取当前项目所在服务器的 IP 地址

    java中获取当前服务器地址主要使用到InetAddress这个类 public static void main(String[] args) { try { //用 getLocalHost() ...

  7. 虚拟机下CentOS 配置IP地址的三种方法

    1.自动获取IP地址(我不是用的这种方法,不做过多介绍) 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #if ...

  8. 虚拟机下CentOS 6.5配置IP地址的三种方法

    实验软件环境:虚拟机Vmware Workstation10.0 .CentOS 6.5 32位 1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分 ...

  9. VM虚拟机下CentOS 6.5配置IP地址的三种方法

    1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...

随机推荐

  1. 给NavigationBar设置颜色

    传统的设置 ) { [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; } else { [[UINavigation ...

  2. Webpack4.x 入门

    概览 新建项目 npm init -y 安装webpack & webpack-cli (c)npm install -D webpack (c)npm install -D webpack- ...

  3. 19.Delete Documents-官方文档摘录

    1 插入例子 db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: & ...

  4. Python下的正则表达式原理和优化笔记

    摘要: 本文旨在总结一些编写表达式的技巧和原理.鉴于介绍python中re模块的使用方法的文章太多.所以本文在基础方面都是略过,而在回溯原理和一些技巧方面记录一点点学习总结. 目录:[ - ] 基础规 ...

  5. Oracle 实现拆分列数据的split()方法

    -- 创建需要划分的字符串 with T1 as( select 'one,two,three,four,five,six,seven,eight,nine,zero' as source_strin ...

  6. linux qt下 QSqlDatabase: QMYSQL driver not loaded

    出现上述问题是qt安装目录未包含mysql驱动. 解决方法如下: 1.查看系统是否存在libqsqlmysql.so find / -name libqsqlmysql.so 2.若不存在该文件则安装 ...

  7. 理解ASM的Extent

    理解ASM的Extent 分类: Oracle 2017-04-14 10:19:44   ASM中分配空间的单位是AU,Extent包含1个或多个AU.在11g之前,1个Extent对应1个AU.而 ...

  8. 记录:python读取excel文件

    由于最近老是用到python读取excel文件,所以特意记录一下python读取excel文件的大体框架. 库:xlrd(读),直接pip安装即可.想要写excel文件的话,安装xlwd库即可,也是直 ...

  9. testng日志 ITestListener

    上一节我们写一个日志类 extends   TestListenerAdapter ----------TestListenerAdapter 是 ITestListener 实现的一个类 这一节,我 ...

  10. python16_day25【crm】

    一.CRM模拟admin功能 1.过滤功能 2.显示数据分页 3.动态菜单 项目:https://github.com/willianflasky/growup/tree/master/s16/hom ...