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.isfile(file_path):
93 err('ERROR: File %s not found\n' % file_path)
96 def check_dir_exists(dir_path):
97 if not os.path.isdir(dir_path):
98 err('ERROR: Directory %s not found\n' % dir_path)
101 def create_dir_if_not_exists(dir_path):
102 if not os.path.isdir(dir_path):
103 log('Creating directory %s' % dir_path)
104 os.makedirs(dir_path)
108 if os.path.isfile(f):
109 log('Deleting file %s' % f)
111 elif os.path.isdir(f):
112 log('Deleting directory %s' % f)
116 def commafy(comma_separated_list):
117 l = [c.strip() for c in comma_separated_list.split(',')]
122 r = exec_cmd('whoami')
124 err('You need be root to run this application')
128 LOG.debug('%s\n' % message)
131 class ArgParser(argparse.ArgumentParser):
133 def error(self, message):
134 sys.stderr.write('ERROR: %s\n' % message)
139 class literal_unicode(unicode):
143 def literal_unicode_representer(dumper, data):
144 return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
152 shutil.copytree(src, dst)
154 if e.errno == errno.ENOTDIR:
155 shutil.copy(src, dst)