Download deployment config after modification
[fuel.git] / deploy / cloud / deployment.py
1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
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
11 import time
12 import re
13
14 from common import (
15     N,
16     E,
17     exec_cmd,
18     run_proc,
19     parse,
20     err,
21     log,
22     delete,
23 )
24
25 SEARCH_TEXT = '(err)'
26 LOG_FILE = '/var/log/puppet.log'
27 GREP_LINES_OF_LEADING_CONTEXT = 100
28 GREP_LINES_OF_TRAILING_CONTEXT = 100
29 LIST_OF_CHAR_TO_BE_ESCAPED = ['[', ']', '"']
30
31 class Deployment(object):
32
33     def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict,
34                  no_health_check, deploy_timeout):
35         self.dea = dea
36         self.yaml_config_dir = yaml_config_dir
37         self.env_id = env_id
38         self.node_id_roles_dict = node_id_roles_dict
39         self.no_health_check = no_health_check
40         self.deploy_timeout = deploy_timeout
41         self.pattern = re.compile(
42             '\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d')
43
44     def collect_error_logs(self):
45         for node_id, roles_blade in self.node_id_roles_dict.iteritems():
46             log_list = []
47             cmd = ('ssh -q node-%s grep \'"%s"\' %s'
48                    % (node_id, SEARCH_TEXT, LOG_FILE))
49             results, _ = exec_cmd(cmd, False)
50             for result in results.splitlines():
51                 log_msg = ''
52
53                 sub_cmd = '"%s" %s' % (result, LOG_FILE)
54                 for c in LIST_OF_CHAR_TO_BE_ESCAPED:
55                     sub_cmd = sub_cmd.replace(c, '\%s' % c)
56                 grep_cmd = ('grep -B%s %s'
57                             % (GREP_LINES_OF_LEADING_CONTEXT, sub_cmd))
58                 cmd = ('ssh -q node-%s "%s"' % (node_id, grep_cmd))
59
60                 details, _ = exec_cmd(cmd, False)
61                 details_list = details.splitlines()
62
63                 found_prev_log = False
64                 for i in range(len(details_list) - 2, -1, -1):
65                     if self.pattern.match(details_list[i]):
66                         found_prev_log = True
67                         break
68                 if found_prev_log:
69                     log_msg += '\n'.join(details_list[i:-1]) + '\n'
70
71                 grep_cmd = ('grep -A%s %s'
72                             % (GREP_LINES_OF_TRAILING_CONTEXT, sub_cmd))
73                 cmd = ('ssh -q node-%s "%s"' % (node_id, grep_cmd))
74
75                 details, _ = exec_cmd(cmd, False)
76                 details_list = details.splitlines()
77
78                 found_next_log = False
79                 for i in range(1, len(details_list)):
80                     if self.pattern.match(details_list[i]):
81                         found_next_log = True
82                         break
83                 if found_next_log:
84                     log_msg += '\n'.join(details_list[:i])
85                 else:
86                     log_msg += details
87
88                 if log_msg:
89                    log_list.append(log_msg)
90
91             if log_list:
92                 role = ('controller' if 'controller' in roles_blade[0]
93                         else 'compute host')
94                 log('_' * 40 + 'Errors in node-%s %s' % (node_id, role)
95                     + '_' * 40)
96                 for log_msg in log_list:
97                     print(log_msg + '\n')
98
99     def run_deploy(self):
100         SLEEP_TIME = 60
101         LOG_FILE = 'cloud.log'
102
103         log('Starting deployment of environment %s' % self.env_id)
104         p = run_proc('fuel --env %s deploy-changes | strings > %s'
105                      % (self.env_id, LOG_FILE))
106
107         ready = False
108         for i in range(int(self.deploy_timeout)):
109             env = parse(exec_cmd('fuel env --env %s' % self.env_id))
110             log('Environment status: %s' % env[0][E['status']])
111             r, _ = exec_cmd('tail -2 %s | head -1' % LOG_FILE, False)
112             if r:
113                 log(r)
114             if env[0][E['status']] == 'operational':
115                 ready = True
116                 break
117             elif (env[0][E['status']] == 'error'
118                   or env[0][E['status']] == 'stopped'):
119                 break
120             else:
121                 time.sleep(SLEEP_TIME)
122
123         p.poll()
124         if p.returncode == None:
125             log('The process deploying the changes has not yet finished.')
126             log('''The file %s won't be deleted''' % LOG_FILE)
127         else:
128             delete(LOG_FILE)
129
130         if ready:
131             log('Environment %s successfully deployed' % self.env_id)
132         else:
133             self.collect_error_logs()
134             err('Deployment failed, environment %s is not operational'
135                 % self.env_id)
136
137     def verify_node_status(self):
138         node_list = parse(exec_cmd('fuel node list'))
139         failed_nodes = []
140         for node in node_list:
141             if node[N['status']] != 'ready' and node[N['cluster']] != 'None':
142                 failed_nodes.append((node[N['id']], node[N['status']]))
143
144         if failed_nodes:
145             summary = ''
146             for node, status in failed_nodes:
147                 summary += '[node %s, status %s]\n' % (node, status)
148             err('Deployment failed: %s' % summary)
149
150     def health_check(self):
151         log('Now running sanity and smoke health checks')
152         r = exec_cmd('fuel health --env %s --check sanity,smoke --force'
153                      % self.env_id)
154         log(r)
155         if 'failure' in r:
156             err('Healthcheck failed!')
157
158     def deploy(self):
159         self.run_deploy()
160         self.verify_node_status()
161         if not self.no_health_check:
162             self.health_check()