Demo entry 2591231

Python

   

Submitted by anonymous on Sep 01, 2015 at 07:34
Language: Python. Code size: 3.2 kB.

import os
import pbs
import copy

e = pbs.event()
j = e.job
args=copy.deepcopy(e.argv)
pbs.logmsg(pbs.LOG_DEBUG, "args are %s " % e.progname)
prog=e.progname
hdir = ""
edir = ""


def add_resource_restriction (call):
	n = j.Resource_List["ncpus"]
	m = j.Resource_List["mem"]
	if n:
		call += " -c " + str(n)
	if m:
		call += " -m " + str(m)
	return call

def add_env (call):
	for i in e.env:
		call += " -e " + str(i)+"="+e.env[i]
	return call

def  get_pbs_dirs ():
	global hdir
	global edir
	#get PBS_HOME & PBS_EXEC folders
	conf = os.popen('cat /etc/pbs.conf').read()
	conf_list = conf.split()
	for a in conf_list:
	        pbs.logmsg(pbs.LOG_DEBUG, "Conf is: %s" % a)
	        if a.startswith("PBS_HOME"):
	                directory = a.split("=",1)
	                hdir = directory[1]
	        if a.startswith("PBS_EXEC"):
	                directory = a.split("=",1)
	                edir = directory[1]
	return 

def create_container ():
	global hdir
	global edir
	is_ms = j.in_ms_mom()
	docker = ""
	call = ""
	search_container = "docker ps | grep "+str(j.id)+" | grep -v grep"
	docker = os.popen(search_container).read()
	jid = j.id
	pbs.logmsg(pbs.LOG_DEBUG, "Docker container: %s" % docker)
	if docker:
		pbs.logmsg(pbs.LOG_DEBUG, "Docker container found!")
	else:
		v = str(j.Variable_List)
		vl = v.split(",")
		image = "DOCKER_IMAGE="
		os_image = "None"
		#get image name
		for i in vl:
		        pbs.logmsg(pbs.LOG_DEBUG, "Variable is: %s" % i)
		        if i.startswith(image):
		                name = i.split("=",1)
		                os_image = name[1]
		if "None" in os_image:
		# No need to run any docker instance.... just bail out!
		        e.accept()
		pbs.logmsg(pbs.LOG_DEBUG, "Image is: %s" % os_image)
		call = "docker run -d -it --name " + str(jid)
		call = add_resource_restriction(call);
		pbs.logmsg(pbs.LOG_DEBUG, "Resource is : %s" % call)
		call = add_env(call);
		pbs.logmsg(pbs.LOG_DEBUG, "Env is : %s" % call)
		get_pbs_dirs();
		pbs.logmsg(pbs.LOG_DEBUG, "Directory is : %s" % hdir)
		job_file=hdir+"/mom_priv/jobs/"+str(jid)+".SC"
		call += " -v "+str(hdir)+":"+str(hdir)+" "+" -v "+str(edir)+":"+str(edir)
		call += " " + str(os_image) + " /bin/bash"
		pbs.logmsg(pbs.LOG_DEBUG, "Call is : %s" % call)
		os.popen(call)
	return
	
def launch_job ():
	global prog
	global hdir
	is_script=prog.find("bash")
	prog=""
	get_pbs_dirs()
	pbs.logmsg(pbs.LOG_DEBUG," is_script %d" % is_script)
	if is_script != -1:
		job_file=hdir+"/mom_priv/jobs/"+str(j.id)+".SC"
	else:
		for arg in args:
			prog=prog+" "+arg

	e.progname="/usr/bin/docker"
	e.argv = []
	e.argv.append("docker")
	e.argv.append("exec")
	e.argv.append(str(j.id))
	e.argv.append("/bin/bash")
	e.argv.append("-c")
	executable=""
	if is_script != -1:
		pbs.logmsg(pbs.LOG_DEBUG, "It's a script")
		e.argv.append(job_file)
	else:
		pbs.logmsg(pbs.LOG_DEBUG, "It's an executable")
		executable=""	
		for arg in args:
			executable+=arg+" "
			pbs.logmsg(pbs.LOG_DEBUG, "arg[1] : %s" % arg)
		e.argv.append(executable)
	return

create_container()
launch_job()
e.accept()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).