-
Notifications
You must be signed in to change notification settings - Fork 0
/
bao.py
487 lines (372 loc) · 13.2 KB
/
bao.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env python3.11
import argparse
import io
import logging
import os
import socket
import stat
import sys
if sys.version_info < (3, 11):
sys.exit("Python 3.11 required")
import shutil
import subprocess
import tempfile
import tomllib
from pathlib import Path
from dataclasses import dataclass
logging.basicConfig(level=logging.DEBUG, format="{levelname} {message}", style="{")
logger = logging.getLogger(__name__)
ROOT_PATH = Path("/home/bao").resolve()
APPS_ROOT_PATH = ROOT_PATH / "apps"
CADDYFILES_PATH = ROOT_PATH / "caddyfiles"
SYSTEMDFILES_PATH = ROOT_PATH / "systemdfiles"
SYSTEMCTL_RESTART_INTERVAL = 3
BAO_BIN_PATH = "/home/bao/.local/bin"
def get_systemctl_config(web_cmd: str, working_directory: str, description: str):
return f"""
[Unit]
Description={description}
[Service]
After=network.target
Restart=always
RestartSec={SYSTEMCTL_RESTART_INTERVAL}
WorkingDirectory={working_directory}
ExecStart={web_cmd}
[Install]
WantedBy=default.target
""".strip()
@dataclass
class Procfile:
web_cmd: str
release_cmd: str
def parse_procfile(content: str):
web_cmd = None
release_cmd = None
for line in content.splitlines():
if line.startswith("web:"):
if web_cmd:
logger.error("cannot have >1 web:")
sys.exit(1)
web_cmd = line.split(":", maxsplit=1)[-1].strip()
elif line.startswith("release:"):
if release_cmd:
logger.error("cannot have >1 release:")
sys.exit(1)
release_cmd = line.split(":", maxsplit=1)[-1].strip()
if not web_cmd.startswith("python"):
logger.info("web cmd should start with python")
sys.exit(1)
if "$PORT" not in web_cmd:
logger.info("$PORT is not present on web cmd")
sys.exit(1)
return Procfile(web_cmd=web_cmd, release_cmd=release_cmd)
def get_app_caddyfile_config(
domain: str, app_root_src_path: str, port: int, static_path: str
):
return (
"""
{domain} {
reverse_proxy localhost:{port}
handle_path /static/* {
file_server {
root {app_root_src_path}/{static_path}
}
}
}
""".strip()
.replace("{domain}", domain)
.replace("{app_root_src_path}", str(app_root_src_path))
.replace("{port}", str(port))
.replace("{static_path}", static_path)
)
@dataclass
class BaoConfigApp:
domain: str
static: str
procfile: str = "Procfile"
@dataclass
class BaoConfig:
"""bao.toml"""
apps: dict[str, BaoConfigApp]
def add_app(app_name: str):
"""
apps/
<appname>/
code/ (source code)
repo/ (git bare repo)
<appname>.service
Caddyfile
"""
pass
def get_free_port():
"""Find a free TCP port (entirely at random)"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0)) # lgtm [py/bind-socket-all-network-interfaces]
port = s.getsockname()[1]
s.close()
return port
def deploy_app(app_name: str):
app_path = APPS_ROOT_PATH / app_name
app_code_path = app_path / "code"
if not (app_code_path / "bao.toml").exists():
logger.info("bao.toml not detected")
sys.exit(1)
if not (app_code_path / "pyproject.toml").exists():
logger.info("pyproject.toml not detected")
sys.exit(1)
# -- parse project config
with open(app_code_path / "bao.toml", "rb") as f:
data = tomllib.load(f)
bao_config = BaoConfig(apps={s: BaoConfigApp(**d) for s, d in data["apps"].items()})
app_config = bao_config.apps.get(app_name)
if not app_config:
logger.info(f"{app_name} not found in bao config")
sys.exit(1)
logger.info(f"config: {app_config}")
if not (app_code_path / app_config.procfile).exists():
logger.info(f"{app_config.procfile} not detected")
sys.exit(1)
with open(app_code_path / app_config.procfile) as f:
procfile = parse_procfile(f.read())
# -- configure poetry
subprocess.run(
["poetry", "install"],
cwd=app_code_path,
check=True,
env={"PATH": f"{BAO_BIN_PATH}:{os.environ['PATH']}"},
)
subprocess.run([str(app_code_path / ".venv/bin/python"), "-V"], check=True)
# TODO: remove check=True and do proper validation and printing
# -- configure node
# this is useful for assets generated with vite for example
if (app_code_path / "package.json").exists():
subprocess.run(
["yarn", "install"],
cwd=app_code_path,
check=True,
env={"PATH": f"{BAO_BIN_PATH}:{os.environ['PATH']}"},
)
# -- configure systemctl
web_cmd = procfile.web_cmd
app_port = get_free_port()
# TODO: maybe use the same port when redeploying?
web_cmd = web_cmd.replace("$PORT", str(app_port))
logger.info(f"--- web cmd: {web_cmd!r}")
systemctl_config = get_systemctl_config(
web_cmd=f"{app_code_path / '.venv/bin'}/{web_cmd}",
working_directory=app_code_path,
description=f"{app_name} configured by bao",
)
app_service_path = app_path / f"{app_name}.service"
with open(app_service_path, "w") as f:
f.write(systemctl_config)
subprocess.run(["systemctl", "--user", "enable", app_service_path], check=True)
# -- configure caddy
app_caddy_config = get_app_caddyfile_config(
domain=app_config.domain,
app_root_src_path=app_code_path,
port=app_port,
static_path=app_config.static,
)
app_caddy_config_path = app_path / "Caddyfile"
app_caddy_config_path.write_text(app_caddy_config)
if (CADDYFILES_PATH / app_name).is_symlink():
(CADDYFILES_PATH / app_name).unlink()
(CADDYFILES_PATH / app_name).symlink_to(app_caddy_config_path)
# -- release cmd
subprocess.run(
procfile.release_cmd,
cwd=app_code_path,
check=True,
shell=True,
env={"PATH": f"{BAO_BIN_PATH}:{os.environ['PATH']}"},
)
# -- start app
subprocess.run(["sudo", "systemctl", "reload", "caddy"], check=True)
logger.info("caddy reloaded")
subprocess.run(["systemctl", "--user", "restart", app_name], check=True)
logger.info(f"{app_name} service restarted")
def remove_app(app_name: str):
app_path = APPS_ROOT_PATH / app_name
app_service_name = f"{app_name}.service"
subprocess.run(["systemctl", "--user", "stop", app_service_name], check=True)
subprocess.run(["systemctl", "--user", "disable", app_service_name], check=True)
subprocess.run(["sudo", "systemctl", "reload", "caddy"], check=True)
shutil.rmtree(app_path)
files_to_delete = (
SYSTEMDFILES_PATH / f"{app_name}.service",
CADDYFILES_PATH / app_name,
)
for file in files_to_delete:
if file.is_file():
file.unlink()
def init_systemctl():
# init user systemd on boot
# ref: https://wiki.archlinux.org/title/systemd/User#Automatic_start-up_of_systemd_user_instances
subprocess.run(["sudo", "loginctl", "enable-linger", "bao"], check=True)
def init_caddy():
with open(CADDYFILES_PATH / "Caddyfile", "w") as f:
f.write(
"""
* {
encode zstd gzip
}
"""
)
global_caddyfile = Path("/etc/caddy/Caddyfile")
config = f"""
# --- added by bao
import {CADDYFILES_PATH!s}/*
"""
if global_caddyfile.is_file():
existent_config = global_caddyfile.read_text()
if config in existent_config:
config = ""
config = existent_config + config
with tempfile.NamedTemporaryFile("w", delete=False) as tf:
tf.write(config)
subprocess.run(["sudo", "mv", tf.name, str(global_caddyfile)], check=True)
subprocess.run(["sudo", "chmod", "755", str(global_caddyfile)], check=True)
# -- configure sudoers
with tempfile.NamedTemporaryFile("w", delete=False) as tf:
tf.write("bao ALL=(root) NOPASSWD: /usr/bin/systemctl reload caddy\n")
subprocess.run(["sudo", "mv", tf.name, "/etc/sudoers.d/bao-caddy"], check=True)
subprocess.run(
["sudo", "chown", "root:root", "/etc/sudoers.d/bao-caddy"], check=True
)
AUTHORIZED_KEYS_TEMPLATE = """command="{entrypoint_path} $SSH_ORIGINAL_COMMAND",no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding {pubkey}\n"""
def init_ssh_access():
baoscript = Path(__file__).resolve()
authorized_keys_path = Path("/home/bao/.ssh/authorized_keys")
authorized_keys = authorized_keys_path.read_text()
new_authorized_keys = io.StringIO()
for line in authorized_keys.splitlines():
if not line.startswith("ssh-"):
new_authorized_keys.write(line)
continue
new_authorized_keys.write(
AUTHORIZED_KEYS_TEMPLATE.format(
entrypoint_path=str(baoscript), pubkey=line.strip()
)
)
with open(authorized_keys_path, "w") as f:
f.write(new_authorized_keys.getvalue())
def init():
"""
apps/
caddyfiles/
Caddyfile
<appname> -> ../apps/<appname>/Caddyfile
systemdfiles -> .config/systemd/user
"""
subprocess.run("sudo echo 'sudo access ok'", shell=True, check=True)
subprocess.run(["sudo", "chmod", "-R", "o=rwx", str(ROOT_PATH)], check=True)
for dir in (
APPS_ROOT_PATH,
CADDYFILES_PATH,
ROOT_PATH / ".config/systemd/user",
):
dir.mkdir(parents=True, exist_ok=True)
systemdfiles_path = ROOT_PATH / "systemdfiles"
if systemdfiles_path.is_symlink():
systemdfiles_path.unlink()
(ROOT_PATH / "systemdfiles").symlink_to(ROOT_PATH / ".config/systemd/user")
# -- install deps
init_systemctl()
init_caddy()
init_ssh_access()
subprocess.run(["sudo", "chmod", "-R", "o=rx", str(ROOT_PATH)], check=True)
subprocess.run(["sudo", "chown", "-R", "bao:bao", str(ROOT_PATH)], check=True)
# -- configure poetry
subprocess.run(
["sudo", "-iu", "bao", "poetry", "config", "virtualenvs.in-project", "true"],
check=True,
)
# --- CLI commands
def cmd_init(args: argparse.Namespace):
init()
def cmd_del(args: argparse.Namespace):
app_name = args.app_name
remove_app(app_name)
def cmd_git_receive_pack(args: argparse.Namespace):
app_name: str = args.app_name[1:-1] # remove quotes
app_path = APPS_ROOT_PATH / app_name
repo_path = app_path / "repo"
app_path.mkdir(exist_ok=True)
if not repo_path.is_dir():
subprocess.run(
["git", "init", "--bare", "--quiet", "repo"],
cwd=app_path,
check=True,
)
git_hook_path = repo_path / "hooks" / "post-receive"
if not git_hook_path.is_file():
with open(git_hook_path, "w") as f:
f.write(
f"""#!/usr/bin/bash
set -e; set -o pipefail;
cat | /home/bao/bao.py git-hook {app_name}
"""
)
git_hook_path.chmod(git_hook_path.stat().st_mode | stat.S_IEXEC)
# FIXME: redeploy when unsuccessfully last deployment even though we have the code
subprocess.run(
["git", "shell", "-c", "git receive-pack 'repo'"], cwd=app_path, check=True
)
# add_app(app_name)
def cmd_git_hook(args: argparse.Namespace):
app_name = args.app_name
app_path = APPS_ROOT_PATH / app_name
logger.info(f"--- post-receive hook called for {app_name}")
lines = list(sys.stdin)
if len(lines) != 1:
print(f"I don't know what do to with this input: {lines}")
oldrev, newrev, refname = lines[0].strip().split(" ")
app_path = APPS_ROOT_PATH / app_name
subprocess.run(
["git", "clone", "repo", "code"],
cwd=app_path,
check=False,
)
code_path = app_path / "code"
# FIXME: "fatal: not a git repository: '.'"
subprocess.run(["ls", "-la"], cwd=code_path)
subprocess.run(
["git", "fetch"],
cwd=code_path,
env={"GIT_DIR": str(code_path / ".git")},
)
subprocess.run(
["git", "reset", "--hard", newrev],
cwd=code_path,
env={"GIT_DIR": str(code_path / ".git")},
)
deploy_app(app_name)
if __name__ == "__main__":
parser = argparse.ArgumentParser("bao", description="PaaS for Python")
subparser = parser.add_subparsers()
init_parser = subparser.add_parser("init", description="init bao")
init_parser.set_defaults(handle=cmd_init)
del_parser = subparser.add_parser("del", description="delete an app")
del_parser.add_argument("app_name")
del_parser.set_defaults(handle=cmd_del)
git_receive_pack_parser = subparser.add_parser(
"git-receive-pack", description="[internal git] used in push"
)
git_receive_pack_parser.add_argument("app_name")
git_receive_pack_parser.set_defaults(handle=cmd_git_receive_pack)
git_hook_parser = subparser.add_parser(
"git-hook", description="[internal git] used in post-receive hook"
)
git_hook_parser.add_argument("app_name")
git_hook_parser.set_defaults(handle=cmd_git_hook)
# logger.info("[Bao]")
# logger.info(f"{sys.argv=}")
args = parser.parse_args(sys.argv[1:] or ["--help"])
# TODO: add validation that if command is different than init, it should be bao user
try:
args.handle(args)
except:
logger.exception("ops")
# init()
# configure_app("ad_automator_test", Path("/tmp/ad_automator_test"))