This repository has been archived by the owner on Nov 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ovirt.py
executable file
·1450 lines (1382 loc) · 57.2 KB
/
ovirt.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
"""
script to create virtual machines on ovirt/rhev
"""
import optparse
import os
from prettytable import PrettyTable
import sys
import time
import ConfigParser
from ovirtsdk.api import API
from ovirtsdk.xml import params
from yaml import dump, load
__author__ = "Karim Boumedhel"
__credits__ = ["Karim Boumedhel"]
__license__ = "GPL"
__version__ = "1.2.13"
__maintainer__ = "Karim Boumedhel"
__email__ = "[email protected]"
__status__ = "Production"
ERR_NOOVIRTFILE = "You need to create a correct ovirt.ini file in your home\
directory.Check documentation"
ERR_CLIENTNOTFOUND = "Client not found"
ERR_CLIENTNOCONF = "Client not found in conf file"
ERR_CLIENTNOPROFILE = "Missing client file in your home directory.\
Check documentation"
usage = "script to interact with ovirt/rhev"
version = "1.2.13"
parser = optparse.OptionParser(
"Usage: %prog [options] vmname", version=version)
creationgroup = optparse.OptionGroup(parser, "Creation options")
creationgroup.add_option("-b", "--bad", dest="bad", action="store_true", help="If set,treat all actions as not for linux guest,meaning net interfaces will be of type e1000 and disk of type ide.Necessary for windows or solaris guests")
creationgroup.add_option("-c", "--cpu", dest="numcpu", type="int", help="Specify Number of CPUS")
creationgroup.add_option("-d", "--disk", dest="disksize2", metavar="DISKSIZE", type="int", help="Specify Disk size,in Go at VM creation")
creationgroup.add_option("-e", "--extra", dest="extra",
type="string", help="Extra parameters to add to cmdline")
creationgroup.add_option("-f", "--diskformat", dest="diskformat",
type="string", help="Specify Disk mode.Can be raw or cow")
creationgroup.add_option("-m", "--memory", dest="memory2", metavar="MEMORY", type="int",
help="Specify Memory, in Mo to override when creating or to set (when vm is down)")
creationgroup.add_option("-n", "--new", dest="new",
action="store_true", help="Create new VM")
creationgroup.add_option("-p", "--profile", dest="profile",
type="string", help="specify Profile")
creationgroup.add_option('-t', '--thin', dest="thin",
action="store_true", help="Use thin provisioning for disk")
creationgroup.add_option("-D", "--storagedomain", dest="storagedomain",
type="string", help="Specify Storage Domain")
creationgroup.add_option("-G", "--cluster", dest="clu",
metavar="CLUSTER", type="string", help="Specify Cluster")
creationgroup.add_option("-N", "--numinterfaces", dest="numinterfaces",
type="int", help="Specify number of net interfaces")
creationgroup.add_option("-Y", "--nolaunch", dest="nolaunch",
action="store_true", help="Dont Launch VM,just create it")
creationgroup.add_option('--mac1', dest="mac1", type="string",
help="Specify mac to assign to first interface of vm when creating it or deploying from template.if a number is provided,only last octet of the mac will be set")
creationgroup.add_option("--mac2", dest="mac2", type="string",
help="Specify mac to assign to second interface of vm when creating it or deploying from template.")
parser.add_option_group(creationgroup)
actiongroup = optparse.OptionGroup(parser, "Action options")
actiongroup.add_option("-a", "--adddisk", dest="adddisk", metavar="DISKSIZE",
type="int", help="Specify Disk size,in Go to add")
actiongroup.add_option("--deldisk", dest="deldisk", metavar="DISKNAME",
type="string", help="Specify Disk name to delete")
actiongroup.add_option("-g", "--guestid", dest="guestid",
type="string", help="Change guestid of VM")
actiongroup.add_option("--iso", dest="iso", type="string",
help="Specify iso to add to VM")
actiongroup.add_option("-j", "--migrate", dest="migrate",
action="store_true", help="Migrate VM")
actiongroup.add_option("-k", "--host", dest="host",
type="string", help="Host to use when migrating a VM")
actiongroup.add_option("--net", dest="net", type="string",
help="Change eth0 net of vm to specify one")
actiongroup.add_option("-o", "--console", dest="console",
action="store_true", help="Get a console")
actiongroup.add_option("-P", "--preferred", dest="preferred",
type="string", help="Set preferred host")
actiongroup.add_option("-q", "--quit", dest="isoquit",
action="store_true", help="Remove iso from VM")
actiongroup.add_option("-r", "--reboot", dest="reboot",
action="store_true", help="Restart vm")
actiongroup.add_option("-s", "--start", dest="start",
action="store_true", help="Start VM")
actiongroup.add_option("--tags", dest="tags",
type="string", help="Add tags to VM")
actiongroup.add_option("-u", "--deletetag", dest="deletetag",
type="string", help="Delete tag from VM")
actiongroup.add_option("--reset", dest="reset", action="store_true",
help="Reset kernel parameters for given VM")
actiongroup.add_option("-w", "--stop", dest="stop",
action="store_true", help="Stop VM")
actiongroup.add_option("-x", "--kernel", dest="kernel",
type="string", help="Specify kernel to boot VM")
actiongroup.add_option("-y", "--initrd", dest="initrd",
type="string", help="Specify initrd to boot VM")
actiongroup.add_option("-z", "--cmdline", dest="cmdline",
type="string", help="Specify cmdline to boot VM")
actiongroup.add_option("-B", "--boot", dest="boot", type="string",
help="Specify Boot sequence,using two values separated by colons.Values can be hd,network,cdrom.If you only provivde one options, second boot option will be set to None")
actiongroup.add_option("-K", "--kill", dest="kill", action="store_true",
help="specify VM to kill in virtual center.Confirmation will be asked unless -F/--forcekill flag is set")
actiongroup.add_option('-Q', '--forcekill', dest='forcekill',
action='store_true', help='Dont ask confirmation when killing a VM')
actiongroup.add_option('-5', '--template', dest='template',
type="string", help='Deploy VM from template')
actiongroup.add_option('-6', '--import', dest='importvm',
type='string', help='Import specified VM')
actiongroup.add_option('-7', "--runonce", dest='runonce', action="store_true",
help='Runonce VM.you will need to pass kernel,initrd and cmdline')
actiongroup.add_option('-8', '--cloudinit', dest='cloudinit', action='store_true',
help='Use Cloudinit when launching VM.you will need a cloudinit.yaml file in your current directory or a specific .yml with the name of your vm . ip1 can be overriden with -1 flag. Note hostname is ignored')
actiongroup.add_option('--dns1', dest='dns1', type='string',
help='dns server to use along with Cloudinit')
actiongroup.add_option('--filepath', dest='filepath', type='string',
help='file path to create along with Cloudinit')
actiongroup.add_option('--filecontent', dest='filecontent',
type='string', help='file content to create along with Cloudinit')
parser.add_option_group(actiongroup)
listinggroup = optparse.OptionGroup(parser, 'Listing options')
listinggroup.add_option("-i", "--info", dest="info",
action="store_true", help="get VM info")
listinggroup.add_option('-l', '--listprofiles', dest='listprofiles',
action='store_true', help='list available profiles')
listinggroup.add_option('-E', '--listexports', dest='listexports',
action='store_true', help='List machines in export domain')
listinggroup.add_option('-H', '--listhosts', dest='listhosts',
action='store_true', help='List hosts')
listinggroup.add_option('-I', '--listisos', dest='listisos',
action='store_true', help='List isos')
listinggroup.add_option('-L', '--listclients', dest="listclients",
action='store_true', help='list available clients')
listinggroup.add_option('-O', '--listtags', dest="listtags",
action='store_true', help='List available tags')
listinggroup.add_option('-T', '--listtemplates', dest='listtemplates',
action='store_true', help='list available templates,')
listinggroup.add_option('-V', '--listvms', dest='listvms', action='store_true',
help='list all vms,along with their status and their ip')
parser.add_option_group(listinggroup)
ipgroup = optparse.OptionGroup(parser, 'Ip options')
ipgroup.add_option("-1", "--ip1", dest="ip1", type="string", help="Specify First IP")
ipgroup.add_option("-2", "--ip2", dest="ip2", type="string", help="Specify Second IP")
ipgroup.add_option("-3", "--ip3", dest="ip3", type="string", help="Specify Third IP")
ipgroup.add_option("-4", "--ip4", dest="ip4", type="string", help="Specify Fourth IP")
ipgroup.add_option("-J", "--dns", dest="dns", type="string", help="Dns domain")
parser.add_option_group(ipgroup)
parser.add_option('-v', '--debug', dest='debug',
default=False, action='store_true', help='Debug')
parser.add_option('--rootpw', dest='rootpw', type='string',
help='Root password when using cloud-init')
parser.add_option("-A", "--activate", dest="activate",
type="string", help="Activate specified storageDomain")
parser.add_option("-C", "--client", dest="client",
type="string", help="Specify Client")
parser.add_option("-M", "--maintenance", dest="maintenance",
type="string", help="Put in maintenance specified storageDomain")
parser.add_option("-S", "--summary", dest="summary",
action="store_true", help="Summary of your ovirt setup")
parser.add_option("-X", "--search", dest="search",
type="string", help="Search VMS")
parser.add_option("-9", "--switchclient", dest="switchclient",
type="string", help="Switch default client")
MB = 1024 * 1024
GB = 1024 * MB
(options, args) = parser.parse_args()
staticroutes = None
backuproutes = None
gwbackup = None
clients = []
boot = options.boot
extra = options.extra
reset = options.reset
client = options.client
guestid = options.guestid
listclients = options.listclients
switchclient = options.switchclient
listisos = options.listisos
host = options.host
listexports = options.listexports
listhosts = options.listhosts
listtemplates = options.listtemplates
listvms = options.listvms
listprofiles = options.listprofiles
debug = options.debug
new = options.new
diskformat = options.diskformat
disksize2 = options.disksize2
if disksize2:
disksize2 = disksize2 * GB
ip1 = options.ip1
ip2 = options.ip2
ip3 = options.ip3
ip4 = options.ip4
dns = options.dns
dns1 = options.dns1
filepath = options.filepath
filecontent = options.filecontent
activate = options.activate
maintenance = options.maintenance
preferred = options.preferred
kernel = options.kernel
initrd = options.initrd
cmdline = options.cmdline
memory2 = options.memory2
if options.memory2:
memory2 = options.memory2 * MB
reboot = options.reboot
start = options.start
runonce = options.runonce
cloudinit = options.cloudinit
importvm = options.importvm
stop = options.stop
summary = options.summary
numcpu = options.numcpu
thin = options.thin
kill = options.kill
forcekill = options.forcekill
clu = options.clu
storagedomain = options.storagedomain
deldisk = options.deldisk
adddisk = options.adddisk
if adddisk:
adddisk = adddisk * GB
bad = options.bad
nolaunch = options.nolaunch
search = options.search
profile = options.profile
console = options.console
listtags = options.listtags
tags = options.tags
deletetag = options.deletetag
installnet = None
boot1, boot2 = "hd", "network"
numinterfaces = options.numinterfaces
iso = options.iso
isoquit = options.isoquit
migrate = options.migrate
template = options.template
mac1 = options.mac1
mac2 = options.mac2
info = options.info
macaddr = []
low = None
guestrhel332 = "rhel_3"
guestrhel364 = "rhel_3x64"
guestrhel432 = "rhel_4"
guestrhel464 = "rhel_4x64"
guestrhel532 = "rhel_5"
guestrhel564 = "rhel_5x64"
guestrhel632 = "rhel_6"
guestrhel664 = "rhel_6x64"
guestother = "other"
guestotherlinux = "other_linux"
guestwindowsxp = "windows_xp"
guestwindows7 = "windows_7"
guestwindows764 = "windows_7x64"
guestwindows2003 = "windows_2003"
guestwindows200364 = "windows_2003x64"
guestwindows2008 = "windows_2008"
guestwindows200864 = "windows_2008x64"
rootpw = options.rootpw
net = options.net
def createprofiles(client):
clientfile = "%s/%s.ini" % (os.environ['HOME'], client)
if not os.path.exists(clientfile):
print "You need to create a %s.ini in your homedirectory.Check documentation" % client
sys.exit(1)
try:
c = ConfigParser.ConfigParser()
c.read(clientfile)
profiles = {}
for prof in c.sections():
for option in c.options(prof):
if prof not in profiles.keys():
profiles[prof] = {option: c.get(prof, option)}
else:
profiles[prof][option] = c.get(prof, option)
return profiles
except:
print ERR_CLIENTNOPROFILE
def findhostbyid(api, id):
hosts = api.hosts
for h in hosts.list():
if h.get_id() == id:
return h.get_name()
def findclubyid(api, id):
clusters = api.clusters
for clu in clusters.list():
if clu.get_id() == id:
return clu.get_name()
def getip(api, id):
hosts = api.hosts
for h in hosts.list():
if h.get_id() == id:
return h.get_address()
def switchstoragedomain(api, storagedomain, activate=True):
action = False
sd = api.storagedomains.get(name=storagedomain)
if not sd:
print "Storage domain not found"
sys.exit(1)
else:
id = sd.get_id()
for ds in api.datacenters.list():
for s in ds.storagedomains.list():
if activate:
if s.get_status().get_state() != "active" and s.get_id() == id:
s.activate()
print "StorageDomain %s activated" % (storagedomain)
action = True
if not activate:
if s.get_status().get_state() == "active" and s.get_id() == id:
s.deactivate()
print "StorageDomain %s put in maintenance" % (storagedomain)
action = True
if not action:
print "No actions needed..."
def checkiso(api, iso=None):
isodomains = []
datacenters = api.datacenters.list()
for ds in datacenters:
for sd in ds.storagedomains.list():
if sd.get_type() == "iso" and sd.get_status().get_state() == "active":
isodomains.append(sd)
if len(isodomains) == 0:
print "No iso domain found.Leaving..."
sys.exit(1)
for sd in isodomains:
if not iso:
print "Isodomain: %s" % (sd.get_name())
if not iso:
print "Available isos:"
isodomainid = sd.get_id()
sdfiles = api.storagedomains.get(id=isodomainid).files
for f in sdfiles.list():
if not iso:
print f.get_id()
elif f.get_id() == iso:
return f
sys.exit(0)
ohost, oport, ouser, opassword, ossl, oca, oorg, oproxy = None, None, None, None, None, None, None, None
# thin provisioning
sparse = True
if bad:
diskinterface, netinterface = 'ide', 'e1000'
else:
diskinterface, netinterface = 'virtio', 'virtio'
if len(args) != 1 and new:
print "Usage: %prog [options] vmname"
sys.exit(1)
ovirtconffile = "%s/ovirt.ini" % (os.environ['HOME'])
# parse ovirt client auth file
if not os.path.exists(ovirtconffile):
print "Missing %s in your home directory.Check documentation" % ovirtconffile
sys.exit(1)
try:
c = ConfigParser.ConfigParser()
c.read(ovirtconffile)
ovirts = {}
default = {}
for cli in c.sections():
for option in c.options(cli):
if cli == "default":
default[option] = c.get(cli, option)
continue
if cli not in ovirts.keys():
ovirts[cli] = {option: c.get(cli, option)}
else:
ovirts[cli][option] = c.get(cli, option)
except:
print ERR_NOOVIRTFILE
os._exit(1)
if listclients:
clientstable = PrettyTable(["Name", "Current"])
clientstable.align["Name"] = "l"
for cli in sorted(ovirts):
clientsinfo = [cli]
if 'client' in default.keys() and default['client'] == cli:
clientsinfo.append('X')
else:
clientsinfo.append('')
clientstable.add_row(clientsinfo)
print clientstable
sys.exit(0)
if switchclient:
if switchclient not in ovirts.keys():
print "Client not defined...Leaving"
else:
mod = open(ovirtconffile).readlines()
f = open(ovirtconffile, "w")
for line in mod:
if line.startswith("client"):
f.write("client=%s\n" % switchclient)
else:
f.write(line)
f.close()
print "Default Client set to %s" % (switchclient)
sys.exit(0)
if not client:
try:
client = default['client']
except:
print "No client defined as default in your ini file or specified in command line"
os._exit(1)
# PARSE DEFAULT SECTION
try:
if not clu and 'clu' in default.keys():
clu = default["clu"]
if not numcpu and 'numcpu' in default.keys():
numcpu = int(default['numcpu'])
if not diskformat and 'diskformat' in default.keys():
diskformat = default['diskformat']
if 'disksize' in default.keys():
disksize = int(default['disksize']) * GB
if 'memory' in default.keys():
memory = int(default['memory']) * MB
if 'low' in default.keys():
low = float(default['low'])
if not storagedomain and 'storagedomain' in default.keys():
storagedomain = default["storagedomain"]
if not numinterfaces and 'numinterfaces' in default.keys():
numinterfaces = int(default["numinterfaces"])
if not ossl and 'ssl' in default.keys():
ossl = True
except:
print "Problem parsing default section in your ini file"
os._exit(1)
try:
ohost = ovirts[client]['host']
oport = ovirts[client]['port']
ouser = ovirts[client]['user']
opassword = ovirts[client]['password']
if 'ssl' in ovirts[client].keys():
ossl = ovirts[client]['ssl']
if 'clu' in ovirts[client].keys():
clu = ovirts[client]['clu']
if not numcpu and 'numcpu' in ovirts[client].keys():
numcpu = int(ovirts[client]['numcpu'])
if 'diskformat' in ovirts[client].keys():
diskformat = ovirts[client]['diskformat']
if 'diskinferface' in ovirts[client].keys():
diskinterface = ovirts[client]['diskformat']
if 'disksize' in ovirts[client].keys():
disksize = int(ovirts[client]['disksize']) * GB
if 'memory' in ovirts[client].keys():
memory = int(ovirts[client]['memory']) * MB
if not storagedomain and 'storagedomain' in ovirts[client].keys():
storagedomain = ovirts[client]['storagedomain']
if not low and 'low' in ovirts[client].keys():
storagedomain = float(ovirts[client]['low'])
if 'numinterfaces' in ovirts[client].keys():
numinterfaces = int(ovirts[client]['numinterfaces'])
if 'netinterface' in ovirts[client].keys():
diskinterface = ovirts[client]['netinterface']
if 'ssl' in ovirts[client].keys():
ossl = True
if 'ca' in ovirts[client].keys():
oca = ovirts[client]['ca']
if 'org' in ovirts[client].keys():
oorg = ovirts[client]['org']
if 'proxy' in ovirts[client].keys():
oproxy = ovirts[client]['proxy']
except KeyError, e:
print "Problem parsing ovirt ini file:Missing parameter %s" % e
os._exit(1)
if ossl:
url = "https://%s:%s/ovirt-engine/api" % (ohost, oport)
else:
url = "http://%s:%s/ovirt-engine/api" % (ohost, oport)
api = API(url=url, username=ouser, password=opassword,
insecure=True, debug=debug)
if listvms:
vms = PrettyTable(["Name", "Status", "Fqdn", "Ips"])
vms.align["Name"] = "l"
for vm in api.vms.list():
name, status = vm.get_name(), vm.status.state
guestinfo = vm.get_guest_info()
fqdn = guestinfo.get_fqdn() if guestinfo is not None else ''
ips = ''
if guestinfo is not None and guestinfo.get_ips() is not None:
for element in guestinfo.get_ips().get_ip():
ips = "%s %s" % (ips, element.get_address())
vms.add_row([name, status, fqdn, ips])
print vms
sys.exit(0)
if listtemplates:
templates = PrettyTable(["Name", "Description"])
templates.align["Name"] = "l"
for t in api.templates.list():
if t.status.get_state() == 'ok':
templates.add_row([t.get_name(), t.get_description()])
print templates
sys.exit(0)
if listisos:
checkiso(api)
sys.exit(0)
if listtags:
tagstable = PrettyTable(["Name"])
tagstable.align["Name"] = "l"
for tag in api.tags.list():
tagstable.add_row([tag.get_name()])
print tagstable
sys.exit(0)
if activate:
switchstoragedomain(api, activate)
sys.exit(0)
if maintenance:
switchstoragedomain(api, maintenance, False)
sys.exit(0)
# LIST HOSTS
if listhosts:
hoststable = PrettyTable(["Name", "Cluster", "Ip", "Vms"])
hoststable.align["Name"] = "l"
# create a dict hostid->vms
hosts = {}
for vm in api.vms.list():
if vm.get_host() is not None:
name, hostid = vm.get_name(), vm.get_host().get_id()
if hostid in hosts.keys():
hosts[hostid].append(name)
else:
hosts[hostid] = [name]
for h in api.hosts.list():
hostinfo = [h.get_name(), findclubyid(
api, h.get_cluster().get_id()), h.get_address()]
hostid = h.get_id()
if hostid in hosts.keys():
hostinfo.append(",".join(hosts[hostid]))
else:
hostinfo.append(None)
hoststable.add_row(hostinfo)
print hoststable
sys.exit(0)
if listexports:
exportstable = PrettyTable(["Name", "Vms", "Templates"])
exportstable.align["Name"] = "l"
for sd in api.storagedomains.list():
if sd.get_type() == "export":
exportdomain = sd.get_name()
exportinfo = [exportdomain]
vmslist = ''
for vm in api.storagedomains.get(name=exportdomain).vms.list():
if vmslist == '':
vmslist = vm.name
else:
vmslist = "%s,%s" % (vmslist, vm.name)
exportinfo.append(vmslist)
templateslist = ''
for template in api.storagedomains.get(name=exportdomain).templates.list():
if templateslist == '':
templateslist = template.name
else:
templateslist = "%s,%s" % (templateslist, template.name)
exportinfo.append(templateslist)
exportstable.add_row(exportinfo)
print exportstable
sys.exit(0)
# SEARCH VMS
if search:
vms = api.vms.list()
vmfound = False
for vm in vms:
if search.replace("*", "").upper() in vm.name.upper():
if not vmfound:
print "Vms found:"
print vm.name
vmfound = True
if not vmfound:
print "No matching vms found"
sys.exit(0)
# REPORT
if summary:
nodcs = []
clusters = api.clusters.list()
clusters = api.clusters.list()
datacenters = api.datacenters.list()
hosts = api.hosts.list()
for ds in datacenters:
print "Datacenter: %s Type: %s Status: %s" % (ds.name, ds.storage_type, ds.get_status().get_state())
for s in ds.storagedomains.list(): # stor.get_status().get_state()
if s.get_status().get_state() == "active":
used = s.get_used() / 1024 / 1024 / 1024
available = s.get_available() / 1024 / 1024 / 1024
print "Storage: %s Id: %s Type: %s Status: %s Total space: %sGb Available space:%sGb" % (s.name, s.get_id(), s.get_type(), s.get_status().get_state(), used + available, available)
else:
print "Storage: %s Id: %s Type: %s Status: %s Total space: N/A Available space:N/A" % (s.name, s.get_id(), s.get_type(), s.get_status().get_state())
for clu in clusters:
if not clu.get_data_center():
if clu not in nodcs:
nodcs.append(clu)
continue
cludc = api.datacenters.get(
id=clu.get_data_center().get_id()).get_name()
if cludc != ds.get_name():
continue
print "Cluster: %s Id: %s" % (clu.name, clu.id)
for net in clu.networks.list():
vlan = net.get_vlan()
if vlan:
vlanid = vlan.get_id()
else:
vlanid = ''
if net.get_display():
print "Network: %s (Set as display network) Id: %s Vlan: %s" % (net.name, net.id, vlanid)
else:
print "Network: %s Id: %s Vlan: %s" % (net.name, net.id, vlanid)
for h in hosts:
spm = h.get_storage_manager().get_valueOf_()
if spm == "true":
spm = "SPM"
else:
spm = ""
cluh = api.clusters.get(id=h.get_cluster().get_id()).get_name()
if cluh == clu.name:
print "Host: %s Cpu: %s %s" % (h.name, h.cpu.name, spm)
# print h.get_memory()
print "\n"
# handles clusters with no associated DC
if len(nodcs) > 0:
print "Datacenter: N/A"
for clu in nodcs:
print "Cluster: %s" % (clu.name)
for net in clu.networks.list():
if net.get_display():
print "Network: %s (Set as display network)" % net.name
else:
print "Network: %s " % net.name
for h in hosts:
cluh = api.clusters.get(id=h.get_cluster().get_id()).get_name()
if cluh == clu.name:
print "Host: %s Cpu: %s" % (h.name, h.cpu.name)
sys.exit(0)
if importvm:
vmfound = False
templatefound = False
for sd in api.storagedomains.list():
if vmfound is True:
break
if sd.get_type() == "export":
exportdomain = sd.get_name()
for vm in api.storagedomains.get(name=exportdomain).vms.list():
if vm.name == importvm:
exportdomainname = exportdomain
exportdomain = sd
vmfound = True
break
for vm in api.storagedomains.get(name=exportdomain).templates.list():
if vm.name == importvm:
exportdomainname = exportdomain
exportdomain = sd
templatefound = True
break
if not vmfound and not templatefound:
print "No matching vm found.Leaving..."
sys.exit(1)
print "vm %s imported from storagedomain %s to cluster %s and storagedomain %s" % (importvm, exportdomainname, clu, storagedomain)
if vmfound:
exportdomain.vms.get(importvm).import_vm(params.Action(
storage_domain=api.storagedomains.get(storagedomain), cluster=api.clusters.get(name=clu)))
if templatefound:
exportdomain.templates.get(importvm).import_template(params.Action(
storage_domain=api.storagedomains.get(storagedomain), cluster=api.clusters.get(name=clu)))
sys.exit(0)
if template:
if len(args) != 1:
print "Usage:ovirt.py -5 template name"
sys.exit(0)
temp = api.templates.get(name=template)
if not temp:
print "Template %s not found..." % (template)
print "Existing templates:"
templates = api.templates.list()
for temp in templates:
if temp.get_name() != "Blank":
print "%s" % (temp.get_name())
sys.exit(0)
else:
name = args[0]
clu = temp.get_cluster()
api.vms.add(params.VM(name=name, cluster=clu, template=temp))
for disk in api.vms.get(name=name).disks.list():
diskid = disk.get_id()
while api.disks.get(id=diskid).get_status().get_state() != "ok":
print "Waiting for disk to be available..."
time.sleep(5)
print "VM %s deployed from %s" % (name, template)
if mac1:
while api.vms.get(name).status.state != "down":
print "Waiting for VM to be down to upgrade mac..."
time.sleep(5)
for nic in api.vms.get(name).nics.list():
if ':' not in mac1:
mac1 = "%s%s" % (nic.mac.address[:-2], mac1)
nic.mac.address = mac1
nic.update()
print "Mac updated"
break
if not cloudinit:
sys.exit(0)
if len(args) == 1 and not new:
name = args[0]
vm = api.vms.get(name=name)
if not vm:
print "Vm %s not found in %s" % (name, client)
sys.exit(1)
if runonce and not new:
if api.vms.get(name).status.state == "up" or api.vms.get(name).status.state == "powering_up":
print "VM %s already started" % (vm.name)
else:
action = params.Action()
if kernel and initrd and cmdline:
action.vm = params.VM(os=params.OperatingSystem(
kernel=kernel, initrd=initrd, cmdline=cmdline))
if cloudinit:
host_name = name
if os.path.exists("%s.yml" % name):
cloudinitfile = "%s.yml" % name
elif os.path.exists('cloudinit.yml'):
cloudinitfile = 'cloudinit.yml'
else:
print "Missing cloudinit file %s.yml and default cloudinit.yml. using default values" % name
cloudinitfile = None
if cloudinitfile is not None:
with open(cloudinitfile, 'r') as f:
info = load(f)
if 'ip1' in info.keys() and ip1 is None:
ip1 = info['ip']
subnet1 = info[
'netmask'] if 'netmask' in info.keys() else None
gateway = info[
'gateway'] if 'gateway' in info.keys() else None
authorized_ssh_keys = '\n'.join(
info['ssh_authorized_keys']) if 'ssh_authorized_keys' in info.keys() else None
domain = info[
'domain'] if 'domain' in info.keys() else None
dns1 = ','.join(
info['dns']) if 'dns' in info.keys() else None
root_password = info[
'password'] if 'password' in info.keys() else None
custom_script = "runcmd:\n%s" % dump(
info['runcmd'], default_flow_style=False) if 'runcmd' in info.keys() else None
if domain:
host_name = "%s.%s" % (name, domain)
if ip1 and subnet1 and gateway:
ip = params.IP(address=ip1, netmask=subnet1, gateway=gateway)
nic = params.GuestNicConfiguration(
name='eth0', boot_protocol='STATIC', ip=ip, on_boot=True)
else:
nic = params.GuestNicConfiguration(
name='eth0', boot_protocol='DHCP', on_boot=True)
nic_configurations = params.GuestNicsConfiguration(
nic_configuration=[nic])
initialization = params.Initialization(regenerate_ssh_keys=True, host_name=host_name, domain=domain, user_name='root', root_password=root_password, nic_configurations=nic_configurations, dns_servers=dns1, authorized_ssh_keys=authorized_ssh_keys, custom_script=custom_script)
else:
initialization = params.Initialization(regenerate_ssh_keys=True, host_name=host_name)
action.vm = params.VM(initialization=initialization)
action.use_cloud_init = True
elif iso:
iso = checkiso(api, iso)
boot1 = params.Boot(dev="cdrom")
boot2 = params.Boot(dev="hd")
cdrom = params.CdRom(file=iso)
vm.cdroms.add(cdrom)
vm.update()
action.vm = params.VM(
os=params.OperatingSystem(boot=[boot1, boot2]))
elif boot:
boot = boot.split(",")
boot1 = boot[0]
if len(boot) != 2 or boot[1] == 'None':
boot2 = None
else:
boot2 = boot[1]
if boot1 == boot2:
print "Same boot options provided"
sys.exit(1)
if boot1 not in ["hd", "cdrom", "network"] or boot2 not in ["hd", "cdrom", "network", None]:
print "incorrect boot options provided.Leaving..."
sys.exit(1)
boot1 = params.Boot(dev=boot1)
boot2 = params.Boot(dev=boot2)
action.vm = params.VM(
os=params.OperatingSystem(boot=[boot1, boot2]))
else:
print "No special options passed for runonce.Leaving..."
sys.exit(0)
while api.vms.get(name).status.state != "down":
print "Waiting for vm to be ready"
time.sleep(5)
api.vms.get(name).start(action=action)
print "VM %s started in runonce mode" % name
sys.exit(0)
if kill:
if not forcekill:
sure = raw_input("Confirm you want to destroy VM %s:(y/N)" % name)
if sure != "Y":
print "Not doing anything"
sys.exit(1)
if api.vms.get(name).status.state == "up" or api.vms.get(name).status.state == "powering_up" or api.vms.get(name).status.state == "reboot_in_progress":
api.vms.get(name).stop()
while api.vms.get(name).status.state != "down":
print "Waiting for vm to stop"
time.sleep(5)
print "VM %s stopped" % name
api.vms.get(name).delete()
print "VM %s killed" % name
sys.exit(0)
if stop:
if api.vms.get(name).status.state == "down":
print "VM already stopped"
sys.exit(0)
api.vms.get(name).stop()
print "VM %s stopped" % name
sys.exit(0)
if migrate:
if host:
host = api.hosts.get(name=host)
if host:
action = params.Action()
action.host = host
vm.migrate(action=action)
else:
print "Specified host doesn't exist.Not doing anything...."
else:
vm.migrate()
print "VM s migration launched"
if isoquit:
for cd in vm.cdroms.list():
cd.delete()
vm.update()
print "Removed iso from VM"
if iso:
isofound = False
isodomains = []
for sd in api.storagedomains.list():
if sd.get_type() == "iso":
isodomains.append(sd)
if len(isodomains) == 0:
print "No iso domain found.Leaving..."
sys.exit(1)
for sd in isodomains:
for f in sd.files.list():
if f.get_id() == iso:
isofound = True
cdparams = params.CdRom(file=f)
if api.vms.get(name).status.state == "up" or api.vms.get(name).status.state == "powering_up":
cdrom = vm.cdroms.get(
id="00000000-0000-0000-0000-000000000000")
isofile = params.File(id=iso)
cdrom.set_file(isofile)
cdrom.update(current=True)
else:
vm.cdroms.add(cdparams)
vm.update()
print "Added iso %s from Isodomain %s" % (iso, sd.get_name())
if not isofound:
print "Iso not available.Leaving..."
sys.exit(1)
if boot:
boot = boot.split(",")
boot1 = boot[0]
if len(boot) != 2 or boot[1] == 'None':
boot2 = None
else:
boot2 = boot[1]
if boot1 == boot2:
print "Same boot options provided"
sys.exit(1)
if boot1 not in ["hd", "cdrom", "network"] or boot2 not in ["hd", "cdrom", "network", None]:
print "incorrect boot options provided.Leaving..."
sys.exit(1)
boot1 = params.Boot(dev=boot1)
boot2 = params.Boot(dev=boot2)
vm.os.boot = [boot1, boot2]
print "boot options correctly changed for %s" % (name)
vm.update()
if reset:
vm.os.kernel, vm.os.initrd, vm.os.cmdline = "", "", ""
vm.update()
print "kernel options resetted for %s" % (name)
if kernel:
vm.os.kernel = kernel
vm.update()
print "kernel correctly changed for %s" % (name)
if initrd:
vm.os.initrd = initrd
vm.update()
print "initrd correctly changed for %s" % (name)
if cmdline:
vm.os.cmdline = cmdline
vm.update()
print "cmdline correctly changed for %s" % (name)
if cmdline and extra:
vm.os.cmdline = "%s %s" % (vm.os.cmdline, extra)
vm.update()
print "extra cmdline correctly changed for %s" % (name)
if tags:
tags = tags.split(",")
for tag in tags:
tagfound = False
for tg in api.tags.list():
if tg.get_name() == tag:
tagfound = True
vm.tags.add(tg)
vm.update()
print "Tag %s added to %s" % (tag, name)
sys.exit(0)
if not tagfound:
print "Tag not available..."
sure = raw_input(
"Do you want me to create tag %s and add it to vm %s:(y/N)" % (tag, name))
if sure != "Y":
print "Not doing anything"
sys.exit(1)
tag = params.Tag(name=tag)
api.tags.add(tag)
print "Tag %s added..." % (tag.get_name())
vm.tags.add(tag)
vm.update()
print "Tag %s added to %s" % (tag, name)
if deletetag:
tags = vm.tags.list()
for tag in tags:
if tag.get_name() == deletetag:
tag.delete()
print "Tag %s removed from %s" % (deletetag, name)
if guestid:
vm.os.type_ = guestid
vm.update()
print "Guestid set to %s for %s" % (guestid, name)
sys.exit(0)
if preferred:
host = api.hosts.get(name=preferred)
if not host:
print "Host %s not found.Not doing anything...." % preferred
sys.exit(0)
placement_policy = params.VmPlacementPolicy(host=host)
vm.placement_policy = placement_policy
vm.update()
if memory2:
vm.memory = memory2
vm.update()
print "Memory updated to %d" % memory2
if deldisk:
api.vms.get(name).disks.get(name=deldisk).delete()
print "Disk %s deleted from %s" % (deldisk, vm.name)
if adddisk:
if not storagedomain:
print "No Storage Domain specified"
sys.exit(1)
if diskformat == "raw":
sparse = False
storagedomain = api.storagedomains.get(name=storagedomain)