left4me/l4d2host/cli.py
mwiegand 6b4eef22c2
feat: server Reset action — wipe runtime, keep DB row
Reset stops the systemd service, unmounts the overlay, and rm -rf's both
runtime/<name> and instances/<name>, but keeps the Server row, blueprint,
and (shared) systemd template. Next Start re-initializes from the current
blueprint, so users can clean up logs/caches/accumulated game state without
losing the server.

Implementation factors a shared _purge_instance helper out of
delete_instance; reset_instance reuses it without the existence guard. New
"reset" lifecycle op flows through the same route + worker + facade plumbing
as the other server ops.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 18:10:32 +02:00

97 lines
2.6 KiB
Python

from pathlib import Path
import json
import subprocess
import typer
from l4d2host.instances import delete_instance, initialize_instance, reset_instance, start_instance, stop_instance
from l4d2host.logs import stream_instance_logs
from l4d2host.status import get_instance_status
from l4d2host.steam_install import SteamInstaller
app = typer.Typer(no_args_is_help=True)
def _exit_from_subprocess_error(exc: subprocess.CalledProcessError) -> None:
if exc.stderr:
typer.echo(exc.stderr, err=True)
raise typer.Exit(code=exc.returncode)
@app.command()
def install() -> None:
try:
SteamInstaller().install_or_update(passthrough=True)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)
@app.command()
def initialize(name: str, spec: Path = typer.Option(..., "-f")) -> None:
try:
initialize_instance(name, spec, passthrough=True)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)
@app.command()
def start(name: str) -> None:
try:
start_instance(name, passthrough=True)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)
@app.command()
def stop(name: str) -> None:
try:
stop_instance(name, passthrough=True)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)
@app.command()
def delete(name: str) -> None:
try:
delete_instance(name, passthrough=True)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)
@app.command()
def reset(name: str) -> None:
try:
reset_instance(name, passthrough=True)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)
@app.command()
def status(name: str, json_output: bool = typer.Option(False, "--json")) -> None:
instance_status = get_instance_status(name)
if json_output:
typer.echo(
json.dumps(
{
"state": instance_status.state,
"raw_active_state": instance_status.raw_active_state,
"raw_sub_state": instance_status.raw_sub_state,
}
)
)
return
typer.echo(instance_status.state)
@app.command()
def logs(
name: str,
lines: int = typer.Option(200, "--lines"),
follow: bool = typer.Option(True, "--follow/--no-follow"),
) -> None:
try:
for line in stream_instance_logs(name, lines=lines, follow=follow):
typer.echo(line)
except subprocess.CalledProcessError as exc:
_exit_from_subprocess_error(exc)