summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/heat_utils.py
blob: 6e54dc6973858d4dad6d335610cd383c6a992e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os

import yaml
from heatclient.client import Client
from heatclient.common.template_format import yaml_loader
from novaclient.exceptions import NotFound
from oslo_serialization import jsonutils

from snaps import file_utils
from snaps.domain.stack import Stack, Resource, Output

from snaps.openstack.utils import (
    keystone_utils, neutron_utils, nova_utils, cinder_utils)

__author__ = 'spisarski'

logger = logging.getLogger('heat_utils')


def heat_client(os_creds):
    """
    Retrieves the Heat client
    :param os_creds: the OpenStack credentials
    :return: the client
    """
    logger.debug('Retrieving Heat Client')
    return Client(os_creds.heat_api_version,
                  session=keystone_utils.keystone_session(os_creds),
                  region_name=os_creds.region_name)


def get_stack(heat_cli, stack_settings=None, stack_name=None):
    """
    Returns the first domain Stack object found. When stack_setting
    is not None, the filter created will take the name attribute. When
    stack_settings is None and stack_name is not, stack_name will be used
    instead. When both are None, the first stack object received will be
    returned, else None
    :param heat_cli: the OpenStack heat client
    :param stack_settings: a StackSettings object
    :param stack_name: the name of the heat stack to return
    :return: the Stack domain object else None
    """

    stack_filter = dict()
    if stack_settings:
        stack_filter['stack_name'] = stack_settings.name
    elif stack_name:
        stack_filter['stack_name'] = stack_name

    stacks = heat_cli.stacks.list(**stack_filter)
    for stack in stacks:
        return Stack(name=stack.identifier, stack_id=stack.id)


def get_stack_by_id(heat_cli, stack_id):
    """
    Returns a domain Stack object for a given ID
    :param heat_cli: the OpenStack heat client
    :param stack_id: the ID of the heat stack to retrieve
    :return: the Stack domain object else None
    """
    stack = heat_cli.stacks.get(stack_id)
    return Stack(name=stack.identifier, stack_id=stack.id)


def get_stack_status(heat_cli, stack_id):
    """
    Returns the current status of the Heat stack
    :param heat_cli: the OpenStack heat client
    :param stack_id: the ID of the heat stack to retrieve
    :return:
    """
    return heat_cli.stacks.get(stack_id).stack_status


def get_stack_status_reason(heat_cli, stack_id):
    """
    Returns the current status of the Heat stack
    :param heat_cli: the OpenStack heat client
    :param stack_id: the ID of the heat stack to retrieve
    :return: reason for stack creation failure
    """
    return heat_cli.stacks.get(stack_id).stack_status_reason


def create_stack(heat_cli, stack_settings):
    """
    Executes an Ansible playbook to the given host
    :param heat_cli: the OpenStack heat client object
    :param stack_settings: the stack configuration
    :return: the Stack domain object
    """
    args = dict()

    if stack_settings.template:
        args['template'] = stack_settings.template
    else:
        args['template'] = parse_heat_template_str(
            file_utils.read_file(stack_settings.template_path))
    args['stack_name'] = stack_settings.name

    if stack_settings.env_values:
        args['parameters'] = stack_settings.env_values

    if stack_settings.resource_files:
        resources = dict()
        for res_file in stack_settings.resource_files:
            heat_resource_contents = file_utils.read_file(res_file)
            base_filename = os.path.basename(res_file)

            if heat_resource_contents and base_filename:
                resources[base_filename] = heat_resource_contents
        args['files'] = resources

    stack = heat_cli.stacks.create(**args)

    return get_stack_by_id(heat_cli, stack_id=stack['stack']['id'])


def delete_stack(heat_cli, stack):
    """
    Deletes the Heat stack
    :param heat_cli: the OpenStack heat client object
    :param stack: the OpenStack Heat stack object
    """
    heat_cli.stacks.delete(stack.id)


def __get_os_resources(heat_cli, res_id):
    """
    Returns all of the OpenStack resource objects for a given stack
    :param heat_cli: the OpenStack heat client
    :param res_id: the resource ID
    :return: a list
    """
    return heat_cli.resources.list(res_id)


def get_resources(heat_cli, res_id, res_type=None):
    """
    Returns all of the OpenStack resource objects for a given stack
    :param heat_cli: the OpenStack heat client
    :param res_id: the SNAPS-OO Stack domain object
    :param res_type: the type name to filter
    :return: a list of Resource domain objects
    """
    os_resources = __get_os_resources(heat_cli, res_id)

    if os_resources:
        out = list()
        for os_resource in os_resources:
            if ((res_type and os_resource.resource_type == res_type)
                    or not res_type):
                out.append(Resource(
                    name=os_resource.resource_name,
                    resource_type=os_resource.resource_type,
                    resource_id=os_resource.physical_resource_id,
                    status=os_resource.resource_status,
                    status_reason=os_resource.resource_status_reason))
        return out


def get_outputs(heat_cli, stack):
    """
    Returns all of the SNAPS-OO Output domain objects for the defined outputs
    for given stack
    :param heat_cli: the OpenStack heat client
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of Output domain objects
    """
    out = list()

    os_stack = heat_cli.stacks.get(stack.id)

    outputs = None
    if os_stack:
        outputs = os_stack.outputs

    if outputs:
        for output in outputs:
            out.append(Output(**output))

    return out


def get_stack_networks(heat_cli, neutron, stack):
    """
    Returns a list of Network domain objects deployed by this stack
    :param heat_cli: the OpenStack heat client object
    :param neutron: the OpenStack neutron client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of Network objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Neutron::Net')
    for resource in resources:
        network = neutron_utils.get_network_by_id(neutron, resource.id)
        if network:
            out.append(network)

    return out


def get_stack_routers(heat_cli, neutron, stack):
    """
    Returns a list of Network domain objects deployed by this stack
    :param heat_cli: the OpenStack heat client object
    :param neutron: the OpenStack neutron client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of Network objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Neutron::Router')
    for resource in resources:
        router = neutron_utils.get_router_by_id(neutron, resource.id)
        if router:
            out.append(router)

    return out


def get_stack_security_groups(heat_cli, neutron, stack):
    """
    Returns a list of SecurityGroup domain objects deployed by this stack
    :param heat_cli: the OpenStack heat client object
    :param neutron: the OpenStack neutron client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of SecurityGroup objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Neutron::SecurityGroup')
    for resource in resources:
        security_group = neutron_utils.get_security_group_by_id(
            neutron, resource.id)
        if security_group:
            out.append(security_group)

    return out


def get_stack_servers(heat_cli, nova, neutron, stack, project_id):
    """
    Returns a list of VMInst domain objects associated with a Stack
    :param heat_cli: the OpenStack heat client object
    :param nova: the OpenStack nova client object
    :param neutron: the OpenStack neutron client object
    :param stack: the SNAPS-OO Stack domain object
    :param project_id: the associated project ID
    :return: a list of VMInst domain objects
    """

    out = list()
    srvr_res = get_resources(heat_cli, stack.id, 'OS::Nova::Server')
    for resource in srvr_res:
        try:
            server = nova_utils.get_server_object_by_id(
                nova, neutron, resource.id, project_id)
            if server:
                out.append(server)
        except NotFound:
            logger.warn('VmInst cannot be located with ID %s', resource.id)

    res_grps = get_resources(heat_cli, stack.id, 'OS::Heat::ResourceGroup')
    for res_grp in res_grps:
        res_ress = get_resources(heat_cli, res_grp.id)
        for res_res in res_ress:
            res_res_srvrs = get_resources(
                heat_cli, res_res.id, 'OS::Nova::Server')
            for res_srvr in res_res_srvrs:
                server = nova_utils.get_server_object_by_id(
                    nova, neutron, res_srvr.id, project_id)
                if server:
                    out.append(server)

    return out


def get_stack_keypairs(heat_cli, nova, stack):
    """
    Returns a list of Keypair domain objects associated with a Stack
    :param heat_cli: the OpenStack heat client object
    :param nova: the OpenStack nova client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of VMInst domain objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Nova::KeyPair')
    for resource in resources:
        try:
            keypair = nova_utils.get_keypair_by_id(nova, resource.id)
            if keypair:
                out.append(keypair)
        except NotFound:
            logger.warn('Keypair cannot be located with ID %s', resource.id)

    return out


def get_stack_volumes(heat_cli, cinder, stack):
    """
    Returns an instance of Volume domain objects created by this stack
    :param heat_cli: the OpenStack heat client object
    :param cinder: the OpenStack cinder client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of Volume domain objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Cinder::Volume')
    for resource in resources:
        try:
            server = cinder_utils.get_volume_by_id(cinder, resource.id)
            if server:
                out.append(server)
        except NotFound:
            logger.warn('Volume cannot be located with ID %s', resource.id)

    return out


def get_stack_volume_types(heat_cli, cinder, stack):
    """
    Returns an instance of VolumeType domain objects created by this stack
    :param heat_cli: the OpenStack heat client object
    :param cinder: the OpenStack cinder client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of VolumeType domain objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Cinder::VolumeType')
    for resource in resources:
        try:
            vol_type = cinder_utils.get_volume_type_by_id(cinder, resource.id)
            if vol_type:
                out.append(vol_type)
        except NotFound:
            logger.warn('VolumeType cannot be located with ID %s', resource.id)

    return out


def get_stack_flavors(heat_cli, nova, stack):
    """
    Returns an instance of Flavor SNAPS domain object for each flavor created
    by this stack
    :param heat_cli: the OpenStack heat client object
    :param nova: the OpenStack cinder client object
    :param stack: the SNAPS-OO Stack domain object
    :return: a list of Volume domain objects
    """

    out = list()
    resources = get_resources(heat_cli, stack.id, 'OS::Nova::Flavor')
    for resource in resources:
        try:
            flavor = nova_utils.get_flavor_by_id(nova, resource.id)
            if flavor:
                out.append(flavor)
        except NotFound:
            logger.warn('Flavor cannot be located with ID %s', resource.id)

    return out


def parse_heat_template_str(tmpl_str):
    """
    Takes a heat template string, performs some simple validation and returns a
    dict containing the parsed structure. This function supports both JSON and
    YAML Heat template formats.
    """
    if tmpl_str.startswith('{'):
        tpl = jsonutils.loads(tmpl_str)
    else:
        try:
            tpl = yaml.load(tmpl_str, Loader=yaml_loader)
        except yaml.YAMLError as yea:
            raise ValueError(yea)
        else:
            if tpl is None:
                tpl = {}
    # Looking for supported version keys in the loaded template
    if not ('HeatTemplateFormatVersion' in tpl or
            'heat_template_version' in tpl or
            'AWSTemplateFormatVersion' in tpl):
        raise ValueError("Template format version not found.")
    return tpl