|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import tempfile |
| 5 | + |
| 6 | +from lib.commands import local_cmd, scp, ssh |
| 7 | + |
| 8 | +@pytest.fixture(scope='function') |
| 9 | +def iso_remaster(request, answerfile): |
| 10 | + marker = request.node.get_closest_marker("installer_iso") |
| 11 | + assert marker is not None, "iso_remaster fixture requires 'installer_iso' marker" |
| 12 | + iso_key = marker.args[0] |
| 13 | + ANSWERFILE_URL = "http://pxe/configs/custom/ydi/install-8.2-uefi-iso-ext.xml" # FIXME |
| 14 | + |
| 15 | + from data import ISO_IMAGES, ISOSR_SRV, ISOSR_PATH, TOOLS |
| 16 | + assert "iso-remaster" in TOOLS |
| 17 | + iso_remaster = TOOLS["iso-remaster"] |
| 18 | + assert os.access(iso_remaster, os.X_OK) |
| 19 | + |
| 20 | + assert iso_key in ISO_IMAGES, f"ISO_IMAGES does not have a value for {iso_key}" |
| 21 | + SOURCE_ISO = ISO_IMAGES[iso_key] |
| 22 | + |
| 23 | + with tempfile.TemporaryDirectory() as isotmp: |
| 24 | + remastered_iso = os.path.join(isotmp, "image.iso") |
| 25 | + iso_patcher_script = os.path.join(isotmp, "iso-patcher") |
| 26 | + |
| 27 | + logging.info("Remastering %s to %s", SOURCE_ISO, remastered_iso) |
| 28 | + |
| 29 | + # generate install.img-patcher script |
| 30 | + with open(img_patcher_script, "xt") as patcher_fd: |
| 31 | + print(f"""#!/bin/bash |
| 32 | +set -ex |
| 33 | +INSTALLIMG="$1" |
| 34 | +
|
| 35 | +curl {ANSWERFILE_URL} -o "$INSTALLIMG/root/answerfile.xml" |
| 36 | +""", |
| 37 | + file=patcher_fd) |
| 38 | + os.chmod(patcher_fd.fileno(), 0o755) |
| 39 | + |
| 40 | + # generate iso-patcher script |
| 41 | + with open(iso_patcher_script, "xt") as patcher_fd: |
| 42 | + passwd = "passw0rd" # FIXME hash |
| 43 | + print(f"""#!/bin/bash |
| 44 | +set -ex |
| 45 | +ISODIR="$1" |
| 46 | +SED_COMMANDS=(-e "s@/vmlinuz@/vmlinuz sshpassword={passwd} atexit=shell@") |
| 47 | +SED_COMMANDS+=(-e "s@/vmlinuz@/vmlinuz install answerfile=file:///root/answerfile.xml network_device=all@") |
| 48 | +
|
| 49 | +sed -i "${{SED_COMMANDS[@]}}" \ |
| 50 | + "$ISODIR"/*/*/grub*.cfg \ |
| 51 | + "$ISODIR"/boot/isolinux/isolinux.cfg |
| 52 | +""", |
| 53 | + file=patcher_fd) |
| 54 | + os.chmod(patcher_fd.fileno(), 0o755) |
| 55 | + |
| 56 | + # do remaster |
| 57 | + local_cmd([iso_remaster, |
| 58 | + "--iso-patcher", iso_patcher_script, |
| 59 | + SOURCE_ISO, remastered_iso |
| 60 | + ]) |
| 61 | + |
| 62 | + # unique filename on server, has to work on FreeBSD-based NAS |
| 63 | + # too, and even v14 has no tool allowing mktemp suffixes |
| 64 | + remote_iso = ssh(ISOSR_SRV, |
| 65 | + ["python3", "-c", |
| 66 | + '"import os, tempfile; ' |
| 67 | + f"f = tempfile.mkstemp(suffix='.iso', dir='{ISOSR_PATH}')[1];" |
| 68 | + "os.chmod(f, 0o644);" |
| 69 | + 'print(f);"' |
| 70 | + ]) |
| 71 | + logging.info("Uploading to ISO-SR server remastered %s as %s", |
| 72 | + remastered_iso, os.path.basename(remote_iso)) |
| 73 | + scp(ISOSR_SRV, remastered_iso, remote_iso) |
| 74 | + # FIXME: is sr-scan ever needed? |
| 75 | + |
| 76 | + try: |
| 77 | + yield os.path.basename(remote_iso) |
| 78 | + finally: |
| 79 | + logging.info("Removing %s from ISO-SR server", os.path.basename(remote_iso)) |
| 80 | + ssh(ISOSR_SRV, ["rm", remote_iso]) |
0 commit comments