5f70813202
- web: move app/web/web/* to app/web/, drop the old standalone compose file and watcher_additions.py (merged into watcher.py) - web: /api/version fingerprint of state, config and queued requests; the page polls it every 2s and reloads as soon as the watcher acts - watcher: self-update. When its own branch has a new commit and the checkout is clean, start a one-off gitops-watcher-updater container that runs git pull + docker compose up -d --build on the host checkout - watcher: refuse to deploy/stop/remove its own compose project, which used to stop the watcher itself mid-way Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
333 lines
11 KiB
Python
333 lines
11 KiB
Python
import os
|
|
import subprocess
|
|
import time
|
|
from datetime import datetime
|
|
|
|
import typer
|
|
from rich.console import Console
|
|
from rich.markup import escape
|
|
from rich.table import Table
|
|
from watcher import (
|
|
CONFIG_PATH,
|
|
NAME_RE,
|
|
STACKS_DIR,
|
|
append_entry,
|
|
compose_cmd,
|
|
delete_entry,
|
|
find_index,
|
|
load_config,
|
|
load_config_rt,
|
|
load_state,
|
|
normalize,
|
|
redact,
|
|
request_check,
|
|
save_config_rt,
|
|
)
|
|
|
|
app = typer.Typer(help="control the gitops-watcher.")
|
|
console = Console()
|
|
|
|
|
|
def ago(iso: str | None) -> str:
|
|
"""'2026-11-11T09:30:00' -> '09:30:00 (5m ago)', or '-' if missing."""
|
|
if not iso:
|
|
return "-"
|
|
try:
|
|
then = datetime.fromisoformat(iso)
|
|
except ValueError:
|
|
return iso
|
|
secs = int((datetime.now() - then).total_seconds())
|
|
if secs < 60:
|
|
rel = f"{secs}s"
|
|
elif secs < 3600:
|
|
rel = f"{secs // 60}m"
|
|
elif secs < 86400:
|
|
rel = f"{secs // 3600}h"
|
|
else:
|
|
rel = f"{secs // 86400}d"
|
|
return f"{then:%m-%d %H:%M:%S} ({rel} ago)"
|
|
|
|
|
|
def fail(msg: str) -> None:
|
|
console.print(f"[red]{msg}[/red]")
|
|
raise typer.Exit(code=1)
|
|
|
|
|
|
def find_repo(name: str, quiet: bool = False) -> dict | None:
|
|
"""The normalized repo entry for `name` from repos.yml.
|
|
|
|
Exits with an error if it's missing or invalid, or returns None when quiet.
|
|
"""
|
|
try:
|
|
config = load_config()
|
|
except Exception as e:
|
|
fail(f"can't read {CONFIG_PATH}: {e}")
|
|
defaults = config.get("defaults") or {}
|
|
for raw in config.get("repos") or []:
|
|
if isinstance(raw, dict) and raw.get("name") == name:
|
|
try:
|
|
return normalize(raw, defaults)
|
|
except ValueError as e:
|
|
if quiet:
|
|
return None
|
|
fail(f"entry for '{name}' is invalid: {e}")
|
|
if quiet:
|
|
return None
|
|
fail(f"no repo named '{name}' in {CONFIG_PATH}")
|
|
|
|
|
|
def config_names() -> list[str] | None:
|
|
"""Repo names from repos.yml, or None if it can't be read."""
|
|
try:
|
|
config = load_config()
|
|
except Exception:
|
|
return None
|
|
defaults = config.get("defaults") or {}
|
|
names = []
|
|
for raw in config.get("repos") or []:
|
|
try:
|
|
names.append(normalize(raw, defaults)["name"])
|
|
except ValueError:
|
|
pass
|
|
return names
|
|
|
|
|
|
@app.command()
|
|
def status():
|
|
"""Show the status of every watched repo."""
|
|
state = load_state()
|
|
names = config_names()
|
|
|
|
# repos in the config but not checked yet show up as "pending"
|
|
for name in names or []:
|
|
state.setdefault(name, {"status": "pending"})
|
|
if not state:
|
|
console.print("[yellow]No data yet. Has the watcher run a round?[/yellow]")
|
|
return
|
|
|
|
table = Table(title="gitops-watcher")
|
|
table.add_column("Repo", style="cyan", no_wrap=True)
|
|
table.add_column("Status")
|
|
table.add_column("Commit")
|
|
table.add_column("Last check")
|
|
table.add_column("Last deploy")
|
|
table.add_column("Error", max_width=60)
|
|
|
|
failing = 0
|
|
for name, info in sorted(state.items()):
|
|
status = info.get("status") or "unknown"
|
|
if status == "ok":
|
|
status_cell = "[green]ok[/green]"
|
|
elif status == "error":
|
|
failing += 1
|
|
status_cell = "[red]error[/red]"
|
|
retry_at = info.get("retry_at")
|
|
if retry_at and retry_at > time.time():
|
|
mins = int((retry_at - time.time()) // 60) + 1
|
|
status_cell += f"\n[dim]retry in {mins}m ({info.get('failures', 1)}x)[/dim]"
|
|
else:
|
|
status_cell = f"[yellow]{escape(status)}[/yellow]"
|
|
if names is not None and name not in names:
|
|
status_cell += "\n[dim]not in config[/dim]"
|
|
|
|
# commit: first 7 chars, or "-" if missing
|
|
commit = (info.get("commit") or "-")[:7]
|
|
|
|
# times: show them, or "-" if missing
|
|
last_check = ago(info.get("last_check"))
|
|
last_deploy = ago(info.get("last_deploy"))
|
|
|
|
# error: last non-empty line, or "" if none. The first line is always
|
|
# "<command> failed:"; the actual cause is at the end of the output.
|
|
# escaped so [brackets] in git/docker output aren't read as rich markup
|
|
lines = [l for l in (info.get("error") or "").splitlines() if l.strip()]
|
|
error = escape(lines[-1]) if lines else ""
|
|
|
|
table.add_row(name, status_cell, commit, last_check, last_deploy, error)
|
|
|
|
console.print(table)
|
|
if names is None:
|
|
console.print("[yellow]couldn't read repos.yml; showing saved state only[/yellow]")
|
|
|
|
# non-zero exit when something is failing, so scripts/cron can alert on it
|
|
raise typer.Exit(code=1 if failing else 0)
|
|
|
|
|
|
@app.command()
|
|
def list_repos():
|
|
"""List repos from repos.yml with their settings."""
|
|
try:
|
|
config = load_config()
|
|
except Exception as e:
|
|
fail(f"can't read {CONFIG_PATH}: {e}")
|
|
defaults = config.get("defaults") or {}
|
|
entries = config.get("repos") or []
|
|
if not entries:
|
|
console.print(f"[yellow]no repos in {CONFIG_PATH}[/yellow]")
|
|
return
|
|
|
|
table = Table(title=str(CONFIG_PATH))
|
|
table.add_column("Repo", style="cyan", no_wrap=True)
|
|
table.add_column("URL")
|
|
table.add_column("Branch")
|
|
table.add_column("Compose file")
|
|
table.add_column("Pull")
|
|
table.add_column("Token")
|
|
|
|
invalid = 0
|
|
for i, raw in enumerate(entries):
|
|
try:
|
|
repo = normalize(raw, defaults)
|
|
except ValueError as e:
|
|
invalid += 1
|
|
label = raw.get("name") if isinstance(raw, dict) and raw.get("name") else f"repos[{i}]"
|
|
table.add_row(escape(str(label)), f"[red]invalid: {escape(str(e))}[/red]", "", "", "", "")
|
|
continue
|
|
|
|
token_env = repo.get("token_env")
|
|
if not token_env:
|
|
token = "[dim]none[/dim]"
|
|
elif os.environ.get(token_env):
|
|
token = f"{token_env} [green]set[/green]"
|
|
else:
|
|
token = f"{token_env} [red]not set[/red]"
|
|
|
|
table.add_row(
|
|
repo["name"],
|
|
escape(redact(repo["url"])),
|
|
repo["branch"],
|
|
repo["compose_file"] or "[dim]auto[/dim]",
|
|
"yes" if repo["pull_image"] else "no",
|
|
token,
|
|
)
|
|
|
|
console.print(table)
|
|
if invalid:
|
|
raise typer.Exit(code=1)
|
|
|
|
|
|
@app.command()
|
|
def logs(
|
|
name: str,
|
|
tail: int = typer.Option(50, help="Number of lines per container."),
|
|
follow: bool = typer.Option(False, "--follow", "-f", help="Keep streaming new lines."),
|
|
):
|
|
"""Show recent container logs for a stack."""
|
|
path = STACKS_DIR / name
|
|
if not path.is_dir():
|
|
fail(f"no stack folder at {path}; has '{name}' been deployed?")
|
|
|
|
# prefer the current config; fall back to the saved state for stacks
|
|
# that were removed from repos.yml but are still running
|
|
repo = find_repo(name, quiet=True)
|
|
if repo:
|
|
compose_file = repo["compose_file"]
|
|
else:
|
|
compose_file = load_state().get(name, {}).get("compose_file")
|
|
|
|
cmd = compose_cmd(name, path, compose_file) + ["logs", "--tail", str(tail)]
|
|
if follow:
|
|
cmd.append("--follow")
|
|
# not captured: output streams straight to the terminal
|
|
try:
|
|
code = subprocess.run(cmd, cwd=path).returncode
|
|
except KeyboardInterrupt:
|
|
code = 0
|
|
raise typer.Exit(code=code)
|
|
|
|
|
|
@app.command()
|
|
def check(
|
|
name: str,
|
|
force: bool = typer.Option(False, "--force", help="Redeploy even if nothing changed."),
|
|
wait: bool = typer.Option(False, "--wait", help="Wait for the result."),
|
|
):
|
|
"""Ask the watcher to check a repo now."""
|
|
if find_repo(name)["paused"]: # find_repo exits if it isn't in repos.yml
|
|
fail(f"'{name}' is paused in {CONFIG_PATH}; unpause it first (or use Start in the website)")
|
|
before = load_state().get(name, {}).get("last_check")
|
|
request_check(name, force)
|
|
console.print(f"requested a check of [cyan]{name}[/cyan]" + (" (forced redeploy)" if force else ""))
|
|
console.print("[dim]clears any retry backoff; the watcher picks it up within a second[/dim]")
|
|
if not wait:
|
|
return
|
|
|
|
with console.status("waiting for the watcher..."):
|
|
try:
|
|
while True:
|
|
time.sleep(1)
|
|
info = load_state().get(name, {})
|
|
if info.get("last_check") != before:
|
|
break
|
|
except KeyboardInterrupt:
|
|
fail("stopped waiting (the check still happens)")
|
|
|
|
if info.get("status") == "ok":
|
|
console.print(f"[green]ok[/green] at {(info.get('commit') or '-')[:7]}")
|
|
else:
|
|
lines = [l for l in (info.get("error") or "").splitlines() if l.strip()]
|
|
fail(f"error: {lines[-1] if lines else 'unknown'}")
|
|
|
|
|
|
@app.command()
|
|
def add(
|
|
name: str,
|
|
url: str,
|
|
branch: str = typer.Option(None, help="Defaults to the branch in repos.yml defaults, or main."),
|
|
token_env: str = typer.Option(None, help="Name of the env var holding the access token."),
|
|
compose_file: str = typer.Option(None, help="Defaults to repos.yml defaults, or auto-detect."),
|
|
):
|
|
"""Add a repo to repos.yml."""
|
|
if not NAME_RE.match(name):
|
|
fail(f"invalid name '{name}': use lowercase letters, digits, '-' and '_'")
|
|
if token_env and not url.startswith("https://"):
|
|
fail("token auth only works with https:// URLs")
|
|
|
|
data = load_config_rt()
|
|
repos = data["repos"]
|
|
if find_index(repos, name) is not None:
|
|
fail(f"'{name}' is already in {CONFIG_PATH}")
|
|
|
|
# only write what was given, so the entry inherits from `defaults`
|
|
entry = {"name": name, "url": url}
|
|
if branch:
|
|
entry["branch"] = branch
|
|
if token_env:
|
|
entry["token_env"] = token_env
|
|
if compose_file:
|
|
entry["compose_file"] = compose_file
|
|
append_entry(repos, entry)
|
|
save_config_rt(data)
|
|
|
|
console.print(f"added [cyan]{name}[/cyan] to {CONFIG_PATH}; the watcher deploys it on its next round")
|
|
if token_env and not os.environ.get(token_env):
|
|
console.print(f"[yellow]note: {token_env} isn't set here. Add it to .env, then recreate the container (docker compose up -d) so the watcher sees it[/yellow]")
|
|
|
|
|
|
@app.command()
|
|
def remove(
|
|
name: str,
|
|
yes: bool = typer.Option(False, "--yes", "-y", help="Don't ask for confirmation."),
|
|
):
|
|
"""Remove a repo from repos.yml."""
|
|
data = load_config_rt()
|
|
repos = data["repos"]
|
|
idx = find_index(repos, name)
|
|
if idx is None:
|
|
fail(f"no repo named '{name}' in {CONFIG_PATH}")
|
|
|
|
console.print(
|
|
f"this removes [cyan]{name}[/cyan] from {CONFIG_PATH}. On its next round the watcher "
|
|
f"runs `docker compose down` for it.\n[dim]its folder and volumes are kept[/dim]"
|
|
)
|
|
if not yes and not typer.confirm("continue?"):
|
|
raise typer.Exit(code=1)
|
|
|
|
delete_entry(repos, idx)
|
|
save_config_rt(data)
|
|
console.print(f"removed [cyan]{name}[/cyan]")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app()
|