Only ensure the run directory on the primary host#1908
Open
andyjeffries wants to merge 1 commit into
Open
Conversation
Every command that takes the deploy lock first runs `mkdir -p` of the run directory (`.kamal`) on *all* configured hosts, via `ensure_run_directory`. But the run directory only needs to exist where something actually reads or writes it, and the only consumer that depends on this preamble is the deploy lock, which is acquired and released solely on the primary host. This is most visible on single-host operations such as `kamal accessory reboot <name>`: rebooting an accessory pinned to one host still opens an SSH connection to every server in the deploy just to create an empty `.kamal` directory it never uses. `ensure_run_directory` has exactly two callers -- `acquire_lock` and `Lock#acquire` -- and in both it is only a precondition for the lock. The lock's directory is created with a plain, non-`-p` `mkdir` (an atomic lock primitive), so its parent must pre-exist; that is the whole reason for the preamble. Since the lock lives only on the primary host, the run directory only needs pre-creating there. Everything else under the run directory self-creates with `mkdir -p` on the hosts where it runs: env files and secrets (`app`/`accessory.ensure_env_directory`), proxy config, and the audit log (`Auditor#record` prepends its own `mkdir -p`). None of them rely on this preamble, so narrowing it to the primary host changes no other behaviour.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces unnecessary SSH fan-out by limiting .kamal run directory creation (ensure_run_directory) to only the primary host, aligning the precondition with how the deploy lock is actually acquired and stored.
Changes:
- Update
ensure_run_directoryto run only onKAMAL.primary_hostinstead of all configured hosts. - Add a CLI lock test asserting
.kamalis created only on the primary host duringkamal lock acquire.
Tip
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/cli/lock_test.rb | Adds coverage to ensure the run directory preamble only targets the primary host during lock acquire. |
| lib/kamal/cli/base.rb | Narrows ensure_run_directory execution scope to the primary host to avoid unnecessary connections. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every command that takes the deploy lock first runs
mkdir -pof the run directory (.kamal) on all configured hosts, viaensure_run_directory. But the run directory only needs to exist where something actually reads or writes it, and the only consumer that depends on this preamble is the deploy lock, which is acquired and released solely on the primary host.This is most visible on single-host operations such as
kamal accessory reboot <name>: rebooting an accessory pinned to one host still opens an SSH connection to every server in the deploy just to create an empty.kamaldirectory it never uses.ensure_run_directoryhas exactly two callers --acquire_lockandLock#acquire-- and in both it is only a precondition for the lock. The lock's directory is created with a plain, non--pmkdir(an atomic lock primitive), so its parent must pre-exist; that is the whole reason for the preamble. Since the lock lives only on the primary host, the run directory only needs pre-creating there.Everything else under the run directory self-creates with
mkdir -pon the hosts where it runs: env files and secrets(
app/accessory.ensure_env_directory), proxy config, and the audit log (Auditor#recordprepends its ownmkdir -p). None of them rely on this preamble, so narrowing it to the primary host changes no other behaviour.