build: cache: Consider UBUNTU_ARCH in .cacheid
[fuel.git] / deploy / common.py
index cc418b5..3530458 100644 (file)
@@ -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
@@ -16,6 +17,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}
@@ -35,25 +37,48 @@ if os.path.isfile(LOGFILE):
 out_handler = logging.FileHandler(LOGFILE, mode='w')
 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):
-    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()
     if check:
         if return_code > 0:
-            print "Failed command: " + str(cmd)
-            print "Command returned response: " + str(response)
+            stderr = stderr.strip()
+            print "Failed command: " + str(masked_cmd)
+            print "Command returned response: " + str(stderr)
             print "Command return code: " + str(return_code)
-            raise Exception(response)
+            raise Exception(stderr)
         else:
-            print "Command: " + str(cmd)
+            print "Command: " + str(masked_cmd)
             print str(response)
             return response
     return response, return_code
@@ -67,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()
@@ -89,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)
 
 
@@ -133,8 +171,8 @@ def commafy(comma_separated_list):
 
 
 def check_if_root():
-    r = exec_cmd('whoami')
-    if r != 'root':
+    uid = os.getuid()
+    if uid != 0:
         err('You need be root to run this application')