summaryrefslogtreecommitdiff
path: root/ambient-run
blob: 751fb23173836ec64d7e8e59cbf6ef50ea2f2115 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python3

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


MAX_CACHE_SIZE = 1024**3
OVMF_FD = "/usr/share/ovmf/OVMF.fd"
OVMF_VARS = "/usr/share/OVMF/OVMF_VARS.fd"


class BuildSpec:
    def __init__(self, filename):
        with open(filename) as f:
            o = yaml.safe_load(f)
        self.image = os.path.expanduser(o["image"])
        self.src = os.path.expanduser(o.get("src", "."))

        if "cache" in o:
            self.cache = os.path.expanduser(o["cache"])
        else:
            self.cache = None

        if "dependencies" in o:
            self.dependencies = os.path.expanduser(o["dependencies"])
        else:
            self.dependencies = None

        if "artifact" in o:
            self.artifact = os.path.expanduser(o["artifact"])
        else:
            self.artifact = None


def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument("--verbose", action="store_true")
    p.add_argument("--log", required=True)
    p.add_argument("build_spec")
    return p.parse_args()


def extract_tar_from_tar(filename, output):
    N = 1024
    BLOCK = 10 * 1024

    with open(filename, "rb") as f:
        tar = tarfile.open(fileobj=f, mode="r:")
        while True:
            info = tar.next()
            if info is None:
                break

        # We've read to the end of the final tar file entry.
        num_bytes = tar.fileobj.tell()

        with open(output, "wb") as o:
            # Copy first num_bytes of input file to output file.

            f.seek(0)
            n = 0
            while n < num_bytes:
                data = f.read(N)
                if data is None:
                    break
                o.write(data)
                n += len(data)

            # Pad output file with zeroes until next block border.

            while (n % BLOCK) != 0:
                o.write(b"\0")
                n += 1


def main():
    args = parse_args()

    if args.verbose:
        level = logging.DEBUG
    else:
        level = logging.WARNING
    logging.basicConfig(
        level=level, stream=sys.stdout, format="%(levelname)s %(message)s"
    )
    logging.debug(f"args: {args}")

    try:
        logging.debug(f"loading build spec from {args.build_spec}")
        build = BuildSpec(args.build_spec)
        logging.debug(f"build: {build}")

        tmp = tempfile.mkdtemp()
        logging.debug(f"created {tmp}")

        image = os.path.join(tmp, "vm.qcow2")
        output_drive = os.path.join(tmp, "artifacts.tar")
        src_tar = os.path.join(tmp, "src.tar")
        vars = os.path.join(tmp, "vars.fd")

        logging.info(f"copying {build.image} to {image}")
        shutil.copyfile(build.image, image)

        logging.info(f"copying {OVMF_VARS}")
        shutil.copyfile(OVMF_FD, vars)

        logging.info(f"create tar of source tree {build.src}")
        subprocess.run(["tar", "-cf", src_tar, "-C", build.src, "."], check=True)

        logging.info(f"create output drive {output_drive}")
        subprocess.run(
            ["qemu-img", "create", "-q", "-f", "raw", output_drive, "1G"], check=True
        )

        cache_drive = os.path.join(tmp, "cache.tar")
        logging.info(f"using {cache_drive} as cache drive")
        tar = tarfile.open(name=cache_drive, mode="w:")
        if build.cache is None:
            tar.close()
        else:
            # Tar up the cache directory.
            tar.add(build.cache, arcname=".")
            tar.close()

            # Make the tar the max size.
            length = os.path.getsize(cache_drive)
            if length < MAX_CACHE_SIZE:
                os.truncate(cache_drive, MAX_CACHE_SIZE)
            else:
                logging.info(
                    "cache drive {cache_drive} is larger than max allowed, oh well"
                )

        deps_drive = os.path.join(tmp, "deps.tar")
        logging.info(f"using {deps_drive} as the dependencies drive")
        tar = tarfile.open(name=deps_drive, mode="w:")
        if build.dependencies is None:
            tar.close()
        else:
            # Tar up the dependencies directory.
            tar.add(build.dependencies, arcname=".")
            tar.close()

        logging.info("run build in VM")
        argv = [
            "kvm",
            "-m",
            "16384",
            "-smp",
            "cpus=4",
            "-drive",
            f"if=pflash,format=raw,unit=0,file={OVMF_FD},readonly=on",
            "-drive",
            f"if=pflash,format=raw,unit=1,file={vars}",
            "-drive",
            f"format=qcow2,if=virtio,file={image}",
            "-drive",
            f"format=raw,if=virtio,file={src_tar},readonly=on",
            "-drive",
            f"format=raw,if=virtio,file={output_drive}",
            "-drive",
            f"format=raw,if=virtio,file={cache_drive}",
            "-drive",
            f"format=raw,if=virtio,file={deps_drive},readonly=on",
            "-nodefaults",
            "-display",
            "none",
            "-chardev",
            "stdio,id=serial0",
            "-serial",
            "chardev:serial0",
        ]
        logging.debug(f"run: {argv}")
        with open(args.log, "wb") as f:
            p = subprocess.run(argv, stdout=f)
            if p.returncode != 0:
                sys.stderr.write(p.stderr.decode())
                raise Exception("qemu-system failed")

        if build.artifact is not None:
            logging.info("extract artifacts, if any")
            extract_tar_from_tar(output_drive, build.artifact)

        if build.cache is not None:
            logging.info(f"extract potentially updated cache from {cache_drive}")
            subprocess.run(
                ["tar", "-xf", cache_drive, "-C", build.cache],
                check=True,
            )

        logging.info("Build finished")
    except Exception as e:
        sys.stderr.write(f"{e}\n")
        sys.exit(1)
    finally:
        logging.debug(f"removing {tmp}")
        shutil.rmtree(tmp)


main()