forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypervisor.py
More file actions
executable file
·349 lines (321 loc) · 13.5 KB
/
hypervisor.py
File metadata and controls
executable file
·349 lines (321 loc) · 13.5 KB
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
#!/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
from himlarcli.placement import Placement
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)
#---------------------------------------------------------------------
# Main functions
#---------------------------------------------------------------------
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(detailed=False)
else:
hosts = nc.get_aggregate_hosts(aggregate=options.aggregate, detailed=False)
if options.format == 'table':
output = {}
if options.verbose:
output['header'] = [
'NAME',
'AGGREGATES',
'VMs',
'VM Status',
'vCPUs',
'MEMORY (GiB)',
'DISK (GB)',
'STATE',
'STATUS',
]
output['align'] = [
'l',
'l',
'r',
'r',
'r',
'r',
'r',
'l',
'l',
]
else:
output['header'] = [
'NAME',
'AGGREGATES',
'STATE',
'STATUS',
]
output['align'] = [
'l',
'l',
'l',
'l',
]
output['sortby'] = 0
counter = 0
region = kc.get_config('openstack', 'region')
for host in hosts:
r_hostname = Color.fg.blu + re.sub(r'\.mgmt\..+?\.uhdc\.no$', '', host.hypervisor_hostname) + Color.reset
r_aggregate = ','.join(aggregates.get(host.hypervisor_hostname, []))
r_status = host.status.upper()
if options.verbose:
resource_usage = get_resource_usage(host.id, region)
r_vcpus = f"{resource_usage['vcpu_used']} / {resource_usage['vcpu_max']} ({resource_usage['vcpu_allocation_ratio']})"
r_mem = f"{resource_usage['memory_gb_used']} / {resource_usage['memory_gb_max']} ({resource_usage['memory_allocation_ratio']})"
search_opts = dict(all_tenants=1, host=host.hypervisor_hostname)
instances = nc.get_all_instances(search_opts=search_opts)
r_vms = str(len(instances))
r_vm_states = ''
count = {
'active': 0,
'shutoff': 0,
'paused': 0,
'error': 0,
'other': 0,
}
for i in instances:
if i.status == 'ACTIVE':
count['active'] += 1
elif i.status == 'SHUTOFF':
count['shutoff'] += 1
elif i.status == 'PAUSED':
count['paused'] += 1
elif i.status == 'ERROR':
count['error'] += 1
else:
count['other'] += 1
if count['active'] > 0:
r_vm_states += f"{Color.fg.grn}{count['active']}{Color.reset} "
else:
r_vm_states += f"{Color.dim}-{Color.reset} "
if count['shutoff'] > 0:
r_vm_states += f"{Color.fg.RED}{count['shutoff']:2d}{Color.reset} "
else:
r_vm_states += f"{Color.dim} -{Color.reset} "
if count['paused'] > 0:
r_vm_states += f"{Color.fg.blu}{count['paused']}{Color.reset} "
else:
r_vm_states += f"{Color.dim}-{Color.reset} "
if count['error'] > 0:
r_vm_states += f"{Color.bg.red}{Color.fg.wht}{Color.bold}{count['error']}{Color.reset} "
else:
r_vm_states += f"{Color.dim}-{Color.reset} "
if count['other'] > 0:
r_vm_states += f"{Color.fg.ylw}{count['other']}{Color.reset}"
else:
r_vm_states += f"{Color.dim}-{Color.reset}"
if re.search(r"^(central1|windows1|placeholder1|hpc1)$", r_aggregate):
r_disk = "-"
else:
r_disk = f"{resource_usage['disk_gb_used']} / {resource_usage['disk_gb_max']} ({resource_usage['disk_allocation_ratio']})"
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_status = Color.fg.red + r_status + Color.reset
if options.verbose:
r_vcpus = Color.dim + r_vcpus + Color.reset
r_mem = Color.dim + r_mem + Color.reset
r_disk = Color.dim + r_disk + 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
if options.verbose:
output[counter] = [
r_hostname,
r_aggregate,
r_vms,
r_vm_states,
r_vcpus,
r_mem,
r_disk,
r_state,
r_status,
]
else:
output[counter] = [
r_hostname,
r_aggregate,
r_state,
r_status,
]
counter += 1
printer.output_dict(output, sort=True, one_line=False)
if options.verbose:
print("-----")
print(f"VM Status: {Color.fg.grn}ACTIVE{Color.reset} ",
f"{Color.fg.RED}SHUTOFF{Color.reset} ",
f"{Color.fg.blu}PAUSED{Color.reset} ",
f"{Color.bg.red}{Color.fg.wht}{Color.bold}ERROR{Color.reset} ",
f"{Color.fg.ylw}other...{Color.reset}")
else:
header = {'header': 'Hypervisor list (name, aggregates, ' +
'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.state,
'4': host.status,
}
printer.output_dict(output, sort=True, one_line=True)
#---------------------------------------------------------------------
# Helper functions
#---------------------------------------------------------------------
def get_resource_usage(hypervisor_uuid:str, region:str):
placement = Placement(kc, region)
resource = placement.get_resource_provider_usages(hypervisor_uuid)
inventory_vcpu = placement.get_resource_provider_inventory(hypervisor_uuid, 'VCPU')
inventory_memory = placement.get_resource_provider_inventory(hypervisor_uuid, 'MEMORY_MB')
inventory_disk = placement.get_resource_provider_inventory(hypervisor_uuid, 'DISK_GB')
data = {
"vcpu_used": resource['usages']['VCPU'],
"vcpu_max": inventory_vcpu['max_unit'],
"vcpu_allocation_ratio": inventory_vcpu['allocation_ratio'],
"memory_gb_used": int(resource['usages']['MEMORY_MB'] / 1024),
"memory_gb_max": int(inventory_memory['max_unit'] / 1024),
"memory_allocation_ratio": inventory_memory['allocation_ratio'],
"disk_gb_used": resource['usages']['DISK_GB'],
"disk_gb_max": inventory_disk['max_unit'],
"disk_allocation_ratio": inventory_disk['allocation_ratio'],
}
return data
# 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()