From 53be2e53348945fa4f1d7c7fe977244bcc47bb67 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 31 Oct 2026 19:08:25 +0000 Subject: [PATCH] Install and start the server only together with the web app ./install builds the web app and the server before replacing either, and fails without Docker or on a failed build, leaving the old pair in place. The supervisor and start refuse to run the server when the web app is not installed. --- scripts/manage.py | 13 ++++++++- scripts/setup-native.py | 55 ++++++++++++++++++++++++++--------- scripts/test_manage_native.py | 33 +++++++++++++++++---- scripts/test_setup_native.py | 43 +++++++++++++++++++++++---- 4 files changed, 118 insertions(+), 26 deletions(-) diff --git a/scripts/manage.py b/scripts/manage.py index b3d56e61..e32389d8 100644 --- a/scripts/manage.py +++ b/scripts/manage.py @@ -176,7 +176,17 @@ def service_command(config, arguments): print("Supervisor:", "active" if active.returncode == 0 else "inactive") +def require_web_app(config): + """The server never runs without its web app: both or neither.""" + web = config.get("web") or {} + root = Path(web["root"]) if web.get("root") else None + if root is None or not (root / "index.html").is_file(): + raise RuntimeError("The web app is not installed" + (f" in {root}" if root else "") + + "; refusing to start the server without it. Run ./install.") + + def start(config): + require_web_app(config) root = Path(config["prefix"]) if registration(config): if platform.system() == "Darwin": @@ -301,7 +311,7 @@ def report(config): print(f" {'Web':<10} {address} (password in {web['auth_file']})") root = Path(web["root"]) if web.get("root") else None built = root is not None and (root / "index.html").is_file() - print(f" {'Web app':<10} {'installed' if built else 'not built; run ./install --web'}") + print(f" {'Web app':<10} {'installed' if built else 'MISSING; the server will not start until ./install builds it'}") updates = Path(web["updates"]) if web.get("updates") else None apk = updates is not None and (updates / "pipelines.apk").is_file() print(f" {'Phone app':<10} " + (f"{updates / 'pipelines.apk'} (Settings → Check for updates)" @@ -379,6 +389,7 @@ def supervise(config): signal.signal(signal.SIGTERM, stopping) signal.signal(signal.SIGINT, stopping) try: + require_web_app(config) if ntfy_mode(config) == "native": ntfy_child = ntfy_start(config) else: diff --git a/scripts/setup-native.py b/scripts/setup-native.py index 13025d1c..45742614 100644 --- a/scripts/setup-native.py +++ b/scripts/setup-native.py @@ -327,7 +327,7 @@ class Setup: os.chmod(target, 0o700) return target - def configure_web(self, prefix, args): + def configure_web(self, prefix, args, web_build=None): node = str(Path(shutil.which("node")).resolve()) version = self.runner([node, "--version"], capture_output=True, text=True) try: @@ -359,7 +359,7 @@ class Setup: updates.mkdir(parents=True, exist_ok=True) return {"node": node, "script": str(target / "serve.mjs"), "auth_file": str(auth), "host": args.web_host, "port": args.web_port, - "root": self.install_app_web(prefix, args), "updates": str(updates)} + "root": self.install_app_web(prefix, args, web_build), "updates": str(updates)} def choose_password(self): """The web sign-in password for a new computer, chosen by the operator. @@ -384,27 +384,50 @@ class Setup: continue return first - def install_app_web(self, prefix, args): + def build_app_web(self): """Builds the Flutter web app in Docker (reused while its sources are - unchanged) and installs it for the web proxy. Without Docker the web - page explains how to build it later (./install --web).""" - target = prefix / "lib/app-web" + unchanged). The web app ships with the server: the install stops here, + before anything is replaced, when it cannot be built.""" if not app_build.docker_available(self.runner): - print("WARNING: Docker is not running, so the web app was not built. " - "Start Docker, then run ./install --web.") - return str(target) - app_build.install_tree(app_build.build("web", runner=self.runner), target) + raise ValueError("Docker is not running, so the web app cannot be built. The server is never " + "installed without it; start Docker and rerun ./install.") + try: + built = app_build.build("web", runner=self.runner) + except RuntimeError as error: + raise ValueError(f"The web app build failed ({error}); nothing was installed") from None + if not (Path(built) / "index.html").is_file(): + raise ValueError(f"The web app build in {built} has no index.html; nothing was installed") + return built + + def install_app_web(self, prefix, args, built=None): + """Installs the built web app for the web proxy.""" + target = prefix / "lib/app-web" + app_build.install_tree(built or self.build_app_web(), target) return str(target) # --- build and skill ------------------------------------------------- + COMMANDS = ("pipelines-server", "pipelines") + def build(self, prefix): - for command in ("pipelines-server", "pipelines"): + """Builds the server and CLI into bin/.next; install_binaries moves + them into place once the web app is installed too.""" + staging = prefix / "bin/.next" + shutil.rmtree(staging, ignore_errors=True) + staging.mkdir(parents=True) + for command in self.COMMANDS: result = self.runner( - ["go", "build", "-trimpath", "-o", str(prefix / "bin" / command), "./cmd/" + command], + ["go", "build", "-trimpath", "-o", str(staging / command), "./cmd/" + command], cwd=str(self.directory)) if result.returncode: - raise ValueError(f"go build failed for ./cmd/{command}; the native install is incomplete") + shutil.rmtree(staging, ignore_errors=True) + raise ValueError(f"go build failed for ./cmd/{command}; nothing was installed") + + def install_binaries(self, prefix): + staging = prefix / "bin/.next" + for command in self.COMMANDS: + os.replace(staging / command, prefix / "bin" / command) + shutil.rmtree(staging, ignore_errors=True) # --- stale-CLI-on-PATH guard ----------------------------------------- @@ -758,9 +781,13 @@ class Setup: else: self.require_compiler() resolution = self.resolve_ntfy() + # All or nothing: the web app and the server are both built before + # either is installed, and a failed build leaves the old pair running. + web_build = self.build_app_web() self.prepare_layout(prefix) - web = self.configure_web(prefix, args) self.build(prefix) + web = self.configure_web(prefix, args, web_build) + self.install_binaries(prefix) self.check_path_shadow(prefix) self.install_skill(prefix) # The native install launches host harnesses, so write host profiles for diff --git a/scripts/test_manage_native.py b/scripts/test_manage_native.py index f2e262ff..650a2a8c 100644 --- a/scripts/test_manage_native.py +++ b/scripts/test_manage_native.py @@ -139,12 +139,14 @@ class ManageNativeTests(unittest.TestCase): patch.object(manage, "wait_health"), patch.object(manage.signal, "signal"), \ patch.object(manage.subprocess, "Popen", return_value=server) as popen: manage.supervise(config) - command = popen.call_args.args[0] + command = popen.call_args_list[0].args[0] self.assertEqual(command[:5], [str(root / "bin/pipelines-server"), "-addr", "127.0.0.1:8080", "-db", str(root / "data/pipelines.db")]) self.assertEqual("-allow-authoring" in command, role is True) def config(self, root, mode="native"): + (root / "lib/app-web").mkdir(parents=True, exist_ok=True) + (root / "lib/app-web/index.html").write_text("app") return { "format": 1, "prefix": str(root), "python": "python3", "server_port": 8080, "ntfy_port": 8090, "opencode": "opencode", "opencode_env": {}, "path": "", @@ -153,8 +155,26 @@ class ManageNativeTests(unittest.TestCase): "label": "net.a-wan.pipelines", "docker": "docker", "project": "pipelines", "timezone": "UTC", "ntfy": {"mode": mode, "bin": str(root / "bin/ntfy"), "config": str(root / "data/ntfy/server.yml"), "base_url": "http://192.168.1.20:8090"}, + "web": {"node": "/bin/node", "script": str(root / "lib/web/serve.mjs"), "host": "127.0.0.1", + "port": 8082, "auth_file": str(root / "auth.json"), "root": str(root / "lib/app-web")}, } + def test_server_never_starts_without_the_web_app(self): + for missing in ("unconfigured", "not built"): + with self.subTest(missing=missing), tempfile.TemporaryDirectory() as directory: + config = self.config(Path(directory)) + if missing == "unconfigured": + config.pop("web") + else: + (Path(directory) / "lib/app-web/index.html").unlink() + with patch.object(manage, "run") as run, patch.object(manage.subprocess, "Popen") as popen: + with self.assertRaisesRegex(RuntimeError, "refusing to start the server"): + manage.start(config) + with self.assertRaisesRegex(RuntimeError, "refusing to start the server"): + manage.supervise(config) + run.assert_not_called() + popen.assert_not_called() + def test_mode_selection_defaults_to_docker(self): self.assertEqual(manage.ntfy_mode({}), "docker") self.assertEqual(manage.ntfy_mode({"ntfy": {}}), "docker") @@ -166,8 +186,11 @@ class ManageNativeTests(unittest.TestCase): with self.subTest(system=system), tempfile.TemporaryDirectory() as directory: config = self.config(Path(directory)) config["registration"] = str(Path(directory) / "service") + response = MagicMock() + response.__enter__.return_value.status = 200 with patch.object(manage.platform, "system", return_value=system), \ - patch.object(manage, "run") as run, patch.object(manage, "wait_health"): + patch.object(manage, "run") as run, patch.object(manage, "wait_health"), \ + patch.object(manage.urllib.request, "urlopen", return_value=response): manage.start(config) calls = [call.args[0] for call in run.call_args_list] if system == "Linux": @@ -249,7 +272,7 @@ class ManageNativeTests(unittest.TestCase): config = self.config(root) config["web"] = {"node": "/bin/node", "script": "/prefix/lib/web/serve.mjs", "host": "0.0.0.0", "port": 8082, "auth_file": "/private/auth.json", - "root": "/prefix/lib/app-web", "updates": "/prefix/lib/app-updates"} + "root": str(root / "lib/app-web"), "updates": "/prefix/lib/app-updates"} ntfy, server, web = MagicMock(), MagicMock(), MagicMock() ntfy.poll.return_value = None server.poll.return_value = None @@ -263,7 +286,7 @@ class ManageNativeTests(unittest.TestCase): call = popen.call_args_list[1] self.assertEqual(call.args[0], ["/bin/node", "/prefix/lib/web/serve.mjs", "--port", "8082", "--host", "0.0.0.0", "--pipelines", "127.0.0.1:8080", - "--root", "/prefix/lib/app-web", + "--root", str(root / "lib/app-web"), "--updates", "/prefix/lib/app-updates"]) self.assertNotIn("PIPELINES_WEB_AUTH", call.kwargs["env"]) self.assertEqual(call.kwargs["env"]["PIPELINES_WEB_AUTH_FILE"], "/private/auth.json") @@ -275,7 +298,7 @@ class ManageNativeTests(unittest.TestCase): with tempfile.TemporaryDirectory() as directory: config = self.config(Path(directory)) config["registration"] = "/scratch/service" - config["web"] = {"host": "0.0.0.0", "port": 8123} + config["web"] = {"host": "0.0.0.0", "port": 8123, "root": config["web"]["root"]} response = MagicMock() response.__enter__.return_value.status = 200 with patch.object(manage.platform, "system", return_value="Linux"), \ diff --git a/scripts/test_setup_native.py b/scripts/test_setup_native.py index 2d6e8ccb..d22fd24e 100644 --- a/scripts/test_setup_native.py +++ b/scripts/test_setup_native.py @@ -49,7 +49,7 @@ def fake_ntfy_serve(command, **kwargs): class Recorder: def __init__(self, token=TOKEN, volumes=(), image="pipelines-pipelines:latest", - fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False, + fail_create=False, fail_copy=False, fail_volume_ls=False, fail_build=False, fail_web=False, host_model="fixture/host-default"): self.calls = [] self.token = token @@ -59,6 +59,7 @@ class Recorder: self.fail_copy = fail_copy self.fail_volume_ls = fail_volume_ls self.fail_build = fail_build + self.fail_web = fail_web self.host_model = host_model self._module_cache = None self.kwargs = [] @@ -95,6 +96,11 @@ class Recorder: stdout = self.image + "\n" if self.image else "" elif command[1] == "create" and self.fail_create: code = 1 + elif command[1] == "run" and "pipelines-app-build" in command: + # The fake app build writes its output to the /out mount. + out = next(v for v in command if v.endswith(":/out")).rsplit(":", 1)[0] + if not self.fail_web: + (Path(out) / "index.html").write_text("app") elif command[1] == "cp": if self.fail_copy: code = 1 @@ -106,12 +112,11 @@ class Recorder: if self.fail_build: code = 1 elif "-o" in command: - # Only the ntfy build is inspected by the installer; its output - # is created so the atomic install has a real file to move. + # Every build output is created so the atomic installs have real + # files to move. output = Path(command[command.index("-o") + 1]) - if output.name == "ntfy": - output.write_bytes(b"#!/bin/sh\n") - output.chmod(0o755) + output.write_bytes(b"#!/bin/sh\n") + output.chmod(0o755) if command[0].endswith("ntfy") and "token" in command: stdout = f"token {self.token} created for pipeline-publisher\n" return subprocess.CompletedProcess(command, code, stdout=stdout, stderr="") @@ -728,6 +733,32 @@ class SetupNativeTests(unittest.TestCase): self.assertFalse(any("start" in call for call in recorder.calls)) self.assertNotIn("Native Pipelines installed", output.getvalue()) + def test_install_is_all_or_nothing_with_the_web_app(self): + cases = {"no docker": dict(docker=False), "web build fails": dict(fail_web=True), + "server build fails": dict(fail_build=True)} + for name, case in cases.items(): + with self.subTest(case=name), tempfile.TemporaryDirectory() as directory: + recorder = Recorder(fail_web=case.get("fail_web", False), + fail_build=case.get("fail_build", False)) + installer = self.installer(directory, recorder) + prefix = installer.resolve_prefix() + (prefix / "bin").mkdir(parents=True) + (prefix / "bin/pipelines-server").write_text("old") + (prefix / "lib/app-web").mkdir(parents=True) + (prefix / "lib/app-web/index.html").write_text("old") + with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ + patch.object(setup_native.app_build, "docker_available", + return_value=case.get("docker", True)), \ + patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ + redirect_stdout(io.StringIO()): + with self.assertRaises(ValueError): + installer.run(setup_native.arguments([])) + # Neither half was replaced and nothing claims an install. + self.assertEqual((prefix / "bin/pipelines-server").read_text(), "old") + self.assertEqual((prefix / "lib/app-web/index.html").read_text(), "old") + self.assertFalse((prefix / setup_native.MARKER).exists()) + self.assertFalse((prefix / "bin/.next").exists()) + def test_picker_cancellation_or_unsaved_models_never_claim_configuration_complete(self): for returncode in (0, 1): with self.subTest(returncode=returncode), tempfile.TemporaryDirectory() as directory: