forked from camptocamp/c2cgeoportal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-run
executable file
·128 lines (109 loc) · 4.78 KB
/
docker-run
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
#!/usr/bin/env python3
import os
import argparse
import subprocess
import netifaces
import configparser
import sys
def main():
config_parser = configparser.ConfigParser()
if os.path.exists(".config"):
config_parser.read(".config")
if "docker-run" not in config_parser:
config_parser["docker-run"] = {}
config = config_parser["docker-run"]
parser = argparse.ArgumentParser(description="Run docker build.")
parser.add_argument("--root", action="store_true", help="Be root in the container")
parser.add_argument("--home", action="store_true", help="Mount the home directory")
parser.add_argument("-ti", action="store_true", help="Use -ti docker run option")
parser.add_argument("--env", action='append', default=[], help="A variable environment to pass or set")
parser.add_argument("--share", action='append', default=[], help="A folder to share as a volume")
parser.add_argument(
"--image", default=config.get("image", "camptocamp/geomapfish-build-dev"),
help="The docker image to use"
)
parser.add_argument(
"--version", default=config.get("version", "2.3"),
help="The docker image version to use"
)
parser.add_argument("cmd", metavar="CMD", help="The command to run")
parser.add_argument("args", metavar="...", nargs=argparse.REMAINDER, help="The command arguments")
options = parser.parse_args()
docker_cmd = ['docker', 'run']
if options.ti:
docker_cmd.append("-ti")
for share in options.share:
docker_cmd.append("--volume={}:{}".format(share, share))
dir_path = os.path.dirname(os.path.realpath(__file__))
build_volume_name = dir_path[1:].replace("/", "-")
git_branch = subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode("utf-8").strip()
git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip()
try:
login = os.getlogin()
except FileNotFoundError:
# workaround on strange error
import pwd
login = pwd.getpwuid(os.getuid())[0]
except OSError:
# workaround on strange error
import pwd
login = pwd.getpwuid(os.getuid())[0]
docker_cmd.extend([
"--rm",
"--volume=/var/run/docker.sock:/var/run/docker.sock",
"--volume={}:/build".format(build_volume_name),
"--volume={pwd}:/src".format(pwd=os.getcwd()),
"--env=USER_NAME={}".format(login),
"--env=USER_ID={}".format(os.getuid() if os.name == "posix" else 1000),
"--env=GROUP_ID={}".format(os.getgid() if os.name == "posix" else 1000),
"--env=GIT_BRANCH={}".format(git_branch),
"--env=GIT_HASH={}".format(git_hash),
])
if options.home:
if options.root:
docker_cmd.append("--volume={home}:/root".format(home=os.environ["HOME"]))
docker_cmd.append("--volume={home}:{home}".format(home=os.environ["HOME"]))
else:
docker_cmd.append("--volume={}:/home".format(build_volume_name + "-home"))
if "SSH_AUTH_SOCK" in os.environ:
docker_cmd.extend([
"--volume={ssh}:{ssh}".format(ssh=os.environ["SSH_AUTH_SOCK"]),
"--env=SSH_AUTH_SOCK",
])
for env in [
"SRID",
"EXTENT",
"APACHE_VHOST",
"CI",
] + options.env:
docker_cmd.append("--env={}".format(env))
if 'docker0' in netifaces.interfaces():
docker_adrs = netifaces.ifaddresses('docker0')[2][0]['addr']
else:
# For Jenkins slave in Docker
docker_adrs = netifaces.gateways()[netifaces.AF_INET][0][0]
# We needs the user HOME directory to correctly create the user in Docker
docker_cmd.append("--env=HOME_DIR={}".format(os.environ["HOME"]))
docker_cmd.append("--env=DOCKER_HOST_={}".format(docker_adrs))
docker_cmd.append("--env=BUILD_VOLUME_NAME={}".format(build_volume_name))
docker_cmd.append("--env=PROJECT_DIRECTORY={}".format(os.getcwd()))
ifaddresses = [netifaces.ifaddresses(iface) for iface in netifaces.interfaces()]
# IP v4
adrs = [e[2][0]['addr'] for e in ifaddresses if 2 in e]
# IP v6
adrs += [e[10][0]['addr'].split('%')[0] for e in ifaddresses if 10 in e]
docker_cmd.append("--env=ADDRESSES={}".format(' '.join(adrs)))
docker_cmd.append("--env=EXTERNAL_PYTHON_VERSION_MAJOR={}".format(sys.version_info.major))
docker_cmd.append("--env=EXTERNAL_PYTHON_VERSION_MINOR={}".format(sys.version_info.minor))
if options.root:
docker_cmd.extend(["--entrypoint", options.cmd])
docker_cmd.append("{}:{}".format(options.image, options.version))
if not options.root:
docker_cmd.append(options.cmd)
docker_cmd.extend(options.args)
try:
subprocess.check_call(docker_cmd)
except subprocess.CalledProcessError:
exit(2)
if __name__ == "__main__":
main()