-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmcli.py
executable file
·118 lines (105 loc) · 4.28 KB
/
vmcli.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
#!/usr/bin/python3
from libvmcli import error
from libcluster import Cluster
from libguest import Guest
from time import sleep
from docopt import docopt
import sys
import os
h = '''
Usage:
vmcli.py --cluster=<name> guest find <guest_name>
vmcli.py --cluster=<name> guest start <guest_name> [<host_name>]
vmcli.py --cluster=<name> guest start_and_show <guest_name> [<host_name>]
vmcli.py --cluster=<name> guest shutdown <guest_name>
vmcli.py --cluster=<name> guest kill <guest_name>
vmcli.py --cluster=<name> guest stop <guest_name>
vmcli.py --cluster=<name> guest cont <guest_name>
vmcli.py --cluster=<name> guest info <guest_name>
vmcli.py --cluster=<name> guest migrate <guest_name> <host_name>
vmcli.py --cluster=<name> guest show <guest_name>
vmcli.py --cluster=<name> host info <host_name>
vmcli.py --cluster=<name> host list_running_guests <host_name>
vmcli.py --cluster=<name> host shutdown_guests <host_name>
vmcli.py --cluster=<name> host show_guests <host_name>
vmcli.py --cluster=<name> cluster info
vmcli.py --cluster=<name> cluster show
vmcli.py --cluster=<name> cluster show_guests
vmcli.py --cluster=<name> cluster list_guests
vmcli.py --cluster=<name> cluster shutdown_guests
vmcli.py --cluster=<name> cluster poweroff
vmcli.py [-h | --help]
vmcli.py [--verions]
Options:
-h --help Show this screen.
--version Show version.
--cluster=name Choose the cluster to use
--conf=path Change path of the main configuration directory [default: /etc/vmcli]
'''
if __name__ == '__main__':
arg = docopt(h, version='0.9.0')
if os.path.isdir('conf'):
conf_path = 'conf'
elif os.path.isdir('~/.vmcli'):
conf_path = '~/.vmcli'
elif os.path.isdir('/etc/vmcli'):
conf_path = '/etc/vmcli'
else:
error('No configuration available')
c = Cluster(conf_path, arg['--cluster'])
if arg['guest']:
if arg['find']:
print(c.guest_find(arg['<guest_name>']))
elif arg['start'] or arg['start_and_show']:
if arg['<host_name>']:
host_name = arg['<host_name>']
else:
host_name = 'choose'
c.start_guest(arg['<guest_name>'], host_name)
if arg['start_and_show']:
host_name = c.guest_find(arg['<guest_name>'])
c.hosts[host_name].guests[arg['<guest_name>']].show()
elif arg['shutdown']:
host_name = c.guest_find(arg['<guest_name>'])
c.hosts[host_name].guests[arg['<guest_name>']].shutdown()
elif arg['kill']:
host_name = c.guest_find(arg['<guest_name>'])
c.hosts[host_name].guests[arg['<guest_name>']].kill()
elif arg['stop']:
host_name = c.guest_find(arg['<guest_name>'])
c.hosts[host_name].guests[arg['<guest_name>']].stop()
elif arg['cont']:
host_name = c.guest_find(arg['<guest_name>'])
c.hosts[host_name].guests[arg['<guest_name>']].cont()
elif arg['migrate']:
c.migrate_guest(arg['<guest_name>'], arg['<host_name>'])
elif arg['info']:
host_name = c.guest_find(arg['<guest_name>'])
print(c.hosts[host_name].guests[arg['<guest_name>']].info())
elif arg['show']:
host_name = c.guest_find(arg['<guest_name>'])
print('Guest not found') if host_name is None else c.hosts[host_name].guests[arg['<guest_name>']].show()
elif arg['host']:
if arg['<host_name>'] not in c.hosts:
error('Host not valid')
if arg['info']:
print(c.hosts[arg['<host_name>']].info())
elif arg['list_running_guests']:
c.hosts[arg['<host_name>']].list_running_guests()
elif arg['show_guests']:
c.hosts[arg['<host_name>']].show_guests()
elif arg['shutdown_guests']:
c.hosts[arg['<host_name>']].shutdown_guests()
elif arg['cluster']:
if arg['info']:
print(c.info())
elif arg['show']:
print(c.show())
elif arg['show_guests']:
print(c.show_guests())
elif arg['list_guests']:
print(c.list_guests())
elif arg['shutdown_guests']:
c.shutdown_guests()
elif arg['poweroff']:
c.poweroff()