summaryrefslogtreecommitdiff
path: root/ambient-build-vm
blob: c2334370583c7132c8c2e384c9a563dda523ae1f (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
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
#!/usr/bin/python3

import argparse
import logging
import os
import shutil
import subprocess
import sys
import tempfile
import yaml


PROG = "ambient-build-vm"
DESC = "build virtual machine image with Debian for Ambient"
VERSION = "0.1.0"
CACHE = os.path.expanduser("~/.cache/ambient")
BASE_VMDB = "base.vmdb"
BASE_PLAYBOOK = "playbook.yml"


def parse_args():
    p = argparse.ArgumentParser(prog=PROG, description=DESC)

    p.add_argument(
        "--version", action="version", version=VERSION, help="show version of program"
    )

    p.add_argument(
        "--data",
        default=f"/usr/share/{PROG}",
        help="look for data files in DATA directory",
    )

    p.add_argument(
        "--cache",
        action="store",
        help="set cache directory (default is %(default)s)",
        default=CACHE,
    )

    p.add_argument(
        "--playbook",
        action="append",
        help="extra Ansible playbook to use with vmdb to build image (default is none)",
    )

    p.add_argument(
        "--image",
        action="store",
        help="filename for resulting image (required)",
        required=True,
    )

    p.add_argument(
        "--debian",
        action="store",
        help="install Debian version (codename) instead of the default (%default)",
        default="bookworm",
    )

    return p.parse_args()


def join(args, relative):
    return os.path.join(args.data, relative)


def load_yaml(filename):
    with open(filename) as f:
        return yaml.safe_load(f)


def write_yaml(filename, obj):
    with open(filename, "w") as f:
        yaml.safe_dump(obj, stream=f, indent=4)


def prepare_build_files(tmp, args):
    vmdb_filename = os.path.join(tmp, "image.vmdb")
    playbook_filename = os.path.join(tmp, "playbook.yml")

    vmdb = load_yaml(join(args, BASE_VMDB))

    for step in vmdb["steps"]:
        if "debootstrap" in step:
            step["debootstrap"] = args.debian

    for playbook in args.playbook or []:
        vmdb["steps"].append(
            {
                # note: the value MUST be the root file system tag in the VMDB file
                "ansible": "/",
                "playbook": playbook,
            }
        )
        shutil.copyfile(playbook, os.path.join(tmp, playbook))

    write_yaml(vmdb_filename, vmdb)
    shutil.copyfile(join(args, BASE_PLAYBOOK), playbook_filename)

    return vmdb_filename


def build_image(tmp, vmdb, tarball, image):
    raw = os.path.join(tmp, "image.img")
    log = os.path.join(tmp, "log")

    # Build image.
    logging.info(f"Building RAW image {raw} with vmdb2, using {tarball}")
    p = subprocess.run(
        ["vmdb2", vmdb, "--output", raw, "--rootfs-tarball", tarball, "--log", log],
        capture_output=True,
    )
    if p.returncode != 0:
        with open(log, "r") as f:
            log = f.read()
        sys.stderr.write(f"{log}\nERROR: failed to build image\n")
        sys.exit(1)

    # Convert built image from raw to QCOW2 format.
    logging.info(f"Converting RAW image to {image}")
    p = subprocess.run(
        ["qemu-img", "convert", "-f", "raw", "-O", "qcow2", raw, image],
        capture_output=True,
    )
    if p.returncode != 0:
        sys.stderr.write(
            f"{p.stderr.decode()}\nERROR: failed to convert image to QCOW2\n"
        )
        sys.exit(1)


def main():
    logging.basicConfig(
        level=logging.DEBUG, stream=sys.stdout, format="%(levelname)s %(message)s"
    )
    logging.info(f"{PROG} {VERSION} starts")

    args = parse_args()
    tarball = os.path.join(args.cache, "ambient.tar.gz")
    tmp = tempfile.mkdtemp()

    logging.info("Creating build files")
    try:
        vmdb = prepare_build_files(tmp, args)
    except Exception as e:
        sys.stderr.write(f"ERROR: failed to create build files: {e}\n")
        shutil.rmtree(tmp)
        sys.exit(1)

    logging.info(f"Build image {args.image}")
    try:
        build_image(tmp, vmdb, tarball, args.image)
    except Exception as e:
        sys.stderr.write(f"ERROR: failed to build image: {e}\n")
        shutil.rmtree(tmp)
        sys.exit(1)

    logging.info("All good")


main()