-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvm_lifecycle.py
288 lines (244 loc) · 9.16 KB
/
vm_lifecycle.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
#!/usr/bin/env python3
# vm_lifecycle.py
#
# Purpose: Demonstrate basic VM examples and waiting for task completion using the Scale Computing REST API
#
# Usage: python3 ./vm_lifecycle.py
#
import base64
import getpass
import http.client as http
import json
import ssl
class InternalException(Exception):
pass
class TaskException(InternalException):
def __init__(self, tag, message, parameters):
self.tag = tag
self.message = message
self.parameters = parameters
def __str__(self):
return '%s "%s" %s' % (self.tag, self.message, self.parameters)
class HTTPResponseException(InternalException):
def __init__(self, response):
self.response = response
self.body = response.read()
def __repr__(self):
return str(self)
def __str__(self):
return str(self.response.status) + ": " + str(self.body)
def get_host():
host = input("Cluster node hostname or IP: ")
if not host:
print('Failed to get host or IP')
exit(2)
return host
def get_credentials():
username = input("Username: ")
if not username:
print('Failed to get username')
exit(2)
password = getpass.getpass("Password: ")
if not password:
print('Failed to get password')
exit(2)
return str(base64.b64encode(bytes('{0}:{1}'.format(username, password), 'utf-8')), 'utf-8')
def get_connection(host):
timeout = 120
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
return http.HTTPSConnection(host, timeout=timeout, context=context)
def get_response(connection):
response = connection.getresponse()
if response.status != http.OK:
raise HTTPResponseException(response)
return json.loads(response.read().decode("utf-8"))
def wait_for_task_completion(connection, task_id):
inprogress = True
while inprogress:
connection.request(
'GET', '{0}/{1}'.format(url, 'TaskTag/{0}'.format(task_id)), None, rest_opts)
task_status = get_response(connection)[0]
if task_status['state'] == 'ERROR':
raise TaskException(
task_id, task_status['formattedMessage'], task_status['messageParameters'])
if task_status['state'] == 'COMPLETE':
inprogress = False
host = get_host()
url = 'https://{0}/rest/v1'.format(host)
credentials = 'Basic {0}'.format(get_credentials())
rest_opts = {
'Content-Type': 'application/json',
'Authorization': credentials,
'Connection': 'keep-alive'
}
def main():
connection = get_connection(host)
default_vm = {
'dom': {
'name': 'python-vmLifeCycle',
'description': 'An example VM created using the rest API',
'mem': 4294967296, # 4GiB
'numVCPU': 2,
'blockDevs': [{
'capacity': 10000000000, # 10GB
'type': 'VIRTIO_DISK',
'cacheMode': 'WRITETHROUGH'
}],
'netDevs': [{
'type': 'VIRTIO'
}]
},
'options': {
# attachGuestToolsISO = true
}
}
print("Create VM")
connection.request(
'POST', '{0}/VirDomain'.format(url), json.dumps(default_vm), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
vmUUID = result['createdUUID']
print("Create a block device for the VM")
block_device_attrs = {
'virDomainUUID': vmUUID,
'capacity': 10000000000, # 10GB
'type': 'VIRTIO_DISK',
'cacheMode': 'WRITETHROUGH'
}
connection.request('POST', '{0}/VirDomainBlockDevice'.format(url), json.dumps(
block_device_attrs), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
print("Start VM")
start_vm = [{
'actionType': 'START',
'virDomainUUID': vmUUID
}]
connection.request(
'POST', '{0}/VirDomain/action'.format(url), json.dumps(start_vm), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
# Create a list of cluster nodes without the one the VM is running on
connection.request('GET', '{0}/Node'.format(url), None, rest_opts)
nodes = get_response(connection)
if len(nodes) > 1:
print("Live migrate VM to another node")
# Get VM Info to determine which node the VM is currently running on
connection.request(
'GET', '{0}/VirDomain/{1}'.format(url, vmUUID), None, rest_opts)
result = get_response(connection)
vm = result[0]
nodeUUIDs = [node['uuid']
for node in nodes if node['uuid'] != vm['nodeUUID']]
migrate_vm_to_node = [{
'actionType': 'LIVEMIGRATE',
'nodeUUID': nodeUUIDs[0], # just use the first node in the list
'virDomainUUID': vmUUID
}]
connection.request('POST', '{0}/VirDomain/action'.format(url), json.dumps(
migrate_vm_to_node), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
print("Stop VM")
stop_vm = [{
'actionType': 'STOP',
'virDomainUUID': vmUUID
}]
connection.request(
'POST', '{0}/VirDomain/action'.format(url), json.dumps(stop_vm), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
print("Update VM properties")
edit_vm_attrs = {
'name': default_vm['dom']['name'] + 'Updated',
'numVCPU': 4,
'mem': 8589934592, # 8GiB
'tags': ",".join(['burger', 'tastes', 'good'])
}
connection.request(
'PATCH', '{0}/VirDomain/{1}'.format(url, vmUUID), json.dumps(edit_vm_attrs), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
# The following (available in v9.1) is an example cloud-init customization that would be recognized
# on first boot by a cloud-image VM configured to run cloud-init. (Note: the VM
# cloned in this example script is not a cloud-image configured to run cloud-init)
# More example cloud-config and documentation can be found here:
# https://cloudinit.readthedocs.io/en/latest/topics/examples.html
#
# yaml for cloud-init meta-data
metaData = '''
dsmode: local
local-hostname:"VMLifeycleTest"
'''
# yaml for cloud-init user-data
userData = '''
#cloud-config
#apt_update: true
#apt_upgrade: true
password: my_secret_password
chpasswd: { expire: false }
ssh_pwauth: true
#ssh_authorized_keys:
# - ssh-rsa my_secret_ssh_key
apt: {sources: {docker.list: {source: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu $RELEASE stable', keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88}}}
packages: [qemu-guest-agent, docker-ce, docker-ce-cli]
bootcmd:
- [ sh, -c, 'sudo echo GRUB_CMDLINE_LINUX="nomodeset" >> /etc/default/grub' ]
- [ sh, -c, 'sudo echo GRUB_GFXPAYLOAD_LINUX="1024x768" >> /etc/default/grub' ]
- [ sh, -c, 'sudo echo GRUB_DISABLE_LINUX_UUID=true >> /etc/default/grub' ]
- [ sh, -c, 'sudo update-grub' ]
runcmd:
- [docker, pull, hello-world]
- [docker, run, hello-world]
- [docker, images, hello-world]
'''
# base64 encode the cloud-init data
metaData64 = str(base64.b64encode(bytes(metaData, 'utf-8')), 'utf-8')
userData64 = str(base64.b64encode(bytes(userData, 'utf-8')), 'utf-8')
cloudInitData64 = {
'userData' : userData64,
'metaData' : metaData64
}
print("Clone VM")
vm_clone_attrs = {
'template': {
'name': 'python-vmLifeCycle-Clone-CloudInit',
'description': 'An example VM cloned using the rest API with cloud-init data',
'cloudInitData' : cloudInitData64
}
}
connection.request('POST', '{0}/VirDomain/{1}/clone'.format(
url, vmUUID), json.dumps(vm_clone_attrs), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
cloneUUID = result['createdUUID']
print("Create VM snapshot")
snapshotUUID = ''
snapshot_attrs = {
'domainUUID': vmUUID,
'label': 'Created by Python Script'
}
connection.request(
'POST', '{0}/VirDomainSnapshot'.format(url), json.dumps(snapshot_attrs), rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
snapshotUUID = result['createdUUID']
print("Delete VM snapshot")
connection.request(
'DELETE', '{0}/VirDomainSnapshot/{1}'.format(url, snapshotUUID), None, rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
print("Delete original VM")
connection.request(
'DELETE', '{0}/VirDomain/{1}'.format(url, vmUUID), None, rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
print("Delete cloned VM")
connection.request(
'DELETE', '{0}/VirDomain/{1}'.format(url, cloneUUID), None, rest_opts)
result = get_response(connection)
wait_for_task_completion(connection, result['taskTag'])
return 0
if __name__ == '__main__':
exit(main())