Skip to content

Commit ecaca0d

Browse files
Update CLAUDE.md
1 parent 849e4c1 commit ecaca0d

1 file changed

Lines changed: 94 additions & 151 deletions

File tree

CLAUDE.md

Lines changed: 94 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
3434

3535
## Architecture
3636

37-
Leverage CLI is a Python-based command-line tool for managing Binbash Leverage projects. It uses a dockerized approach to encapsulate infrastructure tools.
37+
Leverage CLI is a Python-based command-line tool for managing Binbash Leverage projects. It uses host-based execution to run infrastructure tools directly on the system.
3838

3939
### Core Structure
4040
- `leverage/leverage.py` - Main CLI entry point using Click framework
4141
- `leverage/modules/` - Command modules (aws, terraform, kubectl, etc.)
42-
- `leverage/container.py` - Docker container management and execution
42+
- `leverage/modules/runner.py` - Generic binary runner base class
43+
- `leverage/modules/tfrunner.py` - Terraform/OpenTofu-specific runner
4344
- `leverage/conf.py` - Configuration loading from build.env files
4445
- `leverage/tasks.py` - Task system for build scripts
4546
- `leverage/path.py` - Path utilities and git repository handling
4647

4748
### Key Components
4849
- **Module System**: Commands are organized in modules under `leverage/modules/`
49-
- **Container Integration**: Heavy use of Docker containers for tool execution
50+
- **Host-Based Execution**: Direct execution of system binaries (terraform, tofu, kubectl, etc.)
51+
- **Runner Architecture**: Generic Runner class with specialized subclasses (TFRunner)
5052
- **Configuration Management**: Hierarchical loading of build.env files
5153
- **Task System**: Decorator-based task definition system for build scripts
52-
- **AWS Integration**: Extensive AWS credential and service management
54+
- **AWS Integration**: Extensive AWS credential and service management via SSO/MFA
5355

5456
### Command Structure
5557
The CLI follows this pattern:
@@ -69,98 +71,16 @@ Key modules include:
6971
### Version Management
7072
- Supports Python 3.9-3.13
7173
- Version defined in `leverage/__init__.py`
72-
- Minimum tool versions enforced via `MINIMUM_VERSIONS`
73-
- Docker image versioning through `__toolbox_version__`
74+
- Binary version validation on initialization (for TFRunner)
7475

7576
### Configuration
7677
- Uses `build.env` files for project configuration
7778
- Hierarchical loading from project root to current directory
7879
- Environment-specific overrides supported
7980

80-
## Docker Container Architecture for Terraform/OpenTofu
81+
## Execution Architecture
8182

82-
The CLI uses a containerized approach for all Terraform/OpenTofu operations to ensure consistent tool versions and isolated execution environments.
83-
84-
### Container Classes
85-
86-
#### TFContainer (`leverage/container.py:436-687`)
87-
Primary container for Terraform/OpenTofu execution:
88-
- **Image**: `binbash/leverage-toolbox` with user-specific permissions
89-
- **Binaries**: `/bin/terraform` (when `terraform=True`) or `/bin/tofu` (default)
90-
- **Mount Points**:
91-
- Project root → `/leverage` (guest base path)
92-
- AWS credentials directory → `/tmp/.aws`
93-
- Git config file → `/etc/gitconfig`
94-
- Optional: TF plugin cache directory (maintains symlinks)
95-
- Optional: SSH agent socket → `/ssh-agent`
96-
97-
#### TFautomvContainer (`leverage/container.py:689-717`)
98-
Extends TFContainer for TFAutomv operations:
99-
- **Binary**: `/usr/local/bin/tfautomv`
100-
- Inherits all TFContainer mounts and configuration
101-
102-
### Configuration File Management
103-
104-
#### Environment Variables in Containers:
105-
- `COMMON_CONFIG_FILE``common.tfvars`
106-
- `ACCOUNT_CONFIG_FILE``account.tfvars`
107-
- `BACKEND_CONFIG_FILE``backend.tfvars`
108-
- `AWS_SHARED_CREDENTIALS_FILE``/tmp/.aws/credentials`
109-
- `AWS_CONFIG_FILE``/tmp/.aws/config`
110-
- `SSO_CACHE_DIR``/tmp/.aws/sso/cache`
111-
112-
#### Terraform Variable Files:
113-
The `tf_default_args` property automatically includes:
114-
- All `*.tfvars` files from `common/` directory
115-
- All `*.tfvars` files from account-specific directory
116-
117-
### Docker Execution Points
118-
119-
#### Terraform/OpenTofu Commands (`leverage/modules/tf.py`)
120-
- Container creation for `tofu` and `terraform` commands (lines 38, 56)
121-
- Command execution via `tf.start()` for all operations
122-
- **Supported Commands**: `init`, `plan`, `apply`, `destroy`, `output`, `version`, `shell`, `format`, `validate`, `import`, `refresh-credentials`
123-
124-
#### TFAutomv Commands (`leverage/modules/tfautomv.py`)
125-
- Container creation for `tfautomv` commands (line 24)
126-
- Command execution via `tf.start_in_layer()` (line 36)
127-
128-
### Container Lifecycle
129-
130-
1. **Image Verification**: `ensure_image()` builds local image with user permissions
131-
2. **Container Creation**: `_create_container()` with mounted volumes and environment
132-
3. **Authentication Setup**: SSO token validation or MFA credential handling
133-
4. **Command Execution**: Interactive (`_start()`) or silent (`_exec()`)
134-
5. **Cleanup**: Automatic container stop and removal
135-
136-
### Authentication & Credentials
137-
138-
#### SSO Authentication:
139-
- Token validation before container execution
140-
- Automatic credential refresh via `refresh_layer_credentials()`
141-
- Browser-based authentication flow with user code
142-
143-
#### MFA Authentication:
144-
- Script-based authentication via `aws-mfa-entrypoint.sh`
145-
- Environment variable adjustments for credential paths
146-
147-
#### Credential Mounting:
148-
- Host AWS credentials directory mounted to container
149-
- Separate credential files for different authentication methods
150-
151-
### Backend Configuration Management
152-
153-
#### S3 Backend Handling:
154-
- Automatic `backend.tfvars` parameter injection for `init` commands
155-
- Dynamic state key generation based on layer path structure
156-
- Backend block validation in `config.tf` files
157-
- Support for legacy naming conventions (tf- vs terraform-)
158-
159-
**IMPORTANT**: As of the latest update, Leverage CLI now uses **host-based execution** instead of Docker containers:
160-
161-
## Host-Based Execution Architecture
162-
163-
The CLI has been updated to use host-based execution for improved performance and flexibility while maintaining all functionality.
83+
The CLI executes infrastructure tools directly on the host system using the Runner architecture.
16484

16585
### Core Runner Classes
16686

@@ -179,84 +99,103 @@ Generic command runner base class:
17999
Terraform/OpenTofu-specific runner extending Runner:
180100
- **Binaries**: Uses system-installed `terraform` or `tofu` binaries
181101
- **Configuration**: Accepts `terraform=True` for Terraform, defaults to OpenTofu
102+
- **Binary Validation**: Validates binary type by checking `--version` output
103+
- Ensures `tofu` binary is actually OpenTofu (not Terraform)
104+
- Ensures `terraform` binary is actually Terraform (not OpenTofu)
182105
- **Error Messages**: Provides installation URLs when binaries are not found
183106
- Terraform: https://developer.hashicorp.com/terraform/install
184107
- OpenTofu: https://opentofu.org/docs/intro/install/
185108
- **Environment Variables**: Initialized with AWS credential file paths via `env_vars` parameter
186-
- **No Containers**: Direct binary execution on host system
187-
188-
### Command Flow Architecture
189-
190-
#### Terraform/OpenTofu Command Flow (`leverage/modules/tf.py`)
191-
192-
1. **CLI Entry Points**:
193-
- `@click.group() tofu()` (lines 22-35) - Creates TFRunner with OpenTofu binary
194-
- `@click.group() terraform()` (lines 38-51) - Creates TFRunner with Terraform binary
195-
- Both set up credential environment variables for AWS config and credentials files
196-
197-
2. **Command Decoration**:
198-
- `@pass_runner` - Injects TFRunner instance from Click context
199-
- `@pass_paths` - Injects PathsHandler instance for file/directory management
200-
201-
3. **Supported Commands**:
202-
- `init` - Layer initialization with backend configuration injection
203-
- `plan` - Execution plan generation with auto-discovered tfvars
204-
- `apply` - Infrastructure changes with conditional tfvars injection
205-
- `destroy` - Infrastructure destruction
206-
- `output` - Output variable display
207-
- `version` - Binary version display
208-
- `format` - Code formatting (recursive by default)
209-
- `force-unlock` - State file lock removal
210-
- `validate` - Configuration validation
211-
- `validate-layout` - Leverage convention validation
212-
- `import` - Resource import
213-
- `refresh-credentials` - AWS credential refresh
214-
215-
4. **Multi-Layer Support**:
216-
- `--layers` option for operating on multiple layers from account directory
217-
- Layer validation and backend key management via `invoke_for_all_commands()`
218-
- Automatic backend key generation based on layer path structure
219-
220-
### Authentication Management
221-
222-
#### SSO Authentication (`leverage/modules/auth.py`)
223-
224-
**Token Validation** (`check_sso_token()` - lines 98-127):
225-
- Validates SSO token existence in cache directory
109+
110+
### Command Modules
111+
112+
#### Terraform/OpenTofu Commands (`leverage/modules/tf.py`)
113+
114+
**CLI Entry Points**:
115+
- `tofu` - Creates TFRunner with OpenTofu binary (`tofu`)
116+
- `terraform` - Creates TFRunner with Terraform binary (`terraform`)
117+
- Both set up credential environment variables for AWS config and credentials files
118+
119+
**Command Decorators**:
120+
- `@pass_runner` - Injects TFRunner instance from Click context
121+
- `@pass_paths` - Injects PathsHandler instance for file/directory management
122+
123+
**Supported Commands**:
124+
- `init` - Layer initialization with backend configuration injection
125+
- `plan` - Execution plan generation with auto-discovered tfvars
126+
- `apply` - Infrastructure changes with conditional tfvars injection
127+
- `destroy` - Infrastructure destruction
128+
- `output` - Output variable display
129+
- `version` - Binary version display
130+
- `format` - Code formatting (recursive by default)
131+
- `force-unlock` - State file lock removal
132+
- `validate` - Configuration validation
133+
- `validate-layout` - Leverage convention validation
134+
- `import` - Resource import
135+
- `refresh-credentials` - AWS credential refresh
136+
137+
**Multi-Layer Support**:
138+
- `--layers` option for operating on multiple layers from account directory
139+
- Layer validation and backend key management via `invoke_for_all_commands()`
140+
- Automatic backend key generation based on layer path structure
141+
142+
#### Kubectl Commands (`leverage/modules/kubectl.py`)
143+
144+
Uses generic Runner class to execute `kubectl` binary:
145+
- **Binary**: System-installed `kubectl`
146+
- **Configuration**: Sets KUBECONFIG environment variable to project-specific path
147+
- **AWS Integration**: Configures kubectl contexts for EKS clusters
148+
- **Commands**:
149+
- `configure` - Add EKS cluster from current layer to kubectl config
150+
- `discover` - Scan for cluster metadata files and configure selected cluster
151+
- All other kubectl commands pass through to the binary
152+
153+
#### TFAutomv Commands (`leverage/modules/tfautomv.py`)
154+
155+
Uses generic Runner class to execute `tfautomv` binary:
156+
- **Binary**: System-installed `tfautomv`
157+
- **Configuration**: Passes terraform binary path via `--terraform-bin` flag
158+
- **Integration**: Uses same tfvars discovery as Terraform/OpenTofu commands
159+
160+
### Authentication Management (`leverage/modules/auth.py`)
161+
162+
#### SSO Authentication
163+
164+
**Token Validation** (`check_sso_token()`):
165+
- Validates SSO token existence in cache directory (`~/.aws/sso/cache/<sso_role>`)
226166
- Checks token expiration against current time
227167
- Provides clear error messages for missing or expired tokens
228-
- Token file location: `~/.aws/sso/cache/<sso_role>`
229168

230-
**Credential Refresh** (`refresh_layer_credentials()` - lines 130-204):
169+
**Credential Refresh** (`refresh_layer_credentials()`):
231170
- Parses Terraform files to discover required AWS profiles
232171
- Uses boto3 SSO client to retrieve temporary credentials
233172
- Updates AWS config file with credential expiration timestamps
234173
- Writes temporary credentials to AWS credentials file
235174
- Implements 30-minute early renewal to avoid mid-operation expiration
236175
- Supports cross-account profile resolution
237176

238-
**Profile Discovery** (`get_profiles()` - lines 68-88):
177+
**Profile Discovery** (`get_profiles()`):
239178
- Scans `config.tf`, `locals.tf`, `runtime.tf` for profile references
240179
- Extracts profile variables from Terraform configurations
241180
- Reads backend profile from `backend.tfvars`
242181

243182
### Configuration Management
244183

245-
#### Automatic tfvars Discovery (`tf_default_args()` - lines 133-154):
184+
#### Automatic tfvars Discovery (`tf_default_args()`):
246185
- Discovers all `*.tfvars` files in `common/` directory
247186
- Discovers all `*.tfvars` files in account-specific directory
248187
- Returns as `-var-file=<path>` arguments for Terraform/OpenTofu
249188
- Used automatically in plan, destroy, validate, and conditionally in apply
250189

251190
#### Backend Configuration:
252-
- Backend config file path injected during `init` command (line 336)
253-
- Automatic backend key generation in `invoke_for_all_commands()` (lines 291-294)
254-
- Backend key validation in `validate_layout()` (lines 538-550)
191+
- Backend config file path injected during `init` command
192+
- Automatic backend key generation in `invoke_for_all_commands()`
193+
- Backend key validation in `validate_layout()`
255194
- Support for legacy naming conventions (tf- vs terraform-, base- vs tools-)
256195

257196
### Execution Flow
258197

259-
**Standard Command Execution**:
198+
#### Standard Command Execution
260199
1. User runs `leverage tofu|terraform <command> [args]`
261200
2. Click creates TFRunner instance with credential environment variables
262201
3. Command function decorated with `@pass_runner` and `@pass_paths`
@@ -269,26 +208,30 @@ Terraform/OpenTofu-specific runner extending Runner:
269208
- User-provided arguments
270209
7. Exit code returned to CLI
271210

272-
**Multi-Layer Execution**:
211+
#### Multi-Layer Execution
273212
1. User runs command with `--layers layer1,layer2` from account directory
274213
2. `invoke_for_all_commands()` validates all layers
275214
3. Backend keys generated/validated for each layer
276215
4. Command executed sequentially for each layer with layer-specific working directory
277216

278-
### Benefits of Host-Based Execution
217+
### System Requirements
279218

280-
- **Performance**: No container startup overhead or image building
281-
- **Flexibility**: Use any installed tool version (including custom builds)
282-
- **IDE Integration**: Better debugging and tooling support
283-
- **Simplicity**: Direct binary execution with standard environment variables
284-
- **Plugin Compatibility**: Native Terraform/OpenTofu plugin caching
285-
- **Development Speed**: Faster iteration during development
219+
For full functionality, ensure the following binaries are installed and available in PATH:
286220

287-
### Host Requirements
221+
**Required**:
222+
- `terraform` or `tofu` - For Terraform/OpenTofu operations
223+
- `aws` - AWS CLI for SSO authentication (via boto3)
224+
- Python 3.9-3.13
288225

289-
For full functionality, ensure the following binaries are installed and available in PATH:
290-
- `terraform` or `tofu` (for Terraform/OpenTofu operations)
291-
- `aws` CLI (for SSO authentication via boto3)
226+
**Optional**:
227+
- `kubectl` - For Kubernetes operations
228+
- `tfautomv` - For TFAutomv operations
229+
230+
### Benefits of Current Architecture
292231

293-
Optional binaries:
294-
- `tfautomv` (for TFAutomv operations)
232+
- **Performance**: Direct binary execution without overhead
233+
- **Flexibility**: Use any installed tool version (including custom builds)
234+
- **IDE Integration**: Better debugging and tooling support
235+
- **Simplicity**: Standard environment variables and execution
236+
- **Plugin Compatibility**: Native Terraform/OpenTofu plugin caching
237+
- **Development Speed**: Faster iteration during development

0 commit comments

Comments
 (0)