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 ###############################################################################
19 N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5,
20 'roles': 6, 'pending_roles': 7, 'online': 8}
21 E = {'id': 0, 'status': 1, 'name': 2, 'mode': 3, 'release_id': 4,
22 'changes': 5, 'pending_release_id': 6}
23 R = {'id': 0, 'name': 1, 'state': 2, 'operating_system': 3, 'version': 4}
24 RO = {'name': 0, 'conflicts': 1}
26 LOG = logging.getLogger(__name__)
27 LOG.setLevel(logging.DEBUG)
28 formatter = logging.Formatter('%(message)s')
29 out_handler = logging.StreamHandler(sys.stdout)
30 out_handler.setFormatter(formatter)
31 LOG.addHandler(out_handler)
32 out_handler = logging.FileHandler('autodeploy.log', mode='w')
33 out_handler.setFormatter(formatter)
34 LOG.addHandler(out_handler)
35 os.chmod('autodeploy.log', stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
37 def exec_cmd(cmd, check=True):
38 process = subprocess.Popen(cmd,
39 stdout=subprocess.PIPE,
40 stderr=subprocess.STDOUT,
42 response = process.communicate()[0].strip()
43 return_code = process.returncode
49 return response, return_code
53 process = subprocess.Popen(cmd,
54 stdout=subprocess.PIPE,
55 stderr=subprocess.STDOUT,
62 lines = printout.splitlines()
64 parsed = [e.strip() for e in l.split('|')]
65 parsed_list.append(parsed)
72 for l in lines.strip().splitlines():
74 cluttered = [e.strip() for e in l.split(' ')]
78 parsed_list.append(parsed)
79 return parsed if len(parsed_list) == 1 else parsed_list
83 LOG.error('%s\n' % message)
88 LOG.warning('%s\n' % message)
91 def check_file_exists(file_path):
92 if not os.path.dirname(file_path):
93 file_path = '%s/%s' % (CWD, file_path)
94 if not os.path.isfile(file_path):
95 err('ERROR: File %s not found\n' % file_path)
98 def check_dir_exists(dir_path):
99 if not os.path.dirname(dir_path):
100 dir_path = '%s/%s' % (CWD, dir_path)
101 if not os.path.isdir(dir_path):
102 err('ERROR: Directory %s not found\n' % dir_path)
105 def create_dir_if_not_exists(dir_path):
106 if not os.path.isdir(dir_path):
107 log('Creating directory %s' % dir_path)
108 os.makedirs(dir_path)
112 if os.path.isfile(f):
113 log('Deleting file %s' % f)
115 elif os.path.isdir(f):
116 log('Deleting directory %s' % f)
120 def commafy(comma_separated_list):
121 l = [c.strip() for c in comma_separated_list.split(',')]
126 r = exec_cmd('whoami')
128 err('You need be root to run this application')
132 LOG.debug('%s\n' % message)
135 class ArgParser(argparse.ArgumentParser):
137 def error(self, message):
138 sys.stderr.write('ERROR: %s\n' % message)
148 shutil.copytree(src, dst)
150 if e.errno == errno.ENOTDIR:
151 shutil.copy(src, dst)