forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flavor.py
executable file
·226 lines (204 loc) · 9.26 KB
/
flavor.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
#!/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.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from collections import OrderedDict
import glob
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():
for region in regions:
nc = himutils.get_client(Nova, options, logger, region)
flavors = nc.get_flavors(class_filter=options.flavor)
outputs = ['name', 'vcpus', 'ram', 'disk']
header = 'flavors in %s (%s)' % (region, ', '.join(outputs))
printer.output_dict({'header': header})
for flavor in flavors:
output = OrderedDict()
for out in outputs:
output[out] = getattr(flavor, out)
printer.output_dict(objects=output, one_line=True, sort=False)
def action_instances():
for region in regions:
nc = himutils.get_client(Nova, options, logger, region)
flavors = nc.get_flavors(class_filter=options.flavor)
printer.output_dict({'header': 'Instance list %s (id, name, flavor)' % region})
status = dict({'total': 0})
for flavor in flavors:
search_opts = dict(all_tenants=1, flavor=flavor.id)
instances = nc.get_all_instances(search_opts=search_opts)
for i in instances:
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_update():
question = ("This will delete the flavor and recreate it for all other "
"changes than properties. Check project access after. Continue?")
if not himutils.confirm_action(question):
return
for region in regions:
flavors = get_flavor_config(region)
public = flavors['public'] if 'public' in flavors else False
properties = flavors['properties'] if 'properties' in flavors else dict()
if not properties or not properties.get('aggregate_instance_extra_specs:type', None):
properties['aggregate_instance_extra_specs:type'] = 's== standard'
if (options.flavor not in flavors or
not isinstance(flavors[options.flavor], dict)):
himutils.sys_error('%s hash not found in config' % options.flavor)
nc = Nova(options.config, debug=options.debug, log=logger, region=region)
nc.set_dry_run(options.dry_run)
access = nc.get_flavor_access(filters=options.flavor)
for name, spec in sorted(flavors[options.flavor].items()):
# Hack to override properties per flavor
flavor_properties = properties.copy()
if 'properties' in spec:
for p_name, prop in spec['properties'].items():
flavor_properties[p_name] = prop
del spec['properties']
nc.update_flavor(name=name, spec=spec,
properties=flavor_properties, public=public)
# Update access
all_projects = set()
for name, projects in access.items():
for project_id in projects:
all_projects.add(project_id.tenant_id)
for project in all_projects:
nc.update_flavor_access(class_filter=options.flavor,
project_id=project,
action='grant')
def action_purge():
q = 'Purge flavors from class {} in region(s) {}'.format(options.flavor, ','.join(regions))
if not himutils.confirm_action(q):
return
for region in regions:
flavors = get_flavor_config(region)
nc = himutils.get_client(Nova, options, logger, region)
nc.debug_log('Start purge of flavor class {} from region {}'.format(options.flavor, region))
result = nc.purge_flavors(class_filter=options.flavor, flavors=flavors)
if result:
printer.output_msg('Purged flavors of class {} from region {}'
.format(options.flavor, region))
else:
printer.output_msg('Nothing to purge from region {}'.format(region))
def action_delete():
q = 'Delete flavor class {} in region(s) {}'.format(options.flavor, ','.join(regions))
if not himutils.confirm_action(q):
return
for region in regions:
nc = himutils.get_client(Nova, options, logger, region)
nc.debug_log('Start delete all {} from region {}'.format(options.flavor, region))
result = nc.delete_flavors(class_filter=options.flavor)
if result:
printer.output_msg('Deleted all {} from region {}'.format(options.flavor, region))
else:
printer.output_msg('Nothing to delete from region {}'.format(region))
def action_grant():
for region in regions:
nc = himutils.get_client(Nova, options, logger, region)
result = update_access(nc, 'grant', region)
if result:
printer.output_msg("Grant access to {} for {} in {}"
.format(options.flavor, options.project, region))
def action_revoke():
for region in regions:
nc = himutils.get_client(Nova, options, logger, region)
result = update_access(nc, 'revoke', region)
if result:
printer.output_msg("Revoke access to {} for {} in {}"
.format(options.flavor, options.project, region))
def action_list_access():
for region in regions:
nc = himutils.get_client(Nova, options, logger, region)
access = nc.get_flavor_access(filters=options.flavor)
header = 'access to %s flavor in %s' % (options.flavor, region)
printer.output_dict({'header': header})
output = dict()
for name, projects in access.items():
output[name] = list()
for project_id in projects:
project = kc.get_by_id('project', project_id.tenant_id)
if project:
output[name].append(project.name)
else:
himutils.sys_error('project not found %s' % project_id.tenant_id, 0)
continue
printer.output_dict(output)
def action_list_available():
region = kc.get_region()
nc = himutils.get_client(Nova, options, logger, region)
flavors = nc.get_flavors()
flavor_classes = {}
for flavor in flavors:
if len(flavor.name.split('.')) > 1:
flavor_class = '.'.join(flavor.name.split('.')[:-1])
else:
flavor_class = flavor.name
flavor_classes[flavor_class] = flavor_classes.get(flavor_class, 0) + 1
printer.output_dict({'header': 'flavor classes and number of flavors in class'})
printer.output_dict(flavor_classes)
def action_available_flavors():
path = '/opt/himlarcli/config/flavors/*.yaml'
files = glob.glob(path)
for file in files:
f = open(file, 'r')
print(file)
print((f.readlines()[1]))
f.close()
def get_flavor_config(region):
# First look for region version of flavor config, then the default one
if himutils.file_exists('config/flavors/%s-%s.yaml' % (options.flavor, region)):
configfile = 'config/flavors/%s-%s.yaml' % (options.flavor, region)
else:
configfile = 'config/flavors/%s.yaml' % (options.flavor)
kc.debug_log('use flavor config from %s' % configfile)
flavors = himutils.load_config(configfile)
if not flavors:
himutils.sys_error('Could not find flavor config file config/flavors/%s.yaml'
% options.flavor)
return flavors
def update_access(nc, access_action, region):
flavors = get_flavor_config(region)
if options.flavor in flavors and not flavors[options.flavor]:
return False
if 'public' in flavors and flavors['public']:
himutils.sys_error('grant or revoke will not work on public flavors!')
project = kc.get_project_by_name(options.project)
if not project:
himutils.sys_error('project not found %s' % project)
result = nc.update_flavor_access(class_filter=options.flavor,
project_id=project.id,
action=access_action)
if not result:
himutils.sys_error('Update access for {} failed. Check debug log'
.format(options.flavor), 0)
return False
return True
# Run local function with the same name as the action (Note: - => _)
action = locals().get('action_' + options.action.replace('-', '_'))
if not action:
himutils.sys_error("Function action_%s() not implemented" % options.action)
action()