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 ###############################################################################
25 SEARCH_TEXT = 'Puppet (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 = ['[', ']', '"']
31 class Deployment(object):
33 def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict,
36 self.yaml_config_dir = yaml_config_dir
38 self.node_id_roles_dict = node_id_roles_dict
39 self.no_health_check = no_health_check
40 self.pattern = re.compile(
41 '\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d')
43 def collect_error_logs(self):
44 for node_id, roles_blade in self.node_id_roles_dict.iteritems():
46 cmd = ('ssh -q node-%s grep \'"%s"\' %s'
47 % (node_id, SEARCH_TEXT, LOG_FILE))
48 results, _ = exec_cmd(cmd, False)
49 for result in results.splitlines():
52 sub_cmd = '"%s" %s' % (result, LOG_FILE)
53 for c in LIST_OF_CHAR_TO_BE_ESCAPED:
54 sub_cmd = sub_cmd.replace(c, '\%s' % c)
55 grep_cmd = ('grep -B%s %s'
56 % (GREP_LINES_OF_LEADING_CONTEXT, sub_cmd))
57 cmd = ('ssh -q node-%s "%s"' % (node_id, grep_cmd))
59 details, _ = exec_cmd(cmd, False)
60 details_list = details.splitlines()
62 found_prev_log = False
63 for i in range(len(details_list) - 2, -1, -1):
64 if self.pattern.match(details_list[i]):
68 log_msg += '\n'.join(details_list[i:-1]) + '\n'
70 grep_cmd = ('grep -A%s %s'
71 % (GREP_LINES_OF_TRAILING_CONTEXT, sub_cmd))
72 cmd = ('ssh -q node-%s "%s"' % (node_id, grep_cmd))
74 details, _ = exec_cmd(cmd, False)
75 details_list = details.splitlines()
77 found_next_log = False
78 for i in range(1, len(details_list)):
79 if self.pattern.match(details_list[i]):
83 log_msg += '\n'.join(details_list[:i])
88 log_list.append(log_msg)
91 role = ('controller' if 'controller' in roles_blade[0]
93 log('_' * 40 + 'Errors in node-%s %s' % (node_id, role)
95 for log_msg in log_list:
101 LOG_FILE = 'cloud.log'
103 log('Starting deployment of environment %s' % self.env_id)
104 run_proc('fuel --env %s deploy-changes | strings | tee %s'
105 % (self.env_id, LOG_FILE))
108 for i in range(WAIT_LOOP):
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)
114 if env[0][E['status']] == 'operational':
117 elif (env[0][E['status']] == 'error'
118 or env[0][E['status']] == 'stopped'):
121 time.sleep(SLEEP_TIME)
125 log('Environment %s successfully deployed' % self.env_id)
127 self.collect_error_logs()
128 err('Deployment failed, environment %s is not operational'
131 def verify_node_status(self):
132 node_list = parse(exec_cmd('fuel node list'))
134 for node in node_list:
135 if node[N['status']] != 'ready':
136 failed_nodes.append((node[N['id']], node[N['status']]))
140 for node, status in failed_nodes:
141 summary += '[node %s, status %s]\n' % (node, status)
142 err('Deployment failed: %s' % summary)
144 def health_check(self):
145 log('Now running sanity and smoke health checks')
146 r = exec_cmd('fuel health --env %s --check sanity,smoke --force'
150 err('Healthcheck failed!')
154 self.verify_node_status()
155 if not self.no_health_check: