Skip to content

Add brew info caching to speed up munki installcheck scripts#16

Merged
grahamgilbert merged 5 commits intomainfrom
cache-brew-info
Mar 31, 2026
Merged

Add brew info caching to speed up munki installcheck scripts#16
grahamgilbert merged 5 commits intomainfrom
cache-brew-info

Conversation

@grahamgilbert
Copy link
Copy Markdown
Contributor

Summary

  • Cache brew info --json --installed on disk so multiple carafe check invocations during a munki run share a single brew call instead of one per formula. In a typical munki run with N packages, this reduces N (or 2N with --min-version) brew invocations to 1.
  • Cache is on by default with a 60s TTL. New flags: --no-cache to disable, --cache-ttl to override (e.g. --cache-ttl=30s). No changes needed to existing plist scripts.
  • Cache stored securely at /var/root/.carafe/ (mode 0700) rather than /tmp, preventing symlink and injection attacks when running as root.
  • Fallback on cache miss — if a formula isn't in the installed list, fall back to a direct brew info call so typos/aliases still produce errors rather than silently passing --skip-not-installed.
  • Eliminated redundant brew callCheck with --min-version previously called brew info twice per formula; it now reuses the first result for the version comparison.
  • Updated README with caching documentation.

Test plan

  • All existing tests pass
  • TestLoadOrRefreshCache_CacheMiss — cache miss triggers brew call and writes cache
  • TestLoadOrRefreshCache_CacheHit — fresh cache is read without calling brew
  • TestLoadOrRefreshCache_CacheStale — stale cache triggers refresh
  • TestLoadOrRefreshCache_CorruptedCacheFile — corrupt JSON triggers refresh
  • TestLoadOrRefreshCache_BrewError — brew failure propagates correctly
  • TestInfoOutputCached_FormulaFound — installed formula served from cache
  • TestInfoOutputCached_FormulaNotInstalled — cache miss falls back to direct brew call
  • TestGetInfoOutput_NoCaching / TestGetInfoOutput_NegativeTTL — TTL ≤ 0 bypasses cache

🤖 Generated with Claude Code

grahamgilbert and others added 3 commits March 31, 2026 10:31
Cache `brew info --json --installed` to disk (60s TTL by default) so
that multiple `carafe check` invocations during a munki run share a
single brew call instead of one per formula. Also eliminates the
redundant second brew call that occurred when --min-version was set.

New flags on `check`: --no-cache to disable, --cache-ttl to override
the TTL. Caching is on by default.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Move cache from world-writable /tmp to /var/root/.carafe/ (mode 0700)
  so non-root users cannot pre-create or symlink-attack the cache file
- Fall back to a direct brew call on cache miss rather than synthesising
  a "not installed" response, so typos and unresolved aliases still
  produce an error instead of silently passing --skip-not-installed
- Update README cache location and add missing cache tests
- Extract cache path strings to constants; remove redundant ttl variable

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extract buildRootCmd() so the cobra command is testable without running
main(). Add tests for invalid --cache-ttl strings, --no-cache flag, and
valid TTL values. Also fixes a latent bug where the -ldflags version
injection was targeting a local variable instead of a package-level one.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread main.go
grahamgilbert and others added 2 commits March 31, 2026 13:40
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@grahamgilbert grahamgilbert merged commit 381e365 into main Mar 31, 2026
6 checks passed
@grahamgilbert grahamgilbert deleted the cache-brew-info branch March 31, 2026 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants