Documents up-to-date
[doctor.git] / doctor_tests / stack.py
1 ##############################################################################
2 # Copyright (c) 2018 Nokia Corporation 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 import os
10 import time
11
12 from heatclient.common.template_utils import get_template_contents
13 from heatclient import exc as heat_excecption
14
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
18
19
20 class Stack(object):
21
22     def __init__(self, conf, log):
23         self.conf = conf
24         self.log = 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
29         self.stack_id = None
30         self.template = None
31         self.parameters = {}
32         self.files = {}
33
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)
40
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
45
46         status = action_in_progress
47         stack_retries = 160
48         while status == action_in_progress and stack_retries > 0:
49             time.sleep(2)
50             try:
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
56                     break
57                 else:
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)
68         else:
69             self.log.error('stack %s %s' % (self.stack_name, status))
70             raise Exception("stack %s unknown result" % action)
71
72     def wait_stack_delete(self):
73         self._wait_stack_action_complete('DELETE')
74
75     def wait_stack_create(self):
76         self._wait_stack_action_complete('CREATE')
77
78     def wait_stack_update(self):
79         self._wait_stack_action_complete('UPDATE')
80
81     def create(self, stack_name, template, parameters={}, files={}):
82         self.stack_name = stack_name
83         self.template = template
84         self.parameters = parameters
85         self.files = files
86         stack = self.heat.stacks.create(stack_name=self.stack_name,
87                                         files=files,
88                                         template=template,
89                                         parameters=parameters)
90         self.stack_id = stack['stack']['id']
91         try:
92             self.wait_stack_create()
93         except Exception:
94             # It might not always work at first
95             self.log.info('retry creating maintenance stack.......')
96             self.delete()
97             time.sleep(5)
98             stack = self.heat.stacks.create(stack_name=self.stack_name,
99                                             files=files,
100                                             template=template,
101                                             parameters=parameters)
102             self.stack_id = stack['stack']['id']
103             self.wait_stack_create()
104
105     def update(self, stack_name, stack_id, template, parameters={}, files={}):
106         self.heat.stacks.update(stack_name=stack_name,
107                                 stack_id=stack_id,
108                                 files=files,
109                                 template=template,
110                                 parameters=parameters)
111         self.wait_stack_update()
112
113     def delete(self):
114         if self.stack_id is not None:
115             self.heat.stacks.delete(self.stack_name)
116             self.wait_stack_delete()
117         else:
118             self.log.info('no stack to delete')