forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypervisor.py
executable file
·245 lines (224 loc) · 8.82 KB
/
hypervisor.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
#!/usr/bin/env python
from datetime import date
import re
from himlarcli import tests as tests
tests.is_virtual_env()
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from himlarcli.color import Color
parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)
kc = Keystone(options.config, debug=options.debug)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
nc = Nova(options.config, debug=options.debug, log=logger)
nc.set_dry_run(options.dry_run)
def action_instances():
host = nc.get_host(nc.get_fqdn(options.host))
if not host:
himutils.sys_error('Could not find valid host %s' % options.host)
search_opts = dict(all_tenants=1, host=host.hypervisor_hostname)
instances = nc.get_all_instances(search_opts=search_opts)
if options.format == 'table':
output = {}
output['header'] = [
'ID',
'NAME',
'PROJECT',
'AGE',
'STATUS',
'FLAVOR',
]
output['align'] = [
'l',
'l',
'l',
'r',
'l',
'l',
]
output['sortby'] = 2
counter = 0
for i in instances:
project = kc.get_by_id('project', i.tenant_id)
# Filter for project type
if options.type:
if hasattr(project, 'type') and project.type != options.type:
status['type'] = options.type
continue
created = himutils.get_date(i.created, None, '%Y-%m-%dT%H:%M:%SZ')
active_days = (date.today() - created).days
# status color
if i.status == 'ACTIVE':
instance_status = Color.fg.red + i.status + Color.reset
elif i.status == 'SHUTOFF':
instance_status = Color.fg.GRN + i.status + Color.reset
elif i.status == 'PAUSED':
instance_status = Color.fg.BLU + i.status + Color.reset
else:
instance_status = Color.fg.YLW + i.status + Color.reset
# project color
if project is None:
project_name = Color.fg.red + "None" + Color.reset
else:
project_name = Color.fg.cyn + project.name + Color.reset
output[counter] = [
Color.dim + i.id + Color.reset,
Color.fg.GRN + i.name + Color.reset,
project_name,
active_days,
instance_status,
Color.fg.WHT + i.flavor['original_name'] + Color.reset,
]
counter += 1
printer.output_dict(output, sort=True, one_line=False)
else:
printer.output_dict({'header': 'Instance list (id, name, status, updated)'})
status = {'total': 0}
for i in instances:
# Filter for project type
if options.type:
project = kc.get_by_id('project', i.tenant_id)
if hasattr(project, 'type') and project.type != options.type:
status['type'] = options.type
continue
output = {
'1': i.id,
'3': i.name,
'4': i.status,
#'2': i.updated,
#'6'': getattr(i, 'OS-EXT-SRV-ATTR:instance_name'),
'5': i.flavor['original_name']
}
printer.output_dict(output, sort=True, one_line=True)
status['total'] += 1
status[str(i.status).lower()] = status.get(str(i.status).lower(), 0) + 1
printer.output_dict({'header': 'Counts'})
printer.output_dict(status)
def action_show():
host = nc.get_host(hostname=nc.get_fqdn(options.host), detailed=True)
if not host:
himutils.sys_error('Could not find valid host %s' % options.host)
printer.output_dict(host.to_dict())
def action_users():
users = nc.get_users(options.aggregate, simple=True)
printer.output_dict({'header': 'User in %s' % options.aggregate,
'users': list(users)})
def action_move():
hostname = nc.get_fqdn(options.host)
if nc.add_host_to_aggregate(hostname=hostname, aggregate_name=options.aggregate, move=True):
himutils.info(f"Host {hostname} moved to aggregate {options.aggregate}")
def action_add():
hostname = nc.get_fqdn(options.host)
if nc.add_host_to_aggregate(hostname=hostname, aggregate_name=options.aggregate, move=False):
himutils.info(f"Host {hostname} added to aggregate {options.aggregate}")
def action_remove():
hostname = nc.get_fqdn(options.host)
if nc.remove_host_from_aggregate(hostname=hostname, aggregate_name=options.aggregate):
himutils.info(f"Host {hostname} removed from aggregate {options.aggregate}")
def action_enable():
host = nc.get_host(hostname=nc.get_fqdn(options.host), detailed=True)
if not host:
himutils.fatal(f"Could not find valid host {options.host}")
if host.status != 'enabled' and not options.dry_run:
nc.enable_host(host.hypervisor_hostname)
himutils.info(f"Host {host.hypervisor_hostname} enabled")
def action_disable():
host = nc.get_host(hostname=nc.get_fqdn(options.host), detailed=True)
if not host:
himutils.fatal(f"Could not find valid host {options.host}")
if host.status != 'disabled' and not options.dry_run:
nc.disable_host(host.hypervisor_hostname)
himutils.info(f"Host {host.hypervisor_hostname} disabled")
def action_list():
aggregates = nc.get_all_aggregate_hosts()
if options.aggregate == 'all':
hosts = nc.get_hosts()
else:
hosts = nc.get_aggregate_hosts(options.aggregate, True)
if options.format == 'table':
output = {}
output['header'] = [
'NAME',
'AGGREGATES',
'VMs',
'vCPUs',
'MEMORY (GiB)',
'DISK (GB)',
'STATE',
'STATUS',
]
output['align'] = [
'l',
'l',
'r',
'r',
'r',
'r',
'l',
'l',
]
output['sortby'] = 0
counter = 0
for host in hosts:
r_hostname = Color.fg.blu + re.sub('\.mgmt\..+?\.uhdc\.no$', '', host.hypervisor_hostname) + Color.reset
r_aggregate = ','.join(aggregates.get(host.hypervisor_hostname, 'unknown'))
r_vms = str(host.running_vms)
r_vcpus = f"{host.vcpus_used} / {host.vcpus}"
r_mem = f"{int(host.memory_mb_used/1024)} / {int(host.memory_mb/1024)}"
if re.search(r"^(central1|windows1|placeholder1|hpc1)$", r_aggregate):
r_disk = "-"
else:
r_disk = f"{host.local_gb_used} / {host.local_gb}"
r_status = host.status.upper()
if host.status == 'enabled':
r_aggregate = Color.fg.ylw + r_aggregate + Color.reset
r_status = Color.fg.GRN + r_status + Color.reset
else:
r_aggregate = Color.fg.YLW + r_aggregate + Color.reset
r_vms = Color.dim + r_vms + Color.reset
r_vcpus = Color.dim + r_vcpus + Color.reset
r_mem = Color.dim + r_mem + Color.reset
r_disk = Color.dim + r_disk + Color.reset
r_status = Color.fg.red + r_status + Color.reset
if host.state == 'up':
r_state = Color.fg.GRN + host.state.upper() + Color.reset
else:
r_state = Color.fg.red + host.state.upper() + Color.reset
output[counter] = [
r_hostname,
r_aggregate,
r_vms,
r_vcpus,
r_mem,
r_disk,
r_state,
r_status,
]
counter += 1
printer.output_dict(output, sort=True, one_line=False)
else:
header = {'header': 'Hypervisor list (name, aggregates, vms, vcpu_used,' +
'ram_gb_used, local_gb_used, state, status)'}
printer.output_dict(header)
for host in hosts:
output = {
'1': host.hypervisor_hostname,
'2': ','.join(aggregates.get(host.hypervisor_hostname, 'unknown')),
'3': host.running_vms,
'4': host.vcpus_used,
'5': int(host.memory_mb_used/1024),
'6': host.local_gb_used,
'7': host.state,
'8': host.status,
}
printer.output_dict(output, sort=True, one_line=True)
# Run local function with the same name as the action
action = locals().get('action_' + options.action)
if not action:
himutils.sys_error("Function action_%s() not implemented" % options.action)
action()