hy-app-monitor/ops/sync_runtime_compose.py

55 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
# 直接把项目根目录加到 import path脚本可以从任意 cwd 调起。
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from monitors.yumi.compose_templates import managed_compose_host_names, save_local_host_compose_template, sync_remote_compose_templates
def parse_args() -> argparse.Namespace:
# 默认只刷新本地完整模板,不主动改线上 compose。
parser = argparse.ArgumentParser(description="refresh full docker compose templates for runtime hosts")
parser.add_argument("--hosts", nargs="*", default=[], help="target host names, default all managed service hosts")
parser.add_argument(
"--sync-remote",
action="store_true",
help="after refreshing local templates, also sync full compose files to remote hosts",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
host_names = [host_name for host_name in (args.hosts or managed_compose_host_names()) if host_name]
if args.sync_remote:
payload = sync_remote_compose_templates(host_names, refresh_from_remote=True)
print(json.dumps(payload, ensure_ascii=False, indent=2))
return
items = [save_local_host_compose_template(host_name) for host_name in host_names]
print(
json.dumps(
{
"ok": True,
"updatedAt": items[-1]["updatedAt"] if items else "",
"hosts": items,
},
ensure_ascii=False,
indent=2,
)
)
if __name__ == "__main__":
main()