-
Notifications
You must be signed in to change notification settings - Fork 16
/
build
executable file
·156 lines (140 loc) · 5.3 KB
/
build
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
import argparse
import os
import os.path
import platform
import re
import stat
import subprocess
import sys
import urllib.request
import yaml
def call(verbose, arguments, **kwargs):
if verbose:
print_args = [a.replace(" ", "\\ ") for a in arguments]
print_args = [a.replace('"', '\\"') for a in print_args]
print_args = [a.replace("'", "\\'") for a in print_args]
print(" ".join(print_args))
subprocess.check_call(arguments, **kwargs)
def load_env(env_files):
"""
Parse env files and return environment as dict
"""
env = {}
for env_file in env_files:
with open(env_file) as f:
for line in f:
if line and line[0] != "#":
try:
index = line.index("=")
env[line[:index].strip()] = line[index + 1 :].strip()
except ValueError:
# Ignore lines that don't have a '='
pass
return env
def main():
parser = argparse.ArgumentParser(description="Build the project")
parser.add_argument("--verbose", help="Display the docker build commands")
parser.add_argument("--config", action="store_true", help="Build only the configuration image")
parser.add_argument("--geoportal", action="store_true", help="Build only the geoportal image")
parser.add_argument("--upgrade", help="Start upgrading the project to version")
parser.add_argument("env", nargs="*", help="The environment config")
args = parser.parse_args()
if args.upgrade:
major_version = args.upgrade
match = re.match(r"^([0-9]+\.[0-9]+)\.[0-9]+$", args.upgrade)
if match is not None:
major_version = match.group(1)
match = re.match(r"^([0-9]+\.[0-9]+)\.[0-9]+\.[0-9]+$", args.upgrade)
if match is not None:
major_version = match.group(1)
full_version = args.upgrade if args.upgrade != "master" else "latest"
with open("upgrade", "w") as f:
result = urllib.request.urlopen(
"https://raw.githubusercontent.com/camptocamp/c2cgeoportal/{}/scripts/upgrade".format(
major_version
)
)
if result.code != 200:
print("ERROR:")
print(result.read())
sys.exit(1)
f.write(result.read().decode())
os.chmod("upgrade", os.stat("upgrade").st_mode | stat.S_IXUSR)
try:
if platform.system() == "Windows":
subprocess.check_call(["python", "upgrade", full_version])
else:
subprocess.check_call(["./upgrade", full_version])
except subprocess.CalledProcessError:
sys.exit(1)
sys.exit(0)
with open("project.yaml") as project_file:
project_env = yaml.load(project_file, Loader=yaml.SafeLoader)["env"]
if len(args.env) != project_env["required_args"]:
print(project_env["help"])
sys.exit(1)
env_files = [e.format(*args.env) for e in project_env["files"]]
print("Use env files: {}".format(", ".join(env_files)))
for env_file in env_files:
if not os.path.exists(env_file):
print("Error: the env file '{}' does not exist.".format(env_file))
sys.exit(1)
env = load_env(env_files)
base = env["DOCKER_BASE"] if "DOCKER_BASE" in env else "camptocamp/geoportailv3"
tag = ":" + env["DOCKER_TAG"] if "DOCKER_TAG" in env else ""
schema = env["PGSCHEMA"]
http_proxy = ""
https_proxy = ""
if "http_proxy" in env:
http_proxy = env["http_proxy"]
if "https_proxy" in env:
https_proxy = env["https_proxy"]
default = not (args.config or args.geoportal)
if default or args.config:
call(
args.verbose,
[
"docker",
"build",
"--tag={}-config{}".format(base, tag),
"--build-arg=PGSCHEMA=" + schema,
"--build-arg=HTTP_PROXY_URL=" + http_proxy,
"--build-arg=HTTPS_PROXY_URL=" + https_proxy,
".",
],
)
if default or args.geoportal:
git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip().decode()
call(
args.verbose,
[
"docker",
"build",
"--tag={}-geoportal{}".format(base, tag),
"--build-arg=PGSCHEMA=" + schema,
"--build-arg=GIT_HASH=" + git_hash,
"--build-arg=HTTP_PROXY_URL=" + http_proxy,
"--build-arg=HTTPS_PROXY_URL=" + https_proxy,
"geoportal",
],
)
call(
args.verbose,
[
"docker",
"build",
"--target=builder",
"--tag={}-geoportal-dev{}".format(base, tag),
"--build-arg=HTTP_PROXY_URL=" + http_proxy,
"--build-arg=HTTPS_PROXY_URL=" + https_proxy,
"geoportal",
],
)
with open(".env", "w") as dest:
for file_ in env_files:
with open(file_) as src:
dest.write(src.read() + "\n")
dest.write("# Used env files: {}\n".format(" ".join(env_files)))
if __name__ == "__main__":
main()