35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Persist terminal state for an infrastructure CLI request; no interactive steps."""
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import signal
|
|
from datetime import datetime, timezone
|
|
|
|
path = Path(sys.argv[1])
|
|
record = json.loads(path.read_text())
|
|
|
|
|
|
def save(state, **extra):
|
|
record.update(state=state, **extra)
|
|
temp = path.with_suffix('.tmp')
|
|
temp.write_text(json.dumps(record, indent=2) + '\n')
|
|
temp.replace(path)
|
|
|
|
|
|
def interrupted(signum, frame):
|
|
raise InterruptedError('Job interrupted by signal ' + str(signum))
|
|
|
|
|
|
signal.signal(signal.SIGTERM, interrupted)
|
|
signal.signal(signal.SIGINT, interrupted)
|
|
save('running', started=datetime.now(timezone.utc).isoformat())
|
|
try:
|
|
result = subprocess.run(record['command'], check=False, timeout=11 * 3600)
|
|
save('complete' if result.returncode == 0 else 'failed', exit_code=result.returncode,
|
|
finished=datetime.now(timezone.utc).isoformat())
|
|
sys.exit(result.returncode)
|
|
except Exception as error:
|
|
save('failed', error=str(error), finished=datetime.now(timezone.utc).isoformat())
|
|
raise
|