ops: add channel inventory and metinctl
This commit is contained in:
@@ -10,7 +10,7 @@ SCRIPT_DIR = Path(__file__).resolve().parent
|
||||
REPO_ROOT = SCRIPT_DIR.parent.parent
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
import channels
|
||||
import channel_inventory
|
||||
|
||||
|
||||
TEMPLATES_DIR = SCRIPT_DIR / "templates"
|
||||
@@ -24,6 +24,7 @@ def parse_args() -> argparse.Namespace:
|
||||
parser.add_argument("--runtime-root", required=True, help="Absolute path to the live runtime root")
|
||||
parser.add_argument("--systemd-dir", default="/etc/systemd/system", help="systemd unit destination")
|
||||
parser.add_argument("--libexec-dir", default="/usr/local/libexec", help="Helper script destination")
|
||||
parser.add_argument("--bin-dir", default="/usr/local/bin", help="Binary/script destination")
|
||||
parser.add_argument("--env-file", default="/etc/metin/metin.env", help="Optional EnvironmentFile path for runtime overrides")
|
||||
parser.add_argument("--wait-host", default="127.0.0.1", help="DB readiness host")
|
||||
parser.add_argument("--wait-port", type=int, default=9000, help="DB readiness port")
|
||||
@@ -72,31 +73,18 @@ def copy_file(source: Path, destination: Path, mode: int) -> None:
|
||||
|
||||
|
||||
def resolve_channels(args: argparse.Namespace) -> list[int]:
|
||||
available_channels = {int(channel_id) for channel_id in channels.CHANNEL_MAP.keys()}
|
||||
|
||||
if args.channels:
|
||||
selected = set(args.channels)
|
||||
else:
|
||||
selected = {channel_id for channel_id in available_channels if channel_id <= args.channel_limit}
|
||||
|
||||
if 99 in available_channels:
|
||||
selected.add(99)
|
||||
|
||||
unknown = sorted(selected - available_channels)
|
||||
if unknown:
|
||||
print(f"Unknown channels requested: {unknown}", file=sys.stderr)
|
||||
try:
|
||||
return channel_inventory.resolve_selected_channels(
|
||||
channel_limit=args.channel_limit,
|
||||
explicit_channels=args.channels,
|
||||
)
|
||||
except ValueError as exc:
|
||||
print(str(exc), file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
|
||||
return sorted(selected)
|
||||
|
||||
|
||||
def resolve_instances(selected_channels: list[int]) -> list[str]:
|
||||
instances: list[str] = []
|
||||
for channel_id in selected_channels:
|
||||
cores = channels.CHANNEL_MAP[int(channel_id)]
|
||||
for core_id in sorted(int(core) for core in cores.keys()):
|
||||
instances.append(f"channel{channel_id}_core{core_id}")
|
||||
return instances
|
||||
return channel_inventory.get_instances(selected_channels)
|
||||
|
||||
|
||||
def run(command: list[str]) -> None:
|
||||
@@ -111,13 +99,17 @@ def main() -> int:
|
||||
runtime_root = str(Path(args.runtime_root).resolve())
|
||||
systemd_dir = Path(args.systemd_dir)
|
||||
libexec_dir = Path(args.libexec_dir)
|
||||
bin_dir = Path(args.bin_dir)
|
||||
|
||||
selected_channels = resolve_channels(args)
|
||||
instances = resolve_instances(selected_channels)
|
||||
selected_game_units = set(channel_inventory.get_game_units(selected_channels))
|
||||
all_game_units = set(channel_inventory.get_game_units())
|
||||
|
||||
template_values = {
|
||||
"USER_NAME": args.user,
|
||||
"GROUP_NAME": group_name,
|
||||
"REPO_ROOT": str(REPO_ROOT),
|
||||
"RUNTIME_ROOT": runtime_root,
|
||||
"ENV_FILE": args.env_file,
|
||||
"WAIT_HOST": args.wait_host,
|
||||
@@ -144,17 +136,29 @@ def main() -> int:
|
||||
0o755,
|
||||
)
|
||||
copy_file(BIN_DIR / "metin-wait-port", libexec_dir / "metin-wait-port", 0o755)
|
||||
write_text(
|
||||
bin_dir / "metinctl",
|
||||
render_template(BIN_DIR / "metinctl.in", template_values),
|
||||
0o755,
|
||||
)
|
||||
|
||||
verify_units = [str(systemd_dir / unit_name) for unit_name in unit_names]
|
||||
run(["systemd-analyze", "verify", *verify_units])
|
||||
run(["systemctl", "daemon-reload"])
|
||||
|
||||
stale_game_units = sorted(all_game_units - selected_game_units)
|
||||
if stale_game_units:
|
||||
disable_command = ["systemctl", "disable"]
|
||||
if args.restart:
|
||||
disable_command.append("--now")
|
||||
run([*disable_command, *stale_game_units])
|
||||
|
||||
enable_units = [
|
||||
"metin-server.service",
|
||||
"metin-db.service",
|
||||
"metin-db-ready.service",
|
||||
"metin-auth.service",
|
||||
*[f"metin-game@{instance}.service" for instance in instances],
|
||||
*sorted(selected_game_units),
|
||||
]
|
||||
run(["systemctl", "enable", *enable_units])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user