Configure full Docker setup and confirm consequential actions
This commit is contained in:
+2
-4
@@ -1,10 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compatibility entry point for the standard binary and skill installer."""
|
||||
"""Compatibility entry point for the native or full Docker installer."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 1:
|
||||
sys.exit("Install binaries with go install ./cmd/...; set GOBIN to choose the destination. Configure and start services separately.")
|
||||
sys.exit(subprocess.call([str(Path(__file__).resolve().parents[1]/"install.sh")]))
|
||||
sys.exit(subprocess.call([str(Path(__file__).resolve().parents[1]/"install.sh"), *sys.argv[1:]]))
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Configure, authenticate and verify the full Docker installation."""
|
||||
import argparse
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def module(name, file):
|
||||
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(file))
|
||||
result = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(result)
|
||||
return result
|
||||
|
||||
|
||||
configure = module("configure_compose", "configure-compose.py")
|
||||
tokens = module("save_ntfy_token", "save-ntfy-token.py")
|
||||
|
||||
|
||||
class Setup:
|
||||
def __init__(self, directory, runner=subprocess.run, prompt=input):
|
||||
self.directory = Path(directory)
|
||||
self.runner, self.prompt = runner, prompt
|
||||
|
||||
def compose(self, *args, capture=False, check=True, **kwargs):
|
||||
result = self.runner(["docker", "compose", *args], cwd=self.directory,
|
||||
capture_output=capture, text=True, **kwargs)
|
||||
if check and result.returncode:
|
||||
# Captured ntfy output may contain tokens. Never replay it on failure.
|
||||
raise ValueError(f"Docker setup step failed (exit {result.returncode}); no credentials were replaced")
|
||||
return result
|
||||
|
||||
def settings(self, args):
|
||||
settings = self.directory / ".env"
|
||||
if settings.exists():
|
||||
if any((args.ntfy_url, args.bind_address, args.timezone, args.port)):
|
||||
raise ValueError(".env already exists; edit it explicitly rather than replace existing configuration")
|
||||
print("Keeping existing .env and credentials.")
|
||||
return
|
||||
url = args.ntfy_url or self.prompt("Phone-facing ntfy base URL (HTTPS or trusted LAN/VPN, no topic): ").strip()
|
||||
bind = args.bind_address or self.prompt("ntfy bind IP (LAN/VPN IP for direct phone access; 127.0.0.1 for reverse proxy): ").strip()
|
||||
if not bind:
|
||||
raise ValueError("Choose an explicit ntfy bind address; setup will not expose it by guessing")
|
||||
timezone = args.timezone
|
||||
if not timezone:
|
||||
try:
|
||||
timezone = configure.host_timezone()
|
||||
except ValueError:
|
||||
timezone = self.prompt("Timezone (e.g. Europe/London): ").strip()
|
||||
configure.configure(self.directory, url, bind, timezone, args.port or 8081)
|
||||
|
||||
def bootstrap_ntfy(self):
|
||||
token = self.directory / "secrets/ntfy-token.txt"
|
||||
if token.is_symlink():
|
||||
raise ValueError("Refusing a symlink for the publishing token")
|
||||
if token.exists() and token.stat().st_size:
|
||||
print("Keeping existing ntfy publishing token.")
|
||||
else:
|
||||
print("Set a password for ntfy's pipeline-publisher account (not a model-provider account).")
|
||||
self.compose("exec", "ntfy", "ntfy", "user", "add", "--ignore-exists", "pipeline-publisher")
|
||||
self.compose("exec", "-T", "ntfy", "ntfy", "access", "pipeline-publisher", "pipelines", "write-only")
|
||||
result = self.compose("exec", "-T", "ntfy", "ntfy", "token", "add", "pipeline-publisher", capture=True)
|
||||
tokens.save_token(result.stdout + result.stderr, self.directory)
|
||||
# --ignore-exists preserves the phone account's password on re-install.
|
||||
print("Set the phone-reader password if this is a new ntfy account; existing accounts are preserved.")
|
||||
self.compose("exec", "ntfy", "ntfy", "user", "add", "--ignore-exists", "phone-reader")
|
||||
self.compose("exec", "-T", "ntfy", "ntfy", "access", "phone-reader", "pipelines", "read-only")
|
||||
|
||||
def authenticate(self, relogin=False):
|
||||
def connected():
|
||||
result = self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "opencode",
|
||||
"pipelines", "auth", "list", "--format", "json", capture=True)
|
||||
accounts = json.loads(result.stdout)
|
||||
if not isinstance(accounts, list):
|
||||
raise ValueError("Unexpected OpenCode account-list format; authentication not verified")
|
||||
return any(isinstance(account, dict) and account.get("connections") for account in accounts)
|
||||
|
||||
if connected() and not relogin:
|
||||
print("Keeping existing OpenCode provider setup in its Docker volume.")
|
||||
return
|
||||
print("Choose and authenticate your provider in the container's OpenCode login below.")
|
||||
self.compose("run", "--rm", "--no-deps", "--entrypoint", "opencode", "pipelines", "auth", "login")
|
||||
if not connected():
|
||||
raise ValueError("Provider setup incomplete; rerun the installer to continue (credentials/volumes retained)")
|
||||
|
||||
def verify(self):
|
||||
# Read the token inside the container; never put it in argv or output.
|
||||
probe = '''set -eu
|
||||
test -s "$PIPELINES_NTFY_TOKEN_FILE"
|
||||
printf 'header = "Authorization: Bearer %s"\\n' "$(cat "$PIPELINES_NTFY_TOKEN_FILE")" |
|
||||
curl --config - --fail --silent --show-error --max-time 10 --output /dev/null \
|
||||
--data 'Pipelines Docker setup: authenticated publishing verified.' "$PIPELINES_NTFY_URL"
|
||||
'''
|
||||
self.compose("exec", "-T", "pipelines", "sh", "-c", probe, capture=True)
|
||||
result = self.compose("exec", "-T", "pipelines", "pipelines", "--url",
|
||||
"ws://127.0.0.1:8080/connect", "status", "--json", capture=True)
|
||||
status = json.loads(result.stdout)
|
||||
if (status.get("ntfy", {}).get("health") != "healthy"
|
||||
or not status.get("ntfy", {}).get("url") or status.get("topic") != "pipelines"):
|
||||
raise ValueError("Pipelines did not report healthy ntfy; check docker compose logs pipelines ntfy")
|
||||
print("Verified: Pipelines reports healthy ntfy and authenticated publishing succeeded.")
|
||||
print("On your phone: add the ntfy server from .env, log in as phone-reader, subscribe to pipelines.")
|
||||
print("Phone delivery and model inference are not verified by this setup check.")
|
||||
address = self.compose("port", "pipelines", "8080", capture=True).stdout.strip()
|
||||
port = address.rsplit(":", 1)[-1]
|
||||
if not port.isdigit():
|
||||
raise ValueError("Could not identify the published Pipelines port")
|
||||
print(f"Host CLI/TUI connection: pipelines --url ws://127.0.0.1:{port}/connect tui")
|
||||
print("Install host client binaries with ./install.sh --native if needed.")
|
||||
|
||||
def run(self, args):
|
||||
self.compose("version", capture=True)
|
||||
self.settings(args)
|
||||
# Validate the actual resolved Compose configuration before starting anything.
|
||||
self.compose("config", "--quiet", capture=True)
|
||||
self.compose("up", "-d", "--wait", "--wait-timeout", "60", "ntfy")
|
||||
self.bootstrap_ntfy()
|
||||
self.compose("build", "pipelines")
|
||||
self.compose("run", "--rm", "--no-deps", "-T", "--entrypoint", "pipelines", "pipelines", "skill", "install")
|
||||
self.authenticate(args.relogin)
|
||||
# Recreate to reload file secrets and environment, without deleting volumes.
|
||||
self.compose("up", "-d", "--force-recreate", "--wait", "--wait-timeout", "60", "pipelines")
|
||||
self.verify()
|
||||
print("Docker setup complete. Use docker compose ps / logs to manage it; no OS user service was registered.")
|
||||
|
||||
|
||||
def arguments(argv=None):
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--ntfy-url")
|
||||
parser.add_argument("--bind-address")
|
||||
parser.add_argument("--timezone")
|
||||
parser.add_argument("--port", type=int)
|
||||
parser.add_argument("--relogin", action="store_true", help="open OpenCode provider login again without deleting accounts")
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
Setup(Path(__file__).resolve().parents[1]).run(arguments())
|
||||
except (ValueError, OSError, EOFError, json.JSONDecodeError) as error:
|
||||
sys.exit(f"Docker setup incomplete: {error}")
|
||||
@@ -7,6 +7,12 @@ import manage
|
||||
|
||||
|
||||
class InstallationTests(unittest.TestCase):
|
||||
def test_docker_installer_is_available_through_main_entrypoint(self):
|
||||
result=subprocess.run([str(Path(__file__).resolve().parents[1]/"install.sh"),"--docker","--help"],capture_output=True,text=True)
|
||||
self.assertEqual(result.returncode,0)
|
||||
self.assertIn("--ntfy-url",result.stdout)
|
||||
self.assertIn("--relogin",result.stdout)
|
||||
|
||||
def test_standard_installer_needs_only_go_and_honors_gobin(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root=Path(directory)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location("setup", Path(__file__).with_name("setup-docker.py"))
|
||||
setup = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(setup)
|
||||
|
||||
|
||||
class DockerSetupTests(unittest.TestCase):
|
||||
def exercise(self, directory, existing=False, fail=None, confirm="yes"):
|
||||
calls = []
|
||||
token = "tk_" + "a" * 29
|
||||
logged = existing
|
||||
|
||||
def runner(command, **kwargs):
|
||||
nonlocal logged
|
||||
args = command[2:]
|
||||
calls.append(args)
|
||||
code, out = 0, ""
|
||||
if args[-4:] == ["auth", "list", "--format", "json"]:
|
||||
out = '[{"connections":[{"id":"fixture"}]}]' if logged else '[]'
|
||||
if args[-2:] == ["auth", "login"] and confirm == "yes":
|
||||
logged = True
|
||||
if "token" in args:
|
||||
out = f"token {token} created\n"
|
||||
if args[-2:] == ["status", "--json"]:
|
||||
out = '{"ntfy":{"health":"healthy","url":"http://192.168.1.20:8090"},"topic":"pipelines"}'
|
||||
if args[:1] == ["port"]:
|
||||
out = "127.0.0.1:8081\n"
|
||||
if fail and fail in args:
|
||||
code, out = 9, "private provider data/token"
|
||||
return subprocess.CompletedProcess(command, code, stdout=out, stderr="")
|
||||
|
||||
args = setup.arguments([] if existing else ["--ntfy-url", "http://192.168.1.20:8090",
|
||||
"--bind-address", "192.168.1.20", "--timezone", "Europe/London"])
|
||||
installer = setup.Setup(directory, runner=runner, prompt=lambda _: confirm)
|
||||
installer.run(args)
|
||||
return calls, token
|
||||
|
||||
def test_fresh_setup_wires_notifications_and_runs_container_login(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
calls, token = self.exercise(directory)
|
||||
self.assertIn(["run", "--rm", "--no-deps", "--entrypoint", "opencode", "pipelines", "auth", "login"], calls)
|
||||
self.assertTrue(any("--force-recreate" in c for c in calls))
|
||||
self.assertEqual(Path(directory, "secrets/ntfy-token.txt").read_text(), token + "\n")
|
||||
self.assertFalse(any(token in " ".join(c) for c in calls))
|
||||
self.assertLess(next(i for i,c in enumerate(calls) if "token" in c), next(i for i,c in enumerate(calls) if "build" in c))
|
||||
self.assertLess(next(i for i,c in enumerate(calls) if "login" in c), next(i for i,c in enumerate(calls) if "--force-recreate" in c))
|
||||
self.assertTrue(any("curl --config -" in " ".join(c) for c in calls))
|
||||
|
||||
def test_reinstall_preserves_configuration_tokens_and_provider_setup(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
self.exercise(directory)
|
||||
files = [Path(directory, ".env"), Path(directory, "secrets/ntfy-token.txt")]
|
||||
before = [p.read_bytes() for p in files]
|
||||
calls, _ = self.exercise(directory, existing=True)
|
||||
self.assertFalse(any("login" in c or "token" in c for c in calls))
|
||||
self.assertEqual(before, [p.read_bytes() for p in files])
|
||||
|
||||
def test_login_failure_or_cancel_does_not_mark_complete_or_start_server(self):
|
||||
for fail, confirm in [("login", "yes"), (None, "no")]:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
self.exercise(directory, fail=fail, confirm=confirm)
|
||||
self.assertNotIn("private provider", str(raised.exception))
|
||||
self.assertTrue(Path(directory, "secrets/ntfy-token.txt").stat().st_size)
|
||||
|
||||
def test_status_and_publication_failures_are_not_success(self):
|
||||
for failure in ["status", "sh"]:
|
||||
with tempfile.TemporaryDirectory() as directory, self.assertRaises(ValueError):
|
||||
self.exercise(directory, fail=failure)
|
||||
|
||||
def test_existing_env_is_not_replaced_by_flags(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory, ".env")
|
||||
path.write_text("existing settings")
|
||||
with self.assertRaises(ValueError):
|
||||
setup.Setup(directory).settings(setup.arguments(["--ntfy-url", "https://example.com"]))
|
||||
self.assertEqual(path.read_text(), "existing settings")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user