-os.chmod(LOGFILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
-
-def exec_cmd(cmd, check=True):
- nul_f = open(os.devnull, 'w')
- process = subprocess.Popen(cmd,
- stdout=subprocess.PIPE,
- stderr=nul_f,
- shell=True)
- nul_f.close()
- response = process.communicate()[0].strip()
- return_code = process.returncode
+os.chmod(LOGFILE, 0664)
+
+
+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
+ 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, masked_cmd))
+
+ response = response.strip()