|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Setup |
| 8 | +- `poetry install --with=dev --with=main` - Install all dependencies including dev tools |
| 9 | +- `poetry shell` - Activate virtual environment for development |
| 10 | + |
| 11 | +### Testing |
| 12 | +- `poetry run pytest` - Run unit tests |
| 13 | +- `poetry run pytest --verbose --cov=./ --cov-report=xml` - Run unit tests with coverage |
| 14 | +- `make test-unit` - Run unit tests in Docker (with coverage) |
| 15 | +- `make test-unit-no-cov` - Run unit tests in Docker (no coverage) |
| 16 | +- `make test-int` - Run integration tests using bats in Docker |
| 17 | +- `make tests` - Run full test suite (unit + integration) |
| 18 | + |
| 19 | +### Code Quality |
| 20 | +- `poetry run black .` - Format code with Black (line length: 120) |
| 21 | +- `poetry run pylint leverage/` - Run linting |
| 22 | +- `poetry run pre-commit install` - Install pre-commit hooks |
| 23 | +- `poetry run pre-commit run --all-files` - Run pre-commit checks manually |
| 24 | + |
| 25 | +### Build and Distribution |
| 26 | +- `make build` - Build distributables (cleans first) |
| 27 | +- `make check` - Check distributables with twine |
| 28 | +- `poetry build` - Build package using Poetry |
| 29 | +- `make clean` - Clean build artifacts |
| 30 | + |
| 31 | +### Docker |
| 32 | +- `make build-image` - Build Docker testing image |
| 33 | +- All test commands can run in Docker using the testing image |
| 34 | + |
| 35 | +## Architecture |
| 36 | + |
| 37 | +Leverage CLI is a Python-based command-line tool for managing Binbash Leverage projects. It uses a dockerized approach to encapsulate infrastructure tools. |
| 38 | + |
| 39 | +### Core Structure |
| 40 | +- `leverage/leverage.py` - Main CLI entry point using Click framework |
| 41 | +- `leverage/modules/` - Command modules (aws, terraform, kubectl, etc.) |
| 42 | +- `leverage/container.py` - Docker container management and execution |
| 43 | +- `leverage/conf.py` - Configuration loading from build.env files |
| 44 | +- `leverage/tasks.py` - Task system for build scripts |
| 45 | +- `leverage/path.py` - Path utilities and git repository handling |
| 46 | + |
| 47 | +### Key Components |
| 48 | +- **Module System**: Commands are organized in modules under `leverage/modules/` |
| 49 | +- **Container Integration**: Heavy use of Docker containers for tool execution |
| 50 | +- **Configuration Management**: Hierarchical loading of build.env files |
| 51 | +- **Task System**: Decorator-based task definition system for build scripts |
| 52 | +- **AWS Integration**: Extensive AWS credential and service management |
| 53 | + |
| 54 | +### Command Structure |
| 55 | +The CLI follows this pattern: |
| 56 | +``` |
| 57 | +leverage [global-options] <module> <subcommand> [args] |
| 58 | +``` |
| 59 | + |
| 60 | +Key modules include: |
| 61 | +- `project` - Project initialization and management |
| 62 | +- `terraform`/`tf`/`tofu` - Terraform/OpenTofu operations |
| 63 | +- `aws` - AWS service interactions |
| 64 | +- `credentials` - Credential management |
| 65 | +- `kubectl`/`kc` - Kubernetes operations |
| 66 | +- `run` - Custom task execution |
| 67 | +- `shell` - Interactive shell access |
| 68 | + |
| 69 | +### Version Management |
| 70 | +- Supports Python 3.9-3.13 |
| 71 | +- Version defined in `leverage/__init__.py` |
| 72 | +- Minimum tool versions enforced via `MINIMUM_VERSIONS` |
| 73 | +- Docker image versioning through `__toolbox_version__` |
| 74 | + |
| 75 | +### Configuration |
| 76 | +- Uses `build.env` files for project configuration |
| 77 | +- Hierarchical loading from project root to current directory |
| 78 | +- Environment-specific overrides supported |
| 79 | + |
| 80 | +## Docker Container Architecture for Terraform/OpenTofu |
| 81 | + |
| 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 | +#### Execution Flow (Host-Based): |
| 160 | +1. **CLI Command** → 2. **Environment Setup** → 3. **Authentication Context** → 4. **Host Binary Execution** → 5. **Result Return** |
| 161 | + |
| 162 | +**IMPORTANT**: As of the latest update, Leverage CLI now uses **host-based execution** instead of Docker containers: |
| 163 | + |
| 164 | +## Host-Based Execution Architecture |
| 165 | + |
| 166 | +The CLI has been updated to use host-based execution for improved performance and flexibility while maintaining all functionality. |
| 167 | + |
| 168 | +### New Executor Classes |
| 169 | + |
| 170 | +#### TerraformExecutor (`leverage/executors/terraform.py`) |
| 171 | +Host-based Terraform/OpenTofu executor: |
| 172 | +- **Binaries**: Uses system-installed `terraform` or `tofu` binaries |
| 173 | +- **Environment Management**: Context managers handle environment variables |
| 174 | +- **Authentication**: Supports SSO and MFA through host AWS CLI |
| 175 | +- **Configuration**: Automatic discovery and injection of .tfvars files |
| 176 | +- **Working Directory**: Direct filesystem operations (no mounting required) |
| 177 | + |
| 178 | +#### TFAutomvExecutor (`leverage/executors/tfautomv.py`) |
| 179 | +Extends TerraformExecutor for TFAutomv operations: |
| 180 | +- **Binary**: Uses system-installed `tfautomv` binary |
| 181 | +- Inherits all TerraformExecutor functionality |
| 182 | + |
| 183 | +### Context Manager Architecture |
| 184 | + |
| 185 | +#### Environment Context (`leverage/executors/context.py`) |
| 186 | +- **environment_context()**: Temporarily modifies environment variables with automatic cleanup |
| 187 | +- **run_command()**: Executes commands with environment and working directory context |
| 188 | +- Clean separation between interactive and silent execution modes |
| 189 | + |
| 190 | +#### Authentication Context (`leverage/executors/auth.py`) |
| 191 | +- **aws_credentials_context()**: Manages AWS authentication setup |
| 192 | +- SSO token validation and refresh |
| 193 | +- MFA credential path management |
| 194 | +- Automatic credential file discovery |
| 195 | + |
| 196 | +### Benefits of Host-Based Execution |
| 197 | + |
| 198 | +- **Performance**: No container startup overhead or image building |
| 199 | +- **Flexibility**: Use any installed tool version (including custom builds) |
| 200 | +- **IDE Integration**: Better debugging and tooling support |
| 201 | +- **Simplicity**: Direct binary execution with standard environment variables |
| 202 | +- **Plugin Compatibility**: Native plugin caching and management |
| 203 | + |
| 204 | +### Migration from Container-Based Execution |
| 205 | + |
| 206 | +The transition maintains full CLI compatibility: |
| 207 | +- All existing commands work identically |
| 208 | +- Same authentication methods (SSO/MFA) |
| 209 | +- Same configuration file handling |
| 210 | +- Same working directory behavior |
| 211 | +- Improved performance and reduced resource usage |
| 212 | + |
| 213 | +### Host Requirements |
| 214 | + |
| 215 | +For full functionality, ensure the following binaries are installed: |
| 216 | +- `terraform` or `tofu` (for Terraform/OpenTofu operations) |
| 217 | +- `tfautomv` (for TFAutomv operations) |
| 218 | +- `aws` CLI (for authentication) |
| 219 | +- `hcledit` (for configuration management) |
0 commit comments