1 ##############################################################################
2 # Copyright (c) 2018 Nokia Corporation and others.
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 ##############################################################################
12 from heatclient.common.template_utils import get_template_contents
13 from heatclient import exc as heat_excecption
15 from doctor_tests.identity_auth import get_identity_auth
16 from doctor_tests.identity_auth import get_session
17 from doctor_tests.os_clients import heat_client
22 def __init__(self, conf, log):
25 auth = get_identity_auth(project=self.conf.doctor_project)
26 self.heat = heat_client(self.conf.heat_version,
27 get_session(auth=auth))
28 self.stack_name = None
34 # standard yaml.load will not work for hot tpl becasue of date format in
35 # heat_template_version is not string
36 def get_hot_tpl(self, template_file):
37 if not os.path.isfile(template_file):
38 raise Exception('File(%s) does not exist' % template_file)
39 return get_template_contents(template_file=template_file)
41 def _wait_stack_action_complete(self, action):
42 action_in_progress = '%s_IN_PROGRESS' % action
43 action_complete = '%s_COMPLETE' % action
44 action_failed = '%s_FAILED' % action
46 status = action_in_progress
48 while status == action_in_progress and stack_retries > 0:
51 stack = self.heat.stacks.get(self.stack_name)
52 except heat_excecption.HTTPNotFound:
53 if action == 'DELETE':
54 # Might happen you never get status as stack deleted
55 status = action_complete
58 raise Exception('unable to get stack')
59 status = stack.stack_status
60 stack_retries = stack_retries - 1
61 if stack_retries == 0 and status != action_complete:
62 raise Exception("stack %s not completed within 5min, status:"
63 " %s" % (action, status))
64 elif status == action_complete:
65 self.log.info('stack %s %s' % (self.stack_name, status))
66 elif status == action_failed:
67 raise Exception("stack %s failed" % action)
69 self.log.error('stack %s %s' % (self.stack_name, status))
70 raise Exception("stack %s unknown result" % action)
72 def wait_stack_delete(self):
73 self._wait_stack_action_complete('DELETE')
75 def wait_stack_create(self):
76 self._wait_stack_action_complete('CREATE')
78 def wait_stack_update(self):
79 self._wait_stack_action_complete('UPDATE')
81 def create(self, stack_name, template, parameters={}, files={}):
82 self.stack_name = stack_name
83 self.template = template
84 self.parameters = parameters
86 stack = self.heat.stacks.create(stack_name=self.stack_name,
89 parameters=parameters)
90 self.stack_id = stack['stack']['id']
92 self.wait_stack_create()
94 # It might not always work at first
95 self.log.info('retry creating maintenance stack.......')
98 stack = self.heat.stacks.create(stack_name=self.stack_name,
101 parameters=parameters)
102 self.stack_id = stack['stack']['id']
103 self.wait_stack_create()
105 def update(self, stack_name, stack_id, template, parameters={}, files={}):
106 self.heat.stacks.update(stack_name=stack_name,
110 parameters=parameters)
111 self.wait_stack_update()
114 if self.stack_id is not None:
115 self.heat.stacks.delete(self.stack_name)
116 self.wait_stack_delete()
118 self.log.info('no stack to delete')