Commit 3105f5bb authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Added 'vos_job' cli command.

parent dc04dbfd
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ RUN mkdir -p /usr/bin/vos_cli
COPY *.py \
     vos_data \
     vos_import \
     vos_job \
     vos_storage /usr/bin/vos_cli/
RUN chmod -R 755 /usr/bin/vos_cli

client/vos_job

0 → 100644
+115 −0
Original line number Diff line number Diff line
#!/usr/bin/env python

import os
import sys

from amqp_client import AMQPClient
from config import Config
from tabulate import tabulate


class VOSJob(AMQPClient):

    def __init__(self):
        config = Config("/etc/vos_cli/vos_cli.conf")
        self.params = config.loadSection("server")
        self.host = self.params["host"]
        self.port = self.params.getint("port")
        self.params = config.loadSection("vos_job")
        self.rpcQueue = self.params["rpc_queue"]
        self.jobPhases = ("pending", "queued", "executing", "completed", "error")
        super(VOSJob, self).__init__(self.host, self.port, self.rpcQueue)

    def listJobs(self, jobPhase = ""):
        if not jobPhase:
            jobRequest = { "requestType": "JOB_LIST" }
        else:
            jobPhase = jobPhase.upper()
            jobRequest = { "requestType": "JOB_BY_PHASE", "jobPhase": jobPhase }
        jobResponse = self.call(jobRequest)
        if "responseType" not in jobResponse:
            sys.exit("FATAL: Malformed response.\n")
        elif jobResponse["responseType"] == "LST_DONE":
            jobList = jobResponse["jobList"]
            if jobList:
                print("\n" + tabulate(jobResponse["jobList"], headers = "keys", tablefmt = "pretty") + "\n")
            else:
                sys.exit("\nNo active job found.\n")                
        elif jobResponse["responseType"] == "LST_BY_PHASE_DONE":
            jobList = jobResponse["jobList"]
            if jobList:
                print("\n" + tabulate(jobResponse["jobList"], headers = "keys", tablefmt = "pretty") + "\n")
            else:
                sys.exit(f"\nNo {jobPhase} job found.\n")
        elif jobResponse["responseType"] == "ERROR":
            errorCode = jobResponse["errorCode"]
            errorMsg = jobResponse["errorMsg"]
            sys.exit(f"\nError code: {errorCode}\nError message: {errorMsg}\n")
        else:
            sys.exit("\nFATAL: Unknown response type.\n")

    def help(self):
        sys.exit("""
NAME
    vos_job

SYNOPSYS
    vos_data COMMAND [ARGUMENT]

DESCRIPTION
    Client tool to obtain information about jobs.
    
    The client accepts the following commands:
    
    list 
        if launched without any argument it will
        display all the currently active jobs.
        The command can also be used with one
        argument, the job phase. The following 
        values are allowed:
        - pending
        - queued
        - executing
        - completed
        - error
        
    info
        prints a JSON object containing the job 
        info according to the UWS specification.
        A job id is required as argument.
        
    results
        prints a JSON object containing the job 
        results according to the UWS specification.
        A job id is required as argument.
        """)

# Create new VOSJob object
vosJobCli = VOSJob()

# Check the number of input args
if len(sys.argv) == 2:
    script, cmd = sys.argv
    if cmd == "list":
        vosJobCli.listJobs()
    else:
        vos.JobCli.help()
elif len(sys.argv) == 3:
    script, cmd, arg = sys.argv
    if cmd == "list":
        if arg in vosJobCli.jobPhases:
            vosJobCli.listJobs(arg)
        else:
            vosJobCli.help()
    elif cmd == "info":
        #TODO
        #vosJobCli.info()
        pass
    elif cmd == "results":
        #TODO
        #vosJobCli.results()
        pass
    else:
        vosJobCli.help()
else:
    vosJobCli.help()