aboutsummaryrefslogtreecommitdiffstats
path: root/deckit/modules/sbbuilder.py
blob: cefe2a3cdb297d8a39a26b97090a4ae4bae2d0d6 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# deck-it - sbbuilder module
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""sb-builder build state, read-only over ssh.

`sbb-remote stats` carries the dashboard: VM load, uptime, memory, disk, package
counts, orphans, slackrepo results and overlay pulls. `jobs` adds the recent
build list. Both run over one ssh connection and arrive as two JSON objects.
"""

import json
import subprocess

from ..module import Module


def _run(cmd, timeout=8):
    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 stats; sbb-remote jobs"]).lstrip()
        except OSError:
            return {"offline": True}
        try:
            decoder = json.JSONDecoder()
            stats, end = decoder.raw_decode(out)
            jobs, _ = decoder.raw_decode(out[end:].lstrip())
        except (ValueError, TypeError):
            return {"offline": True}
        return {"offline": False, "stats": stats,
                "jobs": jobs.get("jobs", []) if isinstance(jobs, dict) else []}

    return Module(name="sbbuilder", title="Build VM", state=state)