cc652fb491f1ccf2785a19466374d688a36e38a3
[bottlenecks.git] / utils / infra_setup / heat / template.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 """Heat template and stack management"""
11
12 import time
13 import sys
14 import logging
15
16 from heatclient import client as heatclient
17 import common as op_utils
18
19
20 log = logging.getLogger(__name__)
21
22
23 class HeatObject(object):
24     ''' base class for template and stack'''
25     def __init__(self):
26         self._heat_client = None
27         self.uuid = None
28
29     def _get_heat_client(self):
30         '''returns a heat client instance'''
31
32         if self._heat_client is None:
33             sess = op_utils.get_session()
34             heat_endpoint = op_utils.get_endpoint(service_type='orchestration')
35             self._heat_client = heatclient.Client(
36                 op_utils.get_heat_api_version(),
37                 endpoint=heat_endpoint, session=sess)
38
39         return self._heat_client
40
41     def status(self):
42         '''returns stack state as a string'''
43         heat = self._get_heat_client()
44         stack = heat.stacks.get(self.uuid)
45         return getattr(stack, 'stack_status')
46
47
48 class HeatStack(HeatObject):
49     ''' Represents a Heat stack (deployed template) '''
50     stacks = []
51
52     def __init__(self, name):
53         super(HeatStack, self).__init__()
54         self.uuid = None
55         self.name = name
56         self.outputs = None
57         HeatStack.stacks.append(self)
58
59     @staticmethod
60     def stacks_exist():
61         '''check if any stack has been deployed'''
62         return len(HeatStack.stacks) > 0
63
64     def _delete(self):
65         '''deletes a stack from the target cloud using heat'''
66         if self.uuid is None:
67             return
68
69         log.info("Deleting stack '%s', uuid:%s", self.name, self.uuid)
70         heat = self._get_heat_client()
71         template = heat.stacks.get(self.uuid)
72         start_time = time.time()
73         template.delete()
74         status = self.status()
75
76         while status != u'DELETE_COMPLETE':
77             log.debug("stack state %s", status)
78             if status == u'DELETE_FAILED':
79                 raise RuntimeError(
80                     heat.stacks.get(self.uuid).stack_status_reason)
81
82             time.sleep(2)
83             status = self.status()
84
85         end_time = time.time()
86         log.info("Deleted stack '%s' in %d secs", self.name,
87                  end_time - start_time)
88         self.uuid = None
89
90     def delete(self, block=True, retries=3):
91         '''deletes a stack in the target cloud using heat (with retry)
92         Sometimes delete fail with "InternalServerError" and the next attempt
93         succeeds. So it is worthwhile to test a couple of times.
94         '''
95         if self.uuid is None:
96             return
97
98         if not block:
99             try:
100                 self._delete()
101             except RuntimeError as err:
102                 log.warn(err.args)
103             HeatStack.stacks.remove(self)
104             return
105
106         i = 0
107         while i < retries:
108             try:
109                 self._delete()
110                 break
111             except RuntimeError as err:
112                 log.warn(err.args)
113                 time.sleep(2)
114             i += 1
115
116         if self.uuid is not None:
117             sys.exit("delete stack failed!!!")
118         else:
119             HeatStack.stacks.remove(self)
120
121     @staticmethod
122     def delete_all():
123         for stack in HeatStack.stacks[:]:
124             stack.delete()
125
126     def update(self):
127         '''update a stack'''
128         pass
129
130
131 class HeatTemplate(HeatObject):
132     '''Describes a Heat template and a method to deploy template to a stack'''
133
134     def __init__(self, name, template_file=None, heat_parameters=None):
135         super(HeatTemplate, self).__init__()
136         self.name = name
137         self.state = "NOT_CREATED"
138         self.keystone_client = None
139         self.heat_client = None
140         self.heat_parameters = {}
141
142         # heat_parameters is passed to heat in stack create, empty dict when
143         # yardstick creates the template (no get_param in resources part)
144         if heat_parameters:
145             self.heat_parameters = heat_parameters
146
147         if template_file:
148             with open(template_file) as template:
149                 print "Parsing external template:", template_file
150                 template_str = template.read()
151                 self._template = template_str
152             self._parameters = heat_parameters
153         else:
154             sys.exit("\nno such template file.")
155
156         # holds results of requested output after deployment
157         self.outputs = {}
158
159         log.debug("template object '%s' created", name)
160
161     def create(self, block=True):
162         '''creates a template in the target cloud using heat
163         returns a dict with the requested output values from the template'''
164         log.info("Creating stack '%s'", self.name)
165
166         # create stack early to support cleanup, e.g. ctrl-c while waiting
167         stack = HeatStack(self.name)
168
169         heat = self._get_heat_client()
170         end_time = start_time = time.time()
171         print(self._template)
172         stack.uuid = self.uuid = heat.stacks.create(
173             stack_name=self.name, template=self._template,
174             parameters=self.heat_parameters)['stack']['id']
175
176         status = self.status()
177
178         if block:
179             while status != u'CREATE_COMPLETE':
180                 log.debug("stack state %s", status)
181                 if status == u'CREATE_FAILED':
182                     raise RuntimeError(getattr(heat.stacks.get(self.uuid),
183                                                'stack_status_reason'))
184
185                 time.sleep(2)
186                 status = self.status()
187
188             end_time = time.time()
189             outputs = getattr(heat.stacks.get(self.uuid), 'outputs')
190
191         for output in outputs:
192             self.outputs[output["output_key"].encode("ascii")] = \
193                 output["output_value"].encode("ascii")
194
195         log.info("Created stack '%s' in %d secs",
196                  self.name, end_time - start_time)
197
198         stack.outputs = self.outputs
199         return stack