🏡 index : quine-global/quine-anaconda-webui.git

#!/usr/bin/env python3

# derived from test/image-prepare in cockpit-project/cockpit

import argparse
import glob
import os
import shutil
import subprocess
import sys

BOTS_DIR = os.path.realpath(f'{__file__}/../../bots')
sys.path.append(BOTS_DIR)
TEST_HELPERS_DIR = os.path.realpath(f'{__file__}/../helpers')
sys.path.append(TEST_HELPERS_DIR)

# FIXME: python3-bugzilla should be removed from missing_packages once it's included in boot.iso
missing_packages = "fedora-logos udisks2 libudisks2 udisks2-lvm2 udisks2-btrfs udisks2-iscsi python3-bugzilla"

from compose_path import get_compose_url
from machine.machine_core import machine_virtual


def download_from_copr(copr_repo, packages, machine):
    machine.execute(f"dnf -y copr enable {copr_repo}")
    copr_repo_id = f"copr:copr.fedorainfracloud.org:{copr_repo.replace('/', ':').replace('@', 'group_')}"
    machine.execute(
        f"dnf download --destdir /var/tmp/build/ {packages} --repo {copr_repo_id}",
        stdout=sys.stdout, timeout=300
    )


def vm_install(image, compose, verbose, quick):
    subprocess.check_call([os.path.join(BOTS_DIR, "image-download"), image])
    machine = machine_virtual.VirtMachine(image=image)
    try:
        machine.start()
        machine.wait_boot()

        scenario = os.environ.get("TEST_SCENARIO")
        # Make sure builder can build packages if required, /var/tmp/build needs to be owned by builder
        machine.execute("su builder -c 'mkdir -p /var/tmp/build/SRPMS'")

        # If compose is specified enable the custom compose repository and disable the existing ones
        if compose:
            machine.execute("sed -i 's/enabled=1/enabled=0/' /usr/share/dnf5/repos.d/*.repo")

            compose_base_url = get_compose_url(compose)
            repo_url = f"{compose_base_url}/compose/Everything/x86_64/os/"
            machine.execute(f"dnf -y config-manager addrepo --set=baseurl={repo_url}")

        # Pull cockpit dependencies from the image default compose
        # unless we are testing a PR on cockpit-project/cockpit, then pull it from the PR COPR repo
        packages_to_download = missing_packages
        if scenario and "cockpit-pr-" in scenario:
            cockpit_pr = scenario.split("-")[-1]
            # cockpit-storaged is also available in the default rawhide compose, make sure we don't pull it from there
            download_from_copr(f"packit/cockpit-project-cockpit-{cockpit_pr}",
                               "cockpit-bridge cockpit-ws cockpit-storaged cockpit-networkmanager", machine)
        else:
            download_from_copr("@cockpit/main-builds", "cockpit-bridge cockpit-ws cockpit-storaged cockpit-networkmanager", machine)

        if scenario and scenario.startswith("anaconda-webui-pr-"):
            # If we are testing a anaconda-webui PR scenario from a different repository
            # then pull it from the anaconda-webui PR COPR repo
            anaconda_webui_pr = scenario.split("-")[-1]
            # anaconda-webui is also available in the default rawhide compose, make sure we don't pull it from there
            download_from_copr(f"packit/rhinstaller-anaconda-webui-{anaconda_webui_pr}", "anaconda-webui", machine)
        elif compose:
             # If we are testing a custom compose download the anaconda-webui from the compose
             machine.execute("dnf download --destdir /var/tmp/build/ anaconda-webui", stdout=sys.stdout, timeout=300)
        else:
            # Build anaconda-webui from SRPM otherwise
            files_to_clean = glob.glob("anaconda-webui-*.rpm") + glob.glob("anaconda-webui-*.tar.xz") + ["anaconda-webui.spec"]
            subprocess.run(["rm", *files_to_clean])
            subprocess.run(["make", "srpm"])
            srpm = glob.glob("anaconda-webui*.src.rpm")[0]
            vm_srpm = os.path.join("/var/tmp/build/SRPMS", os.path.basename(srpm))
            machine.upload([os.path.realpath(srpm)], vm_srpm)

            # build rpms
            mock_opts = ("--verbose" if verbose else "") + (" --nocheck" if quick else "")
            machine.execute("su builder -c 'mock --no-clean --disablerepo=* --offline --resultdir /var/tmp/build "
                            f"{mock_opts} --rebuild /var/tmp/build/SRPMS/*.src.rpm'", timeout=1800)

        if compose:
            # If we are testing a custom compose do *not* download the anaconda packages, they are in the ISO already
            pass
        elif scenario and "anaconda-pr" in scenario:
            # If we are testing a PR on rhinstaller/anaconda, then pull it from the PR COPR repo
            anaconda_pr = scenario.split("-")[-1]
            # anaconda-core is also available in the default rawhide compose, make sure we don't pull it from there
            download_from_copr(f"packit/rhinstaller-anaconda-{anaconda_pr}", "anaconda-core anaconda-tui", machine)
        else:
            # Pull anaconda-core from the COPR repo packit builds from master branch
            # anaconda-core is also available in the default rawhide compose, make sure we don't pull it from there
            download_from_copr("@rhinstaller/Anaconda", "anaconda-core anaconda-tui", machine)

        # Download missing dependencies rpms
        # FIXME boot.iso on rawhide does not currently contain the new anaconda-webui dependencies
        # This will change once we include this changes upstream and start building boot.iso with the new dependencies
        # Then we can enable this only for the various scenarios above
        if packages_to_download is not None:
            machine.execute(f"dnf download --destdir /var/tmp/build/ {packages_to_download}", stdout=sys.stdout, timeout=300)

        # download rpms
        vm_rpms = machine.execute("find /var/tmp/build -name '*.rpm' -not -name '*.src.rpm'").strip().split()

        destdir = os.path.abspath("tmp/rpms")
        if os.path.exists(destdir):
            shutil.rmtree(destdir)
        os.makedirs(destdir)

        rpms = []
        for rpm in vm_rpms:
            machine.download(rpm, destdir)
            rpms.append(os.path.join(destdir, os.path.basename(rpm)))
        return rpms
    finally:
        machine.stop()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--quick', '-q', action='store_true')
    parser.add_argument('--verbose', '-v', action='store_true')
    parser.add_argument('--image', default='fedora-rawhide')
    parser.add_argument('--compose', default='')
    args = parser.parse_args()

    vm_install(args.image, args.compose, args.verbose, args.quick)


main()