blob: b496117fd96aa17cd7ac250b2ecd291f7628821b [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)}
Michael Polenchuked084462018-06-04 17:04:13 +040068 cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>/dev/null | awk \'/' + name + '/ {print $4}\'')
Jiri Broulik789179a2018-02-13 16:16:46 +010069 if cell_uuid:
70 try:
71 __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 + '?charset=utf8')
72 ret['result'] = True
73 ret['comment'] = 'Cell {0} updated'.format(name)
74 ret['changes'][name] = 'Cell {0} successfuly updated'.format(name)
75 except:
76 ret['result'] = False
77 ret['comment'] = 'Cell {0} not updated'.format(name)
78 ret['changes'][name] = 'Cell {0} failed to be updated'.format(name)
79 return ret
80
81
Jiri Broulik91104db2017-07-07 08:50:44 +020082def api_db_version_present(name=None, version="20"):
83 '''
84 Ensures that specific api_db version is present
85 '''
86 ret = {'name': 'api_db --version',
87 'changes': {},
88 'result': True,
89 'comment': 'Current Api_db version is not < than "{0}".'.format(version)}
90 api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null')
91 try:
92 api_db_version = int(api_db_version)
Jiri Broulik37189a72017-07-11 16:18:55 +020093 version = int(version)
Jiri Broulik91104db2017-07-07 08:50:44 +020094 except:
95 # nova is not installed
96 ret = _no_change('api_db --version', None, test=True)
97 return ret
98 if api_db_version < version:
99 try:
Jiri Broulik37189a72017-07-11 16:18:55 +0200100 __salt__['cmd.shell']('nova-manage api_db sync --version ' + str(version))
Jiri Broulik91104db2017-07-07 08:50:44 +0200101 ret['result'] = True
102 ret['comment'] = 'Nova-manage api_db sync --version {0} was successfuly executed'.format(version)
103 ret['changes']['api_db'] = 'api_db sync --version {0}'.format(version)
104 except:
105 ret['result'] = False
106 ret['comment'] = 'Error while executing nova-manage api_db sync --version {0}'.format(version)
107 ret['changes']['api_db'] = 'Failed to execute api_db sync --version {0}'.format(version)
108 return ret
109
110
111def db_version_present(name=None, version="334"):
112 '''
113 Ensures that specific api_db version is present
114 '''
115 ret = {'name': 'db --version',
116 'changes': {},
117 'result': True,
118 'comment': 'Current db version is not < than "{0}".'.format(version)}
119 db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null')
120 try:
121 db_version = int(db_version)
Jiri Broulik37189a72017-07-11 16:18:55 +0200122 version = int(version)
Jiri Broulik91104db2017-07-07 08:50:44 +0200123 except:
124 # nova is not installed
125 ret = _no_change('db --version', None, test=True)
126 return ret
127
128 if db_version < version:
129 try:
Jiri Broulik37189a72017-07-11 16:18:55 +0200130 __salt__['cmd.shell']('nova-manage db sync --version ' + str(version))
Jiri Broulik91104db2017-07-07 08:50:44 +0200131 ret['result'] = True
132 ret['comment'] = 'Nova-manage db sync --version {0} was successfuly executed'.format(version)
133 ret['changes']['db'] = 'db sync --version {0}'.format(version)
134 except:
135 ret['result'] = False
136 ret['comment'] = 'Error while executing nova-manage db sync --version {0}'.format(version)
137 ret['changes']['db'] = 'Failed to execute db sync --version {0}'.format(version)
138 return ret
139
Jiri Broulik37189a72017-07-11 16:18:55 +0200140def online_data_migrations_present(name=None, api_db_version="20", db_version="334"):
141 '''
142 Ensures that online_data_migrations are enforced if specific version of api_db and db is present
143 '''
144 ret = {'name': 'online_data_migrations',
145 'changes': {},
146 'result': True,
147 'comment': 'Current api_db version != {0} a db version != {1}.'.format(api_db_version, db_version)}
148 cur_api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null')
149 cur_db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null')
150 try:
151 cur_api_db_version = int(cur_api_db_version)
152 cur_db_version = int(cur_db_version)
153 api_db_version = int(api_db_version)
154 db_version = int(db_version)
155 except:
156 # nova is not installed
157 ret = _no_change('online_data_migrations', None, test=True)
158 return ret
159 if cur_api_db_version == api_db_version and cur_db_version == db_version:
160 try:
161 __salt__['cmd.shell']('nova-manage db online_data_migrations')
162 ret['result'] = True
163 ret['comment'] = 'nova-manage db online_data_migrations was successfuly executed'
164 ret['changes']['online_data_migrations'] = 'online_data_migrations on api_db version {0} and db version {1}'.format(api_db_version, db_version)
165 except:
166 ret['result'] = False
167 ret['comment'] = 'Error while executing nova-manage db online_data_migrations'
168 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)
169 return ret
170
Jiri Broulika2c79292017-02-05 21:01:38 +0100171def quota_present(tenant_name, profile, name=None, **kwargs):
172 '''
173 Ensures that the nova quota exists
174 '''
175 changes = {}
176 for key, value in kwargs.items():
177 quota = __salt__['novang.quota_get'](key, tenant_name, profile)
178 if quota != value:
179 arg = {}
180 arg[key] = value
181 changes[key] = value
182 __salt__['novang.quota_update'](tenant_name, profile, **arg)
183 if bool(changes):
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100184 return _updated(tenant_name, 'tenant', changes)
Jiri Broulika2c79292017-02-05 21:01:38 +0100185 else:
186 return _no_change(tenant_name, 'tenant')
187
Jiri Broulika2c79292017-02-05 21:01:38 +0100188
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100189def availability_zone_present(name=None, availability_zone=None, profile=None):
190 '''
191 Ensures that the nova availability zone exists
192 '''
193 name = availability_zone
194 zone_exists = __salt__['novang.availability_zone_get'](name, profile)
195 if zone_exists == False:
196 item_created = __salt__['novang.availability_zone_create'](name, availability_zone, profile)
197 if bool(item_created):
Michael Polenchuked084462018-06-04 17:04:13 +0400198 return _created(availability_zone, 'availabilty zone', item_created)
Jiri Broulika2c79292017-02-05 21:01:38 +0100199 else:
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100200 return _already_exists(availability_zone, 'availabilty zone')
201 return existing_availability_zones
202
Damian Szeluga5dca0f02017-04-13 17:27:15 +0200203def aggregate_present(name=None, aggregate=None, profile=None):
204 '''
205 Ensures that the nova aggregate exists
206 '''
207 name = aggregate
208 aggregate_exists = __salt__['novang.aggregate_get'](name, profile)
209 if aggregate_exists == False:
210 item_created = __salt__['novang.aggregate_create'](name, aggregate, profile)
211 if bool(item_created):
212 return _created(aggregate, 'aggregate', item_created)
213 else:
214 return _already_exists(aggregate, 'aggregate')
215 return existing_aggregate
216
Richard Felkl55d1f572017-02-15 16:41:53 +0100217
218def instance_present(name, flavor, image, networks, security_groups=None, profile=None, tenant_name=None):
219 ret = {'name': name,
220 'changes': {},
221 'result': True,
222 'comment': 'Instance "{0}" already exists'.format(name)}
223 kwargs = {}
224 nics = []
225 existing_instances = __salt__['novang.server_list'](profile, tenant_name)
226 if name in existing_instances:
227 return ret
Adam Tenglere8afccc2017-06-27 17:57:21 +0000228 existing_flavors = __salt__['novang.flavor_list'](profile)
Richard Felkl55d1f572017-02-15 16:41:53 +0100229 if flavor in existing_flavors:
Ondrej Smolab7b0dda2017-02-28 14:36:46 +0100230 flavor_id = existing_flavors[flavor]['id']
Richard Felkl55d1f572017-02-15 16:41:53 +0100231 else:
232 return {'name': name,
233 'changes': {},
234 'result': False,
235 'comment': 'Flavor "{0}" doesn\'t exists'.format(flavor)}
236
Adam Tenglere8afccc2017-06-27 17:57:21 +0000237 existing_image = __salt__['novang.image_list'](image, profile)
Richard Felkl55d1f572017-02-15 16:41:53 +0100238 if not existing_image:
239 return {'name': name,
240 'changes': {},
241 'result': False,
242 'comment': 'Image "{0}" doesn\'t exists'.format(image)}
243 else:
244 image_id = existing_image.get(image).get('id')
245 if security_groups is not None:
246 kwargs['security_groups'] = []
247 for secgroup in security_groups:
248 existing_secgroups = __salt__['novang.secgroup_list'](profile, tenant_name)
249 if not secgroup in existing_secgroups:
250 return {'name': name,
251 'changes': {},
252 'result': False,
253 'comment': 'Security group "{0}" doesn\'t exists'.format(secgroup)}
254 else:
255 kwargs['security_groups'].append(secgroup)
256 for net in networks:
257 existing_network = __salt__['novang.network_show'](net.get('name'), profile)
258 if not existing_network:
259 return {'name': name,
260 'changes': {},
261 'result': False,
262 'comment': 'Network "{0}" doesn\'t exists'.format(net.get(name))}
263 else:
264 network_id = existing_network.get('id')
265 if net.get('v4_fixed_ip') is not None:
266 nics.append({'net-id': network_id, 'v4-fixed-ip': net.get('v4_fixed_ip')})
267 else:
268 nics.append({'net-id': network_id})
269 kwargs['nics'] = nics
270 new_instance_id = __salt__['novang.boot'] (name, flavor_id, image_id, profile, tenant_name, **kwargs)
271 return {'name': name,
272 'changes': {},
273 'result': True,
274 'comment': 'Instance "{0}" was successfuly created'.format(name)}
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100275
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400276
Elena Ezhova9b7a1082017-08-02 17:40:46 +0400277def keypair_present(name, pub_file=None, pub_key=None, profile=None):
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400278 """
279 Ensures that the Nova key-pair exists
280 """
281
282 existing_keypairs = __salt__['novang.keypair_list'](profile)
283 if name in existing_keypairs:
284 return _already_exists(name, 'Keypair')
285 else:
286 res = __salt__['novang.keypair_add'](name, pubfile=pub_file,
Elena Ezhova9b7a1082017-08-02 17:40:46 +0400287 pubkey=pub_key, profile=profile)
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400288 if res and res['name'] == name:
289 return _created(name, 'Keypair', res)
290 return _create_failed(name, 'Keypair')
291
292
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100293def _already_exists(name, resource):
294 changes_dict = {'name': name,
295 'changes': {},
296 'result': True}
297 changes_dict['comment'] = \
298 '{0} {1} already exists'.format(resource, name)
299 return changes_dict
300
301
302def _created(name, resource, resource_definition):
303 changes_dict = {'name': name,
304 'changes': resource_definition,
305 'result': True,
306 'comment': '{0} {1} created'.format(resource, name)}
307 return changes_dict
308
309def _updated(name, resource, resource_definition):
310 changes_dict = {'name': name,
311 'changes': resource_definition,
312 'result': True,
313 'comment': '{0} {1} tenant was updated'.format(resource, name)}
314 return changes_dict
315
316def _update_failed(name, resource):
317 changes_dict = {'name': name,
318 'changes': {},
319 'comment': '{0} {1} failed to update'.format(resource, name),
320 'result': False}
321 return changes_dict
322
323def _no_change(name, resource, test=False):
324 changes_dict = {'name': name,
325 'changes': {},
326 'result': True}
327 if test:
328 changes_dict['comment'] = \
329 '{0} {1} will be {2}'.format(resource, name, test)
330 else:
331 changes_dict['comment'] = \
332 '{0} {1} is in correct state'.format(resource, name)
Damian Szeluga5dca0f02017-04-13 17:27:15 +0200333 return changes_dict
Adam Tenglere8afccc2017-06-27 17:57:21 +0000334
Elena Ezhovac1bcbfa2017-07-13 16:13:43 +0400335
336def _create_failed(name, resource):
337 changes_dict = {'name': name,
338 'changes': {},
339 'comment': '{0} {1} failed to create'.format(resource,
340 name),
341 'result': False}
342 return changes_dict