forked from yellow-sky/ngwdocker
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ngwdocker.py
272 lines (209 loc) · 7.75 KB
/
ngwdocker.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import sys
python3 = True
try:
import importlib.util
except ImportError:
python3 = False
import imp
from collections import OrderedDict
if python3:
from io import StringIO
else:
from io import BytesIO as StringIO
import os
import os.path
from datetime import datetime
import random
import string
from enum import Enum
import yaml
import click
class Mode(Enum):
PRODUCTION = 'production'
DEVELOPMENT = 'development'
class Dockerfile:
def __init__(self):
self.buf = StringIO()
def write(self, *lines):
self.buf.write("\n".join(lines) + "\n")
class PackageBase:
def __init__(self, name):
self.name = name
def options(self, func):
return func
def debpackages(self):
return ()
def envsetup(self):
pass
def load_module(module_name, filename):
try:
if python3:
spec = importlib.util.spec_from_file_location(module_name, filename)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
else:
module = imp.load_source(module_name.split('.')[-1], filename)
return module
except:
return None
def load_packages():
pkgnames = sorted(filter(
lambda p: os.path.isdir(os.path.join('package', p)),
os.listdir('package')))
packages = OrderedDict()
for p in pkgnames:
module = load_module(
'{}.docker'.format(p),
os.path.join('package', p, 'docker.py')
)
if module is not None:
packages[p] = module.Package(p)
return packages
def read_envfile(fn='.env'):
result = dict()
if os.path.isfile(fn):
with open(fn, 'r') as fp:
for l in fp:
var, val = l.rstrip('\n').split('=', 1)
result[var] = val
return result
def write_envfile(values, fn='.env'):
# Keep original file backup on content changes
original = read_envfile(fn)
if original != values and original != dict():
suffix = datetime.now().replace(microsecond=0).isoformat() \
.replace(':', '').replace('-', '').replace('T', '-')
os.rename(fn, fn + '-' + suffix)
# Write new file contents
with open(fn, 'w') as fd:
for k, v in values.items():
fd.write('{}={}\n'.format(k, v))
def pwgen(length=16):
return ''.join(random.SystemRandom().choice(
string.ascii_letters + string.digits) for _ in range(16))
def main(packages, ctx, mode, **kwargs):
dockerfile = Dockerfile()
for p in packages.values():
p.dockerfile = dockerfile
dockerfile.write(
"FROM ubuntu:18.04", "",
"ENV LC_ALL=C.UTF-8",
"")
dockerfile.write("# DEBPACKAGES", "")
debpackages = [
('common', (
'git', 'mc', 'build-essential', 'python-dev',
'virtualenv', 'python-virtualenv'))]
for p in packages.values():
pkgs = p.debpackages()
if len(pkgs) > 0:
debpackages.append((p.name, p.debpackages()))
line = "RUN apt-get update && \\\n" \
" DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \\\n"
line += " \\\n".join(map(lambda i: ' ' + ' '.join(i[1]), debpackages))
line += " && \\\n rm -rf /var/lib/apt/lists/*"
dockerfile.write(line, "")
dockerfile.write(
" # SETUP NGW USER", "",
"ARG uid=1000",
"ARG gid=1000",
"RUN groupadd -g $gid ngw && useradd --home /opt/ngw -u $uid -g $gid ngw",
"RUN mkdir -p /opt/ngw/build /opt/ngw/package /opt/ngw/data",
"RUN chown -R ngw:ngw /opt/ngw",
"WORKDIR /opt/ngw",
"USER ngw",
"")
dockerfile.write(
"# VIRTUALENV", "",
"RUN /usr/bin/virtualenv /opt/ngw/env",
"RUN /opt/ngw/env/bin/pip install --no-cache-dir uwsgi")
for p in packages.values():
dockerfile.write('\n# {}\n'.format(p.name))
p.envsetup()
dockerfile.write("\n# NEXTGISWEB PACKAGES", "")
for p in packages.values():
if mode == Mode.PRODUCTION:
dockerfile.write(
"COPY --chown=ngw:ngw package/{pkg} /opt/ngw/package/{pkg}".format(pkg=p.name),
"RUN /opt/ngw/env/bin/pip install --no-cache-dir -e /opt/ngw/package/{pkg}".format(pkg=p.name))
else:
# Copy only setup.py for requirements and entrypoints installation
dockerfile.write(
"COPY --chown=ngw:ngw package/{pkg}/setup.py /opt/ngw/package/{pkg}/setup.py".format(pkg=p.name),
"RUN /opt/ngw/env/bin/pip install --no-cache-dir -e /opt/ngw/package/{pkg}".format(pkg=p.name))
dockerfile.write("")
if mode == Mode.PRODUCTION:
dockerfile.write(
"RUN /opt/ngw/env/bin/nextgisweb-i18n compile",
"")
dockerfile.write("# FINALIZE", "")
dockerfile.write(
"COPY docker-entrypoint.sh /",
"COPY --chown=ngw:ngw bin /opt/ngw/env/bin",
"COPY --chown=ngw:ngw config /opt/ngw/config",
"")
dockerfile.write('ENTRYPOINT ["/docker-entrypoint.sh"]', '')
dockerfile.write(
'ENV NGWDOCKER_MODE {}'.format(mode.value),
'ENV NGWDOCKER_PACKAGES {}'.format(" ".join(packages.keys())),
'ENV NEXTGISWEB_CONFIG /opt/ngw/config/config.ini',
'ENV NEXTGISWEB_LOGGING /opt/ngw/config/logging.ini',
"")
with open('Dockerfile', 'w') as fd:
fd.write(dockerfile.buf.getvalue())
dcomp = OrderedDict(version="3.2")
envfile = read_envfile()
dcomp['services'] = svc = OrderedDict()
dcomp['volumes'] = vlm = OrderedDict()
# Service: app
srcvol = ["./package:/opt/ngw/package"] if mode == Mode.DEVELOPMENT else []
bargs = OrderedDict(uid=os.getuid(), gid=os.getgid()) \
if mode == Mode.DEVELOPMENT else {}
if 'SESSION_SECRET' not in envfile:
envfile['SESSION_SECRET'] = pwgen()
if 'DATABASE_PASSWORD' not in envfile:
envfile['DATABASE_PASSWORD'] = pwgen()
svc['app'] = OrderedDict(
build=OrderedDict(context=".", args=bargs),
command="pserve-development" if mode == Mode.DEVELOPMENT else 'uwsgi-production',
environment=OrderedDict(
NEXTGISWEB_CORE_DATABASE_PASSWORD="${DATABASE_PASSWORD}",
NEXTGISWEB_PYRAMID_SECRET="${SESSION_SECRET}",
NEXTGISWEB_DEFAULT_LOCALE="ru"),
depends_on=['postgres'],
volumes=['data:/opt/ngw/data'] + srcvol,
ports=['8080:8080'],
)
vlm['data'] = OrderedDict()
svc['postgres'] = OrderedDict(
build=OrderedDict(context='./postgres'),
environment=OrderedDict(POSTGRES_PASSWORD="${DATABASE_PASSWORD}"),
volumes=['postgres:/var/lib/postgresql/data']
)
vlm['postgres'] = OrderedDict()
if ctx.params['minio']:
svc['minio'] = OrderedDict(
image='minio/minio',
)
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
yaml.add_representer(OrderedDict, dict_representer)
write_envfile(envfile)
with open('docker-compose.yaml', 'w') as fd:
yaml.dump(dcomp, fd, default_flow_style=False)
if __name__ == '__main__':
packages = load_packages()
def package_options(func):
for p in packages.values():
func = p.options(func)
return func
@click.command()
@click.option('--production', 'mode', flag_value=Mode.PRODUCTION, default=True, help="Build production environment.")
@click.option('--development', 'mode', flag_value=Mode.DEVELOPMENT, help="Build development environment.")
@click.option('--minio', flag_value=True, help="Add minio docker service to compose file.")
@package_options
@click.pass_context
def cmd(ctx, **kwargs):
main(packages, ctx, **kwargs)
cmd()