This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopm.py
executable file
·728 lines (672 loc) · 28.6 KB
/
opm.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import re
import subprocess
import sys
import xml.etree.ElementTree as ElementTree
class VmDisk:
def __init__(self, image=None, size_mb=None):
self.image = image
self.size_mb = size_mb
def __repr__(self):
return "VmDisk(image={0}, size_mb={1})".format(self.image, self.size_mb)
def pretty_tostring(self):
if self.size_mb is None:
return "image {0} of default size".format(self.image)
else:
return "image {0} of size {1} Mbytes".format(self.image, self.size_mb)
def override_config(self, params):
# logging.debug("Before override disk : {0}".format(self))
# logging.debug("Overriding disk with : {0}".format(params))
try:
self.image = params['image']
# logging.debug("image overridden to {0}".format(self.image))
except KeyError:
pass
try:
self.size_mb = params['size_mb']
# logging.debug("size_mb overridden to {0}".format(self.size_mb))
except KeyError:
pass
# logging.debug("After override vm : {0}".format(self))
def to_arg(self):
if self.size_mb is None:
return self.image
else:
return "{0}:size={1}".format(self.image, self.size_mb)
@staticmethod
def from_one_xml(disk_elem):
# <DISK>
# <IMAGE><![CDATA[ttylinux]]></IMAGE>
# <SIZE_MB><![CDATA[256]]></SIZE_MB>
# <IMAGE_UNAME><![CDATA[serveradmin]]></IMAGE_UNAME>
disk = VmDisk()
# logging.debug("Xml: {0}".format(ElementTree.tostring(disk_elem)))
# extract image
value = disk_elem.find("IMAGE")
if value is not None:
disk.image = value.text
# extract owner in case the image is not ours
value = disk_elem.find("IMAGE_UNAME")
if value is not None:
disk.image = "{0}[{1}]".format(value.text, disk.image)
# extract cpu
value = disk_elem.find("SIZE_MB")
if value is not None:
disk.size_mb = int(value.text)
# return constructed
# logging.debug("Parsed: {0}".format(disk))
return disk
def __eq__(self, other):
# logging.debug("Comparing {0} == {1}".format(self, other))
# image must be defined
if self.image is None or other.image is None:
raise Exception("A disk must be based on an image")
if self.image != other.image:
return False
# using default size always returns true
if self.size_mb is None or other.size_mb is None:
return True
return self.size_mb == other.size_mb
def __ne__(self, other):
# logging.debug("Comparing {0} != {1}".format(self, other))
return not self.__eq__(other)
class VmInfo:
@staticmethod
def from_one_xml(vm_elem):
# <VM>
# <ID>10</ID>
# <GNAME>oneadmin</GNAME>
# <NAME>test</NAME>
# <PERMISSIONS>
# <OWNER_U>1</OWNER_U>
# <OWNER_M>1</OWNER_M>
# <OWNER_A>0</OWNER_A>
# <GROUP_U>0</GROUP_U>
# <GROUP_M>0</GROUP_M>
# <GROUP_A>0</GROUP_A>
# <OTHER_U>0</OTHER_U>
# <OTHER_M>0</OTHER_M>
# <OTHER_A>0</OTHER_A>
# </PERMISSIONS>
# <STATE>8</STATE>
# <TEMPLATE>
# <CPU><![CDATA[0.1]]></CPU>
# <DISK> *many*
# <IMAGE><![CDATA[ttylinux]]></IMAGE>
# <SIZE><![CDATA[40]]></SIZE>
# </DISK>
# <MEMORY><![CDATA[256]]></MEMORY>
# <NIC> *many*
# <NETWORK><![CDATA[cloud]]></NETWORK>
# <NETWORK_UNAME><![CDATA[serveradmin]]></NETWORK_UNAME> *optional*
# <NIC_ID>0</NIC_ID>
# </NIC>
# <VCPU><![CDATA[1]]></VCPU>
# </VM>
vm = VmInfo()
# logging.debug("Xml: {0}".format(ElementTree.tostring(vm_elem)))
# extract name
value = vm_elem.find("NAME")
if value is not None:
vm.name = value.text
# extract group
value = vm_elem.find("GNAME")
if value is not None:
vm.group = value.text
# extract permissions
value = vm_elem.find("PERMISSIONS")
if value is not None:
u_u = int(value.find("OWNER_U").text)
u_m = int(value.find("OWNER_M").text)
u_a = int(value.find("OWNER_A").text)
g_u = int(value.find("GROUP_U").text)
g_m = int(value.find("GROUP_M").text)
g_a = int(value.find("GROUP_A").text)
o_u = int(value.find("OTHER_U").text)
o_m = int(value.find("OTHER_M").text)
o_a = int(value.find("OTHER_A").text)
vm.permissions = "{0}{1}{2}".format(
u_u * 4 + u_m * 2 + u_a,
g_u * 4 + g_m * 2 + g_a,
o_u * 4 + o_m * 2 + o_a)
# extract cpu
value = vm_elem.find("TEMPLATE/CPU")
if value is not None:
vm.cpu = float(value.text)
# extract vcpu
value = vm_elem.find("TEMPLATE/VCPU")
if value is not None:
vm.vcpu = int(value.text)
else:
vm.vcpu = 1
# extract mem_mb
value = vm_elem.find("TEMPLATE/MEMORY")
if value is not None:
vm.mem_mb = int(value.text)
# extract networks
value = vm_elem.findall("TEMPLATE/NIC")
if value is not None:
vm.networks = {}
for nic_elem in value:
name = nic_elem.find("NETWORK").text
order = int(nic_elem.find("NIC_ID").text)
# extract owner in case the image is not ours
owner = nic_elem.find("NETWORK_UNAME")
if owner is not None:
name = "{0}[{1}]".format(owner.text, name)
vm.networks[order] = name
vm.networks = [ vm.networks[key] for key in sorted(vm.networks.keys()) ]
# extract disks
value = vm_elem.findall("TEMPLATE/DISK")
if value is not None:
vm.disks = [ VmDisk.from_one_xml(x) for x in value ]
# extract one_template
vm.one_template = None
# extract id
value = vm_elem.find("ID")
if value is not None:
vm.id = int(value.text)
# extract state
value = vm_elem.find("STATE")
if value is not None:
vm.state = int(value.text)
# return constructed
logging.debug("Parsed: {0}".format(vm))
return vm
def __init__(self, name=None, cpu=None, vcpu=None, mem_mb=None, networks=None, disks=None, one_template=None, group=None, permissions=None, vm_id=None, state=None):
# configuration
self.name = name
self.cpu = cpu
self.vcpu = vcpu
self.mem_mb = mem_mb
self.networks = networks
self.disks = disks
self.one_template = one_template
self.group = group
self.permissions = permissions
# state
self.id = vm_id
self.state = state
def __repr__(self):
return "VmInfo(name={0}, cpu={1}, vcpu={2}, mem_mb={3}, networks={4}, disks={5}, one_template={6}, group={7}, permissions={8}, id={9}, state={10})".format(self.name, self.cpu, self.vcpu, self.mem_mb, self.networks, self.disks, self.one_template, self.group, self.permissions, self.id, self.state)
def pretty_tostring(self):
return "name: {0}\n\tgroup: {1}\n\tpermissions: {2}\n\tcpu: {3}\n\tvcpu: {4}\n\tmem_mb: {5}\n\tone_template: {6}\n\tnetworks: {7}{8}\n\tdisks: {9}{10}".format(
self.name,
self.group,
self.permissions,
self.cpu,
self.vcpu,
self.mem_mb,
self.one_template,
len(self.networks),
"".join([ "\n\t\t{0}".format(name) for name in self.networks]),
len(self.disks),
"".join([ "\n\t\t{0}".format(disk.pretty_tostring()) for disk in self.disks]))
def override_config(self, params):
# logging.debug("Before override vm : {0}".format(self))
# logging.debug("Overriding vm with : {0}".format(params))
try:
self.cpu = params['cpu_percent']
logging.debug("cpu overridden to {0}".format(self.cpu))
except KeyError:
pass
try:
self.vcpu = params['vcpu_count']
logging.debug("vcpu overridden to {0}".format(self.vcpu))
except KeyError:
pass
try:
self.mem_mb = params['mem_mb']
logging.debug("mem_mb overridden to {0}".format(self.mem_mb))
except KeyError:
pass
try:
self.networks = params['networks']
logging.debug("networks overridden to {0}".format(self.networks))
except KeyError:
pass
try:
disk_overrides = params['disks']
self.disks = []
for disk_override in disk_overrides:
disk = VmDisk()
disk.override_config(disk_override)
self.disks.append(disk)
logging.debug("disks overridden to {0}".format(self.disks))
except KeyError:
pass
try:
self.one_template = params['one_template']
logging.debug("one_template overridden to {0}".format(self.one_template))
except KeyError:
pass
try:
self.group = params['group']
logging.debug("group overridden to {0}".format(self.group))
except KeyError:
pass
try:
self.permissions = params['permissions']
logging.debug("permissions overridden to {0}".format(self.permissions))
except KeyError:
pass
# logging.debug("After override vm : {0}".format(self))
def compare_config(self, target):
differences = {}
if self.group is not None and target.group is not None and self.group != target.group:
differences['group'] = [self.group, target.group]
if self.permissions is not None and target.permissions is not None and self.permissions != target.permissions:
differences['permissions'] = [self.permissions, target.permissions]
if self.cpu != target.cpu:
differences['cpu_percent'] = [self.cpu, target.cpu]
if self.vcpu != target.vcpu:
differences['vcpu_count'] = [self.vcpu, target.vcpu]
if self.mem_mb != target.mem_mb:
differences['mem_mb'] = [self.mem_mb, target.mem_mb]
if self.networks != target.networks:
differences['networks'] = [self.networks, target.networks]
if len(self.disks) != len(target.disks):
differences['disks'] = [self.disks, target.disks]
else:
for x in range(len(self.disks)):
if self.disks[x] != target.disks[x]:
differences['disks'] = [self.disks, target.disks]
break
return differences
class OpenNebula:
ENV_ONEXMLRPC="ONE_XMLRPC"
ONE_COMMANDS=["oneuser", "onevm", "onetemplate"]
@staticmethod
def command_implicit_enter(name, *args):
command = [name, *args]
logging.debug("Command with implicit 'enter' on STDIN: {0}".format(command))
try:
result = subprocess.run(command, input=b"\n", stderr=subprocess.PIPE, stdout=subprocess.PIPE)
except Exception as e:
raise Exception("Error while running command {0} (reason : {1})".format(command, e))
if result.returncode != 0:
raise Exception("Error while running command {0} (return code : {1}, stdout: {2}, stderr: {3})".format(command, result.returncode, result.stdout, result.stderr))
logging.debug("STDOUT: {0}".format(result.stdout))
return result.stdout.decode()
@staticmethod
def command(name, *args):
command = [name, *args]
logging.debug("Command: {0}".format(command))
try:
result = subprocess.run(command, stdin=subprocess.DEVNULL, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
except Exception as e:
raise Exception("Error while running command {0} (reason : {1})".format(command, e))
if result.returncode != 0:
raise Exception("Error while running command {0} (return code : {1}, stdout: {2}, stderr: {3})".format(command, result.returncode, result.stdout, result.stderr))
# logging.debug("STDOUT: {0}".format(result.stdout))
return result.stdout.decode()
@classmethod
def verify_environment(cls):
endpoint = os.environ.get(cls.ENV_ONEXMLRPC)
if endpoint is None:
raise Exception("Undefined environment variable {0}, define it with : export {0}=\"http://your_opennebula_host:2633/RPC2\"".format(cls.ENV_ONEXMLRPC))
else:
logging.info("Using {0}={1} to commicate with OpenNebula".format(cls.ENV_ONEXMLRPC, endpoint))
@classmethod
def verify_commands(cls):
for command in cls.ONE_COMMANDS:
retult = None
try:
result = subprocess.run([command, "--version"], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(command, e))
logging.debug("Command '{0}' found, returned {1}".format(command, result.returncode))
def set_user_info(self):
try:
result = self.command("oneuser", "show", "--xml")
except Exception as e:
raise Exception("Error while running command, try to log in using `oneuser login your_user_name --force` first (reason : {0})".format(e))
root = ElementTree.fromstring(result)
# logging.debug("XML: {0}".format(ElementTree.tostring(root)))
self.uid = int(root.find("ID").text)
self.gid = int(root.find("GID").text)
logging.info("User has a valid authorization token (uid={0} gid={0})".format(self.uid, self.gid))
def vm_set_group(self, vm_info, group):
logging.debug("Setting group {0} for vm : {1}".format(group, vm_info))
try:
result = self.command("onevm", "chgrp", str(vm_info.id), group)
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(e))
def vm_set_permissions(self, vm_info, permissions):
logging.debug("Setting permissions {0} for vm : {1}".format(permissions, vm_info))
try:
result = self.command("onevm", "chmod", str(vm_info.id), permissions)
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(e))
def vm_list(self):
vms = {}
try:
result = self.command("onevm", "list", "--xml")
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(e))
root = ElementTree.fromstring(result)
# logging.debug("XML: {0}".format(ElementTree.tostring(root)))
for vm_elem in root.findall("VM"):
vm = VmInfo.from_one_xml(vm_elem)
vms[vm.name] = vm
# logging.debug("VM list: {0}".format(vms))
return vms
def vm_create(self, vm_info):
logging.debug("Creating vm: {0}".format(vm_info))
args = ["--name", vm_info.name,
"--hold", # in case one_template uses PXE implicitely
"--cpu", str(vm_info.cpu),
"--vcpu", str(vm_info.vcpu),
"--memory", "{0}m".format(vm_info.mem_mb)]
if len(vm_info.networks) > 0:
args.append("--nic")
args.append(",".join(vm_info.networks))
if len(vm_info.disks) > 0:
args.append("--disk")
args.append(",".join([ x.to_arg() for x in vm_info.disks]))
try:
if vm_info.one_template is None:
result = self.command("onevm", "create", *args)
else:
logging.warning("Creation of VM {0} from a template might require a prompt. In that case, this tool chooses the default entry (ie simulates 'enter')")
result = self.command_implicit_enter("onetemplate", "instantiate", *args, vm_info.one_template)
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(e))
# store vm id number
if vm_info.one_template is None:
# onevm output
r = r'^ID: (\d+)$'
else:
# onetemplate output
r = r'VM ID: (\d+)\n'
m = re.search(r, result)
if not m:
raise Exception("Could not detect VM id after creation")
vm_info.id = int(m.group(1))
# set group
if vm_info.group is not None:
self.vm_set_group(vm_info, vm_info.group)
# permissions
if vm_info.permissions is not None:
self.vm_set_permissions(vm_info, vm_info.permissions)
def vm_destroy(self, vm_info):
logging.debug("Destroying vm: {0}".format(vm_info))
try:
result = self.command("onevm", "terminate", str(vm_info.id))
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(e))
def vm_resize(self, vm_info, cpu_percent=None, vcpu_count=None, mem_mb=None):
logging.debug("Resizing vm : {0}".format(vm_info))
# setup args
args = []
if cpu_percent is not None:
args.append("--cpu")
args.append(str(cpu_percent))
if vcpu_count is not None:
args.append("--vcpu")
args.append(str(vcpu_count))
if mem_mb is not None:
args.append("--memory")
args.append(str(mem_mb))
# skip early if noop
if len(args) == 0:
logging.info("No difference in vcpu/cpu/mem detected, not resizing VM {0}".format(vm_info.id))
return
# enforce state requirements, see https://docs.opennebula.org/5.4/operation/references/vm_states.html
if vm_info.state not in [2, 4, 5, 8, 9]:
raise Exception("VM {0} is in a state ({1}) where its envelope cannot be modified".format(vm_info.id, vm_info.state))
# actual resize operation
try:
result = self.command("onevm", "resize", *args, str(vm_info.id))
except Exception as e:
raise Exception("Error while running command (reason : {0})".format(e))
logging.info("Resizing VM {0} done".format(vm_info.id))
def vm_synchronize(self, vm_info, differences):
logging.debug("Synchronizing vm : {0}".format(vm_info))
# group
try:
group = differences['group']
except KeyError:
group = None
if group is not None:
group = group[1]
if group is not None:
self.vm_set_group(vm_info, group)
# permissions
try:
permissions = differences['permissions']
except KeyError:
permissions = None
if permissions is not None:
permissions = permissions[1]
if permissions is not None:
self.vm_set_permissions(vm_info, permissions)
# resize
cpu_percent = vcpu_count = mem_mb = None
try:
cpu_percent = differences['cpu_percent']
except KeyError:
cpu_percent = None
if cpu_percent is not None:
cpu_percent = cpu_percent[1]
try:
vcpu_count = differences['vcpu_count']
except KeyError:
vcpu_count = None
if vcpu_count is not None:
vcpu_count = vcpu_count[1]
try:
mem_mb = differences['mem_mb']
except KeyError:
mem_mb = None
if mem_mb is not None:
mem_mb = mem_mb[1]
self.vm_resize(vm_info, cpu_percent, vcpu_count, mem_mb)
# updating VM definition following resize
if cpu_percent is not None:
vm_info.cpu = cpu_percent
if vcpu_count is not None:
vm_info.vcpu = vcpu_count
if mem_mb is not None:
vm_info.mem_mb = mem_mb
logging.debug("VM infos post resize {0}".format(vm_info))
# disks
try:
disks = differences['disks']
except KeyError:
disks = None
if disks is not None:
logging.warning("Changing disk topology could lead to data loss, so this function is not implemented and modifications should be done manually")
# networks
try:
networks = differences['networks']
except KeyError:
networks = None
if networks is not None:
logging.warning("Changing network topology could break the network configuration of the guest (lose mac/ip leases, change interface names) so this function is not implemented and modifications should be done by hand")
def __init__(self):
pass
class App:
def __init__(self, args):
self.args = args
self.setup_logging()
self.target = {}
self.existing = {}
self.one = OpenNebula()
def setup_logging(self):
# root logger
numeric_level = getattr(logging, self.args.log_level.upper())
root_logger = logging.getLogger()
root_logger.setLevel(numeric_level)
# format
log_format = "%(message)s"
if numeric_level == logging.DEBUG:
log_format = " ".join([
"thread=%(threadName)s",
"module=%(module)s",
"func=%(funcName)s",
"line=%(lineno)d",
": {0}"]).format(log_format)
# logging output
handler = logging.StreamHandler()
log_format = "%(asctime)s %(levelname)s {0}".format(log_format)
# finalize
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
root_logger.addHandler(handler)
logging.debug("Command line arguments: {0}".format(self.args))
def apply_class_recursive(self, jdata, vm, current_definition):
try:
vm_class = current_definition['class']
except KeyError:
vm_class = None
# depth-first aplpication
if vm_class is not None:
self.apply_class_recursive(jdata, vm, jdata['classes'][vm_class])
# apply provided overrides
vm.override_config(current_definition)
logging.debug("VM after class override {0}".format(vm))
def load_v4(self, jdata):
defs = {}
self.platform_name = jdata['platform_name'].strip()
if len(self.platform_name) == 0:
raise Exception("Platform name cannot be empty, because every accessible OpenNebula VM would be considered part of the platform !")
for vm_name, vm_host_def in jdata['hosts'].items():
logging.debug("VM {0} definition {1}".format(vm_name, vm_host_def))
# initialize vm data
vm = VmInfo()
vm.name = "{0}-{1}".format(self.platform_name, vm_name)
# load default configuration
try:
defaults = jdata['defaults']
except KeyError:
defaults = None
if defaults is not None:
vm.override_config(defaults)
logging.debug("VM after defaults {0}".format(vm))
# apply overrides recursively by levels (hosts)
self.apply_class_recursive(jdata, vm, vm_host_def)
logging.debug("VM final configuration {0}".format(vm))
# store final
defs[vm.name] = vm
logging.debug("VM definitions: {0}".format(defs))
return defs
def load(self, jsonfile):
with open(jsonfile) as fileobj:
j = json.load(fileobj)
if int(j['format_version']) == 4:
return self.load_v4(j)
raise Exception("Unhandled format {0}".format(j['format_version']))
def create(self, vm_name):
logging.info("VM {0} does not exist, creating it".format(vm_name))
vm = self.target[vm_name]
self.one.vm_create(vm)
logging.debug("Created VM with ID {0}".format(vm.id))
print("{0}: created ID {1}".format(vm.name, vm.id))
def synchronize(self, vm_name):
logging.info("Synchronizing VM {0}".format(vm_name))
current = self.existing[vm_name]
target = self.target[vm_name]
if current.name != target.name:
raise Exception("Both VM do not refer to the same host")
differences = current.compare_config(target)
if len(differences) > 0:
delta = ", ".join([
"changing {0} from {1} to {2}".format(key, change[0], change[1])
for key, change in differences.items()
])
print("{0}: ID {1}, {2}".format(vm_name, current.id, delta))
self.one.vm_synchronize(current, differences)
def destroy(self, vm_name):
logging.info("Destroying unreferenced VM {0}".format(vm_name))
vm = self.existing[vm_name]
self.one.vm_destroy(vm)
logging.debug("Destroyed VM with ID {0}".format(vm.id))
print("{0}: destroyed ID {1}".format(vm.name, vm.id))
def list(self, platform_name):
vms = self.one.vm_list()
# ignoring VM without our prefix
vms = {
key:value for key, value in vms.items()
if value.name.startswith("{0}-".format(platform_name))
}
logging.debug("Filtered VM {0}".format(vms))
logging.info("Existing managed VM : {0}".format(", ".join(vms.keys()) if len(vms) > 0 else "None"))
return vms
def run_all(self):
# parse data file
for json_file in self.args.jsonfile:
logging.info("Processing definition file: {0}".format(json_file))
self.target = self.load(json_file)
self.run()
def run(self):
# handle parse-only
if self.args.action == "parse-only":
for key in sorted(self.target):
print(self.target[key].pretty_tostring())
return
# get existing vm FOR OUR PLATFORM
OpenNebula.verify_environment()
OpenNebula.verify_commands()
self.one.set_user_info()
self.existing = self.list(self.platform_name)
# compute sets for actions
current = set(self.existing.keys())
target = set(self.target.keys())
missing = target.difference(current)
present = target.intersection(current)
unreferenced = current.difference(target)
if self.args.action == "status":
for vm_name in sorted(missing):
print("{0}: missing".format(self.target[vm_name].name))
for vm_name in sorted(present):
print("{0}: present ID {1}".format(self.existing[vm_name].name, self.existing[vm_name].id))
for vm_name in sorted(unreferenced):
print("{0}: unreferenced ID {1}".format(self.existing[vm_name].name, self.existing[vm_name].id))
elif self.args.action == "create-missing":
# create what must be created
for vm_name in sorted(missing):
self.create(vm_name)
elif self.args.action == "synchronize":
# synchronize what could differ
for vm_name in sorted(present):
self.synchronize(vm_name)
elif self.args.action == "delete-unreferenced":
# delete what should not be there
for vm_name in sorted(unreferenced):
self.destroy(vm_name)
elif self.args.action == "delete-all":
# delete everything that exists related to our platform
for vm_name in sorted(present):
self.destroy(vm_name)
def main():
try:
parser = argparse.ArgumentParser(description="one-pf-manage")
parser.add_argument("-l", "--log-level", metavar="LVL", choices=["critical", "error", "warning", "info", "debug"], default="warning")
parser.add_argument("action", choices=["status", "create-missing", "synchronize", "delete-unreferenced", "delete-all", "parse-only"], default="status")
parser.add_argument("jsonfile", nargs='+')
args = parser.parse_args()
app = App(args)
app.run_all()
sys.exit(0)
except KeyboardInterrupt as e:
logging.warning("Caught SIGINT (Ctrl-C), exiting.")
sys.exit(1)
except SystemExit as e:
message = "Exiting with return code {0}".format(e.code)
if e.code == 0:
logging.info(message)
else:
logging.warn(message)
raise e
except Exception as e:
logging.critical("{0}: {1}".format(e.__class__.__name__, e))
# when debugging, we want the stack-trace
if args.log_level == "debug":
raise e
if __name__ == '__main__':
main()