X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=deploy%2Fcommon.py;h=9654b37715a874e88d3788437780d0c1f04510e2;hb=2a9e72d95200161ec27e8f199a76c6ec1f88bff1;hp=3cd3e0e6e20c0a1bcb24a361e99b95fdce1ef351;hpb=aa58ccd04bfa98ae2bfbf2e37a180020e331deaf;p=fuel.git diff --git a/deploy/common.py b/deploy/common.py index 3cd3e0e6e..9654b3771 100644 --- a/deploy/common.py +++ b/deploy/common.py @@ -1,6 +1,7 @@ ############################################################################### # Copyright (c) 2015 Ericsson AB and others. # szilard.cserey@ericsson.com +# peter.barabas@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 @@ -38,7 +39,21 @@ out_handler.setFormatter(formatter) LOG.addHandler(out_handler) os.chmod(LOGFILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) -def exec_cmd(cmd, check=True, attempts=1, delay=5, verbose=False): + +def mask_arguments(cmd, mask_args, mask_str): + cmd_line = cmd.split() + for pos in mask_args: + # Don't mask the actual command; also check if we don't reference + # beyond bounds + if pos == 0 or pos >= len(cmd_line): + continue + cmd_line[pos] = mask_str + return ' '.join(cmd_line) + + +def exec_cmd(cmd, check=True, attempts=1, delay=5, verbose=False, mask_args=[], mask_str='*****'): + masked_cmd = mask_arguments(cmd, mask_args, mask_str) + # a negative value means forever while attempts != 0: attempts = attempts - 1 @@ -52,18 +67,18 @@ def exec_cmd(cmd, check=True, attempts=1, delay=5, verbose=False): break time.sleep(delay) if verbose: - log('%d attempts left: %s' % (attempts, cmd)) + log('%d attempts left: %s' % (attempts, masked_cmd)) response = response.strip() if check: if return_code > 0: stderr = stderr.strip() - print "Failed command: " + str(cmd) + print "Failed command: " + str(masked_cmd) print "Command returned response: " + str(stderr) print "Command return code: " + str(return_code) raise Exception(stderr) else: - print "Command: " + str(cmd) + print "Command: " + str(masked_cmd) print str(response) return response return response, return_code @@ -77,6 +92,17 @@ def run_proc(cmd): return process +def run_proc_wait_terminated(process): + response = process.communicate()[0].strip() + return_code = process.returncode + return response, return_code + + +def run_proc_kill(process): + response = process.kill() + return response + + def parse(printout): parsed_list = [] lines = printout.splitlines() @@ -99,8 +125,10 @@ def clean(lines): return parsed if len(parsed_list) == 1 else parsed_list -def err(message): +def err(message, fun = None, *args): LOG.error('%s\n' % message) + if fun: + fun(*args) sys.exit(1)