Exec Type
This document describes the design of the exec resource type for executing commands.
Overview
The exec resource executes commands with idempotency controls:
- Creates: Skip execution if a file exists
- Refresh Only: Only execute when triggered by a subscribed resource
- Exit Codes: Validate success via configurable return codes
Provider Interface
Exec providers must implement the ExecProvider interface:
Method Responsibilities
| Method | Responsibility |
|---|---|
Status | Check if creates file exists, return current state |
Execute | Run the command, return exit code |
Status Response
The Status method returns an ExecState containing:
The Ensure field in CommonResourceState is set to:
presentif thecreatesfile existsabsentif thecreatesfile does not exist (or not specified)
Available Providers
| Provider | Execution Method | Documentation |
|---|---|---|
posix | Direct exec (no shell) | Posix |
shell | Via /bin/sh -c | Shell |
Properties
| Property | Type | Description |
|---|---|---|
command | string | Command to run (defaults to name if not set) |
cwd | string | Working directory for command execution |
environment | []string | Additional environment variables (KEY=value) |
path | string | Search path for executables (colon-separated) |
returns | []int | Acceptable exit codes (default: [0]) |
timeout | string | Maximum execution time (e.g., 30s, 5m) |
creates | string | File path; skip execution if exists |
refresh_only | bool | Only execute via subscribe refresh |
subscribe | []string | Resources to watch for changes (type#name) |
logoutput | bool | Log command output |
Apply Logic
Idempotency
The exec resource provides idempotency through two mechanisms:
Creates Property
The creates property specifies a file that indicates successful prior execution:
Behavior:
- If
/opt/app/bin/appexists, skip execution - Useful for one-time setup commands
- Provider checks file existence via
Status()
Refresh Only Property
The refresh_only property limits execution to subscribe refreshes:
Behavior:
- Command only runs when subscribed resource changes
- Without a subscribe trigger, command is skipped
- Useful for reload/restart commands
Decision Table
| Condition | Action |
|---|---|
| Subscribe triggered | Execute |
creates file exists | Skip |
refresh_only: true + no trigger | Skip |
refresh_only: false + no creates | Execute |
Subscribe Behavior
Exec resources can subscribe to other resources and execute when they change:
Subscribe takes precedence over other idempotency checks - if a subscribed resource changed, the command executes regardless of creates file existence.
Exit Code Validation
By default, exit code 0 indicates success. The returns property customizes acceptable codes:
Behavior:
- Command succeeds if exit code is in
returnslist - Command fails if exit code is not in
returnslist - Used for desired state validation after execution
Noop Mode
In noop mode, the exec type:
- Queries current state normally (checks
createsfile) - Evaluates subscribe triggers
- Logs what actions would be taken
- Sets appropriate
NoopMessage:- “Would have executed”
- “Would have executed via subscribe”
- Reports
Changed: trueif execution would occur - Does not call provider
Executemethod
Desired State Validation
After execution (in non-noop mode), the type verifies success:
If the exit code is not in the acceptable returns list, an ErrDesiredStateFailed error is returned.
Command vs Name
The command property is optional. If not specified, the name is used as the command:
Using a descriptive name with explicit command is recommended for clarity.
Environment and Path
Commands can be configured with custom environment:
Environment:
- Added to the command’s environment
- Format:
KEY=value - Does not replace existing environment
Path:
- Sets the
PATHfor executable lookup - Must be absolute directories
- Colon-separated list