Files
mtusi2026lab4/scripts/actions.py
T

23 lines
942 B
Python

"""Restart/failover hooks scoped to lab4; never remove volumes."""
import json
import os
from pathlib import Path
import subprocess
import sys
root=Path(__file__).resolve().parents[1]
base=['docker','compose','--project-directory',str(root),'-p','lyakhonov-lab4']
def compose(*args):
return subprocess.check_output(base+list(args),text=True).strip()
mode=sys.argv[1]
if mode=='restart':
compose('restart','api','db','redis','s3','lb','reconciler')
elif mode=='stop-one':
target=os.environ['TARGET_INSTANCE_ID']
for cid in compose('ps','-q','api').splitlines():
info=json.loads(subprocess.check_output(['docker','inspect',cid],text=True))[0]
if target.startswith(info['Config']['Hostname']+'-'):
subprocess.check_call(['docker','stop','--time','1',cid],stdout=subprocess.DEVNULL)
break
else: raise SystemExit('No matching lab4 API instance')
else: raise SystemExit('unknown action')