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)} |
| 46 | cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>&- | grep ' + name + ' | tr -d \"\n\" | awk \'{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 | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 60 | def api_db_version_present(name=None, version="20"): |
| 61 | ''' |
| 62 | Ensures that specific api_db version is present |
| 63 | ''' |
| 64 | ret = {'name': 'api_db --version', |
| 65 | 'changes': {}, |
| 66 | 'result': True, |
| 67 | 'comment': 'Current Api_db version is not < than "{0}".'.format(version)} |
| 68 | api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null') |
| 69 | try: |
| 70 | api_db_version = int(api_db_version) |
Jiri Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 71 | version = int(version) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 72 | except: |
| 73 | # nova is not installed |
| 74 | ret = _no_change('api_db --version', None, test=True) |
| 75 | return ret |
| 76 | if api_db_version < version: |
| 77 | try: |
Jiri Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 78 | __salt__['cmd.shell']('nova-manage api_db sync --version ' + str(version)) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 79 | ret['result'] = True |
| 80 | ret['comment'] = 'Nova-manage api_db sync --version {0} was successfuly executed'.format(version) |
| 81 | ret['changes']['api_db'] = 'api_db sync --version {0}'.format(version) |
| 82 | except: |
| 83 | ret['result'] = False |
| 84 | ret['comment'] = 'Error while executing nova-manage api_db sync --version {0}'.format(version) |
| 85 | ret['changes']['api_db'] = 'Failed to execute api_db sync --version {0}'.format(version) |
| 86 | return ret |
| 87 | |
| 88 | |
| 89 | def db_version_present(name=None, version="334"): |
| 90 | ''' |
| 91 | Ensures that specific api_db version is present |
| 92 | ''' |
| 93 | ret = {'name': 'db --version', |
| 94 | 'changes': {}, |
| 95 | 'result': True, |
| 96 | 'comment': 'Current db version is not < than "{0}".'.format(version)} |
| 97 | db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null') |
| 98 | try: |
| 99 | db_version = int(db_version) |
Jiri Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 100 | version = int(version) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 101 | except: |
| 102 | # nova is not installed |
| 103 | ret = _no_change('db --version', None, test=True) |
| 104 | return ret |
| 105 | |
| 106 | if db_version < version: |
| 107 | try: |
Jiri Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 108 | __salt__['cmd.shell']('nova-manage db sync --version ' + str(version)) |
Jiri Broulik | 91104db | 2017-07-07 08:50:44 +0200 | [diff] [blame] | 109 | ret['result'] = True |
| 110 | ret['comment'] = 'Nova-manage db sync --version {0} was successfuly executed'.format(version) |
| 111 | ret['changes']['db'] = 'db sync --version {0}'.format(version) |
| 112 | except: |
| 113 | ret['result'] = False |
| 114 | ret['comment'] = 'Error while executing nova-manage db sync --version {0}'.format(version) |
| 115 | ret['changes']['db'] = 'Failed to execute db sync --version {0}'.format(version) |
| 116 | return ret |
| 117 | |
Jiri Broulik | 37189a7 | 2017-07-11 16:18:55 +0200 | [diff] [blame] | 118 | def online_data_migrations_present(name=None, api_db_version="20", db_version="334"): |
| 119 | ''' |
| 120 | Ensures that online_data_migrations are enforced if specific version of api_db and db is present |
| 121 | ''' |
| 122 | ret = {'name': 'online_data_migrations', |
| 123 | 'changes': {}, |
| 124 | 'result': True, |
| 125 | 'comment': 'Current api_db version != {0} a db version != {1}.'.format(api_db_version, db_version)} |
| 126 | cur_api_db_version = __salt__['cmd.shell']('nova-manage api_db version 2>/dev/null') |
| 127 | cur_db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null') |
| 128 | try: |
| 129 | cur_api_db_version = int(cur_api_db_version) |
| 130 | cur_db_version = int(cur_db_version) |
| 131 | api_db_version = int(api_db_version) |
| 132 | db_version = int(db_version) |
| 133 | except: |
| 134 | # nova is not installed |
| 135 | ret = _no_change('online_data_migrations', None, test=True) |
| 136 | return ret |
| 137 | if cur_api_db_version == api_db_version and cur_db_version == db_version: |
| 138 | try: |
| 139 | __salt__['cmd.shell']('nova-manage db online_data_migrations') |
| 140 | ret['result'] = True |
| 141 | ret['comment'] = 'nova-manage db online_data_migrations was successfuly executed' |
| 142 | ret['changes']['online_data_migrations'] = 'online_data_migrations on api_db version {0} and db version {1}'.format(api_db_version, db_version) |
| 143 | except: |
| 144 | ret['result'] = False |
| 145 | ret['comment'] = 'Error while executing nova-manage db online_data_migrations' |
| 146 | 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) |
| 147 | return ret |
| 148 | |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 149 | def quota_present(tenant_name, profile, name=None, **kwargs): |
| 150 | ''' |
| 151 | Ensures that the nova quota exists |
| 152 | ''' |
| 153 | changes = {} |
| 154 | for key, value in kwargs.items(): |
| 155 | quota = __salt__['novang.quota_get'](key, tenant_name, profile) |
| 156 | if quota != value: |
| 157 | arg = {} |
| 158 | arg[key] = value |
| 159 | changes[key] = value |
| 160 | __salt__['novang.quota_update'](tenant_name, profile, **arg) |
| 161 | if bool(changes): |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 162 | return _updated(tenant_name, 'tenant', changes) |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 163 | else: |
| 164 | return _no_change(tenant_name, 'tenant') |
| 165 | |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 166 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 167 | def availability_zone_present(name=None, availability_zone=None, profile=None): |
| 168 | ''' |
| 169 | Ensures that the nova availability zone exists |
| 170 | ''' |
| 171 | name = availability_zone |
| 172 | zone_exists = __salt__['novang.availability_zone_get'](name, profile) |
| 173 | if zone_exists == False: |
| 174 | item_created = __salt__['novang.availability_zone_create'](name, availability_zone, profile) |
| 175 | if bool(item_created): |
| 176 | return _created(availability_zone, 'availabilty zone', item_created) |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 177 | else: |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 178 | return _already_exists(availability_zone, 'availabilty zone') |
| 179 | return existing_availability_zones |
| 180 | |
Damian Szeluga | 5dca0f0 | 2017-04-13 17:27:15 +0200 | [diff] [blame] | 181 | def aggregate_present(name=None, aggregate=None, profile=None): |
| 182 | ''' |
| 183 | Ensures that the nova aggregate exists |
| 184 | ''' |
| 185 | name = aggregate |
| 186 | aggregate_exists = __salt__['novang.aggregate_get'](name, profile) |
| 187 | if aggregate_exists == False: |
| 188 | item_created = __salt__['novang.aggregate_create'](name, aggregate, profile) |
| 189 | if bool(item_created): |
| 190 | return _created(aggregate, 'aggregate', item_created) |
| 191 | else: |
| 192 | return _already_exists(aggregate, 'aggregate') |
| 193 | return existing_aggregate |
| 194 | |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 195 | |
| 196 | def instance_present(name, flavor, image, networks, security_groups=None, profile=None, tenant_name=None): |
| 197 | ret = {'name': name, |
| 198 | 'changes': {}, |
| 199 | 'result': True, |
| 200 | 'comment': 'Instance "{0}" already exists'.format(name)} |
| 201 | kwargs = {} |
| 202 | nics = [] |
| 203 | existing_instances = __salt__['novang.server_list'](profile, tenant_name) |
| 204 | if name in existing_instances: |
| 205 | return ret |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 206 | existing_flavors = __salt__['novang.flavor_list'](profile) |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 207 | if flavor in existing_flavors: |
Ondrej Smola | b7b0dda | 2017-02-28 14:36:46 +0100 | [diff] [blame] | 208 | flavor_id = existing_flavors[flavor]['id'] |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 209 | else: |
| 210 | return {'name': name, |
| 211 | 'changes': {}, |
| 212 | 'result': False, |
| 213 | 'comment': 'Flavor "{0}" doesn\'t exists'.format(flavor)} |
| 214 | |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 215 | existing_image = __salt__['novang.image_list'](image, profile) |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 216 | if not existing_image: |
| 217 | return {'name': name, |
| 218 | 'changes': {}, |
| 219 | 'result': False, |
| 220 | 'comment': 'Image "{0}" doesn\'t exists'.format(image)} |
| 221 | else: |
| 222 | image_id = existing_image.get(image).get('id') |
| 223 | if security_groups is not None: |
| 224 | kwargs['security_groups'] = [] |
| 225 | for secgroup in security_groups: |
| 226 | existing_secgroups = __salt__['novang.secgroup_list'](profile, tenant_name) |
| 227 | if not secgroup in existing_secgroups: |
| 228 | return {'name': name, |
| 229 | 'changes': {}, |
| 230 | 'result': False, |
| 231 | 'comment': 'Security group "{0}" doesn\'t exists'.format(secgroup)} |
| 232 | else: |
| 233 | kwargs['security_groups'].append(secgroup) |
| 234 | for net in networks: |
| 235 | existing_network = __salt__['novang.network_show'](net.get('name'), profile) |
| 236 | if not existing_network: |
| 237 | return {'name': name, |
| 238 | 'changes': {}, |
| 239 | 'result': False, |
| 240 | 'comment': 'Network "{0}" doesn\'t exists'.format(net.get(name))} |
| 241 | else: |
| 242 | network_id = existing_network.get('id') |
| 243 | if net.get('v4_fixed_ip') is not None: |
| 244 | nics.append({'net-id': network_id, 'v4-fixed-ip': net.get('v4_fixed_ip')}) |
| 245 | else: |
| 246 | nics.append({'net-id': network_id}) |
| 247 | kwargs['nics'] = nics |
| 248 | new_instance_id = __salt__['novang.boot'] (name, flavor_id, image_id, profile, tenant_name, **kwargs) |
| 249 | return {'name': name, |
| 250 | 'changes': {}, |
| 251 | 'result': True, |
| 252 | 'comment': 'Instance "{0}" was successfuly created'.format(name)} |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 253 | |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 254 | |
Elena Ezhova | 9b7a108 | 2017-08-02 17:40:46 +0400 | [diff] [blame] | 255 | def keypair_present(name, pub_file=None, pub_key=None, profile=None): |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 256 | """ |
| 257 | Ensures that the Nova key-pair exists |
| 258 | """ |
| 259 | |
| 260 | existing_keypairs = __salt__['novang.keypair_list'](profile) |
| 261 | if name in existing_keypairs: |
| 262 | return _already_exists(name, 'Keypair') |
| 263 | else: |
| 264 | res = __salt__['novang.keypair_add'](name, pubfile=pub_file, |
Elena Ezhova | 9b7a108 | 2017-08-02 17:40:46 +0400 | [diff] [blame] | 265 | pubkey=pub_key, profile=profile) |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 266 | if res and res['name'] == name: |
| 267 | return _created(name, 'Keypair', res) |
| 268 | return _create_failed(name, 'Keypair') |
| 269 | |
| 270 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 271 | def _already_exists(name, resource): |
| 272 | changes_dict = {'name': name, |
| 273 | 'changes': {}, |
| 274 | 'result': True} |
| 275 | changes_dict['comment'] = \ |
| 276 | '{0} {1} already exists'.format(resource, name) |
| 277 | return changes_dict |
| 278 | |
| 279 | |
| 280 | def _created(name, resource, resource_definition): |
| 281 | changes_dict = {'name': name, |
| 282 | 'changes': resource_definition, |
| 283 | 'result': True, |
| 284 | 'comment': '{0} {1} created'.format(resource, name)} |
| 285 | return changes_dict |
| 286 | |
| 287 | def _updated(name, resource, resource_definition): |
| 288 | changes_dict = {'name': name, |
| 289 | 'changes': resource_definition, |
| 290 | 'result': True, |
| 291 | 'comment': '{0} {1} tenant was updated'.format(resource, name)} |
| 292 | return changes_dict |
| 293 | |
| 294 | def _update_failed(name, resource): |
| 295 | changes_dict = {'name': name, |
| 296 | 'changes': {}, |
| 297 | 'comment': '{0} {1} failed to update'.format(resource, name), |
| 298 | 'result': False} |
| 299 | return changes_dict |
| 300 | |
| 301 | def _no_change(name, resource, test=False): |
| 302 | changes_dict = {'name': name, |
| 303 | 'changes': {}, |
| 304 | 'result': True} |
| 305 | if test: |
| 306 | changes_dict['comment'] = \ |
| 307 | '{0} {1} will be {2}'.format(resource, name, test) |
| 308 | else: |
| 309 | changes_dict['comment'] = \ |
| 310 | '{0} {1} is in correct state'.format(resource, name) |
Damian Szeluga | 5dca0f0 | 2017-04-13 17:27:15 +0200 | [diff] [blame] | 311 | return changes_dict |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 312 | |
Elena Ezhova | c1bcbfa | 2017-07-13 16:13:43 +0400 | [diff] [blame] | 313 | |
| 314 | def _create_failed(name, resource): |
| 315 | changes_dict = {'name': name, |
| 316 | 'changes': {}, |
| 317 | 'comment': '{0} {1} failed to create'.format(resource, |
| 318 | name), |
| 319 | 'result': False} |
| 320 | return changes_dict |