diff --git a/pipeline/cleanup-cycle.py b/pipeline/cleanup-cycle.py index f4f0149..9abcf46 100644 --- a/pipeline/cleanup-cycle.py +++ b/pipeline/cleanup-cycle.py @@ -1,11 +1,30 @@ #!/usr/bin/env python3 """Bound private artifact retention; caller must hold the coordinator cycle lock.""" +import fcntl import json from pathlib import Path import re import shutil +def lock_run(path, blocking=True): + """Exclusive advisory lock for one run directory. + + The failure archiver holds this lock while it copies a run into + failure-evidence; retention must never remove build trees underneath it. + Returns the open handle, or None when another process owns the run and + blocking is disabled. + """ + handle = open(Path(path) / '.cleanup.lock', 'a+') + try: + fcntl.flock(handle.fileno(), + fcntl.LOCK_EX if blocking else fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + handle.close() + return None + return handle + + def cleanup(parent, active): runs = [] for path in sorted(Path(parent).iterdir()): @@ -23,26 +42,31 @@ def cleanup(parent, active): for path, state in runs: if path in keep: continue - # Explicit artifact locations only. Keep source, status and evidence. - for root in (path, path / 'failure-evidence'): - for variant in ('core', 'gnome', 'kde'): - build = root / variant / 'build' - if not build.resolve().is_relative_to(path.resolve()): - raise ValueError('Build directory escapes run: ' + str(build)) - for name in ('repo', 'output', 'iso-work', 'profile', 'tools', 'extractor', - 'core', 'core-base', 'composed', 'layer', 'purpose-stage', - 'purpose-layer', 'purpose-input', 'flatpak-seed', - 'package', 'package-repo', 'private-package-cache'): - target = build / name - if target.is_symlink(): - target.unlink() - removed.append(str(target)) - elif target.is_dir(): - if not target.resolve().is_relative_to(path.resolve()): - raise ValueError('Artifact escapes run directory: ' + str(target)) - shutil.rmtree(target) - removed.append(str(target)) - record = path / 'artifact-cleanup.json' - record.write_text(json.dumps({'policy': 'evidence-only-D-098', - 'artifacts_expired': True}, indent=2) + '\n') + lock = lock_run(path, blocking=False) + if lock is None: + # Another cycle is still archiving this run; revisit it next time. + continue + with lock: + # Explicit artifact locations only. Keep source, status and evidence. + for root in (path, path / 'failure-evidence'): + for variant in ('core', 'gnome', 'kde', 'container'): + build = root / variant / 'build' + if not build.resolve().is_relative_to(path.resolve()): + raise ValueError('Build directory escapes run: ' + str(build)) + for name in ('repo', 'output', 'iso-work', 'profile', 'tools', 'extractor', + 'core', 'core-base', 'composed', 'layer', 'purpose-stage', + 'purpose-layer', 'purpose-input', 'flatpak-seed', + 'package', 'package-repo', 'private-package-cache'): + target = build / name + if target.is_symlink(): + target.unlink() + removed.append(str(target)) + elif target.is_dir(): + if not target.resolve().is_relative_to(path.resolve()): + raise ValueError('Artifact escapes run directory: ' + str(target)) + shutil.rmtree(target) + removed.append(str(target)) + record = path / 'artifact-cleanup.json' + record.write_text(json.dumps({'policy': 'evidence-only-D-098', + 'artifacts_expired': True}, indent=2) + '\n') return removed diff --git a/pipeline/release-cycle.py b/pipeline/release-cycle.py index a4c5276..8b50a8e 100644 --- a/pipeline/release-cycle.py +++ b/pipeline/release-cycle.py @@ -503,20 +503,31 @@ rm -rf {builds['core']}/iso-work {builds['core']}/profile {builds['core']}/extra raise excluded = ['tools', 'iso-work', 'profile', 'extractor', 'core-base', 'purpose-stage', 'composed', 'layer', 'purpose-layer', 'purpose-input', 'flatpak-seed', '*.qcow2', - '/core/build/core', '/gnome/build/core', '/kde/build/core'] + '/core/build/core', '/gnome/build/core', '/kde/build/core', '/container/build/core', + # An OSTree repo is a rebuildable duplicate of the run's layers and + # dominates the archive (gigabytes); it also kept the transfer alive + # long after the run had ended. + '/core/build/repo', '/gnome/build/repo', '/kde/build/repo', + '/container/build/repo'] if state.get('artifacts_archived'): excluded += ['/core/build', '/gnome/build', '/kde/build', '/container/build'] - destination = run / 'failure-evidence' - destination.mkdir(parents=True, exist_ok=True) - archive_command = ['tar', '-C', remote_root, - *['--exclude=' + ('.' + x if x.startswith('/') else x) for x in excluded], - '-cf', '-', '.'] - transfer = shlex.join(['ssh', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=20', worker, - shlex.join(archive_command)]) - archived = observer.call(['bash', '-o', 'pipefail', '-c', transfer + ' | ' + - shlex.join(['tar', '-C', str(destination), '-xf', '-'])], timeout=1800, check=False) - if archived.returncode == 0: - remote('rm -rf ' + shlex.quote(remote_root)) + # Retention of a newer cycle must not delete these trees while tar is + # still writing them; cleanup-cycle.py takes the same per-run lock. + archive_lock = cleanup_module.lock_run(run) + try: + destination = run / 'failure-evidence' + destination.mkdir(parents=True, exist_ok=True) + archive_command = ['tar', '-C', remote_root, + *['--exclude=' + ('.' + x if x.startswith('/') else x) for x in excluded], + '-cf', '-', '.'] + transfer = shlex.join(['ssh', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=20', worker, + shlex.join(archive_command)]) + archived = observer.call(['bash', '-o', 'pipefail', '-c', transfer + ' | ' + + shlex.join(['tar', '-C', str(destination), '-xf', '-'])], timeout=1800, check=False) + if archived.returncode == 0: + remote('rm -rf ' + shlex.quote(remote_root)) + finally: + archive_lock.close() raise finally: if state.get('invocation_id'):