-
Notifications
You must be signed in to change notification settings - Fork 42
/
vms.py
577 lines (481 loc) · 20.5 KB
/
vms.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# This file is part of Ansible
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import base64
import os
from copy import deepcopy
from ansible.module_utils.basic import _load_params
from .clusters import Cluster, get_cluster_uuid
from .groups import get_entity_uuid
from .images import get_image_uuid
from .prism import Prism
from .projects import Project
from .spec.categories_mapping import CategoriesMapping
from .subnets import get_subnet_uuid
from .users import User
class VM(Prism):
def __init__(self, module):
resource_type = "/vms"
super(VM, self).__init__(module, resource_type=resource_type)
if self.module.params.get("load_params_without_defaults", True):
self.params_without_defaults = _load_params()
else:
self.params_without_defaults = self.module.params
self.require_vm_restart = False
self.build_spec_methods = {
"name": self._build_spec_name,
"desc": self._build_spec_desc,
"project": self._build_spec_project,
"owner": self._build_spec_owner,
"cluster": self._build_spec_cluster,
"vcpus": self._build_spec_vcpus,
"cores_per_vcpu": self._build_spec_cores,
"memory_gb": self._build_spec_mem,
"networks": self._build_spec_networks,
"disks": self._build_spec_disks,
"boot_config": self._build_spec_boot_config,
"guest_customization": self._build_spec_gc,
"timezone": self._build_spec_timezone,
"categories": CategoriesMapping.build_categories_mapping_spec,
"remove_categories": CategoriesMapping.build_remove_all_categories_spec,
}
def list(
self,
data=None,
endpoint=None,
use_base_url=False,
raise_error=True,
no_response=False,
timeout=30,
max_length=500,
):
if data.get("length", 0) > max_length:
spec = deepcopy(data)
resp = {"entities": []}
total_matches = None
total_length = spec["length"]
spec["length"] = max_length
spec["offset"] = spec.get("offset", 0)
while True:
sub_resp = super(VM, self).list(spec)
resp["entities"].extend(sub_resp["entities"])
total_matches = sub_resp["metadata"].get("total_matches")
total_length -= max_length
if total_length <= 0:
break
spec["length"] = (
total_length if total_length < max_length else max_length
)
spec["offset"] += max_length
resp["metadata"] = data
resp["metadata"]["total_matches"] = total_matches
resp["metadata"]["length"] = len(resp["entities"])
else:
resp = super(VM, self).list(data)
return resp
@staticmethod
def is_on(payload):
return True if payload["spec"]["resources"]["power_state"] == "ON" else False
def get_clone_spec(self):
spec, error = self.get_spec({"spec": {"resources": {}}})
if error:
return spec, error
spec["spec"].update(spec["spec"].pop("resources", {}))
spec["spec"].pop("hardware_clock_timezone", None)
spec = {"override_spec": spec["spec"]}
return spec, None
def clone(self, spec):
endpoint = "{0}/clone".format(self.module.params["src_vm_uuid"])
resp = self.create(spec, endpoint)
return resp
def get_ova_image_spec(self):
return deepcopy(
{
"name": self.module.params["name"],
"disk_file_format": self.module.params["file_format"],
}
)
def create_ova_image(self, spec):
endpoint = "{0}/{1}".format(self.module.params["src_vm_uuid"], "export")
resp = self.create(spec, endpoint)
return resp
def power_on(self, payload, raise_error=True):
uuid = payload["metadata"]["uuid"]
payload["spec"]["resources"]["power_state"] = "ON"
resp = self.update(payload, uuid, raise_error=raise_error)
return resp
def soft_shutdown(self, payload, raise_error=True):
uuid = payload["metadata"]["uuid"]
payload["spec"]["resources"]["power_state"] = "OFF"
payload["spec"]["resources"]["power_state_mechanism"]["mechanism"] = "ACPI"
resp = self.update(payload, uuid, raise_error=raise_error)
return resp
def hard_power_off(self, payload, raise_error=True):
uuid = payload["metadata"]["uuid"]
payload["spec"]["resources"]["power_state"] = "OFF"
payload["spec"]["resources"]["power_state_mechanism"]["mechanism"] = "HARD"
resp = self.update(payload, uuid, raise_error=raise_error)
return resp
def is_restart_required(self):
if self.require_vm_restart:
return True
return False
@staticmethod
def set_power_state(spec, power_state):
spec["spec"]["resources"]["power_state"] = power_state
def _get_default_spec(self):
return deepcopy(
{
"api_version": "3.1.0",
"metadata": {"kind": "vm"},
"spec": {
"cluster_reference": {"kind": "cluster", "uuid": None},
"name": None,
"resources": {
"num_sockets": 1,
"num_vcpus_per_socket": 1,
"memory_size_mib": 4096,
"power_state": "ON",
"disk_list": [],
"nic_list": [],
"gpu_list": [],
"boot_config": {
"boot_type": "LEGACY",
"boot_device_order_list": ["CDROM", "DISK", "NETWORK"],
},
"hardware_clock_timezone": "UTC",
},
},
}
)
def _get_default_boot_config_spec(self):
return deepcopy(
{
"boot_type": "LEGACY",
"boot_device_order_list": ["CDROM", "DISK", "NETWORK"],
}
)
def _get_default_network_spec(self):
return deepcopy(
{
"ip_endpoint_list": [],
"subnet_reference": {"kind": "subnet", "uuid": None},
"is_connected": True,
}
)
def _get_default_disk_spec(self):
return deepcopy(
{
"device_properties": {
"device_type": "DISK",
"disk_address": {"adapter_type": None, "device_index": None},
},
"storage_config": {
"storage_container_reference": {
"kind": "storage_container",
"uuid": None,
}
},
"data_source_reference": {"kind": "image", "uuid": None},
}
)
def _build_spec_name(self, payload, value):
payload["spec"]["name"] = value
return payload, None
def _build_spec_desc(self, payload, value):
payload["spec"]["description"] = value
return payload, None
def _build_spec_project(self, payload, param):
if "name" in param:
project = Project(self.module)
name = param["name"]
uuid = project.get_uuid(name)
if not uuid:
error = "Project {0} not found.".format(name)
return None, error
elif "uuid" in param:
uuid = param["uuid"]
payload["metadata"].update(
{"project_reference": {"uuid": uuid, "kind": "project"}}
)
return payload, None
def _build_spec_owner(self, payload, param):
if "name" in param:
owner = User(self.module)
name = param["name"]
uuid = owner.get_uuid(name, key="username")
if not uuid:
error = "Owner {0} not found.".format(name)
return None, error
elif "uuid" in param:
uuid = param["uuid"]
payload["metadata"].update({"owner_reference": {"uuid": uuid, "kind": "user"}})
return payload, None
def _build_spec_cluster(self, payload, param):
uuid, err = get_cluster_uuid(param, self.module)
if err:
return None, err
payload["spec"]["cluster_reference"] = {"kind": "cluster", "uuid": uuid}
return payload, None
def _build_spec_vcpus(self, payload, vcpus):
current_vcpus = payload["spec"]["resources"].get("num_sockets", 0)
self._check_and_set_require_vm_restart(current_vcpus, vcpus)
payload["spec"]["resources"]["num_sockets"] = vcpus
return payload, None
def _build_spec_cores(self, payload, cores):
self.require_vm_restart = True
payload["spec"]["resources"]["num_vcpus_per_socket"] = cores
return payload, None
def _build_spec_mem(self, payload, mem_gb):
mem_mib = mem_gb * 1024
current_mem_mib = payload["spec"]["resources"].get("memory_size_mib", 0)
self._check_and_set_require_vm_restart(current_mem_mib, mem_mib)
payload["spec"]["resources"]["memory_size_mib"] = mem_mib
return payload, None
def _build_spec_networks(self, payload, networks):
cluster_name_uuid_map = {}
nics = []
for network in networks:
if network.get("uuid"):
nic = self._filter_by_uuid(
network["uuid"], payload["spec"]["resources"]["nic_list"]
)
payload["spec"]["resources"]["nic_list"].remove(nic)
if network.get("state") == "absent":
continue
network = self._filter_by_uuid(
network["uuid"], self.params_without_defaults.get("networks", [])
)
else:
nic = self._get_default_network_spec()
if network.get("private_ip"):
nic["ip_endpoint_list"] = [{"ip": network["private_ip"]}]
if network.get("mac_address"):
nic["mac_address"] = network["mac_address"]
nic["is_connected"] = network["is_connected"]
if network.get("subnet"):
if network.get("subnet", {}).get("uuid"):
uuid = network["subnet"]["uuid"]
elif network.get("subnet", {}).get("name"):
config = {"name": network["subnet"]["name"]}
uuid = ""
# check if cluster given for filtering subnet
cluster_uuid = ""
if network.get("subnet", {}).get("cluster"):
if network["subnet"]["cluster"].get("uuid"):
cluster_uuid = network["subnet"]["cluster"].get("uuid")
else:
if not cluster_name_uuid_map:
cluster = Cluster(self.module)
cluster_name_uuid_map = (
cluster.get_all_clusters_name_uuid_map()
)
cluster_uuid = cluster_name_uuid_map.get(
network["subnet"]["cluster"]["name"]
)
if not cluster_uuid:
return None, "Cluster {0} not found".format(
network["subnet"]["cluster"]["name"]
)
config["cluster_uuid"] = cluster_uuid
uuid, err = get_subnet_uuid(config, self.module)
if err:
return None, err
nic["subnet_reference"]["uuid"] = uuid
nics.append(nic)
if payload["spec"]["resources"].get("nic_list"):
payload["spec"]["resources"]["nic_list"] += nics
else:
payload["spec"]["resources"]["nic_list"] = nics
return payload, None
def _build_spec_disks(self, payload, vdisks):
device_indexes = {}
existing_devise_indexes = list(
map(
lambda d: d["device_properties"]["disk_address"],
payload["spec"]["resources"]["disk_list"],
)
)
for vdisk in vdisks:
if vdisk.get("uuid"):
if vdisk.get("state") == "absent":
self._remove_disk(vdisk, payload, existing_devise_indexes)
else:
self._update_disk(vdisk, payload)
else:
disk = self._add_disk(vdisk, device_indexes, existing_devise_indexes)
payload["spec"]["resources"]["disk_list"].append(disk)
return payload, None
def _build_spec_boot_config(self, payload, param):
if not payload["spec"]["resources"].get("boot_config"):
payload["spec"]["resources"][
"boot_config"
] = self._get_default_boot_config_spec()
boot_config = payload["spec"]["resources"]["boot_config"]
if "LEGACY" == param["boot_type"] and "boot_order" in param:
boot_config["boot_device_order_list"] = param["boot_order"]
elif "UEFI" == param["boot_type"]:
boot_config.pop("boot_device_order_list", None)
boot_config["boot_type"] = "UEFI"
elif "SECURE_BOOT" == param["boot_type"]:
boot_config.pop("boot_device_order_list", None)
boot_config["boot_type"] = "SECURE_BOOT"
payload["spec"]["resources"]["machine_type"] = "Q35"
return payload, None
def _build_spec_gc(self, payload, param):
fpath = param["script_path"]
if not os.path.exists(fpath):
error = "File not found: {0}".format(fpath)
return None, error
with open(fpath, "rb") as f:
content = base64.b64encode(f.read())
gc_spec = {"guest_customization": {}}
if "sysprep" in param["type"]:
gc_spec["guest_customization"] = {
"sysprep": {"install_type": "PREPARED", "unattend_xml": content}
}
elif "cloud_init" in param["type"]:
gc_spec["guest_customization"] = {"cloud_init": {"user_data": content}}
if "is_overridable" in param:
gc_spec["guest_customization"]["is_overridable"] = param["is_overridable"]
payload["spec"]["resources"].update(gc_spec)
return payload, None
def _build_spec_timezone(self, payload, value):
payload["spec"]["resources"]["hardware_clock_timezone"] = value
return payload, None
def _check_and_set_require_vm_restart(self, current_value, new_value):
if new_value < current_value:
self.require_vm_restart = True
def _filter_by_uuid(self, uuid, items_list):
try:
return next(filter(lambda d: d.get("uuid") == uuid, items_list))
except BaseException:
self.module.fail_json(
msg="Failed generating VM Spec",
error="Entity {0} not found.".format(uuid),
)
def _generate_disk_spec(
self, vdisk, disk, device_indexes=None, existing_devise_indexes=None
):
if vdisk.get("type"):
disk["device_properties"]["device_type"] = vdisk["type"]
bus = vdisk.get("bus")
if bus:
if bus in ["IDE", "SATA"]:
self.require_vm_restart = True
disk["device_properties"]["disk_address"]["adapter_type"] = bus
index = device_indexes.get(bus, -1) + 1
while True:
if not existing_devise_indexes.count(
{"adapter_type": bus, "device_index": index}
):
device_indexes[bus] = index
break
index += 1
disk["device_properties"]["disk_address"]["device_index"] = device_indexes[
bus
]
if vdisk.get("empty_cdrom", None):
disk.pop("data_source_reference", None)
disk.pop("storage_config", None)
else:
if vdisk.get("size_gb"):
disk_size_bytes = int(vdisk["size_gb"]) * 1024 * 1024 * 1024
if not vdisk.get("uuid") or (
"disk_size_bytes" in disk
and disk_size_bytes >= disk.get("disk_size_bytes", 0)
):
if disk.get("bus") in ["IDE", "SATA"]:
self.require_vm_restart = True
disk["disk_size_bytes"] = disk_size_bytes
else:
if disk.get("device_properties", {}).get("device_type") == "CDROM":
self.module.fail_json(
msg="Unsupported operation: Cannot resize empty cdrom.",
disk=disk,
)
self.module.fail_json(
msg="Unsupported operation: Unable to decrease disk size.",
disk=disk,
)
if vdisk.get("storage_container"):
disk.pop("data_source_reference", None)
uuid, error = get_entity_uuid(
vdisk["storage_container"],
self.module,
key="container_name",
entity_type="storage_container",
)
if error:
return None, error
disk["storage_config"]["storage_container_reference"]["uuid"] = uuid
if not vdisk.get("uuid") and not vdisk.get("size_gb"):
self.module.fail_json(
msg="Unsupported operation: Unable to create disk, 'size_gb' is required for using storage container.",
disk=disk,
)
elif vdisk.get("clone_image"):
uuid, error = get_image_uuid(vdisk["clone_image"], self.module)
if error:
return None, error
disk["data_source_reference"]["uuid"] = uuid
if (
not disk.get("storage_config", {})
.get("storage_container_reference", {})
.get("uuid")
):
disk.pop("storage_config", None)
if not disk.get("data_source_reference", {}).get("uuid"):
disk.pop("data_source_reference", None)
return disk
def _add_disk(self, vdisk, device_indexes, existing_devise_indexes):
disk = self._get_default_disk_spec()
disk = self._generate_disk_spec(
vdisk, disk, device_indexes, existing_devise_indexes
)
return disk
def _update_disk(self, vdisk, payload):
disk = self._filter_by_uuid(
vdisk["uuid"], payload["spec"]["resources"]["disk_list"]
)
disk.pop("disk_size_mib", None)
vdisk = self._filter_by_uuid(
vdisk["uuid"], self.params_without_defaults.get("disks", [])
)
self._generate_disk_spec(vdisk, disk)
def _remove_disk(self, vdisk, payload, existing_devise_indexes):
disk = self._filter_by_uuid(
vdisk["uuid"], payload["spec"]["resources"]["disk_list"]
)
existing_devise_indexes.remove(disk["device_properties"]["disk_address"])
if disk["device_properties"]["disk_address"]["adapter_type"] != "SCSI":
self.require_vm_restart = True
payload["spec"]["resources"]["disk_list"].remove(disk)
# Helper functions
def get_vm_uuid(config, module):
if "name" in config:
vm = VM(module)
name = config.get("name")
uuid = vm.get_uuid(name, "vm_name")
if not uuid:
error = "VM {0} not found.".format(name)
return None, error
elif "uuid" in config:
uuid = config.get("uuid")
return uuid, None
def get_vm_reference_spec(config, module):
uuid = config.get("uuid", "")
name = config.get("name", "")
if ("name" not in config) and ("uuid" not in config):
return None, "Provide name or uuid for building vm reference spec"
elif "name" not in config:
vm = VM(module)
resp = vm.read(config["uuid"])
name = resp["status"]["name"]
elif "uuid" not in config:
uuid, err = get_vm_uuid(config, module)
if err:
return None, err
vm_ref_spec = {"kind": "vm", "name": name, "uuid": uuid}
return vm_ref_spec, None