diff options
author | Jonas Bjurel <jonas.bjurel@ericsson.com> | 2016-06-15 17:19:03 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@172.30.200.206> | 2016-06-15 17:19:03 +0000 |
commit | aa58ccd04bfa98ae2bfbf2e37a180020e331deaf (patch) | |
tree | a9150a573cf9f54cffb6661d971c9c591cf5e618 /deploy/common.py | |
parent | c47a7f7a0198e2ce6ba54cbe485ae87983f785d2 (diff) | |
parent | 2ea2690762b4a1e547d2ca71674125c7e101d7fd (diff) |
Merge changes from topic 'simplify-ipmi-adapter'
* changes:
ipmi_adapter: simplify, retry if command fails
common.py: allow specifying number of attempts in exec_cmd
Diffstat (limited to 'deploy/common.py')
-rw-r--r-- | deploy/common.py | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/deploy/common.py b/deploy/common.py index 41b4e274e..3cd3e0e6e 100644 --- a/deploy/common.py +++ b/deploy/common.py @@ -16,6 +16,7 @@ import argparse import shutil import stat import errno +import time N = {'id': 0, 'status': 1, 'name': 2, 'cluster': 3, 'ip': 4, 'mac': 5, 'roles': 6, 'pending_roles': 7, 'online': 8, 'group_id': 9} @@ -37,13 +38,22 @@ 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): - process = subprocess.Popen(cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=True) - (response, stderr) = process.communicate() - return_code = process.returncode +def exec_cmd(cmd, check=True, attempts=1, delay=5, verbose=False): + # a negative value means forever + while attempts != 0: + attempts = attempts - 1 + process = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True) + (response, stderr) = process.communicate() + return_code = process.returncode + if return_code == 0 or attempts == 0: + break + time.sleep(delay) + if verbose: + log('%d attempts left: %s' % (attempts, cmd)) + response = response.strip() if check: if return_code > 0: |