Examples: Job Submit and Job Info

The following are two simple Python programs that imitate the function of the nc run and nc info commands, only this time these programs are implemented by calling the vov_rest_v3.py package member functions. See the following pages for the source code for these tools, named nc_run2.py and nc_info.py.

These REST programs require that the NC_URL environment variable be set to the Accelerator queue URL, as returned by nc cmd vovbrowser. Here is a an example that demonstrates them.
% export NC_URL="`nc cmd vovbrowser`" 
% ./nc_run2.py sleep 33 
Password: 
New job is 45080 
% ./nc_run2.py sleep 66 
Password: 
New job is 45083 
% ./nc_info.py 45083 
Password: 
Id,User,Group     000045083,kfeind,/time/users 
Command           vw sleep 33 > JOBLOG.1293 
Status            Running 
    Grabbed       Priority:normal#1 User:kfeind#1 Group:time_users#1 
    Host          sdc-s15s-2 
    Tasker        localhost 
    Duration      20s 
Job 000045083 is Running 

nc_run2.py

#!/usr/bin/python3 
# 
# nc_run2.py 
# 
# Usage 
# 
#               export NC_URL=<URL FOR NC QUEUE> 
#               ./nc_run2.py <command> [args] 
# 

import os, sys, random, pwd, json, grp, getpass 
IPATH = os.path.join(os.environ["VOVDIR"], "../common/scripts/python") 
sys.path.append(IPATH) 
import vov_rest_v3 

def getMyPassword(): 
    return getpass.getpass('Password:') 

# 
# Main body 
# 
nc_url = os.environ["NC_URL"] 
scheme = nc_url.split(":")[0] 
hostport = nc_url.split("/")[2] 
url = "{0}://{1}".format(scheme, hostport) 
command = " ".join(sys.argv[1::]) 

vrest = vov_rest_v3.VOVRestV3() 
vrest.authorize(url, getMyPassword()) 

# Job attributes - required 
VOV_JOB_DESC = { 
    "command" : command, 
    "logfile" : "JOBLOG." + str(random.randint(1000,2000)), 
    "rundir" : os.getcwd(), 
    "env" : "BASE", 
} 

# Job attributes - optional / User specified 
VOV_JOB_DESC.update( { 
    "priority,sched" : 4, 
} ) 
r = vrest.submitRequest("POST", url + "/api/v3/jobs", jsonData=VOV_JOB_DESC) 
print ("New job is %s" % json.loads(r)["jobid"]) 

nc_info.py

#!/usr/bin/python3
#
# nc_info.py
#
# Usage
#
#      export NC_URL=<URL FOR NC QUEUE>
#      ./nc_info.py JOB_ID
#

import os, sys, json
IPATH = os.path.join(os.environ["VOVDIR"], "../common/scripts/python")
sys.path.append(IPATH)
import vov_rest_v3

def getMyPassword():
    import getpass
    return getpass.getpass('Password:')

def infoPrint( text ):
    dd =  json.loads(text)
    id    = find(dd, 'ID')
    user  = find(dd, 'USER')
    group = find(dd, 'GROUP')
    print ("%-16s%s,%s,%s" % ("Id,User,Group", id, user, group))
    print ("%-16s%s" % ("Command", find(dd, 'COMMAND')))
    print ("%-16s%s" % ("Status", find(dd, 'STATUSNC')))
    print ("%-16s%s" % ("   Grabbed", find(dd, 'GRABBEDRESOURCES')))
    print ("%-16s%s" % ("   Host", find(dd, 'HOST')))
    print ("%-16s%s" % ("   Tasker", find(dd, 'TASKERNAME')))
    print ("%-16s%s" % ("   Duration", find(dd, 'DURATIONPP')))
    print ("Job %s is %s" % (id, find(dd, 'STATUSNC')))

def find(jobdump, key):
    for c in range (0, len(jobdump['columns'])):
        if (jobdump['columns'][c]['field'] == key):
            break
    return jobdump['rows'][0][c]

#
# Main body
#
nc_url = os.environ['NC_URL']
scheme = nc_url.split(":")[0]
hostport = nc_url.split("/")[2]
url = "{0}://{1}".format(scheme, hostport)
jobid = sys.argv[1]

vrest = vov_rest_v3.VOVRestV3()
vrest.authorize(url, getMyPassword())
query = url + '/api/v3/jobs/' + jobid
json_text = vrest.submitRequest("GET", query)
infoPrint(json_text)