forked from wellcomecollection/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_run.py
executable file
·156 lines (133 loc) · 4.67 KB
/
docker_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
Run a Docker image used by a Make task.
This script does _not_ run inside a container, so it has to work on any
system. This means:
1. No third-party Python libraries!
2. Python 2 and Python 3 compatible.
Arguments after the two flags are passed directly to ``docker run``.
"""
import argparse
import os
import subprocess
import sys
# Root of the Git repository
ROOT = (
subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
.decode("utf8")
.strip()
)
def _aws_credentials_args():
"""
Returns the arguments to add to ``docker run`` for sharing AWS credentials
with the running container.
"""
# THE AWS_PROFILE environment allows you to run operations in a
# non-default profile. If you have multiple profiles in your ~/.aws
# config, use this variable to choose a non-default profile. For example:
#
# AWS_PROFILE=platform ./docker_run.py --aws -- ...
#
# For details:
# https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html
try:
cmd = [
"--env",
"AWS_PROFILE=%s" % os.environ["AWS_PROFILE"],
# We need this environment variable or Terraform doesn't pick up
# the AWS_PROFILE variable. See:
# https://github.com/terraform-providers/terraform-provider-aws/issues/233
"--env",
"AWS_SDK_LOAD_CONFIG=1",
]
except KeyError:
cmd = []
if "AWS_ACCESS_KEY_ID" in os.environ:
print("*** Trying environment variables for AWS config...")
cmd.extend(
[
"--env",
"AWS_ACCESS_KEY_ID=%s" % os.environ.get("AWS_ACCESS_KEY_ID", ""),
"--env",
"AWS_SECRET_ACCESS_KEY=%s"
% os.environ.get("AWS_SECRET_ACCESS_KEY", ""),
"--env",
"AWS_REGION=%s" % os.environ.get("AWS_REGION", ""),
"--env",
"AWS_DEFAULT_REGION=%s" % os.environ.get("AWS_DEFAULT_REGION", ""),
]
)
else:
# If we're running this script from inside another Docker image,
# we need to make sure we still mount the correct AWS directory
# in the new container.
try:
user_dir = os.environ["USER_DIR"]
except KeyError:
user_dir = os.environ["HOME"]
print("*** Missing environment variable, using ~/.aws")
aws_path = os.path.join(user_dir, ".aws")
cmd.extend(["--volume", "%s:/root/.aws" % aws_path])
cmd.extend(["--env", "USER_DIR=%s" % user_dir])
return cmd
def parse_args():
parser = argparse.ArgumentParser(
description="Run a Docker image used by a Make task."
)
parser.add_argument(
"--aws",
dest="share_aws_creds",
action="store_const",
const=True,
help="Whether to share AWS credentials with the running container",
)
parser.add_argument(
"--dind",
dest="docker_in_docker",
action="store_const",
const=True,
help="Whether to allow this container to run Docker",
)
parser.add_argument(
"--sbt",
dest="share_sbt_dirs",
action="store_const",
const=True,
help="Whether to share sbt directories with the running container",
)
parser.add_argument(
"--root",
dest="expose_host_root_folder",
action="store_const",
const=True,
help="Whether to expose the name of the root folder of the repository in the host",
)
return parser.parse_known_args()
if __name__ == "__main__":
namespace, additional_args = parse_args()
cmd = ["docker", "run", "--tty", "--rm"]
if namespace.share_aws_creds:
cmd += _aws_credentials_args()
if namespace.expose_host_root_folder or namespace.docker_in_docker:
cmd += ["--volume", "%s:%s" % (ROOT, ROOT)]
cmd += ["--workdir", ROOT]
if namespace.docker_in_docker:
cmd += ["--volume", "/var/run/docker.sock:/var/run/docker.sock"]
if namespace.share_sbt_dirs:
cmd += ["--volume", "%s/.sbt:/root/.sbt" % os.environ["HOME"]]
cmd += ["--volume", "%s/.ivy2:/root/.ivy2" % os.environ["HOME"]]
if not namespace.share_aws_creds:
cmd += _aws_credentials_args()
if namespace.expose_host_root_folder:
cmd += ["-e", "ROOT=%s" % ROOT]
if additional_args[0] == "--":
additional_args = additional_args[1:]
cmd += additional_args
try:
print("*** Running %r" % " ".join(cmd))
rc = subprocess.call(cmd)
if rc != 0:
sys.exit(rc)
except KeyboardInterrupt:
sys.exit(1)