|
1 | 1 | import logging
|
| 2 | +import os |
2 | 3 | import pytest
|
3 | 4 | import tempfile
|
4 | 5 |
|
5 | 6 | from packaging import version
|
6 | 7 |
|
7 | 8 | import lib.config as global_config
|
8 | 9 |
|
| 10 | +from lib.commands import local_cmd, scp, ssh |
9 | 11 | from lib.common import wait_for, vm_image, is_uuid
|
10 | 12 | from lib.common import setup_formatted_and_mounted_disk, teardown_formatted_and_mounted_disk
|
11 | 13 | from lib.netutil import is_ipv6
|
@@ -254,6 +256,75 @@ def sr_disk_for_all_hosts(request, host):
|
254 | 256 | logging.info(f">> Disk or block device {disk} is present and free on all pool members")
|
255 | 257 | yield candidates[0]
|
256 | 258 |
|
| 259 | +@pytest.fixture(scope='session') |
| 260 | +def iso_remaster(): |
| 261 | + SOURCE_ISO = "/home/user/iso/xcp-ng-8.2.1-20231130.iso" # FIXME dict in data.py |
| 262 | + ANSWERFILE_URL = "http://pxe/configs/custom/ydi/install-8.2-uefi-iso-ext.xml" # FIXME |
| 263 | + |
| 264 | + from data import ISOSR_SRV, ISOSR_PATH, TOOLS |
| 265 | + assert "iso-remaster" in TOOLS |
| 266 | + iso_remaster = TOOLS["iso-remaster"] |
| 267 | + assert os.access(iso_remaster, os.X_OK) |
| 268 | + |
| 269 | + with tempfile.TemporaryDirectory() as isotmp: |
| 270 | + remastered_iso = os.path.join(isotmp, "image.iso") |
| 271 | + iso_patcher_script = os.path.join(isotmp, "iso-patcher") |
| 272 | + |
| 273 | + logging.info("Remastering %s to %s", SOURCE_ISO, remastered_iso) |
| 274 | + |
| 275 | + # generate install.img-patcher script |
| 276 | + with open(img_patcher_script, "xt") as patcher_fd: |
| 277 | + print(f"""#!/bin/bash |
| 278 | +set -ex |
| 279 | +INSTALLIMG="$1" |
| 280 | +
|
| 281 | +curl {ANSWERFILE_URL} -o "$INSTALLIMG/root/answerfile.xml" |
| 282 | +""", |
| 283 | + file=patcher_fd) |
| 284 | + os.chmod(patcher_fd.fileno(), 0o755) |
| 285 | + |
| 286 | + # generate iso-patcher script |
| 287 | + with open(iso_patcher_script, "xt") as patcher_fd: |
| 288 | + passwd = "passw0rd" # FIXME hash |
| 289 | + print(f"""#!/bin/bash |
| 290 | +set -ex |
| 291 | +ISODIR="$1" |
| 292 | +SED_COMMANDS=(-e "s@/vmlinuz@/vmlinuz sshpassword={passwd} atexit=shell@") |
| 293 | +SED_COMMANDS+=(-e "s@/vmlinuz@/vmlinuz install answerfile=file:///root/answerfile.xml network_device=all@") |
| 294 | +
|
| 295 | +sed -i "${{SED_COMMANDS[@]}}" \ |
| 296 | + "$ISODIR"/*/*/grub*.cfg \ |
| 297 | + "$ISODIR"/boot/isolinux/isolinux.cfg |
| 298 | +""", |
| 299 | + file=patcher_fd) |
| 300 | + os.chmod(patcher_fd.fileno(), 0o755) |
| 301 | + |
| 302 | + # do remaster |
| 303 | + local_cmd([iso_remaster, |
| 304 | + "--iso-patcher", iso_patcher_script, |
| 305 | + SOURCE_ISO, remastered_iso |
| 306 | + ]) |
| 307 | + |
| 308 | + # unique filename on server, has to work on FreeBSD-based NAS |
| 309 | + # too, and even v14 has no tool allowing mktemp suffixes |
| 310 | + remote_iso = ssh(ISOSR_SRV, |
| 311 | + ["python3", "-c", |
| 312 | + '"import os, tempfile; ' |
| 313 | + f"f = tempfile.mkstemp(suffix='.iso', dir='{ISOSR_PATH}')[1];" |
| 314 | + "os.chmod(f, 0o644);" |
| 315 | + 'print(f);"' |
| 316 | + ]) |
| 317 | + logging.info("Uploading to ISO-SR server remastered %s as %s", |
| 318 | + remastered_iso, os.path.basename(remote_iso)) |
| 319 | + scp(ISOSR_SRV, remastered_iso, remote_iso) |
| 320 | + # FIXME: is sr-scan ever needed? |
| 321 | + |
| 322 | + try: |
| 323 | + yield os.path.basename(remote_iso) |
| 324 | + finally: |
| 325 | + logging.info("Removing %s from ISO-SR server", os.path.basename(remote_iso)) |
| 326 | + ssh(ISOSR_SRV, ["rm", remote_iso]) |
| 327 | + |
257 | 328 | @pytest.fixture(scope='module')
|
258 | 329 | def vm_ref(request):
|
259 | 330 | ref = request.param
|
|
0 commit comments