Implement lab 3 replica failover and shared idempotency for grade 4
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
"""Replace each original API container, keeping its peer available."""
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
from pathlib import Path
|
||||
import os
|
||||
os.chdir(Path(__file__).resolve().parents[1])
|
||||
|
||||
def command(*args):
|
||||
return subprocess.check_output(args, text=True)
|
||||
|
||||
def ready():
|
||||
deadline = time.monotonic() + 60
|
||||
while time.monotonic() < deadline:
|
||||
ids = command('docker','compose','ps','-q','api').split()
|
||||
states = json.loads(command('docker','inspect',*ids)) if ids else []
|
||||
if len(states) == 2 and all(x['State'].get('Health',{}).get('Status')=='healthy' for x in states):
|
||||
time.sleep(3) # allow HAProxy readiness/DNS to converge
|
||||
return
|
||||
time.sleep(.5)
|
||||
raise RuntimeError('API did not become healthy')
|
||||
|
||||
if __name__ == '__main__':
|
||||
ready()
|
||||
old = command('docker','compose','ps','-q','api').split()
|
||||
for container in old:
|
||||
command('docker','stop','-t','8',container)
|
||||
command('docker','rm',container)
|
||||
command('docker','compose','up','-d','--no-deps','--no-build','--scale','api=2','api')
|
||||
ready()
|
||||
new = command('docker','compose','ps','-q','api').split()
|
||||
assert not set(old) & set(new)
|
||||
print(json.dumps({'old_containers':old,'new_containers':new}))
|
||||
Reference in New Issue
Block a user