diff options
Diffstat (limited to 'fuel/deploy/common.py')
-rw-r--r-- | fuel/deploy/common.py | 63 |
1 files changed, 58 insertions, 5 deletions
diff --git a/fuel/deploy/common.py b/fuel/deploy/common.py index dc12637..3472e89 100644 --- a/fuel/deploy/common.py +++ b/fuel/deploy/common.py @@ -1,8 +1,19 @@ +############################################################################### +# Copyright (c) 2015 Ericsson AB and others. +# szilard.cserey@ericsson.com +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################### + import subprocess import sys import os import logging import argparse +import shutil +import errno N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5, 'roles': 6, 'pending_roles': 7, 'online': 8} @@ -21,6 +32,7 @@ out_handler = logging.FileHandler('autodeploy.log', mode='w') out_handler.setFormatter(formatter) LOG.addHandler(out_handler) + def exec_cmd(cmd, check=True): process = subprocess.Popen(cmd, stdout=subprocess.PIPE, @@ -35,6 +47,7 @@ def exec_cmd(cmd, check=True): return response return response, return_code + def run_proc(cmd): process = subprocess.Popen(cmd, stdout=subprocess.PIPE, @@ -42,14 +55,16 @@ def run_proc(cmd): shell=True) return process + def parse(printout): parsed_list = [] lines = printout.splitlines() for l in lines[2:]: - parsed = [e.strip() for e in l.split('|')] - parsed_list.append(parsed) + parsed = [e.strip() for e in l.split('|')] + parsed_list.append(parsed) return parsed_list + def clean(lines): parsed_list = [] parsed = [] @@ -62,42 +77,80 @@ def clean(lines): parsed_list.append(parsed) return parsed if len(parsed_list) == 1 else parsed_list + def err(message): LOG.error('%s\n' % message) sys.exit(1) + +def warn(message): + LOG.warning('%s\n' % message) + + def check_file_exists(file_path): if not os.path.isfile(file_path): err('ERROR: File %s not found\n' % file_path) + def check_dir_exists(dir_path): if not os.path.isdir(dir_path): err('ERROR: Directory %s not found\n' % dir_path) + def create_dir_if_not_exists(dir_path): if not os.path.isdir(dir_path): log('Creating directory %s' % dir_path) os.makedirs(dir_path) + +def delete(f): + if os.path.isfile(f): + log('Deleting file %s' % f) + os.remove(file) + elif os.path.isdir(f): + log('Deleting directory %s' % f) + shutil.rmtree(f) + + def commafy(comma_separated_list): l = [c.strip() for c in comma_separated_list.split(',')] return ','.join(l) -def delete_file(file): - if os.path.exists(file): - os.remove(file) def check_if_root(): r = exec_cmd('whoami') if r != 'root': err('You need be root to run this application') + def log(message): LOG.debug('%s\n' % message) + class ArgParser(argparse.ArgumentParser): + def error(self, message): sys.stderr.write('ERROR: %s\n' % message) self.print_help() sys.exit(2) + +class literal_unicode(unicode): + pass + + +def literal_unicode_representer(dumper, data): + return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|') + + +def backup(path): + src = path + dst = path + '_orig' + delete(dst) + try: + shutil.copytree(src, dst) + except OSError as e: + if e.errno == errno.ENOTDIR: + shutil.copy(src, dst) + else: + raise |