| 1 |
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/common/pywrap", sys.argv) |
| 2 |
# Build and run a bots/image-customize command to prepare a VM for testing Cockpit. |
| 3 |
# |
| 4 |
# Copyright (C) 2022 Red Hat, Inc. |
| 5 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
|
| 7 |
|
| 8 |
import argparse |
| 9 |
import os |
| 10 |
import shlex |
| 11 |
import shutil |
| 12 |
import subprocess |
| 13 |
import sys |
| 14 |
from collections.abc import Iterator |
| 15 |
from contextlib import contextmanager |
| 16 |
|
| 17 |
from lib import testmap |
| 18 |
from lib.constants import DEFAULT_IMAGE |
| 19 |
from machine.machine_core import machine_virtual |
| 20 |
|
| 21 |
BASE_DIR = os.path.realpath(f'{__file__}/../..') |
| 22 |
TEST_DIR = f'{BASE_DIR}/test' |
| 23 |
BOTS_DIR = f'{BASE_DIR}/bots' |
| 24 |
|
| 25 |
BUILD_VM_MEMORY_MB = 4096 |
| 26 |
|
| 27 |
|
| 28 |
@contextmanager |
| 29 |
def create_machine(image: str) -> Iterator[machine_virtual.VirtMachine]: |
| 30 |
subprocess.check_call([os.path.join(BOTS_DIR, "image-download"), image]) |
| 31 |
network = machine_virtual.VirtNetwork(image=image) |
| 32 |
machine = machine_virtual.VirtMachine(image=image, networking=network.host(restrict=True), |
| 33 |
memory_mb=BUILD_VM_MEMORY_MB) |
| 34 |
try: |
| 35 |
machine.boot() |
| 36 |
yield machine |
| 37 |
finally: |
| 38 |
machine.stop() |
| 39 |
|
| 40 |
|
| 41 |
def build_rpms( |
| 42 |
dist_tars: list[str], machine: machine_virtual.VirtMachine, *, verbose: bool, quick: bool |
| 43 |
) -> list[str]: |
| 44 |
"""build RPMs from a tarball in an machine |
| 45 |
|
| 46 |
Return local rpm path list. |
| 47 |
""" |
| 48 |
machine.upload(dist_tars, "/var/tmp/") |
| 49 |
main_tar = os.path.join("/var/tmp", os.path.basename(dist_tars[0])) |
| 50 |
|
| 51 |
# build srpm |
| 52 |
machine.execute(fr""" |
| 53 |
su builder -c 'rpmbuild --define "_topdir /var/tmp/build" -ts "{main_tar}"' |
| 54 |
""") |
| 55 |
|
| 56 |
# build rpms |
| 57 |
mock_opts = ("--verbose" if verbose else "") + (" --nocheck" if quick else "") |
| 58 |
machine.execute(fr""" |
| 59 |
su builder -c 'mock --no-clean --disablerepo=* --offline \ |
| 60 |
--resultdir /var/tmp/build {mock_opts} \ |
| 61 |
--rebuild /var/tmp/build/SRPMS/*.src.rpm' |
| 62 |
""", timeout=1800) |
| 63 |
|
| 64 |
# download rpms |
| 65 |
vm_rpms = machine.execute("find /var/tmp/build -name '*.rpm' -not -name '*.src.rpm'").strip().split() |
| 66 |
|
| 67 |
destdir = os.path.abspath("tmp/rpms") |
| 68 |
if os.path.exists(destdir): |
| 69 |
shutil.rmtree(destdir) |
| 70 |
os.makedirs(destdir) |
| 71 |
|
| 72 |
rpms: list[str] = [] |
| 73 |
for rpm in vm_rpms: |
| 74 |
machine.download(rpm, destdir) |
| 75 |
rpms.append(os.path.join(destdir, os.path.basename(rpm))) |
| 76 |
return rpms |
| 77 |
|
| 78 |
|
| 79 |
# |
| 80 |
# Helper functions to build image-customize options for various steps |
| 81 |
# |
| 82 |
|
| 83 |
def build_install_package(dist_tars: list[str], image: str, *, quick: bool) -> list[str]: |
| 84 |
"""Default rpm/deb/arch package build/install""" |
| 85 |
|
| 86 |
# our images have distro cockpit packages pre-installed, remove them |
| 87 |
args = ["--run-command"] |
| 88 |
if 'debian' in image or 'ubuntu' in image: |
| 89 |
# set up pbuilder hook to make install-tests |
| 90 |
cmd = """ |
| 91 |
set -ex |
| 92 |
dpkg --purge cockpit cockpit-ws cockpit-bridge cockpit-system |
| 93 |
mkdir -p /var/tmp/tests |
| 94 |
echo 'HOOKDIR="/etc/pbuilder-hooks"' >> /etc/pbuilderrc |
| 95 |
echo 'BINDMOUNTS="/var/tmp/tests"' >> /etc/pbuilderrc |
| 96 |
""" |
| 97 |
args.append(cmd) |
| 98 |
args += ["--upload", os.path.join(TEST_DIR, "pbuilder-hooks") + ":/etc/"] |
| 99 |
else: |
| 100 |
# subscription-manager-cockpit needs these, thus --nodeps |
| 101 |
args.append(r""" |
| 102 |
if rpm -q cockpit-ws >/dev/null 2>&1 |
| 103 |
then rpm --erase --nodeps --verbose cockpit cockpit-ws cockpit-bridge cockpit-system |
| 104 |
fi |
| 105 |
""") |
| 106 |
|
| 107 |
# first in dist_tars is the main release; upload additional ones |
| 108 |
for extra_tar in dist_tars[1:]: |
| 109 |
args += ["--upload", extra_tar + ":/var/tmp/"] |
| 110 |
args += ["--build", dist_tars[0]] |
| 111 |
|
| 112 |
# install the playground |
| 113 |
args += ["--upload", os.path.join(BASE_DIR, "dist/playground") + ":/usr/local/share/cockpit/"] |
| 114 |
|
| 115 |
# install mock pam module; we don't want to ship it as package, so fish it out of the build root |
| 116 |
args.append("--run-command") |
| 117 |
if 'debian' in image or 'ubuntu' in image: |
| 118 |
args.append(r"find /var/tmp/tests -name mock-pam-conv-mod.so -exec cp {} /lib/x86_64-linux-gnu/security \;") |
| 119 |
elif 'arch' in image: |
| 120 |
args.append(r"find /var/lib/archbuild -name mock-pam-conv-mod.so -exec cp {} /usr/lib/security/ \;") |
| 121 |
else: |
| 122 |
args.append(r"find /var/lib/mock -name mock-pam-conv-mod.so -exec cp {} /usr/lib64/security/ \;") |
| 123 |
|
| 124 |
if not quick and ('debian' in image or 'ubuntu' in image): |
| 125 |
args.append("--run-command") |
| 126 |
suppress = {'initial-upload-closes-no-bugs', 'newer-standards-version'} |
| 127 |
# older OSes don't yet know about watch format v5; drop ubuntu-stable once that moves to lintian 2.127.0 |
| 128 |
if image in ["debian-trixie", "ubuntu-stable"]: |
| 129 |
suppress.add("missing-debian-watch-file-standard") |
| 130 |
|
| 131 |
args.append(fr""" |
| 132 |
cd /var/tmp/build; |
| 133 |
runuser -u admin -- \ |
| 134 |
env TMPDIR=/var/tmp lintian --fail-on warning,error \ |
| 135 |
--tag-display-limit 0 --display-info \ |
| 136 |
--suppress-tags {','.join(suppress)} \ |
| 137 |
cockpit*.changes >&2""") |
| 138 |
return args |
| 139 |
|
| 140 |
|
| 141 |
def build_install_ostree(dist_tars: list[str], image: str, *, verbose: bool, quick: bool) -> list[str]: |
| 142 |
"""Special treatment of build/install on CoreOS |
| 143 |
|
| 144 |
OSTree image can't build packages, build them on corresponding OSes and |
| 145 |
install them into the OSTree and the cockpit/ws container. |
| 146 |
""" |
| 147 |
with create_machine(testmap.get_build_image(image)) as machine: |
| 148 |
rpms = build_rpms(dist_tars, machine, verbose=verbose, quick=quick) |
| 149 |
args: list[str] = [] |
| 150 |
for rpm in rpms: |
| 151 |
args += ["--upload", f"{rpm}:/var/tmp/"] |
| 152 |
args += [ |
| 153 |
"--upload", os.path.join(BASE_DIR, "containers") + ":/var/tmp/", |
| 154 |
"--script", os.path.join(TEST_DIR, "ostree.install"), |
| 155 |
"--upload", os.path.join(BASE_DIR, "dist/playground") + ":/usr/local/share/cockpit/"] |
| 156 |
return args |
| 157 |
|
| 158 |
|
| 159 |
def build_install_bootc(dist_tars: list[str], image: str, *, verbose: bool, quick: bool) -> list[str]: |
| 160 |
"""Special treatment of build/install on Bootc |
| 161 |
|
| 162 |
bootc image can't build packages, build them on corresponding OSes, build a new bootc |
| 163 |
image with the rpms, and deploy it. |
| 164 |
""" |
| 165 |
with create_machine(testmap.get_build_image(image)) as machine: |
| 166 |
rpms = build_rpms(dist_tars, machine, verbose=verbose, quick=quick) |
| 167 |
args = ["--run-command", "mkdir -p /var/tmp/rpms"] |
| 168 |
for rpm in rpms: |
| 169 |
if not any(r in rpm for r in [".src", "debug", "packagekit"]): |
| 170 |
args += ["--upload", f"{rpm}:/var/tmp/rpms/"] |
| 171 |
args += [ |
| 172 |
"--upload", os.path.join(BASE_DIR, "dist/playground") + ":/var/tmp/", |
| 173 |
"--script", os.path.join(TEST_DIR, "bootc.install"), |
| 174 |
] |
| 175 |
return args |
| 176 |
|
| 177 |
|
| 178 |
def build_install_container(dist_tars: list[str], image: str, *, verbose: bool, quick: bool) -> list[str]: |
| 179 |
"""Build VM with cockpit/ws container |
| 180 |
|
| 181 |
This can test an image with beibooting from a cockpit/ws container, without |
| 182 |
any installed cockpit packages. |
| 183 |
""" |
| 184 |
builder = testmap.get_build_image_for_ws_container_inside_of(image) |
| 185 |
if builder is None: |
| 186 |
raise ValueError(f"Don't know how to build a container for {image}") |
| 187 |
|
| 188 |
with create_machine(builder) as machine: |
| 189 |
rpms = build_rpms(dist_tars, machine, verbose=verbose, quick=quick) |
| 190 |
args: list[str] = [] |
| 191 |
for rpm in rpms: |
| 192 |
args += ["--upload", f"{rpm}:/var/tmp/"] |
| 193 |
args += [ |
| 194 |
# copy the playground where ws-container.install expects it |
| 195 |
"--run-command", "mkdir -p /var/tmp/install/usr/share/cockpit/", |
| 196 |
"--upload", os.path.join(BASE_DIR, "dist/playground") + ":/var/tmp/install/usr/share/cockpit/", |
| 197 |
# remove preinstalled rpms |
| 198 |
"--run-command", "dnf -C remove -y cockpit-bridge cockpit-ws", |
| 199 |
"--upload", os.path.join(BASE_DIR, "containers") + ":/var/tmp/", |
| 200 |
"--script", os.path.join(TEST_DIR, "ws-container.install"), |
| 201 |
] |
| 202 |
return args |
| 203 |
|
| 204 |
|
| 205 |
def validate_packages() -> list[str]: |
| 206 |
"""Post-install package checks""" |
| 207 |
|
| 208 |
# check for files that are shipped by more than one RPM |
| 209 |
return ["--run-command", |
| 210 |
"""set -eu |
| 211 |
fail= |
| 212 |
for f in $(find $(rpm -ql $(rpm -qa '*cockpit*') | sort | uniq -d) -maxdepth 0 -type f); do |
| 213 |
# -debugsource overlap is legit |
| 214 |
[ "${f#/usr/src/debug}" = "$f" ] || continue |
| 215 |
echo "ERROR: $f is shipped by multiple packages: $(rpm -qf $f)" >&2 |
| 216 |
fail=1 |
| 217 |
done |
| 218 |
[ -z "${fail}" ] || exit 1 |
| 219 |
"""] |
| 220 |
|
| 221 |
|
| 222 |
def main() -> int: |
| 223 |
parser = argparse.ArgumentParser( |
| 224 |
description='Prepare testing environment, download images and build and install cockpit', |
| 225 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 226 |
parser.add_argument('-e', '--env', metavar='NAME=VAL', action='append', |
| 227 |
help="Add a line to /etc/environment") |
| 228 |
parser.add_argument('-d', '--debug', action='append_const', dest='env', const='COCKPIT_DEBUG=all', |
| 229 |
help="Equivalent to --env=COCKPIT_DEBUG=all") |
| 230 |
parser.add_argument('-v', '--verbose', action='store_true', help='Display verbose progress details') |
| 231 |
parser.add_argument('-q', '--quick', action='store_true', help='Skip unit tests to build faster') |
| 232 |
parser.add_argument('-o', '--overlay', action='store_true', |
| 233 |
help='Install into existing test/image/ overlay instead of from pristine base image') |
| 234 |
parser.add_argument('--network', action='store_true', |
| 235 |
help='Enable internet access (only for local experiments, not for production!)') |
| 236 |
parser.add_argument('--container', action='store_true', help='Install cockpit/ws container instead of rpms') |
| 237 |
parser.add_argument('image', nargs='?', default=DEFAULT_IMAGE, help='The image to use') |
| 238 |
args = parser.parse_args() |
| 239 |
|
| 240 |
customize = [os.path.join(BOTS_DIR, "image-customize")] |
| 241 |
if not args.network: |
| 242 |
customize.append("--no-network") |
| 243 |
if not args.overlay: |
| 244 |
customize.append("--fresh") |
| 245 |
if args.verbose: |
| 246 |
customize.append("--verbose") |
| 247 |
if args.quick: |
| 248 |
customize.append("--quick") |
| 249 |
|
| 250 |
# esbuild likes to use a lot of memory. |
| 251 |
customize += ["--memory-mb", str(BUILD_VM_MEMORY_MB)] |
| 252 |
|
| 253 |
dist_tars = subprocess.check_output([f'{BASE_DIR}/tools/make-dist'], text=True).strip().splitlines() |
| 254 |
|
| 255 |
# Don't rebuild bundle if built with NODE_ENV=development |
| 256 |
if os.environ.get('NODE_ENV') == 'development': |
| 257 |
customize += ["--build-options", '--define "rebuild_bundle 0"'] |
| 258 |
|
| 259 |
if args.image == "fedora-coreos": |
| 260 |
customize += build_install_ostree(dist_tars, args.image, verbose=args.verbose, quick=args.quick) |
| 261 |
elif args.image.endswith("-bootc"): |
| 262 |
customize += build_install_bootc(dist_tars, args.image, verbose=args.verbose, quick=args.quick) |
| 263 |
elif args.container: |
| 264 |
customize += build_install_container(dist_tars, args.image, verbose=args.verbose, quick=args.quick) |
| 265 |
else: |
| 266 |
customize += build_install_package(dist_tars, args.image, quick=args.quick) |
| 267 |
|
| 268 |
if not args.quick: |
| 269 |
customize += validate_packages() |
| 270 |
|
| 271 |
# post build/install test preparation |
| 272 |
customize += ["--script", os.path.join(TEST_DIR, "vm.install")] |
| 273 |
|
| 274 |
if args.env: |
| 275 |
customize += ['--run-command', r'printf "%s\n" >>/etc/environment ' + shlex.join(args.env)] |
| 276 |
|
| 277 |
customize.append(args.image) |
| 278 |
|
| 279 |
# show final command for easy copy&paste reproduction/debugging |
| 280 |
if args.verbose: |
| 281 |
print(' '.join([shlex.quote(arg) for arg in customize])) |
| 282 |
|
| 283 |
return subprocess.call(customize) |
| 284 |
|
| 285 |
|
| 286 |
if __name__ == "__main__": |
| 287 |
sys.exit(main()) |