diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index 48170856..8de056d7 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -192,9 +192,7 @@ func main() { chatNames = chats.Backends() } if *mdnsOn { - if stopMDNS := advertise(*addr, *mdnsPort); stopMDNS != nil { - defer stopMDNS() - } + defer keepAdvertising(*addr, *mdnsPort)() } printBanner(startup{Build: buildinfo.Current(), Addr: *addr, Database: *db, Artifacts: artifactsRoot, Authoring: *allowAuthoring, OpenCode: openCodeStatus, Chat: chatNames}) diff --git a/cmd/pipelines-server/mdns.go b/cmd/pipelines-server/mdns.go index 1552ad51..c8d6b3e0 100644 --- a/cmd/pipelines-server/mdns.go +++ b/cmd/pipelines-server/mdns.go @@ -2,9 +2,12 @@ package main import ( "log" + "net" "os" + "sort" "strconv" "strings" + "time" "git.a-wan.net/alan/pipelines/internal/buildinfo" "git.a-wan.net/alan/pipelines/internal/discovery" @@ -14,12 +17,16 @@ import ( // server carries on: discovery is a convenience, never a startup requirement. // The returned stop (nil when nothing was published) withdraws the advert. func advertise(addr string, proxyPort int) func() { + return advertiseWith(addr, proxyPort, discovery.LANAddresses(), discovery.TailscaleAddresses()) +} + +func advertiseWith(addr string, proxyPort int, lan, tailnet []net.IP) func() { hostname, _ := os.Hostname() // The installer exports the TLS front's origin (tailscale serve) for the // same proxy; it is advertised so clients can choose that route by name. advert, err := discovery.Plan(discovery.Config{ListenAddr: addr, ProxyPort: proxyPort, - Version: buildinfo.Current().Version, Hostname: hostname, IPs: discovery.LANAddresses(), - TailscaleIPs: discovery.TailscaleAddresses(), PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL")}) + Version: buildinfo.Current().Version, Hostname: hostname, IPs: lan, + TailscaleIPs: tailnet, PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL")}) if err != nil { log.Printf("mDNS: not advertising: %v", err) return nil @@ -51,3 +58,73 @@ func envPort(value string) int { } return port } + +// addressRecheck is how often the advert is compared with the host's current +// addresses. A laptop that changes networks must not keep advertising the IP +// it had at startup. +const addressRecheck = 30 * time.Second + +// keepAdvertising publishes the server now and republishes whenever the +// host's LAN or tailnet addresses change. The returned stop ends both. +func keepAdvertising(addr string, proxyPort int) func() { + lan, tailnet := discovery.LANAddresses(), discovery.TailscaleAddresses() + stop := advertiseWith(addr, proxyPort, lan, tailnet) + done := make(chan struct{}) + go func() { + ticker := time.NewTicker(addressRecheck) + defer ticker.Stop() + for { + select { + case <-done: + return + case <-ticker.C: + nextLAN, nextTailnet := discovery.LANAddresses(), discovery.TailscaleAddresses() + if sameAddresses(lan, nextLAN) && sameAddresses(tailnet, nextTailnet) { + continue + } + if stop != nil { + stop() + } + lan, tailnet = nextLAN, nextTailnet + log.Printf("mDNS: addresses changed to %s; republishing", joinIPs(lan)) + stop = advertiseWith(addr, proxyPort, lan, tailnet) + } + } + }() + return func() { + close(done) + if stop != nil { + stop() + } + } +} + +// sameAddresses compares two address sets regardless of order. +func sameAddresses(a, b []net.IP) bool { + if len(a) != len(b) { + return false + } + as, bs := make([]string, len(a)), make([]string, len(b)) + for i := range a { + as[i], bs[i] = a[i].String(), b[i].String() + } + sort.Strings(as) + sort.Strings(bs) + for i := range as { + if as[i] != bs[i] { + return false + } + } + return true +} + +func joinIPs(ips []net.IP) string { + parts := make([]string, 0, len(ips)) + for _, ip := range ips { + parts = append(parts, ip.String()) + } + if len(parts) == 0 { + return "no LAN address" + } + return strings.Join(parts, ", ") +} diff --git a/cmd/pipelines-server/mdns_test.go b/cmd/pipelines-server/mdns_test.go index 1aef770f..3cadc060 100644 --- a/cmd/pipelines-server/mdns_test.go +++ b/cmd/pipelines-server/mdns_test.go @@ -1,6 +1,9 @@ package main -import "testing" +import ( + "net" + "testing" +) func TestMDNSEnvironmentSwitches(t *testing.T) { for value, want := range map[string]bool{"": true, "1": true, "on": true, "off": false, "0": false, "FALSE": false, " disabled ": false} { @@ -23,3 +26,24 @@ func TestAdvertiseSkipsLoopbackWithoutFailing(t *testing.T) { t.Fatal("loopback-only server was advertised") } } + +func TestSameAddressesIgnoresOrder(t *testing.T) { + a := []net.IP{net.ParseIP("10.0.0.75"), net.ParseIP("192.168.104.82")} + b := []net.IP{net.ParseIP("192.168.104.82"), net.ParseIP("10.0.0.75")} + if !sameAddresses(a, b) { + t.Fatal("same addresses in another order compared unequal") + } + if sameAddresses(a, b[:1]) || sameAddresses(nil, b) || sameAddresses(a, []net.IP{net.ParseIP("10.0.0.75"), net.ParseIP("10.0.0.76")}) { + t.Fatal("different address sets compared equal") + } + if !sameAddresses(nil, nil) { + t.Fatal("two empty sets must compare equal") + } +} + +// keepAdvertising on a loopback-only server publishes nothing, keeps its +// watcher running and stops cleanly. +func TestKeepAdvertisingStopsCleanly(t *testing.T) { + stop := keepAdvertising("127.0.0.1:0", 0) + stop() +} diff --git a/scripts/app_build.py b/scripts/app_build.py index 72c4a090..0a1116ff 100644 --- a/scripts/app_build.py +++ b/scripts/app_build.py @@ -260,8 +260,82 @@ def load_manifest(prefix): return json.loads(path.read_text()) -def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=print): - """Builds the APK for this installation and offers it at /updates/.""" +def native_flutter(flutter=None): + """The host's Flutter executable for native builds, or None.""" + selected = flutter or os.environ.get("PIPELINES_FLUTTER") or shutil.which("flutter") + return selected if selected and Path(selected).is_file() and os.access(selected, os.X_OK) else None + + +def android_sdk_dir(): + """The host's Android SDK, or None when the Docker build is the only option.""" + for candidate in (os.environ.get("ANDROID_HOME"), os.environ.get("ANDROID_SDK_ROOT"), + str(Path.home() / "Library/Android/sdk"), str(Path.home() / "Android/Sdk")): + if candidate and (Path(candidate) / "platform-tools").is_dir(): + return candidate + return None + + +def apk_certificate_sha256(apk, sdk): + """The APK's verified signer certificate, read with the SDK's apksigner.""" + tools = sorted((Path(sdk) / "build-tools").glob("*/apksigner"), reverse=True) + if not tools: + raise RuntimeError("The Android SDK has no build-tools/apksigner; install build tools") + result = subprocess.run([str(tools[0]), "verify", "--print-certs", str(apk)], capture_output=True, text=True) + if result.returncode: + raise RuntimeError("apksigner could not verify the APK: " + (result.stderr or result.stdout).strip()[-300:]) + digests = [line.split(":", 1)[1].strip().lower() for line in result.stdout.splitlines() + if "certificate SHA-256 digest" in line] + if len(digests) != 1: + raise RuntimeError("APK must carry exactly one signer certificate") + return digests[0] + + +def build_apk_native(app, cache, flutter, sdk, arguments, runner=subprocess.run, log=print): + """Builds the release APK on this machine with the host Flutter and Android SDK. + + Signs with the same debug keystore the Docker build used (signing_dir()), so + phones keep accepting updates, and writes the same cache entry layout.""" + key = digest("apk", app, arguments) + entry = Path(cache) / f"apk-{key}" + if (entry / ".complete").is_file() and (entry / "pipelines.apk").is_file() and (entry / "signing-certificate-sha256.txt").is_file(): + log(f"Reusing the apk build {key} (app sources unchanged).") + return entry + log(f"Building the app (apk) on this machine with {flutter} and the Android SDK at {sdk}.") + Path(cache).mkdir(parents=True, exist_ok=True) + work = Path(tempfile.mkdtemp(prefix="apk-", dir=cache)) + try: + built = int(time.time()) + code = built // 60 + signing = signing_dir() + env = {**os.environ, "ANDROID_HOME": sdk, "ANDROID_SDK_ROOT": sdk, + # The Android Gradle plugin reads debug.keystore from the user home it is given. + "ANDROID_USER_HOME": str(signing), "ANDROID_SDK_HOME": str(signing)} + command = [flutter, "build", "apk", "--release", *arguments, + f"--build-number={code}", f"--build-name=0.1.{code}"] + result = runner(command, cwd=str(app), env=env, capture_output=True, text=True) + if result.returncode: + raise RuntimeError("flutter build apk failed:\n" + (result.stderr or result.stdout).strip()[-2000:]) + produced = app / "build/app/outputs/flutter-apk/app-release.apk" + if not produced.is_file(): + raise RuntimeError(f"flutter build apk produced no APK at {produced}") + shutil.copyfile(produced, work / "pipelines.apk") + (work / "signing-certificate-sha256.txt").write_text(apk_certificate_sha256(work / "pipelines.apk", sdk) + "\n") + (work / ".complete").write_text(json.dumps({"key": key, "built": built}) + "\n") + if entry.exists(): + shutil.rmtree(entry) + work.rename(entry) + except BaseException: + shutil.rmtree(work, ignore_errors=True) + raise + prune(Path(cache), "apk", keep=entry) + return entry + + +def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=print, flutter=None, native=None): + """Builds the APK for this installation and offers it at /updates/. + + Builds natively when the host has Flutter and an Android SDK (faster, no + Docker CPU), otherwise in Docker; --docker / --native force one.""" manifest = load_manifest(prefix) web = manifest.get("web") or {} updates = web.get("updates") @@ -271,7 +345,16 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin # The build number is the source key's build date, so the same sources # reuse one APK and every rebuild is newer than the last. arguments = (f"--dart-define=PIPELINES_SERVER_URL={server}",) - output = build("apk", runner, arguments=arguments, log=log) + host_flutter, sdk = native_flutter(flutter), android_sdk_dir() + use_native = native if native is not None else bool(host_flutter and sdk) + if use_native: + if not host_flutter or not sdk: + raise RuntimeError("A native APK build needs Flutter (--flutter or PIPELINES_FLUTTER) and an Android SDK (ANDROID_HOME)") + output = build_apk_native(APP, default_cache(), host_flutter, sdk, arguments, runner=runner, log=log) + else: + if not docker_available(): + raise RuntimeError("Docker is required to build the app; start Docker and retry") + output = build("apk", runner, arguments=arguments, log=log) fingerprint = apk_signing_fingerprint(output) apk_sha256 = hashlib.sha256((output / "pipelines.apk").read_bytes()).hexdigest() built = json.loads((output / ".complete").read_text())["built"] @@ -317,7 +400,12 @@ def main(argv=None): parser.add_argument("--notes", default="", help="APK release notes shown in Check for updates") parser.add_argument("--publish", action="store_true", help="Publish desktop archive and update feed to this installation") parser.add_argument("--output", type=Path, help="Desktop archive output directory (default: build cache)") - parser.add_argument("--flutter", help="Native Flutter executable for macOS (or PIPELINES_FLUTTER/PATH)") + parser.add_argument("--flutter", help="Native Flutter executable for macOS and Android (or PIPELINES_FLUTTER/PATH)") + build_mode = parser.add_mutually_exclusive_group() + build_mode.add_argument("--native", dest="native", action="store_true", default=None, + help="APK: build on this machine with the host Flutter and Android SDK") + build_mode.add_argument("--docker", dest="native", action="store_false", + help="APK: build in Docker even when a host toolchain exists") args = parser.parse_args(argv) if args.kind in ("linux", "macos"): spec = importlib.util.spec_from_file_location("pipelines_desktop_build", Path(__file__).with_name("app_desktop_build.py")) @@ -334,12 +422,12 @@ def main(argv=None): raise ValueError("This installation has no app update directory; rerun ./install") desktop.publish_desktop(args.kind, bundle, archive, Path(updates), notes=args.notes) return - if not docker_available(): - raise RuntimeError("Docker is required to build the app; start Docker and retry") if args.kind == "apk": - build_apk(args.prefix, args.server_url, args.notes) + build_apk(args.prefix, args.server_url, args.notes, flutter=args.flutter, native=args.native) print_web_urls(load_manifest(args.prefix)) return + if not docker_available(): + raise RuntimeError("Docker is required to build the app; start Docker and retry") manifest = load_manifest(args.prefix) web = manifest.get("web") or {} if not web.get("root"):