Command execution and orchestration
Kamal's command system turns a CLI request into local or remote deployment work. Thor selects a command, the shared commander prepares configuration and target scope, and the command layer builds the Docker, SSH, and file actions that SSHKit runs. This page gives the shared model; see application deployment, accessory operations, proxy management, image building, and host operations for the main work areas.
Command dispatch
The CLI is the entry point for user intent. bin/kamal starts Kamal::Cli::Main, and the main class exposes top-level actions and subcommands for application, accessory, build, proxy, server, registry, lock, and prune work. A top-level action can invoke child actions to form a larger workflow. For example, deployment coordinates image delivery, proxy boot, application boot, and cleanup.
The dispatch path keeps policy in the CLI layer and command construction in the command layer:
| Stage | Responsibility | Result |
|---|---|---|
| CLI dispatch | Match the command and parse shared flags | A selected operation with user scope and options |
| Commander state | Load configuration and resolve hosts and roles | A shared deployment context |
| CLI orchestration | Order work, hooks, locks, and output | A controlled workflow |
| Command objects | Express Docker, SSH, registry, and file actions | Commands for local or remote execution |
| SSHKit | Run actions on selected hosts or locally | Results and command errors |
This split lets one workflow combine several areas without making each command know how to parse the full CLI or manage global state.
References: bin/kamal• L6-17 lib/…/main.rb• L236-261 lib/…/base.rb• L5-16
Commander context
Kamal::Commander is the shared context for one CLI run. It lazily loads the deployment configuration, creates command objects, and exposes the selected hosts, roles, primary targets, application hosts, proxy hosts, and accessory hosts. The CLI base class fills this context from shared options such as configuration file, destination, version, host, role, and primary-host selection.
Host and role filters change the target set before work starts. Commands then use that set to run across all matching targets, one primary target, or a narrower user-selected group. The commander also holds cross-cutting state for verbosity, connection setup, deploy-lock ownership, and output logging.
References: lib/…/commander.rb• L7-15 lib/…/commander.rb• L31-70 lib/…/commander.rb• L84-126 lib/…/base.rb• L17-75
Command layers
The command layer provides the shared vocabulary for work on the deployment targets. A command object receives the loaded configuration and returns an action such as a Docker command, an SSH command, a file transfer, or a shell pipeline. The CLI remains responsible for when to run that action and on which hosts.
Kamal::Commands::Base supplies common command forms and SSH settings. Higher-level command objects represent the target being managed. The app and accessory commands describe container work, the proxy command describes the traffic entry point, the builder command selects a build target, and the server and lock commands support host setup and safe coordination.
References: lib/…/base.rb• L1-15 lib/…/base.rb• L17-41 lib/…/base.rb• L43-101 lib/…/commander.rb• L84-126
Remote execution
Remote execution uses SSHKit to apply command objects to a host set. The shared on path runs a connection hook once before the first remote action, then the selected command runs on each host. A command can capture output for display, execute without returning output, or use SSH directly for an interactive session.
Local work uses the same execution interface with run_locally. This supports local image builds, local registry setup, hooks, and interactive commands while keeping the output and error path consistent with remote work.
The execution model is:
flowchart LR
A[CLI request] --> B[Commander context]
B --> C[Target selection]
C --> D{Execution site}
D -->|Local| E[SSHKit local runner]
D -->|Remote| F[SSHKit host runner]
E --> G[Result or error]
F --> G
References: lib/…/base.rb• L256-267 lib/…/base.rb• L315-327 lib/…/base.rb• L13-15 lib/…/base.rb• L103-133
Workflow coordination
Mutating workflows use a common coordination envelope. modify records the command and target context for output and notifications. When a workflow needs exclusive access, it acquires the deploy lock before remote changes and releases it after the work completes. Nested calls can reuse the lock, so a top-level workflow can safely invoke child commands.
Hooks extend the workflow at named points. They run locally with deployment details and can receive secrets when the workflow allows them. The pre-connect hook runs once before the first remote connection. A failed hook becomes a CLI hook error, while a failed remote action is reported through the SSHKit error path.
sequenceDiagram
participant User
participant CLI as CLI layer
participant C as Commander
participant L as Lock and hooks
participant S as SSHKit
participant H as Remote hosts
User->>CLI: Run a mutating command
CLI->>C: Load config and target scope
CLI->>L: Run pre-work hooks and acquire lock
CLI->>S: Dispatch command actions
S->>H: Execute Docker or host work
H-->>S: Return status and output
S-->>CLI: Success or error
CLI->>L: Run post-work hooks and release lock
References: lib/…/base.rb• L78-123 lib/…/base.rb• L135-221 lib/…/base.rb• L224-253 lib/…/commander.rb• L141-163
Deployment composition
The top-level deployment commands show how the layers compose. setup first bootstraps Docker and then performs deployment with accessories. deploy delivers or pulls the app image, runs the pre-deploy hook, ensures the proxy is running, boots the app, and prunes old resources. redeploy uses the existing host setup and proxy. rollback changes the selected app version and boots it only when that version exists on the hosts.
The child pages describe each work area in detail:
- Application deployment covers app containers, roles, health checks, hooks, and app commands.
- Accessory operations covers named services and their host data.
- Proxy management covers the traffic proxy and its app targets.
- Image building covers image build, push, pull, and builder choices.
- Host operations covers server bootstrap and direct host commands.
References: lib/…/main.rb• L1-54 lib/…/main.rb• L56-110 lib/…/main.rb• L187-228
Output and failure handling
The command system treats output as part of the execution contract. Verbosity can be changed for normal, detailed, quiet, or raw output. Remote command output can be grouped by host, while audit commands record important changes without mixing those records into normal user output.
The executable catches SSHKit runner failures and other uncaught errors at the CLI boundary. It prints a short error by default and adds a backtrace when verbose mode is active. This gives users one failure surface even when a workflow has several nested command calls.
References: bin/kamal• L8-17 lib/…/base.rb• L8-17 lib/…/base.rb• L93-102 lib/…/base.rb• L224-251 lib/…/commander.rb• L165-216
Change checklist
When these files or sections are changed, remember to consider these:
- Keep shared CLI options aligned with commander state. A new host, role, version, lock, or output option must reach every subcommand that uses it.
- Preserve invocation tracking when a command invokes another command. It supplies the command and subcommand names used by logs, hooks, and change notifications.
- Preserve nested lock behavior. A child operation must reuse an existing lock and must not release a lock owned by its parent workflow.
- Keep local and remote execution paths distinct for interactive and raw output. Mixing them can corrupt output or change the expected terminal behavior.
- Keep command objects independent of CLI ordering. Changes to Docker or SSH command forms affect every workflow that calls the command object.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No selected host or role matches a filter | The command stops before remote work begins | Commander validates filtered hosts and roles and raises an argument error |
| A deploy lock is already held | A mutating command fails or waits until its configured timeout | The CLI checks lock state and supports bounded polling with --lock-wait |
| A remote action fails | The workflow stops and the CLI reports the SSHKit error | The executable handles runner errors at one boundary; verbose mode exposes the backtrace |
| A hook exits with failure | The workflow raises a hook-specific error | Hook execution wraps the command failure and honors --skip-hooks |
| Raw output is combined with an interactive or detached operation | Output or command semantics become invalid | CLI commands reject incompatible option combinations |
Key files
| File | Why to read it |
|---|---|
bin/kamal | Shows the process entry point and the final error boundary. |
| lib/…/main.rb | Shows top-level workflow composition and subcommand registration. |
| lib/…/base.rb | Shows shared option setup, commander initialization, locks, hooks, and execution helpers. |
| lib/…/commander.rb | Shows shared configuration, target selection, command factories, and output state. |
| lib/…/base.rb | Shows the common contract for building Docker and SSH actions. |
| lib/…/app.rb | Shows how a target-specific CLI command coordinates app work across roles and hosts. |
| lib/…/build.rb | Shows how local build work and remote image delivery use the same execution model. |