#!/usr/bin/env python
import sys
import subprocess
sys.path.append('../lib/')
from argo_communicator import ArgoCommunicator
import logging, os

if os.path.split(os.getcwd())[-1] == 'lib' or os.path.split(os.getcwd())[-1] == 'cron_scripts':
    sys.path.append('../include')
else:
    sys.path.append('include')

import py_functions

py_functions.setLogging()

def runcmd(cmd):
    try:
        output = subprocess.check_output(cmd)
    except:
        raise

    return output

if __name__ == "__main__":
    argo = ArgoCommunicator()
    secant_conf_path = os.environ.get('SECANT_CONFIG_DIR', '/etc/secant') + '/' + 'secant.conf'
    url = py_functions.getSettingsFromBashConfFile(secant_conf_path, "IMAGE_LIST_URL")
    dir = py_functions.getSettingsFromBashConfFile(secant_conf_path, "IMAGE_LIST_DIR")
    state_dir = py_functions.getSettingsFromBashConfFile(secant_conf_path, "STATE_DIR")
    registered_dir = state_dir + "/registered"
    if not os.path.isdir(registered_dir):
        os.mkdir(registered_dir, 755)

    images = argo.get_templates_for_assessment(dir)
    logging.info("Secant consumer: Obtained %d image list(s) for assessment" % (len(images)))

    for img_list in images:
        #sudo -u cloudkeeper /opt/cloudkeeper/bin/cloudkeeper --image-lists=https://vmcaster.appdb.egi.eu/store/vappliance/demo.va.public/image.list --debug
        #img_list = "https://vmcaster.appdb.egi.eu/store/vappliance/demo.va.public/image.list"
        logging.debug("Trying to register image list %s" % (img_list))
        img_url = "%s/%s" % (url, img_list)
        try:
            debug_info = runcmd(["sudo", "-u", "cloudkeeper", "/opt/cloudkeeper/bin/cloudkeeper", "--image-lists=" + img_url, "--debug"])
        except:
            logging.error("Secant consumer: Registering image list %s failed: %s" % (img_list, debug_info))
            continue
        logging.debug("Secant consumer: Image list %s has been registered" % (img_list))
        os.rename("%s/%s" % (dir, img_list), "%s/%s" % (registered_dir, img_list))

@@ -15,16 +15,54 @@
  python%0A
+from __future__ import print_function%0A
 import s
@@ -396,126 +396,8 @@
 ()%0A%0A
-def runcmd(cmd):%0A    try:%0A        output = subprocess.check_output(cmd)%0A    except:%0A        raise%0A%0A    return output%0A%0A
 if _
@@ -1489,44 +1489,14 @@
     
-try:%0A            debug_info = run
 cmd
+ = 
 (%5B%22s
@@ -1610,122 +1610,328 @@
     
-except:%0A            logging.error(%22Secant consumer: Registering image list %25s failed: %25s%22 %25 (img_list, debug_info)
+try:%0A            subprocess.check_output(cmd, stderr=subprocess.STDOUT)%0A        except subprocess.CalledProcessError as e:%0A            logging.error(%22Secant consumer: Registering image list %25s failed: %25s%22 %25 (img_list, e.output))%0A            print(%22Failed to register image %25s, check the log.%22 %25 img_list, file=sys.stderr
 )%0A  

Improve execution of cloudkeeper and its status