forked from olileach/vcloud-autodeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvCloudDeployVm.py
252 lines (176 loc) · 10.1 KB
/
vCloudDeployVm.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
import base64
import requests
import xml.etree.ElementTree as ET
import time
from vCloudLogger import vCloud_Logger
class vCloud_Deploy(object):
vm_name = None
vm_ip = None
vm_href = None
vm_temp_name = None
def __init__(self):
self.login = None
self.headers = None
self.endpoint = None
self.org = None
self.root = None
self.vapp_template = None
self.vapp = None
self.x = vCloud_Logger()
def sessions(self, username, org, password, key, secret, endpoint):
self.endpoint = endpoint
self.login = {'Accept':'application/*+xml;version=5.1', \
'Authorization':'Basic '+ base64.b64encode(username + "@" + org + ":" + password), \
'x-id-sec':base64.b64encode(key + ":" + secret)}
p = requests.post(self.endpoint + 'sessions', headers = self.login)
self.headers = {'Accept':'application/*+xml;version=5.1'}
for k,v in p.headers.iteritems():
if k == 'x-json':
access_token_value = 'Bearer %s' % v[21:57]
self.headers["Authorization:"]=access_token_value
if k == "x-vcloud-authorization" : self.headers[k]=v
self.org_url()
self.x.log(lvl='i',msg=("session headers created ...OK"))
def org_url(self):
g = requests.get(self.endpoint + 'org', data=None, headers = self.headers)
root = ET.fromstring(g.content)
for child in root:
self.org = child.get("href")
g = requests.get(self.org, data=None, headers = self.headers)
self.root = ET.fromstring(g.content)
return self.org
def vapp_templates(self, template_name):
vCloud_Deploy.vm_temp_name = template_name
r = requests.get(self.endpoint + '/vAppTemplates/query',headers = self.headers)
root = ET.fromstring(r.content)
for child in root:
if child.tag == ('''{http://www.vmware.com/vcloud/v1.5}VAppTemplateRecord'''):
for k,v in child.attrib.iteritems():
if v == template_name : self.vapp_template = child.attrib['href']
self.x.log(lvl='i',msg=("vapp template variables set ...OK"))
def vapps(self, vapp_name):
r = requests.get(self.endpoint + '/vApps/query',headers = self.headers)
root = ET.fromstring(r.content)
for child in root:
if child.tag == ('''{http://www.vmware.com/vcloud/v1.5}VAppRecord'''):
for k,v in child.attrib.iteritems():
if v == vapp_name : self.vapp = child.attrib['href']
self.recompose_vapp()
def recompose_vapp(self):
post_headers = self.headers
post_headers['Content-Type']='application/vnd.vmware.vcloud.recomposeVAppParams+xml'
xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RecomposeVAppParams
xmlns="http://www.vmware.com/vcloud/v1.5"
xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
xmlns:environment_1="http://schemas.dmtf.org/ovf/environment/1">
<Description> "api deployed vm to ol-vapp-04" </Description>
<SourcedItem sourceDelete="false">
<Source href="%s"/>
</SourcedItem>
<AllEULAsAccepted>true</AllEULAsAccepted>
</RecomposeVAppParams>""" % (self.vapp_template)
post = requests.post(self.vapp + '/action/recomposeVApp', data=xml, headers=post_headers)
result = ET.fromstring(post.text)
self.x.log(lvl='i',msg=("recomposed vapp ...OK"))
self.task_progress(task_id=result.attrib['id'])
def task_progress(self, task_id):
task_id_href = None
for child in self.root:
if child.get('type') == 'application/vnd.vmware.vcloud.tasksList+xml':
r = requests.get(child.get('href'),headers = self.headers)
root = ET.fromstring(r.content)
for child in root:
for k,v in child.attrib.iteritems():
if k == 'id' and v == task_id:
task_id_href = child.attrib['href']
x = 0
self.x.log(lvl='i',msg=("checking progress of deployed vm ...OK"))
while x == 0:
time.sleep(5)
g = requests.get(task_id_href, headers = self.headers)
root = ET.fromstring(g.content)
for child in root.iter():
tag = child.tag
tag = tag.replace("{http://www.vmware.com/vcloud/v1.5}","")
if tag == 'Progress' :
if child.text == '100' : x = 1
time.sleep(15)
self.query_vms()
def query_vms(self):
r = requests.get(self.endpoint + 'vms/query', headers = self.headers)
root = ET.fromstring(r.content)
self.x.log(lvl='i',msg=("checking for vm name ...OK"))
for child in root:
if child.tag == ('''{http://www.vmware.com/vcloud/v1.5}VMRecord'''):
for k,v in child.attrib.iteritems():
if v == vCloud_Deploy.vm_temp_name:
for k,v in child.attrib.iteritems():
if k == "href" and 'vAppTemplate' not in v :
vCloud_Deploy.vm_href = v
self.update_name()
def update_name(self):
#minorErrorCode="BUSY_ENTITY" message="The entity is busy completing an operation." when vapp was busy # try execpt needed.
xml = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vcloud:Vm
xmlns:vcloud="http://www.vmware.com/vcloud/v1.5"
name="%s">
</vcloud:Vm>""" % (vCloud_Deploy.vm_name)
post_headers = self.headers
post_headers['Content-Type']='application/vnd.vmware.vcloud.vm+xml'
post = requests.put(vCloud_Deploy.vm_href, data=xml, headers=post_headers)
result = ET.fromstring(post.text)
self.x.log(lvl='i',msg=("updated vm name ...OK"))
time.sleep(15)
self.update_network()
def update_network(self):
xml = """<?xml version="1.0" encoding="UTF-8"?>
<NetworkConnectionSection
xmlns="http://www.vmware.com/vcloud/v1.5"
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1">
<ovf:Info>Specifies the available VM network connections</ovf:Info>
<PrimaryNetworkConnectionIndex>0</PrimaryNetworkConnectionIndex>
<NetworkConnection network="ol-lan-02" needsCustomization="true">
<NetworkConnectionIndex>0</NetworkConnectionIndex>
<IsConnected>true</IsConnected>
<IpAddressAllocationMode>POOL</IpAddressAllocationMode>
</NetworkConnection>
</NetworkConnectionSection>"""
post_headers = self.headers
post_headers['Content-Type']='application/vnd.vmware.vcloud.networkConnectionSection+xml'
post = requests.put(vCloud_Deploy.vm_href + '/networkConnectionSection', data=xml, headers=post_headers)
result = ET.fromstring(post.text)
self.x.log(lvl='i',msg=("setting and connectng vm network ...OK"))
time.sleep(15)
self.update_hostname()
def update_hostname(self):
xml = """<?xml version="1.0" encoding="UTF-8"?>
<GuestCustomizationSection
xmlns="http://www.vmware.com/vcloud/v1.5"
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
ovf:required="false">
<ovf:Info>Specifies Guest OS Customization Settings</ovf:Info>
<Enabled>true</Enabled>
<ComputerName>%s</ComputerName>
</GuestCustomizationSection>""" % (vCloud_Deploy.vm_name)
post_headers = self.headers
post_headers['Content-Type']='application/vnd.vmware.vcloud.guestcustomizationsection+xml'
post = requests.put(vCloud_Deploy.vm_href + '/guestCustomizationSection', data=xml, headers=post_headers)
result = ET.fromstring(post.text)
time.sleep (5)
self.ip()
def ip(self):
g = requests.get(vCloud_Deploy.vm_href, headers = self.headers)
root = ET.fromstring(g.content)
for child in root.iter():
if child.tag == '{http://www.vmware.com/vcloud/v1.5}IpAddress':
vCloud_Deploy.vm_ip = child.text
self.x.log(lvl='i',msg=("capturing vm IP address ...OK"))
time.sleep (15)
self.power_on_vm()
def power_on_vm(self):
g = requests.post(vCloud_Deploy.vm_href + '/power/action/powerOn', headers=self.headers)
root = ET.fromstring(g.content)
self.x.log(lvl='i',msg=("virtual machine powered on ...OK"))