From 5f95a5ddd3ff07555cf7b5e0c28ed51bcf77c61c Mon Sep 17 00:00:00 2001 From: Romain Primet Date: Sun, 7 Jan 2018 15:43:03 +0000 Subject: [PATCH] Provide a flag to pass environment variables at runtime --- repo2docker/app.py | 17 ++++++++++++++++- tests/argumentvalidation.py | 12 +++++++++++- tests/env.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 tests/env.py diff --git a/repo2docker/app.py b/repo2docker/app.py index 2ac995ee..380ae56e 100644 --- a/repo2docker/app.py +++ b/repo2docker/app.py @@ -297,6 +297,14 @@ class Repo2Docker(Application): help='Username of the primary user in the image', ) + argparser.add_argument( + '--env', '-e', + dest='environment', + nargs='+', + help='Environment variables to define at container run time', + default=[] + ) + return argparser def json_excepthook(self, etype, evalue, traceback): @@ -389,6 +397,12 @@ class Repo2Docker(Application): if args.build_memory_limit: self.build_memory_limit = args.build_memory_limit + if args.environment and not self.run: + print("To specify environment variables, you also need to run the container") + sys.exit(1) + + self.environment = args.environment + def push_image(self): client = docker.APIClient(version='auto', **kwargs_from_env()) # Build a progress setup for each layer, and only emit per-layer @@ -442,7 +456,8 @@ class Repo2Docker(Application): ports=ports, detach=True, command=run_cmd, - volumes=container_volumes + volumes=container_volumes, + environment=self.environment ) while container.status == 'created': time.sleep(0.5) diff --git a/tests/argumentvalidation.py b/tests/argumentvalidation.py index f059cd16..ced73636 100644 --- a/tests/argumentvalidation.py +++ b/tests/argumentvalidation.py @@ -118,4 +118,14 @@ def test_volume_no_run_fail(): builddir = os.path.dirname(__file__) args_list = ['--no-run', '-v', '/data:/data'] - assert not validate_arguments(builddir, args_list, 'To Mount volumes with -v, you also need to run the container') \ No newline at end of file + assert not validate_arguments(builddir, args_list, 'To Mount volumes with -v, you also need to run the container') + +def test_env_no_run_fail(): + """ + Test to check if repo2docker fails when both --no-run and -e arguments are given + """ + builddir = os.path.dirname(__file__) + args_list = ['--no-run', '-e', 'FOO=bar', '--'] + + assert not validate_arguments(builddir, args_list, 'To specify environment variables, you also need to run the container') + diff --git a/tests/env.py b/tests/env.py new file mode 100644 index 00000000..21d3f9e0 --- /dev/null +++ b/tests/env.py @@ -0,0 +1,30 @@ +""" +Test that volume mounts work when running +""" +import os +import subprocess +import tempfile +import time + +def test_env(): + """ + Validate that you can define environment variables + """ + ts = str(time.time()) + with tempfile.TemporaryDirectory() as tmpdir: + username = os.getlogin() + subprocess.check_call([ + 'repo2docker', + '-v', '{}:/home/{}'.format(tmpdir, username), + '-e', 'FOO={}'.format(ts), 'BAR=baz', + '--', + tmpdir, + '/bin/bash', + '-c', 'echo -n $FOO > ts && echo -n $BAR > bar' + ]) + + with open(os.path.join(tmpdir, 'ts')) as f: + assert f.read().strip() == ts + with open(os.path.join(tmpdir, 'bar')) as f: + assert f.read().strip() == 'baz' +