~sircmpwn/builds.sr.ht

builds.sr.ht/master-shell -rwxr-xr-x 1.8 KiB
56c93dc7Dhruvin Gandhi submit_GET: accept note via query params a day ago
                                                                                
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
#!/usr/bin/env python3
from buildsrht.manifest import Manifest
from buildsrht.runner import queue_build
from buildsrht.types import Job, Task, User
from getopt import getopt, GetoptError
from srht.config import cfg, get_origin
from srht.database import DbSession
import os
import shlex
import sys
import yaml

db = DbSession(cfg("builds.sr.ht", "connection-string"))
db.init()

def fail(reason):
    owner = cfg("sr.ht", "owner-name")
    email = cfg("sr.ht", "owner-email")
    print(reason)
    print(f"Please reach out to {owner} <{email}> for support.")
    sys.exit(1)

username = sys.argv[1]
user = User.query.filter(User.username == username).one_or_none()
if not user:
    fail(f"Unknown user {username}")
cmd = os.environ.get("SSH_ORIGINAL_COMMAND") or ""
cmd = shlex.split(cmd)
if len(cmd) == 0:
    fail(f"Hi ~{username}! You've successfully authenticated, but I do not provide an interactive shell. Goodbye!")

if cmd[0] == "submit":
    try:
        opts, args = getopt(cmd[1:], "n:")
    except GetoptError as ex:
        fail(str(ex))

    if os.isatty(sys.stdin.fileno()):
        print("Enter build manifest:")
    _manifest = sys.stdin.read()
    _manifest = _manifest.replace("\t", "    ") # Appease YAML
    try:
        manifest = Manifest(yaml.safe_load(_manifest))
    except Exception as ex:
        fail(str(ex))
    job = Job(user, _manifest)
    job.image = manifest.image
    job.note = [y for x, y in opts if x == "-n"] or None
    db.session.add(job)
    db.session.flush()
    for task in manifest.tasks:
        t = Task(job, task.name)
        db.session.add(t)
        db.session.flush() # assigns IDs for ordering purposes
    queue_build(job, manifest) # commits the session
    url = f"{get_origin('builds.sr.ht', external=True)}/~{username}/job/{job.id}"
    print(url)
else:
    fail(f"Unknown command {cmd[0]}")