comparison scriptrunner.py @ 3:e1f5be390bc3 draft

planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit a0b1360bc3e6b97f793caa1e6d60d8864308ee83
author mvdbeek
date Fri, 08 Jul 2016 17:09:50 -0400
parents 813b55d27809
children ea796129e49b
comparison
equal deleted inserted replaced
2:813b55d27809 3:e1f5be390bc3
61 return binds 61 return binds
62 62
63 def switch_to_docker(opts): 63 def switch_to_docker(opts):
64 import docker #need local import, as container does not have docker-py 64 import docker #need local import, as container does not have docker-py
65 current_user = getpass.getuser() 65 current_user = getpass.getuser()
66 user_id = os.getuid()
67 group_id = os.getgid()
66 docker_client=docker.Client() 68 docker_client=docker.Client()
67 toolfactory_path=abspath(sys.argv[0]) 69 toolfactory_path=abspath(sys.argv[0])
68 binds=construct_bind(host_path=opts.script_path, ro=False) 70 binds=construct_bind(host_path=opts.script_path, ro=False)
69 binds=construct_bind(binds=binds, host_path=abspath(opts.output_dir), ro=False) 71 binds=construct_bind(binds=binds, host_path=abspath(opts.output_dir), ro=False)
70 if len(opts.input_tab)>0: 72 if len(opts.input_tab)>0:
74 if opts.make_HTML: 76 if opts.make_HTML:
75 binds=construct_bind(binds=binds, host_path=opts.output_html, ro=False) 77 binds=construct_bind(binds=binds, host_path=opts.output_html, ro=False)
76 binds=construct_bind(binds=binds, host_path=toolfactory_path) 78 binds=construct_bind(binds=binds, host_path=toolfactory_path)
77 volumes=binds.keys() 79 volumes=binds.keys()
78 sys.argv=[abspath(opts.output_dir) if sys.argv[i-1]=='--output_dir' else arg for i,arg in enumerate(sys.argv)] ##inject absolute path of working_dir 80 sys.argv=[abspath(opts.output_dir) if sys.argv[i-1]=='--output_dir' else arg for i,arg in enumerate(sys.argv)] ##inject absolute path of working_dir
79 cmd=['python', '-u']+sys.argv+['--dockerized', '1'] 81 cmd=['python', '-u']+sys.argv+['--dockerized', '1', "--user_id", str(user_id), "--group_id", str(group_id)]
80 image_exists = [ True for image in docker_client.images() if opts.docker_image in image['RepoTags'] ] 82 image_exists = [ True for image in docker_client.images() if opts.docker_image in image['RepoTags'] ]
81 if not image_exists: 83 if not image_exists:
82 docker_client.pull(opts.docker_image) 84 docker_client.pull(opts.docker_image)
83 container=docker_client.create_container( 85 container=docker_client.create_container(
84 image=opts.docker_image, 86 image=opts.docker_image,
373 if self.opts.make_HTML: 375 if self.opts.make_HTML:
374 self.makeHtml() 376 self.makeHtml()
375 return retval 377 return retval
376 378
377 379
380 def change_group_id(group_id):
381 """
382 To avoid issues with wrong user ids, we change the user id of the 'galaxy' user in the container
383 to the user id with which the script has been called initially.
384 """
385 old_gid = 1450 # specified in Dockerfile
386 cmd = ["/usr/sbin/groupmod", "-g", group_id , "galaxy"]
387 subprocess.call(cmd)
388
389
390 def change_user_id(new_id):
391 """
392 To avoid issues with wrong user ids, we change the user id of the 'galaxy' user in the container
393 to the user id with which the script has been called initially.
394 """
395 cmd = ["/usr/sbin/usermod", "-u", new_id, "galaxy"]
396 subprocess.call(cmd)
397
398
399 def update_permissions():
400 cmd = ["/bin/chown", "-R", "galaxy:galaxy", "/home/galaxy"]
401 subprocess.call(cmd)
402
378 def main(): 403 def main():
379 u = """ 404 u = """
380 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as: 405 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
381 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript" 406 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
382 </command> 407 </command>
394 a('--user_email',default='Unknown') 419 a('--user_email',default='Unknown')
395 a('--bad_user',default=None) 420 a('--bad_user',default=None)
396 a('--make_HTML',default=None) 421 a('--make_HTML',default=None)
397 a('--new_tool',default=None) 422 a('--new_tool',default=None)
398 a('--dockerized',default=0) 423 a('--dockerized',default=0)
424 a('--group_id',default=None)
425 a('--user_id',default=None)
399 a('--output_format', default='tabular') 426 a('--output_format', default='tabular')
400 a('--input_format', dest='input_formats', action='append', default=[]) 427 a('--input_format', dest='input_formats', action='append', default=[])
401 a('--additional_parameters', dest='additional_parameters', action='append', default=[]) 428 a('--additional_parameters', dest='additional_parameters', action='append', default=[])
402 opts = op.parse_args() 429 opts = op.parse_args()
403 assert not opts.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to admin_users in universe_wsgi.ini' % (opts.bad_user,opts.bad_user) 430 assert not opts.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to admin_users in universe_wsgi.ini' % (opts.bad_user,opts.bad_user)
408 except: 435 except:
409 pass 436 pass
410 if opts.dockerized==0: 437 if opts.dockerized==0:
411 switch_to_docker(opts) 438 switch_to_docker(opts)
412 return 439 return
440 change_user_id(opts.user_id)
441 change_group_id(opts.group_id)
442 os.setuid(int(opts.user_id))
443 os.setgid(int(opts.group_id))
444 update_permissions()
413 r = ScriptRunner(opts) 445 r = ScriptRunner(opts)
414 retcode = r.run() 446 retcode = r.run()
415 os.unlink(r.sfile) 447 os.unlink(r.sfile)
416 if retcode: 448 if retcode:
417 sys.exit(retcode) # indicate failure to job runner 449 sys.exit(retcode) # indicate failure to job runner