1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
4 # peter.barabas@ericsson.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ###############################################################################
22 N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5,
23 'roles': 6, 'pending_roles': 7, 'online': 8, 'group_id': 9}
24 E = {'id': 0, 'status': 1, 'name': 2, 'release_id': 3, 'pending_release_id': 4}
25 R = {'id': 0, 'name': 1, 'state': 2, 'operating_system': 3, 'version': 4}
26 RO = {'name': 0, 'conflicts': 1}
28 LOG = logging.getLogger(__name__)
29 LOG.setLevel(logging.DEBUG)
30 formatter = logging.Formatter('%(message)s')
31 out_handler = logging.StreamHandler(sys.stdout)
32 out_handler.setFormatter(formatter)
33 LOG.addHandler(out_handler)
34 LOGFILE = 'autodeploy.log'
35 if os.path.isfile(LOGFILE):
37 out_handler = logging.FileHandler(LOGFILE, mode='w')
38 out_handler.setFormatter(formatter)
39 LOG.addHandler(out_handler)
40 os.chmod(LOGFILE, 0664)
43 def mask_arguments(cmd, mask_args, mask_str):
44 cmd_line = shlex.split(cmd)
46 # Don't mask the actual command; also check if we don't reference
48 if pos == 0 or pos >= len(cmd_line):
50 cmd_line[pos] = mask_str
51 return ' '.join(cmd_line)
54 def exec_cmd(cmd, check=True, attempts=1, delay=5, verbose=False, mask_args=[], mask_str='*****'):
55 masked_cmd = mask_arguments(cmd, mask_args, mask_str)
57 # a negative value means forever
60 process = subprocess.Popen(cmd,
61 stdout=subprocess.PIPE,
62 stderr=subprocess.PIPE,
64 (response, stderr) = process.communicate()
65 return_code = process.returncode
66 if return_code == 0 or attempts == 0:
70 log('%d attempts left: %s' % (attempts, masked_cmd))
72 response = response.strip()
75 stderr = stderr.strip()
76 print("Failed command: " + str(masked_cmd))
77 print("Command returned response: " + str(stderr))
78 print("Command return code: " + str(return_code))
79 raise Exception(stderr)
81 print("Command: " + str(masked_cmd))
84 return response, return_code
88 process = subprocess.Popen(cmd,
89 stdout=subprocess.PIPE,
90 stderr=subprocess.STDOUT,
95 def run_proc_wait_terminated(process):
96 response = process.communicate()[0].strip()
97 return_code = process.returncode
98 return response, return_code
101 def run_proc_kill(process):
102 response = process.kill()
108 lines = printout.splitlines()
110 parsed = [e.strip() for e in l.split('|')]
111 parsed_list.append(parsed)
118 for l in lines.strip().splitlines():
120 cluttered = [e.strip() for e in l.split(' ')]
124 parsed_list.append(parsed)
125 return parsed if len(parsed_list) == 1 else parsed_list
128 def err(message, fun=None, *args):
129 LOG.error('%s\n' % message)
136 LOG.warning('%s\n' % message)
139 def check_file_exists(file_path):
140 if not os.path.dirname(file_path):
141 file_path = '%s/%s' % (CWD, file_path)
142 if not os.access(file_path, os.R_OK):
143 err('ERROR: File %s not found\n' % file_path)
146 def check_dir_exists(dir_path):
147 if not os.path.dirname(dir_path):
148 dir_path = '%s/%s' % (CWD, dir_path)
149 if not os.path.isdir(dir_path):
150 err('ERROR: Directory %s not found\n' % dir_path)
153 def create_dir_if_not_exists(dir_path):
154 if not os.path.isdir(dir_path):
155 log('Creating directory %s' % dir_path)
156 os.makedirs(dir_path)
160 if os.path.isfile(f):
161 log('Deleting file %s' % f)
163 elif os.path.isdir(f):
164 log('Deleting directory %s' % f)
168 def commafy(comma_separated_list):
169 l = [c.strip() for c in comma_separated_list.split(',')]
176 err('You need be root to run this application')
180 LOG.debug('%s\n' % message)
183 class ArgParser(argparse.ArgumentParser):
185 def error(self, message):
186 sys.stderr.write('ERROR: %s\n' % message)
196 shutil.copytree(src, dst)
198 if e.errno == errno.ENOTDIR:
199 shutil.copy(src, dst)