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) |
| 71 | except: |
| 72 | # nova is not installed |
| 73 | ret = _no_change('api_db --version', None, test=True) |
| 74 | return ret |
| 75 | if api_db_version < version: |
| 76 | try: |
| 77 | __salt__['cmd.shell']('nova-manage api_db sync --version ' + version) |
| 78 | ret['result'] = True |
| 79 | ret['comment'] = 'Nova-manage api_db sync --version {0} was successfuly executed'.format(version) |
| 80 | ret['changes']['api_db'] = 'api_db sync --version {0}'.format(version) |
| 81 | except: |
| 82 | ret['result'] = False |
| 83 | ret['comment'] = 'Error while executing nova-manage api_db sync --version {0}'.format(version) |
| 84 | ret['changes']['api_db'] = 'Failed to execute api_db sync --version {0}'.format(version) |
| 85 | return ret |
| 86 | |
| 87 | |
| 88 | def db_version_present(name=None, version="334"): |
| 89 | ''' |
| 90 | Ensures that specific api_db version is present |
| 91 | ''' |
| 92 | ret = {'name': 'db --version', |
| 93 | 'changes': {}, |
| 94 | 'result': True, |
| 95 | 'comment': 'Current db version is not < than "{0}".'.format(version)} |
| 96 | db_version = __salt__['cmd.shell']('nova-manage db version 2>/dev/null') |
| 97 | try: |
| 98 | db_version = int(db_version) |
| 99 | except: |
| 100 | # nova is not installed |
| 101 | ret = _no_change('db --version', None, test=True) |
| 102 | return ret |
| 103 | |
| 104 | if db_version < version: |
| 105 | try: |
| 106 | __salt__['cmd.shell']('nova-manage db sync --version ' + version) |
| 107 | ret['result'] = True |
| 108 | ret['comment'] = 'Nova-manage db sync --version {0} was successfuly executed'.format(version) |
| 109 | ret['changes']['db'] = 'db sync --version {0}'.format(version) |
| 110 | except: |
| 111 | ret['result'] = False |
| 112 | ret['comment'] = 'Error while executing nova-manage db sync --version {0}'.format(version) |
| 113 | ret['changes']['db'] = 'Failed to execute db sync --version {0}'.format(version) |
| 114 | return ret |
| 115 | |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 116 | def quota_present(tenant_name, profile, name=None, **kwargs): |
| 117 | ''' |
| 118 | Ensures that the nova quota exists |
| 119 | ''' |
| 120 | changes = {} |
| 121 | for key, value in kwargs.items(): |
| 122 | quota = __salt__['novang.quota_get'](key, tenant_name, profile) |
| 123 | if quota != value: |
| 124 | arg = {} |
| 125 | arg[key] = value |
| 126 | changes[key] = value |
| 127 | __salt__['novang.quota_update'](tenant_name, profile, **arg) |
| 128 | if bool(changes): |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 129 | return _updated(tenant_name, 'tenant', changes) |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 130 | else: |
| 131 | return _no_change(tenant_name, 'tenant') |
| 132 | |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 133 | |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 134 | def availability_zone_present(name=None, availability_zone=None, profile=None): |
| 135 | ''' |
| 136 | Ensures that the nova availability zone exists |
| 137 | ''' |
| 138 | name = availability_zone |
| 139 | zone_exists = __salt__['novang.availability_zone_get'](name, profile) |
| 140 | if zone_exists == False: |
| 141 | item_created = __salt__['novang.availability_zone_create'](name, availability_zone, profile) |
| 142 | if bool(item_created): |
| 143 | return _created(availability_zone, 'availabilty zone', item_created) |
Jiri Broulik | a2c7929 | 2017-02-05 21:01:38 +0100 | [diff] [blame] | 144 | else: |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 145 | return _already_exists(availability_zone, 'availabilty zone') |
| 146 | return existing_availability_zones |
| 147 | |
Damian Szeluga | 5dca0f0 | 2017-04-13 17:27:15 +0200 | [diff] [blame] | 148 | def aggregate_present(name=None, aggregate=None, profile=None): |
| 149 | ''' |
| 150 | Ensures that the nova aggregate exists |
| 151 | ''' |
| 152 | name = aggregate |
| 153 | aggregate_exists = __salt__['novang.aggregate_get'](name, profile) |
| 154 | if aggregate_exists == False: |
| 155 | item_created = __salt__['novang.aggregate_create'](name, aggregate, profile) |
| 156 | if bool(item_created): |
| 157 | return _created(aggregate, 'aggregate', item_created) |
| 158 | else: |
| 159 | return _already_exists(aggregate, 'aggregate') |
| 160 | return existing_aggregate |
| 161 | |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 162 | |
| 163 | def instance_present(name, flavor, image, networks, security_groups=None, profile=None, tenant_name=None): |
| 164 | ret = {'name': name, |
| 165 | 'changes': {}, |
| 166 | 'result': True, |
| 167 | 'comment': 'Instance "{0}" already exists'.format(name)} |
| 168 | kwargs = {} |
| 169 | nics = [] |
| 170 | existing_instances = __salt__['novang.server_list'](profile, tenant_name) |
| 171 | if name in existing_instances: |
| 172 | return ret |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 173 | existing_flavors = __salt__['novang.flavor_list'](profile) |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 174 | if flavor in existing_flavors: |
Ondrej Smola | b7b0dda | 2017-02-28 14:36:46 +0100 | [diff] [blame] | 175 | flavor_id = existing_flavors[flavor]['id'] |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 176 | else: |
| 177 | return {'name': name, |
| 178 | 'changes': {}, |
| 179 | 'result': False, |
| 180 | 'comment': 'Flavor "{0}" doesn\'t exists'.format(flavor)} |
| 181 | |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 182 | existing_image = __salt__['novang.image_list'](image, profile) |
Richard Felkl | 55d1f57 | 2017-02-15 16:41:53 +0100 | [diff] [blame] | 183 | if not existing_image: |
| 184 | return {'name': name, |
| 185 | 'changes': {}, |
| 186 | 'result': False, |
| 187 | 'comment': 'Image "{0}" doesn\'t exists'.format(image)} |
| 188 | else: |
| 189 | image_id = existing_image.get(image).get('id') |
| 190 | if security_groups is not None: |
| 191 | kwargs['security_groups'] = [] |
| 192 | for secgroup in security_groups: |
| 193 | existing_secgroups = __salt__['novang.secgroup_list'](profile, tenant_name) |
| 194 | if not secgroup in existing_secgroups: |
| 195 | return {'name': name, |
| 196 | 'changes': {}, |
| 197 | 'result': False, |
| 198 | 'comment': 'Security group "{0}" doesn\'t exists'.format(secgroup)} |
| 199 | else: |
| 200 | kwargs['security_groups'].append(secgroup) |
| 201 | for net in networks: |
| 202 | existing_network = __salt__['novang.network_show'](net.get('name'), profile) |
| 203 | if not existing_network: |
| 204 | return {'name': name, |
| 205 | 'changes': {}, |
| 206 | 'result': False, |
| 207 | 'comment': 'Network "{0}" doesn\'t exists'.format(net.get(name))} |
| 208 | else: |
| 209 | network_id = existing_network.get('id') |
| 210 | if net.get('v4_fixed_ip') is not None: |
| 211 | nics.append({'net-id': network_id, 'v4-fixed-ip': net.get('v4_fixed_ip')}) |
| 212 | else: |
| 213 | nics.append({'net-id': network_id}) |
| 214 | kwargs['nics'] = nics |
| 215 | new_instance_id = __salt__['novang.boot'] (name, flavor_id, image_id, profile, tenant_name, **kwargs) |
| 216 | return {'name': name, |
| 217 | 'changes': {}, |
| 218 | 'result': True, |
| 219 | 'comment': 'Instance "{0}" was successfuly created'.format(name)} |
Jiri Broulik | 70d9e3f | 2017-02-15 18:37:13 +0100 | [diff] [blame] | 220 | |
| 221 | def _already_exists(name, resource): |
| 222 | changes_dict = {'name': name, |
| 223 | 'changes': {}, |
| 224 | 'result': True} |
| 225 | changes_dict['comment'] = \ |
| 226 | '{0} {1} already exists'.format(resource, name) |
| 227 | return changes_dict |
| 228 | |
| 229 | |
| 230 | def _created(name, resource, resource_definition): |
| 231 | changes_dict = {'name': name, |
| 232 | 'changes': resource_definition, |
| 233 | 'result': True, |
| 234 | 'comment': '{0} {1} created'.format(resource, name)} |
| 235 | return changes_dict |
| 236 | |
| 237 | def _updated(name, resource, resource_definition): |
| 238 | changes_dict = {'name': name, |
| 239 | 'changes': resource_definition, |
| 240 | 'result': True, |
| 241 | 'comment': '{0} {1} tenant was updated'.format(resource, name)} |
| 242 | return changes_dict |
| 243 | |
| 244 | def _update_failed(name, resource): |
| 245 | changes_dict = {'name': name, |
| 246 | 'changes': {}, |
| 247 | 'comment': '{0} {1} failed to update'.format(resource, name), |
| 248 | 'result': False} |
| 249 | return changes_dict |
| 250 | |
| 251 | def _no_change(name, resource, test=False): |
| 252 | changes_dict = {'name': name, |
| 253 | 'changes': {}, |
| 254 | 'result': True} |
| 255 | if test: |
| 256 | changes_dict['comment'] = \ |
| 257 | '{0} {1} will be {2}'.format(resource, name, test) |
| 258 | else: |
| 259 | changes_dict['comment'] = \ |
| 260 | '{0} {1} is in correct state'.format(resource, name) |
Damian Szeluga | 5dca0f0 | 2017-04-13 17:27:15 +0200 | [diff] [blame] | 261 | return changes_dict |
Adam Tengler | e8afccc | 2017-06-27 17:57:21 +0000 | [diff] [blame] | 262 | |