Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
Jiri Broulik | 5589c01 | 2017-06-20 11:28:52 +0200 | [diff] [blame] | 3 | Custom Nova state |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 4 | ''' |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 5 | import logging |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 6 | import collections |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 7 | from functools import wraps |
| 8 | LOG = logging.getLogger(__name__) |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def __virtual__(): |
| 12 | ''' |
| 13 | Only load if the nova module is in __salt__ |
| 14 | ''' |
Jiri Broulik | 5589c01 | 2017-06-20 11:28:52 +0200 | [diff] [blame] | 15 | return 'novang' |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 16 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 17 | |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 18 | def flavor_present(name, flavor_id=0, ram=0, disk=0, vcpus=1, profile=None): |
| 19 | ''' |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 20 | Ensures that the nova flavor exists |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 21 | ''' |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 22 | ret = {'name': name, |
| 23 | 'changes': {}, |
| 24 | 'result': True, |
| 25 | 'comment': 'Flavor "{0}" already exists'.format(name)} |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 26 | project = __salt__['novang.flavor_list'](profile) |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 27 | if 'Error' in project: |
| 28 | pass |
| 29 | elif name in project: |
| 30 | pass |
| 31 | else: |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 32 | __salt__['novang.flavor_create'](name, flavor_id, ram, disk, vcpus, profile) |
Jiri Broulik | 0ce9fc9 | 2017-02-01 23:10:40 +0100 | [diff] [blame] | 33 | ret['comment'] = 'Flavor {0} has been created'.format(name) |
| 34 | ret['changes']['Flavor'] = 'Created' |
| 35 | return ret |
| 36 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 37 | |
Jiri Broulik | 5589c01 | 2017-06-20 11:28:52 +0200 | [diff] [blame] | 38 | def 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 Polenchuk | ed08446 | 2018-06-04 17:04:13 +0400 | [diff] [blame] | 46 | cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>/dev/null | awk \'/' + name + '/ {print $4}\'') |
Jiri Broulik | 7e72aa2 | 2017-07-03 16:05:11 +0200 | [diff] [blame] | 47 | if cell_uuid: |
Jiri Broulik | 5589c01 | 2017-06-20 11:28:52 +0200 | [diff] [blame] | 48 | 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 Broulik | 789179a | 2018-02-13 16:16:46 +0100 | [diff] [blame] | 60 | def 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 Lubianyi | 7a32599 | 2021-01-27 10:15:00 +0200 | [diff] [blame] | 68 | 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 Broulik | 789179a | 2018-02-13 16:16:46 +0100 | [diff] [blame] | 73 | if cell_uuid: |
| 74 | try: |
Roman Lubianyi | 7a32599 | 2021-01-27 10:15:00 +0200 | [diff] [blame] | 75 | __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 Broulik | 789179a | 2018-02-13 16:16:46 +0100 | [diff] [blame] | 76 | 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 Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 86 | def 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 Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 97 | version = int(version) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 98 | 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 Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 104 | __salt__['cmd.shell']('nova-manage api_db sync --version ' + str(version)) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 105 | 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 | |
| 115 | def 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 Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 126 | version = int(version) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 127 | 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 Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 134 | __salt__['cmd.shell']('nova-manage db sync --version ' + str(version)) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 135 | 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 Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 144 | def 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 Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 175 | def 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 Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 188 | return _updated(tenant_name, 'tenant', changes) |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 189 | else: |
| 190 | return _no_change(tenant_name, 'tenant') |
| 191 | |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 192 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 193 | def 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 Polenchuk | ed08446 | 2018-06-04 17:04:13 +0400 | [diff] [blame] | 202 | return _created(availability_zone, 'availabilty zone', item_created) |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 203 | else: |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 204 | return _already_exists(availability_zone, 'availabilty zone') |
| 205 | return existing_availability_zones |
| 206 | |
Damian Szeluga | 5dca0f0 | 2017-04-13 17:27:15 +0200 | [diff] [blame] | 207 | def 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 Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 221 | |
| 222 | def 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 Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 232 | existing_flavors = __salt__['novang.flavor_list'](profile) |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 233 | if flavor in existing_flavors: |
Ondrej Smola | b7b0dda | 2017-02-28 14:36:46 +0100 | [diff] [blame] | 234 | flavor_id = existing_flavors[flavor]['id'] |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 235 | else: |
| 236 | return {'name': name, |
| 237 | 'changes': {}, |
| 238 | 'result': False, |
| 239 | 'comment': 'Flavor "{0}" doesn\'t exists'.format(flavor)} |
| 240 | |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 241 | existing_image = __salt__['novang.image_list'](image, profile) |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 242 | 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 Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 279 | |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 280 | |
Elena Ezhova | 9b7a108 | 2017-08-02 17:40:46 +0400 | [diff] [blame] | 281 | def keypair_present(name, pub_file=None, pub_key=None, profile=None): |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 282 | """ |
| 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 Ezhova | 9b7a108 | 2017-08-02 17:40:46 +0400 | [diff] [blame] | 291 | pubkey=pub_key, profile=profile) |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 292 | if res and res['name'] == name: |
| 293 | return _created(name, 'Keypair', res) |
| 294 | return _create_failed(name, 'Keypair') |
| 295 | |
| 296 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 297 | def _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 | |
| 306 | def _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 | |
| 313 | def _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 | |
| 320 | def _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 | |
| 327 | def _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 Szeluga | 5dca0f0 | 2017-04-13 17:27:15 +0200 | [diff] [blame] | 337 | return changes_dict |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 338 | |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 339 | |
| 340 | def _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 |