-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_network_vm.py
133 lines (111 loc) · 3.9 KB
/
change_network_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import atexit
import argparse
import sys
import time
import ssl
import os
from pyVmomi import vim, vmodl
from pyVim.connect import Disconnect, SmartConnect
inputs = {
'vcenter_ip': '',
'vcenter_password': '',
'vcenter_user': '',
'vm_name': '',
'isDHCP': False,
'vm_ip': '',
'subnet': '',
'gateway': '',
'dns': ['8.8.8.8', '1.1.1.1'],
'domain': 'domain.com'
}
def connect_to_api():
hostname = inputs['vcenter_ip']
username = inputs['vcenter_user']
password = inputs['vcenter_password']
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
si = SmartConnect(host=hostname, user=username,
pwd=password, port=443, sslContext=context)
atexit.register(Disconnect, si)
return si.RetrieveContent()
def get_obj(content, vimtype, name):
"""
Return an object by name, if name is None the
first found object is returned
"""
obj = None
container = content.viewManager.CreateContainerView(
content.rootFolder, vimtype, True)
for c in container.view:
if name:
if c.name == name:
obj = c
break
else:
obj = c
break
container.Destroy()
return obj
def wait_for_task(task, actionName='job', hideResult=False):
"""
Waits and provides updates on a vSphere task
"""
while task.info.state == vim.TaskInfo.State.running:
time.sleep(2)
if task.info.state == vim.TaskInfo.State.success:
if task.info.result is not None and not hideResult:
print('%s completed successfully, result: %s' %
(actionName, task.info.result))
else:
print('%s completed successfully.' % actionName)
else:
print('%s did not complete successfully: %s' %
(actionName, task.info.error))
raise task.info.error
return task.info.result
def main():
try:
print("Connected to VCENTER SERVER !")
content = connect_to_api()
vm_name = inputs['vm_name']
vm = get_obj(content, [vim.VirtualMachine], vm_name)
if vm.runtime.powerState != 'poweredOff':
sys.exit("WARNING:: Power off your VM before reconfigure")
adaptermap = vim.vm.customization.AdapterMapping()
globalip = vim.vm.customization.GlobalIPSettings()
adaptermap.adapter = vim.vm.customization.IPSettings()
isDHDCP = inputs['isDHCP']
if not isDHDCP:
"""Static IP Configuration"""
adaptermap.adapter.ip = vim.vm.customization.FixedIp()
adaptermap.adapter.ip.ipAddress = inputs['vm_ip']
adaptermap.adapter.subnetMask = inputs['subnet']
adaptermap.adapter.gateway = inputs['gateway']
globalip.dnsServerList = inputs['dns']
else:
"""DHCP Configuration"""
adaptermap.adapter.ip = vim.vm.customization.DhcpIpGenerator()
adaptermap.adapter.dnsDomain = inputs['domain']
globalip = vim.vm.customization.GlobalIPSettings()
ident = vim.vm.customization.LinuxPrep(
domain=inputs['domain'], hostName=vim.vm.customization.FixedName(name=vm_name))
customspec = vim.vm.customization.Specification()
# For only one adapter
customspec.identity = ident
customspec.nicSettingMap = [adaptermap]
customspec.globalIPSettings = globalip
# Configuring network for a single NIC
# For multipple NIC configuration contact me.
print("Reconfiguring VM Networks...")
task = vm.Customize(spec=customspec)
# Wait for Network Reconfigure to complete
wait_for_task(task, si)
except vmodl.MethodFault, e:
print("Caught vmodl fault: %s" % e.msg)
return 1
except Exception, e:
print("Caught exception: %s" % str(e))
return 1
# Start program
if __name__ == "__main__":
main()