aboutsummaryrefslogtreecommitdiffstats
path: root/deckit/modules/sbbuilder.py
blob: 3a48a1d4fc6760253d91daffefd8ad24fa52e3d2 (plain)
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
"""sb-builder build progress, read-only over ssh.

The build VM's `sbb-remote jobs` already returns JSON; this module runs it over
the configured ssh host alias and relays it. Nothing here writes to the VM.
"""

import json
import subprocess

from ..module import Module


def _run(cmd, timeout=5):
    try:
        return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout).stdout
    except (OSError, subprocess.TimeoutExpired):
        return ""


def make(cfg: dict) -> Module:
    host = cfg.get("sbbuilder", {}).get("host", "buildsystem")
    run = cfg.get("run", _run)

    def state():
        try:
            out = run(["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=4",
                       host, "sbb-remote", "jobs"])
        except OSError:
            return {"offline": True}
        try:
            data = json.loads(out)
        except (ValueError, TypeError):
            return {"offline": True}
        return {"offline": False, "jobs": data.get("jobs", [])}

    return Module(name="sbbuilder", title="Builds", state=state)