infrastructure/pipeline/cycle-terminal.py

25 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Systemd stop hook: expose timeout/kill failures even if Python could not save."""
import importlib.util
import json
import os
from pathlib import Path
spec = importlib.util.spec_from_file_location('cycle', Path(__file__).with_name('release-cycle.py'))
cycle = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cycle)
config = json.loads(Path(os.environ.get('ARCHIPELAGO_CYCLE_CONFIG', '/etc/archipelago/release-cycle.json')).read_text())
base = Path(config['coordinator_root'])
parent = base / 'data/private/release-cycle'
latest = parent / 'latest.json'
if latest.exists():
state = json.loads(latest.read_text())
if state.get('invocation_id') == os.environ.get('INVOCATION_ID') and state['state'] not in ('complete', 'failed'):
state.update(failed_phase=state['state'], state='failed', stage_title='Ошибка',
error='Coordinator terminated: ' + os.environ.get('SERVICE_RESULT', 'unknown'))
cycle.atomic(parent / state['run'] / 'state.json', state)
cycle.atomic(latest, state)
cycle.atomic(base / 'data/public/pipeline.json', state)
text = f"Прогон: {state['run']}\nЭтап: Ошибка\nПричина: {state['error']}\n"
(parent / 'latest.txt.tmp').write_text(text)
(parent / 'latest.txt.tmp').replace(parent / 'latest.txt')