Update from a git repository

router.update.repository names a public repository whose tags vX.Y.Z are
releases. router-update check (daily, and from the Updates page) looks for a
newer one and the Overview says so; router-update --git builds the router
from a tag like a bundle update: the settings are migrated, the new version
must be confirmed, the nixpkgs pin stays. The flake of such a router comes
from the release's config-flake.nix. The global flake registry is off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:57:00 +01:00
co-authored by Claude Sonnet 5.5
parent a2dbeb5dea
commit 3fc54e678e
13 changed files with 589 additions and 26 deletions
+46
View File
@@ -39,6 +39,52 @@
http.server.ThreadingHTTPServer(("0.0.0.0", 8080), Handler).serve_forever()
'';
# A git server over (smart) HTTP: `git http-backend` for the repositories under
# the directory in argv[1], on the port in argv[2]. Enough for ls-remote and fetch.
gitServer =
pkgs:
pkgs.writeScript "git-server.py" ''
#!${pkgs.python3}/bin/python3
import http.server, os, subprocess, sys
root, port = sys.argv[1], int(sys.argv[2])
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.cgi()
def do_POST(self):
self.cgi()
def cgi(self):
path, _, query = self.path.partition("?")
body = self.rfile.read(int(self.headers.get("Content-Length") or 0))
env = dict(os.environ, GIT_PROJECT_ROOT=root, GIT_HTTP_EXPORT_ALL="1", PATH_INFO=path,
QUERY_STRING=query, REQUEST_METHOD=self.command, REMOTE_ADDR=self.client_address[0],
CONTENT_TYPE=self.headers.get("Content-Type", ""), CONTENT_LENGTH=str(len(body)),
HTTP_CONTENT_ENCODING=self.headers.get("Content-Encoding", ""))
done = subprocess.run(["${pkgs.git}/bin/git", "http-backend"], input=body, env=env, capture_output=True)
head, _, out = done.stdout.partition(b"\r\n\r\n")
status, headers = 200, []
for line in head.split(b"\r\n"):
name, _, value = line.decode().partition(": ")
if name.lower() == "status":
status = int(value.split()[0])
elif name:
headers.append((name, value))
self.send_response(status)
for name, value in headers:
self.send_header(name, value)
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
def log_message(self, *args):
pass
http.server.ThreadingHTTPServer(("0.0.0.0", port), Handler).serve_forever()
'';
# `driverInteractive` uses the full QEMU, which would open a window per VM;
# `nix run .#lab` wants the consoles in the terminal instead.
interactiveDefaults.virtualisation.qemu.options = [
+94 -3
View File
@@ -64,6 +64,18 @@ let
'';
};
# Version 3 is only a source tree: the builder serves it from a git repository.
v3 = mkVersion {
name = "v3";
version = "3.0.0";
serial = 3000;
extra = ''
rm $out/migrations/*.jq
echo '.system.timeZone = "Europe/Berlin"' > $out/migrations/1.jq
echo '.system.timeZone = "Europe/Paris"' > $out/migrations/2.jq
'';
};
release =
v:
v.lib.mkRelease {
@@ -186,12 +198,17 @@ pkgs.testers.runNixOSTest {
}
];
networking.firewall.enable = false;
environment.systemPackages = [ pkgs.python3 ];
environment.systemPackages = [
pkgs.python3
pkgs.git
];
virtualisation.additionalPaths = [
release1
release2
v3.src
];
environment.etc."channels.py".source = common.channelServer pkgs;
environment.etc."git-server.py".source = common.gitServer pkgs;
environment.etc."keys/good.sec".source = "${keys}/good.sec";
environment.etc."keys/evil.sec".source = "${keys}/evil.sec";
};
@@ -234,9 +251,12 @@ pkgs.testers.runNixOSTest {
def csrf_from_query(path):
return builder.succeed(f"curl -sk -b /tmp/jar {ui}{path} | grep -o 'upload?csrf=[^\"]*' | head -1 | cut -d= -f2").strip()
def update_finished():
# Stops at a failed update rather than waiting for one that will not come.
target.wait_until_succeeds(
"test $(systemctl show -p ActiveState --value router-update.service) = inactive"
" && test $(systemctl show -p Result --value router-update.service) = success", timeout=900)
"state=$(systemctl show -p ActiveState --value router-update.service);"
" test $state = inactive -o $state = failed", timeout=300)
result = target.succeed("systemctl show -p Result --value router-update.service").strip()
assert result == "success", (result, target.succeed("journalctl -u router-update.service --no-pager | tail -30"))
v1_flake = flake_source()
@@ -359,5 +379,76 @@ pkgs.testers.runNixOSTest {
rc, out = update_from(8003)
assert rc != 0 and "not newer" in out, out
assert version() == "2.0.0"
with subtest("a release in a git repository is found, built and installed like a bundle"):
repo = "http://${builderIp}:8005/router.git"
builder.succeed("cp -r ${v3.src} /root/v3 && chmod -R u+w /root/v3")
builder.succeed("cd /root/v3 && git init -q -b main && git add -A"
" && git -c user.name=test -c user.email=test@example.org commit -q -m 'version 3' && git tag v3.0.0")
commit = builder.succeed("git -C /root/v3 rev-parse HEAD").strip()
builder.succeed("mkdir -p /srv/git && git clone -q --bare /root/v3 /srv/git/router.git")
builder.succeed("systemd-run --unit=git-server /etc/git-server.py /srv/git 8005")
builder.wait_for_open_port(8005, "${builderIp}")
# Nothing to look at until a repository is named.
assert "no repository" in target.succeed("router-update check 2>&1")
login()
token = csrf("/update")
form = lambda path, data: builder.succeed(
f"curl -sk -b /tmp/jar -o /tmp/out -w '%{{http_code}}' --data-urlencode csrf={token} {data} {ui}{path}").strip()
assert form("/update/repository", f"--data-urlencode repository={repo}/") == "303"
assert form("/update/gitcheck", "") == "303"
target.wait_until_succeeds(
"test $(systemctl show -p ActiveState --value router-update-check.service) = inactive"
" && test $(systemctl show -p Result --value router-update-check.service) = success", timeout=120)
check = json.loads(target.succeed("cat /var/lib/router/update/check.json"))
assert check["newer"] is True and check["newest"] == {"tag": "v3.0.0", "version": "3.0.0", "rev": commit} and check["installed"] == "2.0.0", check
assert "nixos-router 3.0.0 is out" in builder.succeed(f"curl -sk -b /tmp/jar {ui}/")
assert "Install 3.0.0" in builder.succeed(f"curl -sk -b /tmp/jar {ui}/update")
# Only tags that are versions are releases; a newer tag of another kind is ignored.
builder.succeed("cd /root/v3 && git tag nightly && git tag v4.0 && git push -q /srv/git/router.git nightly v4.0")
target.succeed("router-update check >&2")
assert json.loads(target.succeed("cat /var/lib/router/update/check.json"))["newest"]["tag"] == "v3.0.0"
rc, out = target.execute("router-update --git v9.9.9 2>&1")
assert rc != 0 and "no such release tag" in out, out
rc, out = target.execute("router-update --git 'v3.0.0; reboot' 2>&1")
assert rc != 0 and "not a release tag" in out, out
assert version() == "2.0.0"
login()
token = csrf("/update")
assert form("/update/gitinstall", "--data-urlencode tag=v3.0.0") == "303"
update_finished()
assert version() == "3.0.0", release_json()
flake = target.succeed("cat /var/lib/router/config/flake.nix")
assert f"git+{repo}?ref=refs/tags/v3.0.0&rev={commit}" in flake, flake
lock = json.loads(target.succeed("cat /var/lib/router/config/flake.lock"))
locked = lock["nodes"][lock["nodes"]["root"]["inputs"]["nixos-router"]]["locked"]
assert locked["type"] == "git" and locked["rev"] == commit, locked
settings = target.succeed("cat /var/lib/router/config/settings.json")
assert '"version": 2' in settings and "Europe/Paris" in settings, settings
assert "router-update-check" not in settings
pinned()
target.succeed("systemctl is-active router-rollback.timer")
login()
token = csrf("/")
builder.succeed(f"curl -sk -b /tmp/jar -o /dev/null --data-urlencode csrf={token} {ui}/confirm")
target.wait_until_fails("systemctl is-active router-rollback.timer", timeout=60)
target.wait_until_succeeds("test $(systemctl show -p ActiveState --value router-confirm.service) = inactive")
assert target.succeed("readlink /etc/localtime").strip().endswith("Europe/Paris")
with subtest("the router built from git rebuilds to exactly itself without the repository, and is up to date"):
builder.succeed("systemctl stop git-server")
out = target.succeed("router-apply 2>&1")
assert "no changes" in out, out
builder.succeed("systemd-run --unit=git-server-again /etc/git-server.py /srv/git 8005")
builder.wait_for_open_port(8005, "${builderIp}")
target.succeed("router-update check >&2")
check = json.loads(target.succeed("cat /var/lib/router/update/check.json"))
assert check["newer"] is False and check["installed"] == "3.0.0", check
rc, out = target.execute("router-update --git latest 2>&1")
assert rc != 0 and "not newer" in out, out
'';
}