forked from sijis/pyvmomi-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-vm.py
79 lines (64 loc) · 2.99 KB
/
create-vm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import vmutils
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--username',
help='Username of the vcenter user that you want to login with. ')
parser.add_argument('--password',
help='Password of the vcenter user that you want to login with. ')
parser.add_argument('--vcenter',
help='Domain of the vsphere client. i.e.: somevcenter.yourhost.com ')
parser.add_argument('--vcenterport',
help='Port number of the vsphere client. Example: 443 ')
parser.add_argument('--vmname',
help='Name of virtual machine as it appears in vsphere. i.e.: example-mysqldb-server ')
parser.add_argument('--vmtemplate',
help='Name of template to use as vmware server ')
ARGS = parser.parse_args()
try:
si = SmartConnect(user=ARGS.username,
pwd=ARGS.password,
host=ARGS.vcenter,
port=ARGS.vcenterport)
except IOError, e:
pass
# Finding source VM
newvm = ARGS.vmname
template_vm = vmutils.get_vm_by_name(si, 'ARGS.vmtemplate')
'''
There are two roads for modifying a vm creation from a template
1. ConfigSpec -> CloneSpec
2. ConfigSpec -> (AdapterMapping -> GlobalIPSettings -> LinuxPrep) -> CustomSpec -> CloneSpec
Notes:
ConfigSpec and CustomSpecificiation are purely optional and
independent
'''
# cpu/ram changes
#mem = 512 * 1024 # convert to GB
mem = 512 # MB
vmconf = vim.vm.ConfigSpec(numCPUs=1, memoryMB=mem)
#vmconf.deviceChange = devices
# Network adapter settings
adaptermap = vim.vm.customization.AdapterMapping()
adaptermap.adapter = vim.vm.customization.IPSettings(ip=vim.vm.customization.DhcpIpGenerator(), dnsDomain='domain.local')
# static ip
#adaptermap.adapter = vim.vm.customization.IPSettings(ip=vim.vm.customization.FixedIp(address='10.0.1.10'),
# subnetMask='255.255.255.0', gateway='10.0.0.1')
# IP
globalip = vim.vm.customization.GlobalIPSettings()
# for static ip
#globalip = vim.vm.customization.GlobalIPSettings(dnsServerList=['10.0.1.4', '10.0.1.1'])
# Hostname settings
ident = vim.vm.customization.LinuxPrep(domain='domain.local', hostName=vim.vm.customization.FixedName(name=newvm))
# Putting all these pieces together in a custom spec
customspec = vim.vm.customization.Specification(nicSettingMap=[adaptermap], globalIPSettings=globalip, identity=ident)
# Creating relocate spec and clone spec
resource_pool = vmutils.get_resource_pool(si, 'DEV')
relocateSpec = vim.vm.RelocateSpec(pool=resource_pool)
#cloneSpec = vim.vm.CloneSpec(powerOn=True, template=False, location=relocateSpec, customization=customspec, config=vmconf)
cloneSpec = vim.vm.CloneSpec(powerOn=True, template=False, location=relocateSpec, customization=None, config=vmconf)
# Creating clone task
clone = template_vm.Clone(name=newvm, folder=template_vm.parent, spec=cloneSpec)
# close out connection
Disconnect(si)