/
tasks.py
246 lines (213 loc) · 6.69 KB
/
tasks.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
import os
import pathlib
import subprocess
import webbrowser
from invoke import task
from invoke.tasks import call
from spherical.dev.tasks import clean, dev, flake, isort, test # noqa: F401
from spherical.dev.utils import flatten_options, named_args
CONTAINERS_BASE = 'containers'
IMAGE = 'lektorium'
CONTAINER = IMAGE
LEKTOR_BASE = PROXY_BASE = 'alpine'
LEKTOR_IMAGE = f'{IMAGE}-lektor'
PROXY_IMAGE = f'{IMAGE}-proxy'
PROXY_CONTAINER = PROXY_IMAGE
def get_config(ctx, env, cfg, auth, network):
if env is None:
env = named_args('-e ', ctx.get('env', {}))
if cfg is None:
cfg = ctx['cfg']
cfg = f'{cfg["repo"]}:{",".join(cfg["server"])}'
if auth is None:
auth = ctx.get('auth', None)
if auth is not None:
auth = ','.join(auth)
network = network or ctx.get('network')
return env, cfg, auth, network
@task
def build_lektor_image(ctx):
lektor_dir = f'{CONTAINERS_BASE}/lektor'
ctx.run((
'docker build '
f'--build-arg BASE_IMAGE={LEKTOR_BASE} '
f'--tag {LEKTOR_IMAGE} {lektor_dir}'
))
@task
def build_nginx_image(ctx, server_name=None):
if server_name is None:
server_name = ctx['server-name']
proxy_dir = f'{CONTAINERS_BASE}/nginx-proxy'
ctx.run((
'docker build '
f'--build-arg BASE_IMAGE={PROXY_BASE} '
f'--build-arg SERVER_NAME={server_name} '
f'--tag {PROXY_IMAGE} {proxy_dir}'
))
def lektorium_labels(server_name, port=80):
labels = {
'enable': 'true',
'http.services.lektorium.loadbalancer.server.port': f'{port}',
'http.routers': {
'lektorium': {
'entrypoints': 'websecure',
'rule': f"Host(`{server_name}`)",
'tls.certresolver': 'le',
},
},
}
return named_args("--label ", flatten_options(labels, "traefik"))
@task
def run_nginx(ctx, network=None):
*_, network = get_config(ctx, None, None, None, network)
ctx.run(f'docker kill {PROXY_CONTAINER}', warn=True)
ctx.run(f'docker rm {PROXY_CONTAINER}', warn=True)
ctx.run((
f'docker run '
f'-d --restart unless-stopped '
f'--net {network} '
f'--name {PROXY_CONTAINER} '
f'{lektorium_labels(ctx["server-name"])} '
f'{PROXY_IMAGE} '
))
@task
def run_traefik(ctx, image='traefik', ip=None, network=None):
if ctx.get('skip-traefik', False):
return
env, *_, network = get_config(ctx, None, None, None, network)
ctx.run(f'docker kill {PROXY_CONTAINER}', warn=True)
ctx.run(f'docker rm {PROXY_CONTAINER}', warn=True)
resolver = flatten_options(
ctx['certificate-resolver'],
'certificatesresolvers.le.acme',
)
resolver = named_args('--', resolver)
labels = {
'enable': 'true',
'http': {
'middlewares': {
'redirect-to-https.redirectscheme.scheme': 'https',
},
'routers': {
'redirect-to-https': {
'entrypoints': 'web',
'rule': 'HostRegexp(`{host:.+}`)',
'middlewares': 'redirect-to-https',
},
},
},
}
labels = named_args('--label ', flatten_options(labels, 'traefik'))
ctx.run((
'docker create '
'--restart unless-stopped '
f'--name {PROXY_CONTAINER} '
'-v /var/run/docker.sock:/var/run/docker.sock '
'-v traefik-letsencrypt:/letsencrypt '
f'-p {ip or ""}{":" if ip else ""}80:80 '
f'-p {ip or ""}{":" if ip else ""}443:443 '
f'{env} '
f'{labels} '
f'{image} '
'--accessLog '
'--api.dashboard '
f'{resolver} '
'--entrypoints.web.address=:80 '
'--entrypoints.websecure.address=:443 '
'--log.level=DEBUG '
'--log '
'--providers.docker.exposedbydefault=false '
))
ctx.run(f'docker network create {network}', warn=True)
ctx.run(f'docker network connect {network} {PROXY_CONTAINER}')
ctx.run(f'docker start {PROXY_CONTAINER}')
@task
def build_server_image(ctx):
if 'key' not in ctx:
raise RuntimeError('pleas provde key for access server to gitlab')
server_dir = f'{CONTAINERS_BASE}/server'
with (pathlib.Path(server_dir) / 'key').open('w') as key_file:
key = os.linesep.join((
'-----BEGIN RSA PRIVATE KEY-----',
*ctx['key'],
'-----END RSA PRIVATE KEY-----',
'',
))
key_file.write(key)
ctx.run(f'rm {server_dir}/lektorium*.whl', warn=True)
ctx.run(f'pip wheel -w {server_dir} --no-deps .')
ctx.run((
f'docker build --tag {IMAGE} '
f'-f {server_dir}/Dockerfile.ubuntu '
f'{server_dir}'
))
@task(build_lektor_image, build_server_image)
def build(ctx):
pass
@task
def local(ctx, cfg=None, auth=None):
_, cfg, auth, _ = get_config(ctx, None, cfg, auth, None)
ctx.run(f'python -m lektorium {cfg} {auth}')
@task
def run(
ctx,
create_options='--restart unless-stopped',
start_options=None,
env=None,
cfg=None,
auth=None,
network=None,
):
env, cfg, auth, network = get_config(ctx, env, cfg, auth, network)
ctx.run(f'docker stop {CONTAINER}', warn=True)
ctx.run(f'docker kill {CONTAINER}', warn=True)
ctx.run(f'docker rm {CONTAINER}', warn=True)
ctx.run((
f'docker create {create_options} {env} '
f'--name {CONTAINER} '
f'--net {network} '
f'-v lektorium-sessions:/sessions '
f'-v /var/run/docker.sock:/var/run/docker.sock '
f'{lektorium_labels(ctx["server-name"], 8000)} '
f'{IMAGE} '
f'{cfg} "{auth}"'
))
ctx.run(f'docker network create {network}', warn=True)
ctx.run(f'docker network connect {network} {CONTAINER}')
ctx.run(f'docker start {start_options or ""} {CONTAINER}')
@task(build, run_traefik, run)
def deploy(ctx):
pass
@task
def debug(ctx, env=None, cfg=None, auth=None, network=None):
env, cfg, auth, network = get_config(ctx, env, cfg, auth, network)
call(
run,
create_options='-ti --rm',
start_options='-i',
env=env,
cfg=cfg,
auth=auth,
network=network,
)
@task
def list(ctx):
proc = subprocess.Popen(
'python -u -m lektorium LIST',
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
target = None
for line in proc.stdout:
print(line.decode().rstrip())
if b'Running on' in line:
target = line[20:-10].decode()
break
if target is not None:
webbrowser.open(target)
try:
proc.communicate()
except Exception:
proc.kill()
proc.wait()