50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Sequential process replacement; at least one existing API stays running."""
|
|
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
|
|
from cluster import compose, instances, wait_lb
|
|
|
|
|
|
def replace_all(base="http://127.0.0.1:8083"):
|
|
original = instances()
|
|
if len(original) < 2:
|
|
raise RuntimeError("Rolling replacement requires at least two healthy replicas")
|
|
wait_lb(base, len(original))
|
|
replacements = []
|
|
for current in original:
|
|
subprocess.run(
|
|
["docker", "stop", "--time", "5", current["id"]],
|
|
check=True,
|
|
capture_output=True,
|
|
timeout=8,
|
|
)
|
|
subprocess.run(["docker", "rm", current["id"]], check=True, capture_output=True)
|
|
compose(
|
|
"up", "-d", "--no-deps", "--no-recreate", "--scale", "api=" + str(len(original)), "api"
|
|
)
|
|
aliases = wait_lb(base, len(original))
|
|
if current["instance"] in aliases:
|
|
raise RuntimeError("Old API process is still serving after replacement")
|
|
replacements.append(
|
|
{
|
|
"old_container": current["id"],
|
|
"old_instance": current["instance"],
|
|
"live_instances": aliases,
|
|
}
|
|
)
|
|
return replacements
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:8083")
|
|
args = parser.parse_args()
|
|
print(json.dumps(replace_all(args.base_url)))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|