Republish mDNS on address change; build the APK natively when the host can
The server compares its LAN and tailnet addresses every 30 seconds and republishes the advert when they change, so a machine that moves networks stops advertising the address it had at startup. ./install --apk builds on this machine with the host Flutter and Android SDK when both exist (--docker forces the container build), signing with the same shared debug keystore so phones keep accepting updates.
This commit is contained in:
+95
-7
@@ -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"):
|
||||
|
||||
Reference in New Issue
Block a user