blob: 3433d16f672b749b59f8c56280d4393e9af31309 [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)}
46 cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>&- | grep ' + name + ' | tr -d \"\n\" | awk \'{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 Broulik91104db2017-07-07 08:50:44 +020060def 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
88def 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 Broulika2c79292017-02-05 21:01:38 +0100116def 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 Broulik70d9e3f2017-02-15 18:37:13 +0100129 return _updated(tenant_name, 'tenant', changes)
Jiri Broulika2c79292017-02-05 21:01:38 +0100130 else:
131 return _no_change(tenant_name, 'tenant')
132
Jiri Broulika2c79292017-02-05 21:01:38 +0100133
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100134def 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 Broulika2c79292017-02-05 21:01:38 +0100144 else:
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100145 return _already_exists(availability_zone, 'availabilty zone')
146 return existing_availability_zones
147
Damian Szeluga5dca0f02017-04-13 17:27:15 +0200148def 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 Felkl55d1f572017-02-15 16:41:53 +0100162
163def 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 Tenglere8afccc2017-06-27 17:57:21 +0000173 existing_flavors = __salt__['novang.flavor_list'](profile)
Richard Felkl55d1f572017-02-15 16:41:53 +0100174 if flavor in existing_flavors:
Ondrej Smolab7b0dda2017-02-28 14:36:46 +0100175 flavor_id = existing_flavors[flavor]['id']
Richard Felkl55d1f572017-02-15 16:41:53 +0100176 else:
177 return {'name': name,
178 'changes': {},
179 'result': False,
180 'comment': 'Flavor "{0}" doesn\'t exists'.format(flavor)}
181
Adam Tenglere8afccc2017-06-27 17:57:21 +0000182 existing_image = __salt__['novang.image_list'](image, profile)
Richard Felkl55d1f572017-02-15 16:41:53 +0100183 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 Broulik70d9e3f2017-02-15 18:37:13 +0100220
221def _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
230def _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
237def _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
244def _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
251def _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 Szeluga5dca0f02017-04-13 17:27:15 +0200261 return changes_dict
Adam Tenglere8afccc2017-06-27 17:57:21 +0000262