blob: 88cffcb93a5872ace11ecb85477df0af3f458e13 [file] [log] [blame]
Jiri Broulik0ce9fc92017-02-01 23:10:40 +01001# -*- coding: utf-8 -*-
2'''
Jiri Broulik5589c012017-06-20 11:28:52 +02003Custom Nova state
Jiri Broulik0ce9fc92017-02-01 23:10:40 +01004'''
Jiri Broulika2c79292017-02-05 21:01:38 +01005import logging
Richard Felkl55d1f572017-02-15 16:41:53 +01006import collections
Jiri Broulika2c79292017-02-05 21:01:38 +01007from functools import wraps
8LOG = logging.getLogger(__name__)
Jiri Broulik0ce9fc92017-02-01 23:10:40 +01009
10
11def __virtual__():
12 '''
13 Only load if the nova module is in __salt__
14 '''
Jiri Broulik5589c012017-06-20 11:28:52 +020015 return 'novang'
Jiri Broulik0ce9fc92017-02-01 23:10:40 +010016
Jiri Broulik70d9e3f2017-02-15 18:37:13 +010017
Jiri Broulik0ce9fc92017-02-01 23:10:40 +010018def flavor_present(name, flavor_id=0, ram=0, disk=0, vcpus=1, profile=None):
19 '''
Jiri Broulik70d9e3f2017-02-15 18:37:13 +010020 Ensures that the nova flavor exists
Jiri Broulik0ce9fc92017-02-01 23:10:40 +010021 '''
Jiri Broulik0ce9fc92017-02-01 23:10:40 +010022 ret = {'name': name,
23 'changes': {},
24 'result': True,
25 'comment': 'Flavor "{0}" already exists'.format(name)}
Adam Tenglere8afccc2017-06-27 17:57:21 +000026 project = __salt__['novang.flavor_list'](profile)
Jiri Broulik0ce9fc92017-02-01 23:10:40 +010027 if 'Error' in project:
28 pass
29 elif name in project:
30 pass
31 else:
Adam Tenglere8afccc2017-06-27 17:57:21 +000032 __salt__['novang.flavor_create'](name, flavor_id, ram, disk, vcpus, profile)
Jiri Broulik0ce9fc92017-02-01 23:10:40 +010033 ret['comment'] = 'Flavor {0} has been created'.format(name)
34 ret['changes']['Flavor'] = 'Created'
35 return ret
36
Jiri Broulik70d9e3f2017-02-15 18:37:13 +010037
Jiri Broulik5589c012017-06-20 11:28:52 +020038def map_instances(name='cell1'):
39 '''
40 Ensures that the nova instances are mapped to cell
41 '''
42 ret = {'name': name,
43 'changes': {},
44 'result': False,
45 'comment': 'Cell "{0}" does not exists'.format(name)}
Michael Polenchuked084462018-06-04 17:04:13 +040046 cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>/dev/null | awk \'/' + name + '/ {print $4}\'')
Jiri Broulik7e72aa22017-07-03 16:05:11 +020047 if cell_uuid:
Jiri Broulik5589c012017-06-20 11:28:52 +020048 try:
49 __salt__['cmd.shell']('nova-manage cell_v2 map_instances --cell_uuid ' + cell_uuid)
50 ret['result'] = True
51 ret['comment'] = 'Instances were mapped to cell named {0}'.format(name)
52 ret['changes']['Instances'] = 'Mapped to cell named {0}'.format(name)
53 except:
54 ret['result'] = False
55 ret['comment'] = 'Error while mapping instances to cell named {0}'.format(name)
56 ret['changes']['Instances'] = 'Failed to map to cell named {0}'.format(name)
57 return ret
58
59
Jiri Broulik789179a2018-02-13 16:16:46 +010060def update_cell(name='cell1', transport_url='none:///', db_engine='mysql', db_name='nova_upgrade', db_user='nova', db_password=None, db_address='0.0.0.0'):
61 '''
62 Ensures that the nova cell is setup correctly
63 '''
64 ret = {'name': name,
65 'changes': {},
66 'result': False,
67 'comment': 'Cell "{0}" does not exists'.format(name)}
Roman Lubianyi7a325992021-01-27 10:15:00 +020068 cell_uuid, ssl_enable = (__salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>/dev/null | awk \'/' + name + '/ {print $4, $8}\'')).split(' ', 1)
69 if 'ssl' in ssl_enable:
70 db_options = '?charset=utf8&ssl_ca=/etc/nova/ssl/mysql/ca-cert.pem&ssl_cert=/etc/nova/ssl/mysql/client-cert.pem&ssl_key=/etc/nova/ssl/mysql/client-key.pem'
71 else:
72 db_options = '?charset=utf8'
Jiri Broulik789179a2018-02-13 16:16:46 +010073 if cell_uuid:
74 try:
Roman Lubianyi7a325992021-01-27 10:15:00 +020075 __salt__['cmd.shell']('nova-manage cell_v2 update_cell --cell_uuid ' + cell_uuid + ' --transport-url ' + transport_url + ' --database_connection ' + '"' + db_engine + '+pymysql://' + db_user + ':' + db_password + '@' + db_address + '/' + db_name + db_options + '"')
Jiri Broulik789179a2018-02-13 16:16:46 +010076 ret['result'] = True
77 ret['comment'] = 'Cell {0} updated'.format(name)
78 ret['changes'][name] = 'Cell {0} successfuly updated'.format(name)
79 except:
80 ret['result'] = False
81 ret['comment'] = 'Cell {0} not updated'.format(name)
82 ret['changes'][name] = 'Cell {0} failed to be updated'.format(name)
83 return ret
84
85
Jiri Broulik91104db2017-07-07 08:50:44 +020086def api_db_version_present(name=None, version="20"):
87 '''
88 Ensures that specific api_db version is present
89 '''
90 ret = {'name': 'api_db --version',
91 'changes': {},
92 'result': True,
93 'comment': 'Current Api_db version is not < than "{0}".'.format(version)}
94 api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null')
95 try:
96 api_db_version = int(api_db_version)
Jiri Broulik37189a72017-07-11 16:18:55 +020097 version = int(version)
Jiri Broulik91104db2017-07-07 08:50:44 +020098 except:
99 # nova is not installed
100 ret = _no_change('api_db --version', None, test=True)
101 return ret
102 if api_db_version < version:
103 try:
Jiri Broulik37189a72017-07-11 16:18:55 +0200104 __salt__['cmd.shell']('nova-manage api_db sync --version ' + str(version))
Jiri Broulik91104db2017-07-07 08:50:44 +0200105 ret['result'] = True
106 ret['comment'] = 'Nova-manage api_db sync --version {0} was successfuly executed'.format(version)
107 ret['changes']['api_db'] = 'api_db sync --version {0}'.format(version)
108 except:
109 ret['result'] = False
110 ret['comment'] = 'Error while executing nova-manage api_db sync --version {0}'.format(version)
111 ret['changes']['api_db'] = 'Failed to execute api_db sync --version {0}'.format(version)
112 return ret
113
114
115def db_version_present(name=None, version="334"):
116 '''
117 Ensures that specific api_db version is present
118 '''
119 ret = {'name': 'db --version',
120 'changes': {},
121 'result': True,
122 'comment': 'Current db version is not < than "{0}".'.format(version)}
123 db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null')
124 try:
125 db_version = int(db_version)
Jiri Broulik37189a72017-07-11 16:18:55 +0200126 version = int(version)
Jiri Broulik91104db2017-07-07 08:50:44 +0200127 except:
128 # nova is not installed
129 ret = _no_change('db --version', None, test=True)
130 return ret
131
132 if db_version < version:
133 try:
Jiri Broulik37189a72017-07-11 16:18:55 +0200134 __salt__['cmd.shell']('nova-manage db sync --version ' + str(version))
Jiri Broulik91104db2017-07-07 08:50:44 +0200135 ret['result'] = True
136 ret['comment'] = 'Nova-manage db sync --version {0} was successfuly executed'.format(version)
137 ret['changes']['db'] = 'db sync --version {0}'.format(version)
138 except:
139 ret['result'] = False
140 ret['comment'] = 'Error while executing nova-manage db sync --version {0}'.format(version)
141 ret['changes']['db'] = 'Failed to execute db sync --version {0}'.format(version)
142 return ret
143
Jiri Broulik37189a72017-07-11 16:18:55 +0200144def online_data_migrations_present(name=None, api_db_version="20", db_version="334"):
145 '''
146 Ensures that online_data_migrations are enforced if specific version of api_db and db is present
147 '''
148 ret = {'name': 'online_data_migrations',
149 'changes': {},
150 'result': True,
151 'comment': 'Current api_db version != {0} a db version != {1}.'.format(api_db_version, db_version)}
152 cur_api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null')
153 cur_db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null')
154 try:
155 cur_api_db_version = int(cur_api_db_version)
156 cur_db_version = int(cur_db_version)
157 api_db_version = int(api_db_version)
158 db_version = int(db_version)
159 except:
160 # nova is not installed
161 ret = _no_change('online_data_migrations', None, test=True)
162 return ret
163 if cur_api_db_version == api_db_version and cur_db_version == db_version:
164 try:
165 __salt__['cmd.shell']('nova-manage db online_data_migrations')
166 ret['result'] = True
167 ret['comment'] = 'nova-manage db online_data_migrations was successfuly executed'
168 ret['changes']['online_data_migrations'] = 'online_data_migrations on api_db version {0} and db version {1}'.format(api_db_version, db_version)
169 except:
170 ret['result'] = False
171 ret['comment'] = 'Error while executing nova-manage db online_data_migrations'
172 ret['changes']['online_data_migrations'] = 'Failed to execute online_data_migrations on api_db version {0} and db version {1}'.format(api_db_version, db_version)
173 return ret
174
Jiri Broulika2c79292017-02-05 21:01:38 +0100175def quota_present(tenant_name, profile, name=None, **kwargs):
176 '''
177 Ensures that the nova quota exists
178 '''
179 changes = {}
180 for key, value in kwargs.items():
181 quota = __salt__['novang.quota_get'](key, tenant_name, profile)
182 if quota != value:
183 arg = {}
184 arg[key] = value
185 changes[key] = value
186 __salt__['novang.quota_update'](tenant_name, profile, **arg)
187 if bool(changes):
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100188 return _updated(tenant_name, 'tenant', changes)
Jiri Broulika2c79292017-02-05 21:01:38 +0100189 else:
190 return _no_change(tenant_name, 'tenant')
191
Jiri Broulika2c79292017-02-05 21:01:38 +0100192
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100193def availability_zone_present(name=None, availability_zone=None, profile=None):
194 '''
195 Ensures that the nova availability zone exists
196 '''
197 name = availability_zone
198 zone_exists = __salt__['novang.availability_zone_get'](name, profile)
199 if zone_exists == False:
200 item_created = __salt__['novang.availability_zone_create'](name, availability_zone, profile)
201 if bool(item_created):
Michael Polenchuked084462018-06-04 17:04:13 +0400202 return _created(availability_zone, 'availabilty zone', item_created)
Jiri Broulika2c79292017-02-05 21:01:38 +0100203 else:
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100204 return _already_exists(availability_zone, 'availabilty zone')
205 return existing_availability_zones
206
Damian Szeluga5dca0f02017-04-13 17:27:15 +0200207def aggregate_present(name=None, aggregate=None, profile=None):
208 '''
209 Ensures that the nova aggregate exists
210 '''
211 name = aggregate
212 aggregate_exists = __salt__['novang.aggregate_get'](name, profile)
213 if aggregate_exists == False:
214 item_created = __salt__['novang.aggregate_create'](name, aggregate, profile)
215 if bool(item_created):
216 return _created(aggregate, 'aggregate', item_created)
217 else:
218 return _already_exists(aggregate, 'aggregate')
219 return existing_aggregate
220
Richard Felkl55d1f572017-02-15 16:41:53 +0100221
222def instance_present(name, flavor, image, networks, security_groups=None, profile=None, tenant_name=None):
223 ret = {'name': name,
224 'changes': {},
225 'result': True,
226 'comment': 'Instance "{0}" already exists'.format(name)}
227 kwargs = {}
228 nics = []
229 existing_instances = __salt__['novang.server_list'](profile, tenant_name)
230 if name in existing_instances:
231 return ret
Adam Tenglere8afccc2017-06-27 17:57:21 +0000232 existing_flavors = __salt__['novang.flavor_list'](profile)
Richard Felkl55d1f572017-02-15 16:41:53 +0100233 if flavor in existing_flavors:
Ondrej Smolab7b0dda2017-02-28 14:36:46 +0100234 flavor_id = existing_flavors[flavor]['id']
Richard Felkl55d1f572017-02-15 16:41:53 +0100235 else:
236 return {'name': name,
237 'changes': {},
238 'result': False,
239 'comment': 'Flavor "{0}" doesn\'t exists'.format(flavor)}
240
Adam Tenglere8afccc2017-06-27 17:57:21 +0000241 existing_image = __salt__['novang.image_list'](image, profile)
Richard Felkl55d1f572017-02-15 16:41:53 +0100242 if not existing_image:
243 return {'name': name,
244 'changes': {},
245 'result': False,
246 'comment': 'Image "{0}" doesn\'t exists'.format(image)}
247 else:
248 image_id = existing_image.get(image).get('id')
249 if security_groups is not None:
250 kwargs['security_groups'] = []
251 for secgroup in security_groups:
252 existing_secgroups = __salt__['novang.secgroup_list'](profile, tenant_name)
253 if not secgroup in existing_secgroups:
254 return {'name': name,
255 'changes': {},
256 'result': False,
257 'comment': 'Security group "{0}" doesn\'t exists'.format(secgroup)}
258 else:
259 kwargs['security_groups'].append(secgroup)
260 for net in networks:
261 existing_network = __salt__['novang.network_show'](net.get('name'), profile)
262 if not existing_network:
263 return {'name': name,
264 'changes': {},
265 'result': False,
266 'comment': 'Network "{0}" doesn\'t exists'.format(net.get(name))}
267 else:
268 network_id = existing_network.get('id')
269 if net.get('v4_fixed_ip') is not None:
270 nics.append({'net-id': network_id, 'v4-fixed-ip': net.get('v4_fixed_ip')})
271 else:
272 nics.append({'net-id': network_id})
273 kwargs['nics'] = nics
274 new_instance_id = __salt__['novang.boot'] (name, flavor_id, image_id, profile, tenant_name, **kwargs)
275 return {'name': name,
276 'changes': {},
277 'result': True,
278 'comment': 'Instance "{0}" was successfuly created'.format(name)}
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100279
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400280
Elena Ezhova9b7a1082017-08-02 17:40:46 +0400281def keypair_present(name, pub_file=None, pub_key=None, profile=None):
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400282 """
283 Ensures that the Nova key-pair exists
284 """
285
286 existing_keypairs = __salt__['novang.keypair_list'](profile)
287 if name in existing_keypairs:
288 return _already_exists(name, 'Keypair')
289 else:
290 res = __salt__['novang.keypair_add'](name, pubfile=pub_file,
Elena Ezhova9b7a1082017-08-02 17:40:46 +0400291 pubkey=pub_key, profile=profile)
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400292 if res and res['name'] == name:
293 return _created(name, 'Keypair', res)
294 return _create_failed(name, 'Keypair')
295
296
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100297def _already_exists(name, resource):
298 changes_dict = {'name': name,
299 'changes': {},
300 'result': True}
301 changes_dict['comment'] = \
302 '{0} {1} already exists'.format(resource, name)
303 return changes_dict
304
305
306def _created(name, resource, resource_definition):
307 changes_dict = {'name': name,
308 'changes': resource_definition,
309 'result': True,
310 'comment': '{0} {1} created'.format(resource, name)}
311 return changes_dict
312
313def _updated(name, resource, resource_definition):
314 changes_dict = {'name': name,
315 'changes': resource_definition,
316 'result': True,
317 'comment': '{0} {1} tenant was updated'.format(resource, name)}
318 return changes_dict
319
320def _update_failed(name, resource):
321 changes_dict = {'name': name,
322 'changes': {},
323 'comment': '{0} {1} failed to update'.format(resource, name),
324 'result': False}
325 return changes_dict
326
327def _no_change(name, resource, test=False):
328 changes_dict = {'name': name,
329 'changes': {},
330 'result': True}
331 if test:
332 changes_dict['comment'] = \
333 '{0} {1} will be {2}'.format(resource, name, test)
334 else:
335 changes_dict['comment'] = \
336 '{0} {1} is in correct state'.format(resource, name)
Damian Szeluga5dca0f02017-04-13 17:27:15 +0200337 return changes_dict
Adam Tenglere8afccc2017-06-27 17:57:21 +0000338
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400339
340def _create_failed(name, resource):
341 changes_dict = {'name': name,
342 'changes': {},
343 'comment': '{0} {1} failed to create'.format(resource,
344 name),
345 'result': False}
346 return changes_dict