-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcvm.py
293 lines (245 loc) · 11.3 KB
/
cvm.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import requests
import urllib3
import sys
import re
import os
from passwd import user_api, pass_api, vc_user, vc_pass
from python_terraform import *
import atexit
from pyVim import connect
from pyVmomi import vmodl
from pyVmomi import vim
from tools import tasks
import datetime
# import paramaners list
from parameters import template_list, template_linux, template_wind, port_int
urllib3.disable_warnings()
# get ip address
def ipam_create_ip(hostname, infraname, cidr):
try:
token = requests.post('https://ipam.phoenixit.ru/api/apiclient/user/', auth=(user_api, pass_api), verify=False).json()['data']['token']
headers = {'token':token}
if cidr is None:
print("!!! --net is not defined, quit...")
quit()
cidr_url = 'https://ipam.phoenixit.ru/api/apiclient/subnets/cidr/' + cidr
get_subnet_id = requests.get(url=cidr_url, headers=headers, verify=False).json()['data'][0]['id']
print ("### SUBnet ID for ["+cidr+"] is: ["+get_subnet_id+"]")
if infraname is None:
print ("!!! Description is None, exit")
quit()
get_ip_url = "https://ipam.phoenixit.ru/api/apiclient/addresses/first_free/"+get_subnet_id
ip = requests.get(url=get_ip_url, headers=headers, verify=False).json()['data']
create_url = "https://ipam.phoenixit.ru/api/apiclient/addresses/?subnetId="+get_subnet_id+"&ip="+ip+"&hostname="+hostname+"&description="+infraname
print ("### PhpIPAM, create_url: " + create_url)
create = requests.post(url = create_url , headers=headers, verify=False).json()['success']
if create == True:
print ("### NEW IP for ["+hostname+"] is: ["+ip+"]")
return ip # get ip address
else:
print("!!! При выделении ip произошла ошибка.")
quit()
except:
print("!!! При выделении IP произошла ошибка! ",sys.exc_info())
quit()
def ipam_rm_ip(ip, cidr):
print ("function: ipam_rm_ip("+ ip +")")
token = requests.post('https://ipam.phoenixit.ru/api/apiclient/user/', auth=(user_api, pass_api), verify=False).json()['data']['token']
headers = {'token':token}
cidr_url = 'https://ipam.phoenixit.ru/api/apiclient/subnets/cidr/' + cidr
get_subnet_id = requests.get(url=cidr_url, headers=headers, verify=False).json()['data'][0]['id']
print ("### SUBnet ID for ["+cidr+"] is: ["+get_subnet_id+"]")
rm_ip_url = "https://ipam.phoenixit.ru/api/apiclient/addresses/"+ip+"/40/"
rm_ip = requests.delete(url=rm_ip_url, headers=headers, verify=False).json()
print(rm_ip)
#except:
# print("!!! При удалении IP произошла ошибка! ",sys.exc_info())
# quit()
#folder project terraform (linux&windows) return ter_dir (./linux, ./windows)
def template(vm_template, vm_destination):
if vm_template in template_linux:
if vm_destination == 'host':
ter_dir = './linux_host'
else:
ter_dir = './linux'
print ("### TER DIR: ["""+ter_dir+"]")
return ter_dir
elif vm_template in template_wind:
if vm_template == 'template_WinSrv2016EN':
ter_dir = './windows_2016'
else:
ter_dir = './windows'
if vm_destination == 'host':
ter_dir = ter_dir+'_host'
print ("### TER DIR: ["+ter_dir+"]")
return ter_dir
else:
print ('!!! No template found: '+vm_template+'!')
i=1
print("For Linux: ")
for template in template_linux:
print (" "+str(i)+". "+template)
i=i+1
i=1
print("For Windows: ")
for template in template_wind:
print (" "+str(i)+". "+template)
i=i+1
quit()
## функция пробегает по всем элементам массива и выводит их на экран
def print_array(arr):
for a in arr:
print( str(a) )
def create_vm_terraform(ter_dir, hostname, ip, cidr, vc_host, vc_user, vc_pass, vc_dc, vm_destination2, vc_storage, vm_template,
vm_cpu, vm_ram, vm_disk_size, debug ):
vm_ip_gw = re.sub('[/]', '', cidr)[:-3] + '1' # get GW (example 192.168.222.1)
vm_netmask = cidr[-2:] # get prefix netmask (example /24)вд
if port_int[cidr]:
vm_portgroup = port_int.get(cidr)
print ("### PortGroup is: " + vm_portgroup)
else:
print ('!!! No network portgroup!')
#print("IP: " + ip)
tf = Terraform(working_dir=ter_dir, variables={ 'vc_host': vc_host,
'vc_user': vc_user, 'vc_pass': vc_pass,
'vc_dc': vc_dc, 'vc_destination': vm_destination2, 'vc_storage': vc_storage,
'vm_portgroup': vm_portgroup, 'vm_template': vm_template,
'vm_hostname': hostname, 'vm_cpu': vm_cpu, 'vm_ram': vm_ram,
'vm_disk_size': vm_disk_size, 'vm_ip': ip, 'vm_ip_gw': vm_ip_gw,
'vm_netmask': vm_netmask} )
print ("\nTeraform Init....")
out_tinit = tf.init()
if debug:
print_array(out_tinit)
print ("\nTeraform Plan....")
out_tplan = tf.plan()
if debug:
print_array(out_tplan)
#try:
# #out = tf.plan(no_color=IsFlagged, refresh=False)
# if debug: # is debug mode print all output
# print( str(out[2]) )
# print( str(out[3]) )
# #else:
# # tf.plan( no_color=IsFlagged, refresh=False, capture_output=True )
#except:
# print ("!!! ERROR in create_vm_terraform(tf.plan()): ", sys.exc_info())
# bye()
print ("\nTeraform Apply....")
approve = {"auto-approve":True}
out_tapply = tf.apply(auto_approve=True)
if debug:
print_array(out_tapply)
err = str(out_tapply).find("Error")
print( "ERRORs: "+str(err) )
if err > 0: # если в выводе teraform apply есть слово Error то пишем ошибку и выходим.
print("create_vm_terraform: Error applying plan")
print_array(out_tapply)
quit()
#try:
# if debug: # is debug mode print all output
# print( tf.apply(auto_approve=True, capture_output=True) )
# else:
# tf.apply(auto_approve=True)
#except:
# print ("!!! ERROR in create_vm_terraform(tf.apply()): ", sys.exc_info())
# bye()
# remove teraform state file
if os.path.exists(ter_dir+"/terraform.tfstate"):
os.remove(ter_dir+"/terraform.tfstate")
print("### Teraform state file removed")
else:
print("### Teraform state file is't Exist")
#change folder, write notes
def notes_write_vm(vc_host, vc_user, vc_pass, ip, infraname, expired):
service_instance = connect.SmartConnectNoSSL(host=vc_host, user=vc_user, pwd=vc_pass, port=443)
config_uuid = service_instance.content.searchIndex.FindByIp(None, ip, True)
uuid = config_uuid.summary.config.instanceUuid
message = "AutoDeploy, IP:" + ip + ", " + infraname
if expired is not None:
message = message + ", exp: " + expired
vm = service_instance.content.searchIndex.FindByUuid(None, uuid, True, True)
print("Found vm: {0}".format(vm.name))
spec = vim.vm.ConfigSpec()
spec.annotation = message
task = vm.ReconfigVM_Task(spec)
def move_vm_to_folder(vc_host, ip, folder_vm, cluster, dc):
if ip is None:
print("!!! Please enter --ip xxx.xxx.xxx.xxx")
quit()
if folder_vm is None:
print("!!! Please enter --folder Name")
quit()
#default folders path
folder_dc_pass = { 'vc-linx.srv.local':'Datacenter-Linx/vm/', 'vcsa.srv.local':'PHX/vm/', 'vc-khut.srv.local':'ATK/vm/' }
# for multi datacenter VCSA
if cluster in ["sav-r11-cl2","sav-r24-cl1","sav-r24-cl2","sav-r24_e5-26","r24_SandyBridge","r39-cluster"]:
folder_dc_pass['vcsa.srv.local'] = 'ATK/vm/'
folder_dc = folder_dc_pass.get(vc_host)
service_instance = connect.SmartConnectNoSSL(host=vc_host, user=vc_user, pwd=vc_pass, port=443)
vm = service_instance.content.searchIndex.FindByIp(None, ip, True)
if vm == None:
print("!!! VM: ["+ ip + "] IS NOT EXIST!")
quit()
folder = service_instance.content.searchIndex.FindByInventoryPath(folder_dc + folder_vm)
print( "Folder: "+str(folder) )
if folder != None:
print ("Folder: "+str(folder_dc+folder_vm)+" Exist.")
folder.MoveIntoFolder_Task([vm])
else:
print("!!! Folder: " +folder_dc+folder_vm+ " is not Exist.")
print("Create folder: " + folder_vm)
os.system('./tools/create_folder_in_datacenter.py -s "'+vc_host+'" -u "'+vc_user+'" -p "'+vc_pass+'" -d "'+ dc +'" -f "'+folder_vm+'"')
print("Check folder")
folder = service_instance.content.searchIndex.FindByInventoryPath(folder_dc + folder_vm)
if folder != None:
print("Move vm "+ str(vm) +" to folder: "+folder_vm)
folder.MoveIntoFolder_Task([vm])
else:
print("!!! Failed to create folder: " + folder_dc + folder_vm)
quit()
def main(hostname, infraname, cidr, vc_host, vc_dc, vm_destination2, vc_storage, vm_template, vm_cpu, vm_ram, vm_disk_size, folder_vm, ip, debug, expire_vm_date, vm_destination):
ter_dir = template(vm_template, vm_destination)
# remove teraform state file
if os.path.exists(ter_dir+"/terraform.tfstate"):
os.remove(ter_dir+"/terraform.tfstate")
print("!!! Teraform state file exist, removed.")
else:
print("### Teraform state file is't Exist, it's OK.")
if ip is None:
ip = ipam_create_ip(hostname, infraname, cidr)
else:
print ("### YOUR IP is: ["+ip+"]")
create_vm_terraform(ter_dir, hostname, ip, cidr, vc_host, vc_user, vc_pass, vc_dc, vm_destination2, vc_storage, vm_template, vm_cpu, vm_ram, vm_disk_size, debug)
return ip
#exemple expire_vm_date 'DD/MM/YY'
#hostname this is name vm vcenter
def scheduledTask_poweroff(hostname, expire_vm_date, vc_host):
si = connect.SmartConnectNoSSL(host=vc_host, user=vc_user, pwd=vc_pass, port=443)
try:
datefind = re.findall('(\w\w)',expire_vm_date)
(d, m, y) = (datefind[0], datefind[1], datefind[len(datefind)-1])
#dt = datetime.strptime(expire_vm_date+" 10:30", "%d/%m/%y %H:%M")
dt = datetime.datetime.strptime(d + "/" + m + "/" + y +" 10:30", "%d/%m/%y %H:%M")
print("### Expired date is: " + str(dt))
except:
print("!!! Invalid date specified"+expire_vm_date+"->"+str(dt))
quit()
view = si.content.viewManager.CreateContainerView(si.content.rootFolder, [vim.VirtualMachine],True)
vms = [vm for vm in view.view if vm.name == hostname]
if not vms:
print('!!! VM not found')
connect.Disconnect(si)
quit()
return -1
vm = vms[0]
spec = vim.scheduler.ScheduledTaskSpec()
today = datetime.datetime.today().strftime("%d%m%Y.%H%M")
spec.name = '[%s] PowerOff [%s]' % (today, hostname)
spec.description = 'expire date order vm'
spec.scheduler = vim.scheduler.OnceTaskScheduler()
spec.scheduler.runAt = dt
spec.action = vim.action.MethodAction()
spec.action.name = vim.VirtualMachine.PowerOff
spec.enabled = True
si.content.scheduledTaskManager.CreateScheduledTask(vm, spec)