22fec13923afe400c27105fa4ccdcde7cfdf774d
[yardstick.git] / yardstick / vTC / apexlake / experimental_framework / deployment_unit.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import time
17
18 from experimental_framework import heat_manager
19 from experimental_framework import common
20
21 MAX_RETRY = 3
22
23
24 class DeploymentUnit:
25     """
26     This unit is in charge to manage the deployment of the workloads under
27     test and any other workloads necessary to
28     the benchmark
29     """
30
31     def __init__(self, openstack_credentials):
32         self.heat_manager = heat_manager.HeatManager(openstack_credentials)
33         self.deployed_stacks = list()
34
35     def destroy_heat_template(self, stack_name):
36         """
37         Destroys a stack
38         :param stack_name: Stack of the name to be destroyed (sting)
39         :return: None
40         """
41         try:
42             if self.heat_manager.check_stack_status(stack_name):
43                 if stack_name in self.deployed_stacks:
44                     self.deployed_stacks.remove(stack_name)
45                 self.heat_manager.delete_stack(stack_name)
46
47             status = self.heat_manager.check_stack_status(stack_name)
48             while status and 'DELETE_IN_PROGRESS' in status:
49                 common.LOG.info(status)
50                 time.sleep(5)
51                 status = self.heat_manager.check_stack_status(stack_name)
52             return True
53         except Exception as e:
54             common.LOG.debug(e.message)
55             return False
56
57     def destroy_all_deployed_stacks(self):
58         """
59         Destroys all the stacks currently deployed
60         :return: None
61         """
62         for stack in self.deployed_stacks:
63             if self.heat_manager.is_stack_deployed(stack):
64                 self.destroy_heat_template(stack)
65
66     def deploy_heat_template(self, template_file, stack_name, parameters,
67                              attempt=0):
68         """
69         Deploys a heat template and in case of failure retries 3 times
70         :param template_file: full path file name of the heat template
71         :param stack_name: name of the stack to deploy
72         :param parameters: parameters to be given to the heat template
73         :param attempt: number of current attempt
74         :return: returns True in case the creation is completed
75                  returns False in case the creation is failed
76         """
77         if not os.path.isfile(template_file):
78             raise ValueError('The specified file does not exist ("' +
79                              template_file + '")')
80         try:
81             self.heat_manager.create_stack(template_file, stack_name,
82                                            parameters)
83             deployed = True
84         except Exception as e:
85             common.LOG.debug(e.message)
86             deployed = False
87
88         if not deployed and 'COMPLETE' in \
89                 self.heat_manager.check_stack_status(stack_name):
90             try:
91                 self.destroy_heat_template(stack_name)
92             except Exception as e:
93                 common.LOG.debug(e.message)
94                 pass
95
96         status = self.heat_manager.check_stack_status(stack_name)
97         while status and 'CREATE_IN_PROGRESS' in status:
98             time.sleep(5)
99             status = self.heat_manager.check_stack_status(stack_name)
100         if status and ('FAILED' in status or 'NOT_FOUND' in status):
101             if attempt < MAX_RETRY:
102                 attempt += 1
103                 try:
104                     self.destroy_heat_template(stack_name)
105                 except Exception as e:
106                     common.LOG.debug(e.message)
107                     pass
108                 return self.deploy_heat_template(template_file, stack_name,
109                                                  parameters, attempt)
110             else:
111                 try:
112                     self.destroy_heat_template(stack_name)
113                 except Exception as e:
114                     common.LOG.debug(e.message)
115                 finally:
116                     return False
117         if self.heat_manager.check_stack_status(stack_name) and \
118             'COMPLETE' in self.heat_manager.\
119                 check_stack_status(stack_name):
120             self.deployed_stacks.append(stack_name)
121             return True