176 lines
8.4 KiB
Python
176 lines
8.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate the configured, signed update target on a retained GNOME VM, then rollback.
|
|
|
|
The candidate must already be the target of the VM's configured track. Private
|
|
pre-publication validation requires a private track; plans are never rewritten.
|
|
This optional rollout probe is not part of release-cycle installed-ISO acceptance.
|
|
"""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import pexpect
|
|
import shlex
|
|
import subprocess
|
|
import time
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--vm', type=Path, required=True)
|
|
parser.add_argument('--binary', type=Path, required=True)
|
|
parser.add_argument('--candidate', required=True)
|
|
parser.add_argument('--evidence', type=Path, required=True)
|
|
parser.add_argument('--resume', action='store_true', help='Resume validation of the recorded booted candidate')
|
|
parser.add_argument('--ssh-port', type=int, default=22296)
|
|
parser.add_argument('--bootstrap-update-profile', type=Path)
|
|
parser.add_argument('--bootstrap-update-key', type=Path)
|
|
args = parser.parse_args()
|
|
assert len(args.candidate) == 64 and all(c in '0123456789abcdef' for c in args.candidate)
|
|
args.evidence.mkdir(parents=True, exist_ok=True)
|
|
password = (args.vm / 'credential').read_text()
|
|
log = (args.evidence / 'guest.log').open('a' if args.resume else 'w')
|
|
|
|
|
|
def run(script, timeout=180, check=True):
|
|
c = pexpect.spawn('ssh', ['-tt', '-o', 'ConnectTimeout=5', '-o',
|
|
'UserKnownHostsFile=' + str(args.vm / 'known_hosts'), '-p', str(args.ssh_port),
|
|
'coretest@127.0.0.1', 'sudo -p SUDO_AUTH: bash -ec ' + shlex.quote(script)],
|
|
encoding='utf-8', timeout=timeout)
|
|
try:
|
|
c.expect('[Pp]assword:', timeout=15); c.sendline(password)
|
|
auth = c.expect(['SUDO_AUTH:', 'Permission denied', '[Pp]assword:'], timeout=15)
|
|
if auth != 0: raise RuntimeError('Guest authentication rejected')
|
|
c.sendline(password)
|
|
c.logfile_read = log
|
|
c.expect(pexpect.EOF)
|
|
text = c.before
|
|
c.close()
|
|
if check and c.exitstatus != 0:
|
|
raise RuntimeError('Guest command failed: ' + str(c.exitstatus))
|
|
return text
|
|
finally:
|
|
if c.isalive(): c.close(force=True)
|
|
log.flush()
|
|
|
|
|
|
def state():
|
|
text = run('ostree admin status --json')
|
|
return json.JSONDecoder().raw_decode(text[text.index('{'):])[0]['deployments']
|
|
|
|
|
|
def booted():
|
|
return next(d['checksum'] for d in state() if d['booted'])
|
|
|
|
|
|
def wait_boot(expected=None):
|
|
for _ in range(120):
|
|
try:
|
|
value = booted()
|
|
if expected is None or value == expected: return value
|
|
except (pexpect.ExceptionPexpect, RuntimeError, ValueError, StopIteration):
|
|
pass
|
|
time.sleep(3)
|
|
raise RuntimeError('Guest failed to boot expected deployment')
|
|
|
|
|
|
result = {'candidate': args.candidate, 'passed': False}
|
|
try:
|
|
if args.resume:
|
|
previous = json.loads((args.evidence / 'summary.json').read_text())
|
|
assert previous['candidate'] == args.candidate
|
|
original = previous['original']
|
|
pending = previous['updated']
|
|
wait_boot(pending)
|
|
result.update(original=original, updated=pending)
|
|
else:
|
|
original = wait_boot()
|
|
result['original'] = original
|
|
if not args.resume:
|
|
c = pexpect.spawn('scp', ['-o', 'UserKnownHostsFile=' + str(args.vm / 'known_hosts'),
|
|
'-P', str(args.ssh_port), str(args.binary), 'coretest@127.0.0.1:/var/tmp/archipelago-updater'],
|
|
encoding='utf-8', timeout=120)
|
|
c.expect('[Pp]assword:'); c.sendline(password); c.expect(pexpect.EOF); c.close()
|
|
if c.exitstatus: raise RuntimeError('Updater upload failed')
|
|
if args.bootstrap_update_profile:
|
|
if not args.bootstrap_update_key:
|
|
raise RuntimeError('Bootstrap profile requires a public key')
|
|
for source, target in ((args.bootstrap_update_profile, 'rollout-update.json'),
|
|
(args.bootstrap_update_key, 'rollout-update.asc')):
|
|
c = pexpect.spawn('scp', ['-o', 'UserKnownHostsFile=' + str(args.vm / 'known_hosts'),
|
|
'-P', str(args.ssh_port), str(source), 'coretest@127.0.0.1:/var/tmp/' + target],
|
|
encoding='utf-8', timeout=120)
|
|
c.expect('[Pp]assword:'); c.sendline(password); c.expect(pexpect.EOF); c.close()
|
|
if c.exitstatus: raise RuntimeError('Bootstrap upload failed')
|
|
run('''test ! -e /etc/archipelago/update.json
|
|
install -m644 /var/tmp/rollout-update.json /etc/archipelago/update.json
|
|
''')
|
|
run('''set -e
|
|
systemctl is-active archipelagod.service
|
|
archipelago self-check
|
|
getent passwd coretest > /var/tmp/rollout-account
|
|
test "$(cat /var/home/coretest/.archipelago-clean-persistence)" = "clean dropin candidate"
|
|
/var/tmp/archipelago-updater update plan --json > /var/tmp/rollout-channel-plan
|
|
''')
|
|
text = run('cat /var/tmp/rollout-channel-plan')
|
|
plan = json.JSONDecoder().raw_decode(text[text.index('{'):])[0]
|
|
assert plan['target'] == args.candidate, 'Candidate must match the configured verified track'
|
|
plan_id = plan['plan_id']
|
|
assert len(plan_id) == 64 and all(c in '0123456789abcdef' for c in plan_id)
|
|
run('printf %s ' + shlex.quote(plan_id) + ' > /var/tmp/rollout-plan-id\n'
|
|
'/var/tmp/archipelago-updater update apply --json --plan ' + shlex.quote(plan_id), timeout=3600)
|
|
pending = next(d['checksum'] for d in state() if d['staged'])
|
|
result['updated'] = pending
|
|
run('systemctl reboot', check=False)
|
|
wait_boot(pending)
|
|
run('''set -e
|
|
archipelago self-check
|
|
nano --version
|
|
systemctl is-active gdm NetworkManager ostree-finalize-staged.service
|
|
getent passwd coretest | cmp - /var/tmp/rollout-account
|
|
test "$(cat /var/home/coretest/.archipelago-clean-persistence)" = "clean dropin candidate"
|
|
diff -r /sysroot/boot/loader/entries /efi/loader/entries
|
|
python3 - <<'PY'
|
|
import json, subprocess
|
|
policy = json.load(open('/usr/share/archipelago/desktop/flatpak-apps.json'))
|
|
assert policy['delivery'] == 'installation-media'
|
|
for ref in policy['applications']:
|
|
subprocess.run(['flatpak', 'info', '--system', ref], check=True)
|
|
runtime = subprocess.check_output(['flatpak', 'info', '--system', '--show-runtime', ref], text=True).strip()
|
|
subprocess.run(['flatpak', 'info', '--system', runtime], check=True)
|
|
PY
|
|
ostree admin status --json > /var/tmp/rollout-before-stale
|
|
if archipelago update apply --json --plan "$(cat /var/tmp/rollout-plan-id)"; then exit 1; fi
|
|
ostree admin status --json > /var/tmp/rollout-after-stale
|
|
cmp /var/tmp/rollout-before-stale /var/tmp/rollout-after-stale
|
|
''')
|
|
metadata = run('cat /usr/lib/archipelago/extensions.json')
|
|
metadata = json.JSONDecoder().raw_decode(metadata[metadata.index('{'):])[0]
|
|
assert metadata['base'] == args.candidate
|
|
assert metadata['version'] == 3 and metadata['requested'] == ['nano']
|
|
# Authenticate through native GDM; do not inject autologin configuration.
|
|
ui = ['python3', str(Path(__file__).resolve().parent / 'gnome_vm_input.py'), str(args.vm)]
|
|
active = run('''loginctl list-sessions --no-legend | awk '$3=="coretest" {print $1}' | xargs -r -n1 loginctl show-session -p Type --value | grep -qx wayland && echo WAYLAND_ALREADY_ACTIVE || true''')
|
|
if 'WAYLAND_ALREADY_ACTIVE' not in active:
|
|
subprocess.run(ui + ['--keys', 'spc', '--screen', 'update-gdm.png'], check=True)
|
|
time.sleep(5)
|
|
subprocess.run(ui + ['--text-file', str(args.vm / 'credential'), '--keys', 'ret',
|
|
'--screen', 'update-login.png'], check=True)
|
|
wait_script = '''until loginctl list-sessions --no-legend | awk '$3=="coretest" {print $1}' | xargs -r -n1 loginctl show-session -p Type --value | grep -qx wayland; do sleep 3; done'''
|
|
run('timeout 120 bash -c ' + shlex.quote(wait_script), timeout=150)
|
|
result['wayland_login'] = True
|
|
index = next(d['index'] for d in state() if d['checksum'] == original)
|
|
run('ostree admin set-default ' + str(index) + '; systemctl reboot', check=False)
|
|
wait_boot(original)
|
|
run('''set -e
|
|
archipelago self-check
|
|
nano --version
|
|
getent passwd coretest | cmp - /var/tmp/rollout-account
|
|
diff -r /sysroot/boot/loader/entries /efi/loader/entries
|
|
test "$(cat /var/home/coretest/.archipelago-clean-persistence)" = "clean dropin candidate"
|
|
''')
|
|
result['passed'] = True
|
|
run('systemctl poweroff', check=False)
|
|
except BaseException as error:
|
|
result['error'] = str(error)
|
|
raise
|
|
finally:
|
|
(args.evidence / 'summary.json').write_text(json.dumps(result, indent=2) + '\n')
|
|
log.close()
|