Skip to content

Commit

Permalink
environment: docker workdir (#15)
Browse files Browse the repository at this point in the history
* environment: docker workdir
* respect workdir option in spec

* ok
  • Loading branch information
lukasheinrich committed Oct 27, 2017
1 parent d8d0803 commit 2f0cae3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
39 changes: 23 additions & 16 deletions packtivity/handlers/execution_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,41 @@ def prepare_docker_context(state,environment,log,metadata):
resources = environment['resources']
)
log.debug(report)

do_cvmfs = 'CVMFS' in environment['resources']
do_auth = ('GRIDProxy' in environment['resources']) or ('KRB5Auth' in environment['resources'])
log.debug('do_auth: %s do_cvmfs: %s',do_auth,do_cvmfs)

par_mounts = prepare_par_mounts(environment['par_mounts'], state)

return prepare_docker(state,do_cvmfs,do_auth,par_mounts,log,metadata)

def run_docker_with_script(state,environment,job,log,metadata):
image = environment['image']
imagetag = environment['imagetag']

script = job['script']
interpreter = job['interpreter']

log.debug('script is:')
log.debug('\n--------------\n'+script+'\n--------------')
docker_mod = prepare_docker_context(state,environment,log,metadata)
if 'PACKTIVITY_DRYRUN' in os.environ:
return

indocker = interpreter
envmod = 'source {} && '.format(environment['envscript']) if environment['envscript'] else ''
indocker = envmod+indocker

try:
with logutils.setup_logging_topic(metadata,state,'run', return_logger = True) as runlog:
subcmd = 'docker run --rm -i {docker_mod} {image}:{imagetag} sh -c \'{indocker}\' '.format(image = image, imagetag = imagetag, docker_mod = docker_mod, indocker = indocker)
subcmd = 'docker run --rm -i {workdir_flag} {docker_mod} {image}:{imagetag} sh -c \'{indocker}\' '.format(
image = image,
workdir_flag = '-w {}'.format(environment['workdir']) if environment['workdir'] is not None else '',
imagetag = imagetag,
docker_mod = docker_mod,
indocker = indocker
)
log.debug('running docker cmd: %s',subcmd)
proc = subprocess.Popen(shlex.split(subcmd), stdin = subprocess.PIPE, stderr = subprocess.STDOUT, stdout = subprocess.PIPE, bufsize=1, close_fds = True)

Expand Down Expand Up @@ -163,28 +169,29 @@ def run_docker_with_script(state,environment,job,log,metadata):
log.exception("Unexpected error: %s",sys.exc_info())
raise
finally:

log.debug('finally for run')

def prepare_full_docker_with_oneliner(state,environment,command,log,metadata):
image = environment['image']
imagetag = environment['imagetag']

report = '''\n\
--------------
running one liner in container.
command: {command}
--------------
'''.format(command = command)
log.debug(report)

docker_mod = prepare_docker_context(state,environment,log,metadata)

envmod = 'source {} &&'.format(environment['envscript']) if environment['envscript'] else ''
in_docker_cmd = '{envmodifier} {command}'.format(envmodifier = envmod, command = command)
fullest_command = 'docker run --rm {docker_mod} {image}:{imagetag} sh -c \'{in_dock}\''.format(

fullest_command = 'docker run --rm {workdir_flag} {docker_mod} {image}:{imagetag} sh -c \'{in_dock}\''.format(
docker_mod = docker_mod,
workdir_flag = '-w {}'.format(environment['workdir']) if environment['workdir'] is not None else '',
image = image,
imagetag = imagetag,
in_dock = in_docker_cmd
Expand Down Expand Up @@ -272,9 +279,9 @@ def docker_enc_handler(environment,state,job,metadata):
tag = environment['imagetag']
)
docker_pull(docker_pull_cmd,log,state,metadata)

log.info('running job')

if 'command' in job:
# log.info('running oneliner command')
docker_run_cmd_str = prepare_full_docker_with_oneliner(state,environment,job['command'],log,metadata)
Expand Down Expand Up @@ -324,4 +331,4 @@ def test_process(environment,state,job,metadata):
log.info('a complicated test environment')
log.info('job: {}'.format(job))
log.info('env: {}'.format(environment))
log.info('state {}'.format(state))
log.info('state {}'.format(state))
4 changes: 4 additions & 0 deletions packtivity/syncbackends.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ def build_env(environment,parameters,state,pack_config):
builds an environment template description and builds a fully-defined env
this will use a handler in the future (just as build_job)
'''

env = copy.deepcopy(environment)
if environment['environment_type'] == 'docker-encapsulated':
for i,x in enumerate(env['par_mounts']):
script = x.pop('jqscript')
x['mountcontent'] = jq.jq(script).transform(parameters, text_output = True)

if env['workdir'] is not None:
env['workdir'] = state.contextualize_data(env['workdir'])
return env

def run_in_env(environment,job,state,metadata,pack_config):
Expand Down

0 comments on commit 2f0cae3

Please sign in to comment.