forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.py
executable file
·245 lines (221 loc) · 8.84 KB
/
aggregate.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 himlarcli import tests as tests
tests.is_virtual_env()
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
#from himlarcli.neutron import Neutron
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from himlarcli.state import State
from himlarcli.state import Instance
from himlarcli.color import Color
parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)
kc = Keystone(options.config, debug=options.debug)
kc.set_domain(options.domain)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
# Region
if hasattr(options, 'region'):
regions = kc.find_regions(region_name=options.region)
else:
regions = kc.find_regions()
def action_list():
if options.format == 'table':
output = {}
output['header'] = [
'NAME',
'REGION',
'HOSTS',
'AVAILABILITY ZONE',
]
output['align'] = [
'l',
'l',
'r',
'l',
]
output['sortby'] = 0
counter = 0
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
aggregates = nova.get_aggregates(simple=False)
for aggr in aggregates:
output[counter] = [
Color.fg.ylw + aggr.name + Color.reset,
region,
len(aggr.hosts),
Color.fg.CYN + aggr.metadata['availability_zone'] + Color.reset,
]
counter += 1
printer.output_dict(output, one_line=True)
else:
printer.output_dict({'header': 'Host aggregate (#hosts, name, az)'})
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
aggregates = nova.get_aggregates(simple=False)
for aggr in aggregates:
output = {
'2': aggr.name,
'3': aggr.metadata['availability_zone'],
'1': len(aggr.hosts)
}
printer.output_dict(output, one_line=True)
def action_show():
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
aggregate = nova.get_aggregate(options.aggregate)
if not aggregate:
continue
printer.output_dict({'header': 'Host aggregate in {}'.format(region)})
printer.output_dict(aggregate.to_dict())
def action_orphan_instances():
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
aggregate = nova.get_aggregate(options.aggregate)
if not aggregate:
himutils.warning(f"No aggregate {options.aggregate} found in {region}")
continue
instances = nova.get_instances(options.aggregate)
printer.output_dict({'header': 'Instance list (id, host, status, flavor)'})
status = dict({'total': 0})
for i in instances:
project = kc.get_by_id('project', i.tenant_id)
if project:
continue
output = {
'1': i.id,
'2': nova.get_compute_host(i),
#'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_instances():
if options.format == 'table':
output = {}
output['header'] = [
'HYPERVISOR NAME',
'INSTANCE NAME',
'PROJECT',
'INSTANCE ID',
'STATUS',
'FLAVOR',
]
output['align'] = [
'l',
'l',
'l',
'l',
'l',
'l',
]
output['sortby'] = 0
counter = 0
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
#neutron = himutils.get_client(Neutron, options, logger)
#network = neutron.list_networks()
aggregate = nova.get_aggregate(options.aggregate)
if not aggregate:
himutils.warning(f"No aggregate {options.aggregate} found in {region}")
continue
instances = nova.get_instances(options.aggregate)
if options.format == 'table':
for i in instances:
project = kc.get_by_id('project', i.tenant_id)
if project is None:
project_name = Color.fg.red + "None" + Color.reset
else:
project_name = Color.fg.blu + project.name + Color.reset
# 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
output[counter] = [
Color.fg.ylw + nova.get_compute_host(i) + Color.reset,
Color.fg.CYN + i.name + Color.reset,
project_name,
Color.dim + i.id + Color.reset,
instance_status,
Color.fg.WHT + i.flavor['original_name'] + Color.reset,
]
counter += 1
else:
printer.output_dict({'header': 'Instance list (id, host, status, flavor)'})
status = dict({'total': 0})
for i in instances:
output = {
'1': i.id,
'2': nova.get_compute_host(i),
#'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
if options.format != 'table':
printer.output_dict({'header': 'Counts'})
printer.output_dict(status)
if options.format == 'table':
printer.output_dict(output)
def action_stop_instance():
msg = 'Remember to purge db with state.py first! Continue?'
if not options.dry_run and not himutils.confirm_action(msg):
return
state = himutils.get_client(State, options, logger)
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
instances = nova.get_instances(options.aggregate,
nova.get_fqdn_host(options.host))
for i in instances:
instance_data = i.to_dict().copy()
instance_data['region'] = region
instance_data['aggregate'] = options.aggregate
instance_data['host'] = getattr(i, 'OS-EXT-SRV-ATTR:host')
instance_data['instance_id'] = i.id
instance_data.pop('created', None) # not instance created, but db
instance = state.get_first(Instance, instance_id=i.id)
if instance is None: # add to db if not found
instance = Instance.create(instance_data) #pylint: disable=E1101
state.add(instance)
nova.stop_instance(i, 'NREC maintenance in progress')
state.close()
def action_start_instance():
state = himutils.get_client(State, options, logger)
pre = state.log_prefix()
for region in regions:
nova = himutils.get_client(Nova, options, logger, region)
instances = state.get_all(Instance, region=region,
aggregate=options.aggregate)
for i in instances:
if options.host and nova.get_fqdn_host(options.host) != i.host:
logger.debug('%s instance host do not match %s', pre, i.host)
continue
logger.debug('%s instance found %s', pre, i.name)
if i.status == 'ACTIVE':
instance = nova.get_by_id('server', i.instance_id)
if instance:
nova.start_instance(instance, unlock=True)
state.close()
# Run local function with the same name as the action (Note: - => _)
action = locals().get('action_' + options.action.replace('-', '_'))
if not action:
himutils.fatal(f"Function action_{options.action}() not implemented")
action()