~sircmpwn/builds.sr.ht

builds.sr.ht/contrib/submit_image_build -rwxr-xr-x 1.5 KiB
5054bcbbHaowen Liu crontab: remove hirsute 10 days 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
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
from buildsrht.manifest import Manifest
from getopt import getopt
import json
import os
import requests
import sys
import yaml

opts, args = getopt(sys.argv[1:], "n:p:t:u:a:f:")
usage = f"Usage: {sys.argv[0]} [-n note] [-u builds url] [-p images path] [-a arch] [-f from image] image"

if len(args) != 1:
    print(usage)
    sys.exit(1)

opts = { key: value for (key, value) in opts }

with open("/etc/sr.ht/image-refresh-token") as f:
    token = f.read().strip()

arch = opts.get("-a", "x86_64")
note = opts.get("-n")
path = opts.get("-p", "/var/lib/images")
url = opts.get("-u", "https://builds.sr.ht")
from_image = opts.get("-f", None)
image = args[0]

if "/" in image:
    [image, release] = image.split("/")
else:
    release = None

with open(os.path.join(path, image, "build.yml")) as f:
    manifest = Manifest(yaml.safe_load(f.read()))

if release:
    if not manifest.environment:
        manifest.environment = dict()
    manifest.environment["release"] = release
    manifest.environment["arch"] = arch

if from_image:
    manifest.image = from_image

url = f"{url}/query"
query = """
mutation SubmitBuild($manifest: String!, $note: String!, $tags: [String!]!) {
    submit(manifest: $manifest, note: $note, tags: $tags) {
        id
    }
}
"""

r = requests.post(url, json={
    "query": query,
    "variables": {
        "manifest": manifest.to_yaml(),
        "note": note,
        "tags": [image] + ([release] if release else [])
    },
}, headers={
    "Authorization": f"Bearer {token}"
})

print(r.json())
sys.exit(r.status_code != 200)