Add w10k: deploy Puppet environments as sha-pinned git worktrees #1

Merged
benvin merged 11 commits from benvin/scaffold into main 2026-11-08 21:41:22 +11:00
Member

g10k rewrites environments in place, so a compile during a deploy can read mixed old and new code. w10k deploys each branch as an immutable sha-pinned worktree and swaps it in atomically.

  • mirror r10k.yaml sources; build a worktree per sha with modules symlinked in
  • treat Puppetfiles and forge responses as untrusted: remote-only :git, os.Root writes, size caps
  • swap <basedir>/<env> via symlink + rename(2); write g10k's .g10k-deploy.json
  • give an env name to the branch needing no correction; on a corrected-only collision, error and keep the live env
  • fail only the affected env on a timeout, parse error or panic
  • purge and GC only on full deploys; delete after --gc-grace, unmarking anything live
  • flock the cachedir; bound -j; release binaries and an RPM on v* tags
g10k rewrites environments in place, so a compile during a deploy can read mixed old and new code. w10k deploys each branch as an immutable sha-pinned worktree and swaps it in atomically. - mirror r10k.yaml sources; build a worktree per sha with modules symlinked in - treat Puppetfiles and forge responses as untrusted: remote-only `:git`, os.Root writes, size caps - swap `<basedir>/<env>` via symlink + rename(2); write g10k's `.g10k-deploy.json` - give an env name to the branch needing no correction; on a corrected-only collision, error and keep the live env - fail only the affected env on a timeout, parse error or panic - purge and GC only on full deploys; delete after `--gc-grace`, unmarking anything live - flock the cachedir; bound `-j`; release binaries and an RPM on `v*` tags
unkin-agent added 1 commit 2026-11-08 17:38:42 +11:00
Add w10k: deploy Puppet environments as sha-pinned git worktrees
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
fc5d90b684
Author
Member
  • internal/deploy/git.go:30 — url goes to git fetch as a bare arg; a branch Puppetfile :git => '--upload-pack=<cmd>;' runs <cmd> as the deploy user → put -- before url, and reject :git values starting with - in parseMod
  • internal/deploy/deploy.go:400-407 — MkdirAll/RemoveAll/Symlink follow symlinks: a committed symlink used as install_path deletes and writes outside the worktree, and a nested install_path (e.g. modules/thing) writes into the shared forge//modules/ cache dir that live envs use → build module paths with os.OpenRoot(target) (Root.MkdirAll/RemoveAll/Symlink), and reject module paths that sit under another module's path
  • internal/deploy/deploy.go:232,260 — "complete" means .g10k-deploy.json exists, but a branch can commit that file; if checkout fails after worktree add, the next run treats the half-built worktree as complete and swaps it live without its modules (it also feeds a branch-controlled module list into GC's moduleRefs) → keep the completion marker outside the worktree (e.g. <target>.done in the cachedir), or delete/refuse a committed marker straight after worktree add
  • internal/deploy/deploy.go:101 — urlKey is lossy (/a/m_od and /a/m/od give the same key), so two URLs share one mirror and one fetch: once; a branch can feed tag 1.0 from its own URL into production's module → add a hash of the full URL to the key (<sanitized>-<sha256[:12]>)
  • internal/deploy/deploy.go:375 — forge cache dir and forge:/latest: once keys leave out the forge base URL; a branch's forge 'https://evil' fills forge/<owner>-<name>-<ver> for every env → put the forge base (hashed) in the dir and once keys
  • internal/deploy/deploy.go:201,279,282,340 — one goroutine per env and per module, and cat-file/rev-parse run outside d.limit, so -j does not cap git processes (about 3×branches run at once) → put those calls inside limit, or bound the env fan-out with sem
  • .woodpecker/release.yaml:73 — rpm PUT has no auth; node-lookup/agent-tools now send a Vault-minted artifactapi-writer bearer → copy their Vault k8s login + OIDC token block
  • .woodpecker/build.yaml:6, test.yaml:6, release.yaml:7,22,45, pre-commit.yaml:6 — golang:1.25, almalinux9-gobuilder:20260606, almalinux9-rpmbuilder:latest → artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9 / rpmbuilder:0.1.0-alma9 plus the GOCACHE_* env, as in node-lookup
  • nit: internal/deploy/deploy.go:449-455 — dir→symlink cutover leaves link missing between the two renames → renameat2(RENAME_EXCHANGE) the symlink with the dir, then rename the dir aside
  • nit: internal/deploy/puppetfile.go:228 — duplicate check uses moduledir as it is at that line, but install uses the final moduledir; a later moduledir lets two mods collide → store the resolved path on the Module when parsing
  • nit: scripts/build-rpm.sh:16-17, packaging/nfpm.yaml — no fish completion, unlike the sibling repos → generate it and ship it to /usr/share/fish/vendor_completions.d/
- internal/deploy/git.go:30 — `url` goes to `git fetch` as a bare arg; a branch Puppetfile `:git => '--upload-pack=<cmd>;'` runs `<cmd>` as the deploy user → put `--` before `url`, and reject `:git` values starting with `-` in parseMod - internal/deploy/deploy.go:400-407 — `MkdirAll`/`RemoveAll`/`Symlink` follow symlinks: a committed symlink used as `install_path` deletes and writes outside the worktree, and a nested `install_path` (e.g. `modules/thing`) writes into the shared `forge/`/`modules/` cache dir that live envs use → build module paths with `os.OpenRoot(target)` (Root.MkdirAll/RemoveAll/Symlink), and reject module paths that sit under another module's path - internal/deploy/deploy.go:232,260 — "complete" means `.g10k-deploy.json` exists, but a branch can commit that file; if checkout fails after `worktree add`, the next run treats the half-built worktree as complete and swaps it live without its modules (it also feeds a branch-controlled module list into GC's moduleRefs) → keep the completion marker outside the worktree (e.g. `<target>.done` in the cachedir), or delete/refuse a committed marker straight after `worktree add` - internal/deploy/deploy.go:101 — `urlKey` is lossy (`/a/m_od` and `/a/m/od` give the same key), so two URLs share one mirror and one `fetch:` once; a branch can feed tag `1.0` from its own URL into production's module → add a hash of the full URL to the key (`<sanitized>-<sha256[:12]>`) - internal/deploy/deploy.go:375 — forge cache dir and `forge:`/`latest:` once keys leave out the forge base URL; a branch's `forge 'https://evil'` fills `forge/<owner>-<name>-<ver>` for every env → put the forge base (hashed) in the dir and once keys - internal/deploy/deploy.go:201,279,282,340 — one goroutine per env and per module, and `cat-file`/`rev-parse` run outside `d.limit`, so `-j` does not cap git processes (about 3×branches run at once) → put those calls inside `limit`, or bound the env fan-out with `sem` - .woodpecker/release.yaml:73 — rpm PUT has no auth; node-lookup/agent-tools now send a Vault-minted artifactapi-writer bearer → copy their Vault k8s login + OIDC token block - .woodpecker/build.yaml:6, test.yaml:6, release.yaml:7,22,45, pre-commit.yaml:6 — `golang:1.25`, `almalinux9-gobuilder:20260606`, `almalinux9-rpmbuilder:latest` → `artifactapi.k8s.syd1.au.unkin.net/docker-internal/gobuilder:0.1.2-alma9` / `rpmbuilder:0.1.0-alma9` plus the GOCACHE_* env, as in node-lookup - nit: internal/deploy/deploy.go:449-455 — dir→symlink cutover leaves `link` missing between the two renames → `renameat2(RENAME_EXCHANGE)` the symlink with the dir, then rename the dir aside - nit: internal/deploy/puppetfile.go:228 — duplicate check uses `moduledir` as it is at that line, but install uses the final `moduledir`; a later `moduledir` lets two mods collide → store the resolved path on the Module when parsing - nit: scripts/build-rpm.sh:16-17, packaging/nfpm.yaml — no fish completion, unlike the sibling repos → generate it and ship it to `/usr/share/fish/vendor_completions.d/`
unkin-agent added 2 commits 2026-11-08 17:52:15 +11:00
- reject option-like :git URLs and refs; pass -- before git positionals
- link modules without following symlinks; reject nested module paths
- judge worktree completeness by an out-of-tree done file
- key git mirrors by URL hash and forge releases by forge base
- bound every git and forge call with the -j semaphore
Use gobuilder/rpmbuilder images and authenticate RPM upload
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
fd1ddea356
- run Go steps on docker-internal gobuilder with the shared GOCACHE
- package with docker-internal rpmbuilder
- upload the RPM with a Vault-minted artifactapi-writer token
- ship fish completion in the RPM
Author
Member
  • internal/deploy/deploy.go:491 — writeAtomic does os.WriteFile(target/.g10k-deploy.json.tmp), which follows a symlink the branch committed at that path; a branch with .g10k-deploy.json.tmp -> <basedir>/production/manifests/site.pp (or a path into forge//modules/) overwrites a live env or a shared cache file with JSON → open the tmp with O_CREATE|O_EXCL|O_NOFOLLOW after an os.Remove, or write via os.OpenRoot(target)
  • internal/deploy/git.go:13 — git runs with no timeout and HTTP low-speed abort is off, so a branch :git => 'http://<host that accepts and never answers>' hangs w10k deploy indefinitely while it holds the cachedir flock; no env, production included, can deploy again until someone kills it → wrap each git call in a per-op context.WithTimeout and pass -c http.lowSpeedLimit=1 -c http.lowSpeedTime=60
  • nit: internal/deploy/deploy.go:519 — dir→symlink cutover leaves link missing between the two renames → renameat2(RENAME_EXCHANGE) the tmp symlink with the dir, then remove the aside dir later
  • nit: internal/deploy/deploy.go:685 — moduleRefs only scans worktrees of sources that are still configured; if a source is dropped from r10k.yaml, its env links stay in basedir, but GC deletes the modules they use → scan every worktrees/* dir, not only d.Sources()
- internal/deploy/deploy.go:491 — `writeAtomic` does `os.WriteFile(target/.g10k-deploy.json.tmp)`, which follows a symlink the branch committed at that path; a branch with `.g10k-deploy.json.tmp -> <basedir>/production/manifests/site.pp` (or a path into `forge/`/`modules/`) overwrites a live env or a shared cache file with JSON → open the tmp with `O_CREATE|O_EXCL|O_NOFOLLOW` after an `os.Remove`, or write via `os.OpenRoot(target)` - internal/deploy/git.go:13 — git runs with no timeout and HTTP low-speed abort is off, so a branch `:git => 'http://<host that accepts and never answers>'` hangs `w10k deploy` indefinitely while it holds the cachedir flock; no env, production included, can deploy again until someone kills it → wrap each git call in a per-op `context.WithTimeout` and pass `-c http.lowSpeedLimit=1 -c http.lowSpeedTime=60` - nit: internal/deploy/deploy.go:519 — dir→symlink cutover leaves `link` missing between the two renames → `renameat2(RENAME_EXCHANGE)` the tmp symlink with the dir, then remove the aside dir later - nit: internal/deploy/deploy.go:685 — `moduleRefs` only scans worktrees of sources that are still configured; if a source is dropped from r10k.yaml, its env links stay in basedir, but GC deletes the modules they use → scan every `worktrees/*` dir, not only `d.Sources()`
unkin-agent added 1 commit 2026-11-08 18:07:18 +11:00
Confine worktree writes, time out git calls, keep removed sources' modules
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
af20148ea4
- write the deploy marker and module links through os.Root
- extract forge tarballs through os.Root
- bound each git call with w10k.git_timeout (default 10m) and http low-speed abort
- count module refs from every worktree and basedir link, not only configured sources
Author
Member
  • internal/deploy/puppetfile.go:167,170 — mod 'x', git: / :git => at EOF: next() consumes tEOF, peek() at :176 indexes past the slice → panic in the deployEnv goroutine kills the whole run (every env + GC) from one branch → bounds-check before next()/peek() and return a parse error; add the case to TestParsePuppetfileErrors.
  • internal/deploy/deploy.go:349 — Puppetfile :git accepts local paths / file://, so any branch can clone any repo readable by the w10k user into its env → run module fetches with -c protocol.file.allow=never (source remotes from r10k.yaml can stay unrestricted).
  • internal/deploy/deploy.go:674,676 — GC deletes module/forge dirs in place, and :372/:405 treat any existing dir as complete; a crash mid-removal leaves a half-deleted module that the next deploy links into a live env and never revalidates → rename to a tombstone before deleting, or gate reuse on an out-of-tree done marker like env worktrees.
  • nit: internal/deploy/deploy.go:671 — git/<key>.git mirrors are never collected once no env references the URL; worktrees/state/mirror of a source dropped from r10k.yaml are never collected either → sweep orphan mirrors and source dirs.
  • nit: .woodpecker/lint.yaml:6 — golangci-lint:latest is unpinned → pin a version.
- internal/deploy/puppetfile.go:167,170 — `mod 'x', git:` / `:git =>` at EOF: `next()` consumes tEOF, `peek()` at :176 indexes past the slice → panic in the deployEnv goroutine kills the whole run (every env + GC) from one branch → bounds-check before `next()`/`peek()` and return a parse error; add the case to TestParsePuppetfileErrors. - internal/deploy/deploy.go:349 — Puppetfile `:git` accepts local paths / `file://`, so any branch can clone any repo readable by the w10k user into its env → run module fetches with `-c protocol.file.allow=never` (source remotes from r10k.yaml can stay unrestricted). - internal/deploy/deploy.go:674,676 — GC deletes module/forge dirs in place, and :372/:405 treat any existing dir as complete; a crash mid-removal leaves a half-deleted module that the next deploy links into a live env and never revalidates → rename to a tombstone before deleting, or gate reuse on an out-of-tree done marker like env worktrees. - nit: internal/deploy/deploy.go:671 — `git/<key>.git` mirrors are never collected once no env references the URL; worktrees/state/mirror of a source dropped from r10k.yaml are never collected either → sweep orphan mirrors and source dirs. - nit: .woodpecker/lint.yaml:6 — `golangci-lint:latest` is unpinned → pin a version.
unkin-agent added 2 commits 2026-11-08 20:26:03 +11:00
Harden Puppetfile parsing, module sources and cache GC
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
8a336b62a5
- return an error for an option with no value instead of reading past EOF
- recover panics per environment, module and single-flight as errors
- accept only https, http, ssh and user@host:path module remotes
- fetch modules with protocol.file.allow=never
- reuse module and forge dirs only with an out-of-tree done marker
- rename GC'd dirs aside before deleting and sweep leftovers
- add a Puppetfile fuzz target; serve test modules over git smart HTTP
unkin-agent added 1 commit 2026-11-08 20:29:40 +11:00
Serialize worktree commands per repo
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
53e78a5f75
Author
Member
  • internal/deploy/deploy.go:263 — full is computed per source from the shared want, and line 273 empties it. So w10k deploy production with production in the first source runs a full deploy and purge of every later source → work out full once in Deploy and pass it to deploySource.
  • internal/deploy/deploy.go:733 — when moduleRefs errors, every mark is kept, including marks on worktrees that went live again this run. If that worktree is unreferenced later, GC deletes it in that same run with no grace → on error, keep only marks under modules/ and forge/ (or keep none, which only delays deletion).
  • nit: internal/deploy/deploy.go:520 — envs at the same sha and module set share one worktree, so every env but the first gets a .g10k-deploy.json whose name is the first env → leave name out of the shared marker, or include the env name in the target key.
- internal/deploy/deploy.go:263 — `full` is computed per source from the shared `want`, and line 273 empties it. So `w10k deploy production` with production in the first source runs a full deploy and purge of every later source → work out `full` once in `Deploy` and pass it to `deploySource`. - internal/deploy/deploy.go:733 — when `moduleRefs` errors, every mark is kept, including marks on worktrees that went live again this run. If that worktree is unreferenced later, GC deletes it in that same run with no grace → on error, keep only marks under `modules/` and `forge/` (or keep none, which only delays deletion). - nit: internal/deploy/deploy.go:520 — envs at the same sha and module set share one worktree, so every env but the first gets a `.g10k-deploy.json` whose `name` is the first env → leave `name` out of the shared marker, or include the env name in the target key.
unkin-agent added 2 commits 2026-11-08 20:41:01 +11:00
Write the branch as the deploy marker name, as g10k does
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
d7ab79f874
Author
Member
  • internal/deploy/deploy.go:219 — Deploy runs d.GC on a targeted deploy too, so w10k deploy main deletes past-grace worktrees and modules → run GC only when len(only) == 0
  • internal/deploy/deploy.go:715 — a Live error returns before unseen marks are dropped and state is saved; a mark on a worktree that was live in that run survives, and the next run deletes it in the same run that unreferences it → on a Live error skip that source's sweep and the module sweep, then drop unseen marks and write state, as the moduleRefs error path does
  • internal/deploy/deploy.go:251 — on an env-name collision the branch that sorts first wins, and - . / sort before _: pushing feature-x takes env feature_x from branch feature_x and bypasses its branch protection → let the branch whose name needs no correction win, and skip corrected branches that collide with it
- internal/deploy/deploy.go:219 — `Deploy` runs `d.GC` on a targeted deploy too, so `w10k deploy main` deletes past-grace worktrees and modules → run GC only when `len(only) == 0` - internal/deploy/deploy.go:715 — a `Live` error returns before unseen marks are dropped and state is saved; a mark on a worktree that was live in that run survives, and the next run deletes it in the same run that unreferences it → on a `Live` error skip that source's sweep and the module sweep, then drop unseen marks and write state, as the `moduleRefs` error path does - internal/deploy/deploy.go:251 — on an env-name collision the branch that sorts first wins, and `-` `.` `/` sort before `_`: pushing `feature-x` takes env `feature_x` from branch `feature_x` and bypasses its branch protection → let the branch whose name needs no correction win, and skip corrected branches that collide with it
unkin-agent added 2 commits 2026-11-08 20:54:58 +11:00
Keep GC off targeted deploys, always drop live marks, let exact branches win env names
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
66580c9b37
Author
Member
  • internal/deploy/deploy.go:313 — full-deploy purge removes a live env once a second branch corrects to the same name (feature-x live, push feature.x → feature_x deleted); "skip" turns into "unpublish" for anyone with push → keep skipped collision names out of purge (return them from Environments). Repro: deploy feature-x, add branch feature.x, redeploy, assert basedir/feature_x still exists.
  • internal/deploy/forge.go:64 — forgeLatest decodes the forge metadata body uncapped, and versionStr has no length bound: a Puppetfile forge pointing at a server returning a 256 MiB version is read in full and accepted (err=nil, ~2.6 GB RSS under -race), so one branch can OOM the whole run → wrap the body in capReader (e.g. 1 MiB) and bound the version length.
- internal/deploy/deploy.go:313 — full-deploy `purge` removes a live env once a second branch corrects to the same name (`feature-x` live, push `feature.x` → `feature_x` deleted); "skip" turns into "unpublish" for anyone with push → keep skipped collision names out of `purge` (return them from `Environments`). Repro: deploy `feature-x`, add branch `feature.x`, redeploy, assert `basedir/feature_x` still exists. - internal/deploy/forge.go:64 — `forgeLatest` decodes the forge metadata body uncapped, and `versionStr` has no length bound: a Puppetfile `forge` pointing at a server returning a 256 MiB `version` is read in full and accepted (`err=nil`, ~2.6 GB RSS under -race), so one branch can OOM the whole run → wrap the body in `capReader` (e.g. 1 MiB) and bound the version length.
benvin merged commit 358d17ca2f into main 2026-11-08 21:41:22 +11:00
benvin deleted branch benvin/scaffold 2026-11-08 21:41:22 +11:00
This repo is archived. You cannot comment on pull requests.