|
| 1 | +# Infrastructure Configuration Architecture |
| 2 | + |
| 3 | +This directory contains the configuration architecture for managing deployments across |
| 4 | +different environments and infrastructure providers. |
| 5 | + |
| 6 | +## 🏗️ Architectural Overview |
| 7 | + |
| 8 | +The configuration system uses a **three-layer architecture** to cleanly separate concerns: |
| 9 | + |
| 10 | +```text |
| 11 | +Provider Type → Provider Context → Environment Configuration |
| 12 | +``` |
| 13 | + |
| 14 | +### 1. **Provider Type** (Hosting Company) |
| 15 | + |
| 16 | +- **What**: Templates for different hosting companies/platforms |
| 17 | +- **Where**: `templates/providers/` |
| 18 | +- **Examples**: `hetzner.env.tpl`, `aws.env.tpl`, `digitalocean.env.tpl`, `libvirt.env.tpl` |
| 19 | +- **Purpose**: Defines what configuration variables each provider type requires |
| 20 | +- **Versioned**: Templates are committed to git for reference |
| 21 | + |
| 22 | +### 2. **Provider Context** (Specific Credentials & Configuration) |
| 23 | + |
| 24 | +- **What**: Actual credentials and resource specifications for a specific provider account |
| 25 | +- **Where**: `providers/` |
| 26 | +- **Examples**: `hetzner-staging.env`, `hetzner-production.env`, `aws-dev.env` |
| 27 | +- **Purpose**: Real API tokens, server types, locations, account-specific settings |
| 28 | +- **Security**: Files are git-ignored and contain sensitive information |
| 29 | + |
| 30 | +### 3. **Environment Configuration** (Deployment Target) |
| 31 | + |
| 32 | +- **What**: Complete configuration for a specific deployment environment |
| 33 | +- **Where**: `environments/` |
| 34 | +- **Examples**: `staging-hetzner-staging.env`, `production-hetzner-production.env` |
| 35 | +- **Purpose**: Combines application config + infrastructure config for deployment |
| 36 | +- **Security**: Files are git-ignored and contain sensitive information |
| 37 | + |
| 38 | +## 🔄 Configuration Flow |
| 39 | + |
| 40 | +### Example: Staging Deployment on Hetzner |
| 41 | + |
| 42 | +1. **Provider Type**: `hetzner` (uses `templates/providers/hetzner.env.tpl`) |
| 43 | +2. **Provider Context**: `hetzner-staging` (uses `providers/hetzner-staging.env`) |
| 44 | +3. **Environment**: `staging-hetzner-staging` (uses `environments/staging-hetzner-staging.env`) |
| 45 | + |
| 46 | +### Configuration Variables in Environment File |
| 47 | + |
| 48 | +```bash |
| 49 | +# Environment identification |
| 50 | +ENVIRONMENT_TYPE=staging |
| 51 | + |
| 52 | +# Provider identification |
| 53 | +PROVIDER=hetzner # Points to terraform/providers/hetzner/ |
| 54 | +PROVIDER_CONTEXT=hetzner-staging # Points to providers/hetzner-staging.env |
| 55 | + |
| 56 | +# Application configuration (common across providers) |
| 57 | +TRACKER_DOMAIN=tracker.staging.example.com |
| 58 | +GRAFANA_DOMAIN=grafana.staging.example.com |
| 59 | +SSL_EMAIL=admin@example.com |
| 60 | + |
| 61 | +# Infrastructure configuration (provider-specific, inherited from provider context) |
| 62 | +HETZNER_SERVER_TYPE=cx31 |
| 63 | +HETZNER_LOCATION=nbg1 |
| 64 | +HETZNER_API_TOKEN=xxx |
| 65 | +``` |
| 66 | + |
| 67 | +## 📁 Directory Structure |
| 68 | + |
| 69 | +```text |
| 70 | +infrastructure/config/ |
| 71 | +├── README.md # ← This file (architecture overview) |
| 72 | +│ |
| 73 | +├── templates/ # Template files (committed to git) |
| 74 | +│ ├── providers/ # Provider type templates |
| 75 | +│ │ ├── hetzner.env.tpl # Hetzner Cloud provider template |
| 76 | +│ │ ├── aws.env.tpl # AWS provider template (future) |
| 77 | +│ │ ├── digitalocean.env.tpl # DigitalOcean provider template (future) |
| 78 | +│ │ └── libvirt.env.tpl # libvirt provider template |
| 79 | +│ ├── environments/ # Environment templates |
| 80 | +│ │ ├── base.env.tpl # Base environment template |
| 81 | +│ │ ├── development.env.tpl # Development environment template |
| 82 | +│ │ ├── staging.env.tpl # Staging environment template |
| 83 | +│ │ └── production.env.tpl # Production environment template |
| 84 | +│ └── application/ # Application configuration templates |
| 85 | +│ └── ... # Docker, nginx, service configs |
| 86 | +│ |
| 87 | +├── providers/ # Provider contexts (git-ignored) |
| 88 | +│ ├── .gitignore # Ignores *.env files |
| 89 | +│ ├── README.md # Provider context documentation |
| 90 | +│ ├── hetzner-staging.env # Hetzner staging account credentials |
| 91 | +│ ├── hetzner-production.env # Hetzner production account credentials |
| 92 | +│ └── libvirt.env # Local libvirt configuration |
| 93 | +│ |
| 94 | +└── environments/ # Environment configurations (git-ignored) |
| 95 | + ├── .gitignore # Ignores *.env files |
| 96 | + ├── README.md # Environment configuration documentation |
| 97 | + ├── development-libvirt.env # Development using libvirt |
| 98 | + ├── staging-hetzner-staging.env # Staging using Hetzner staging account |
| 99 | + └── production-hetzner-production.env # Production using Hetzner production account |
| 100 | +``` |
| 101 | + |
| 102 | +## 🎯 Usage Examples |
| 103 | + |
| 104 | +### Creating a New Environment |
| 105 | + |
| 106 | +#### Option 1: Using Generation Scripts (Recommended) |
| 107 | + |
| 108 | +```bash |
| 109 | +# Generate staging environment using Hetzner staging provider |
| 110 | +./infrastructure/scripts/configure-env.sh staging hetzner-staging |
| 111 | + |
| 112 | +# This creates: environments/staging-hetzner-staging.env |
| 113 | +# Using templates: templates/environments/staging.env.tpl + providers/hetzner-staging.env |
| 114 | +``` |
| 115 | + |
| 116 | +#### Option 2: Manual Creation |
| 117 | + |
| 118 | +```bash |
| 119 | +# 1. Copy environment template |
| 120 | +cp templates/environments/staging.env.tpl environments/my-staging.env |
| 121 | + |
| 122 | +# 2. Set provider information |
| 123 | +echo "PROVIDER=hetzner" >> environments/my-staging.env |
| 124 | +echo "PROVIDER_CONTEXT=hetzner-staging" >> environments/my-staging.env |
| 125 | + |
| 126 | +# 3. Customize application settings |
| 127 | +vim environments/my-staging.env |
| 128 | +``` |
| 129 | + |
| 130 | +### Deployment Commands |
| 131 | + |
| 132 | +```bash |
| 133 | +# Deploy staging environment |
| 134 | +make infra-apply ENVIRONMENT_TYPE=staging ENVIRONMENT_FILE=staging-hetzner-staging |
| 135 | +make app-deploy ENVIRONMENT_TYPE=staging ENVIRONMENT_FILE=staging-hetzner-staging |
| 136 | + |
| 137 | +# Deploy production environment |
| 138 | +make infra-apply ENVIRONMENT_TYPE=production ENVIRONMENT_FILE=production-hetzner-production |
| 139 | +make app-deploy ENVIRONMENT_TYPE=production ENVIRONMENT_FILE=production-hetzner-production |
| 140 | +``` |
| 141 | + |
| 142 | +## 🔐 Security Model |
| 143 | + |
| 144 | +### Git-Ignored Files (Sensitive Data) |
| 145 | + |
| 146 | +- `providers/*.env` - Contains API tokens, credentials |
| 147 | +- `environments/*.env` - Contains complete deployment configuration |
| 148 | +- These files must be backed up separately and securely |
| 149 | + |
| 150 | +### Committed Files (Templates) |
| 151 | + |
| 152 | +- `templates/**/*.tpl` - Template files with placeholder values |
| 153 | +- Safe to commit, contain no sensitive information |
| 154 | + |
| 155 | +### File Permissions |
| 156 | + |
| 157 | +```bash |
| 158 | +# Secure your configuration files |
| 159 | +chmod 600 providers/*.env |
| 160 | +chmod 600 environments/*.env |
| 161 | +``` |
| 162 | + |
| 163 | +## 🛠️ Provider Implementation |
| 164 | + |
| 165 | +### Adding a New Provider Type |
| 166 | + |
| 167 | +1. **Create provider template**: `templates/providers/newprovider.env.tpl` |
| 168 | +2. **Create terraform provider**: `../terraform/providers/newprovider/` |
| 169 | +3. **Create provider context**: `providers/newprovider-context.env` |
| 170 | +4. **Test configuration**: Generate environment and test deployment |
| 171 | + |
| 172 | +### Provider Context vs Provider Type |
| 173 | + |
| 174 | +| Aspect | Provider Type | Provider Context | |
| 175 | +| -------------- | ---------------------- | ---------------------------- | |
| 176 | +| **Purpose** | Template/blueprint | Actual implementation | |
| 177 | +| **Location** | `templates/providers/` | `providers/` | |
| 178 | +| **Content** | Variable definitions | Real values | |
| 179 | +| **Examples** | `hetzner.env.tpl` | `hetzner-staging.env` | |
| 180 | +| **Git Status** | Committed | Git-ignored | |
| 181 | +| **Security** | Safe (no secrets) | Sensitive (contains secrets) | |
| 182 | + |
| 183 | +## 🔄 Migration Guide |
| 184 | + |
| 185 | +### From Old Architecture |
| 186 | + |
| 187 | +If you have files using the old naming convention: |
| 188 | + |
| 189 | +```bash |
| 190 | +# Old naming (mixed concepts) |
| 191 | +environments/staging-hetzner-staging.env with PROVIDER=hetzner-staging |
| 192 | + |
| 193 | +# New naming (separated concepts) |
| 194 | +environments/staging-hetzner-staging.env with PROVIDER=hetzner + PROVIDER_CONTEXT=hetzner-staging |
| 195 | +``` |
| 196 | + |
| 197 | +Update your environment files: |
| 198 | + |
| 199 | +```bash |
| 200 | +# Change from: |
| 201 | +PROVIDER=hetzner-staging |
| 202 | + |
| 203 | +# Change to: |
| 204 | +PROVIDER=hetzner |
| 205 | +PROVIDER_CONTEXT=hetzner-staging |
| 206 | +``` |
| 207 | + |
| 208 | +## 🧪 Testing Configuration |
| 209 | + |
| 210 | +### Validate Configuration Structure |
| 211 | + |
| 212 | +```bash |
| 213 | +# Test that all required files exist |
| 214 | +make infra-validate-config ENVIRONMENT_TYPE=staging ENVIRONMENT_FILE=staging-hetzner-staging |
| 215 | + |
| 216 | +# Test infrastructure planning |
| 217 | +make infra-plan ENVIRONMENT_TYPE=staging ENVIRONMENT_FILE=staging-hetzner-staging |
| 218 | +``` |
| 219 | + |
| 220 | +### Configuration Debugging |
| 221 | + |
| 222 | +```bash |
| 223 | +# Check what provider context will be loaded |
| 224 | +grep PROVIDER_CONTEXT environments/staging-hetzner-staging.env |
| 225 | + |
| 226 | +# Check what provider type will be used |
| 227 | +grep "PROVIDER=" environments/staging-hetzner-staging.env |
| 228 | + |
| 229 | +# Verify provider context file exists |
| 230 | +ls -la providers/hetzner-staging.env |
| 231 | +``` |
| 232 | + |
| 233 | +## 📚 Related Documentation |
| 234 | + |
| 235 | +- **Templates**: See `templates/environments/README.md` for template usage |
| 236 | +- **Providers**: See `providers/README.md` for provider context management |
| 237 | +- **Environments**: See `environments/README.md` for environment file management |
| 238 | +- **Deployment**: See `../../docs/guides/deployment-guide.md` for complete deployment workflows |
| 239 | + |
| 240 | +## 🎯 Best Practices |
| 241 | + |
| 242 | +### Naming Conventions |
| 243 | + |
| 244 | +- **Provider Types**: Use company/service name (e.g., `hetzner`, `aws`, `digitalocean`) |
| 245 | +- **Provider Contexts**: Use `{type}-{context}` (e.g., `hetzner-staging`, `hetzner-production`) |
| 246 | +- **Environments**: Use `{environment}-{provider-context}` (e.g., `staging-hetzner-staging`) |
| 247 | + |
| 248 | +### Security Practices |
| 249 | + |
| 250 | +- Never commit provider contexts or environment files |
| 251 | +- Use strong, unique passwords for all secrets |
| 252 | +- Backup configuration files securely and regularly |
| 253 | +- Restrict file permissions on sensitive files |
| 254 | +- Rotate API tokens and passwords regularly |
| 255 | + |
| 256 | +### Operational Practices |
| 257 | + |
| 258 | +- Test configuration changes in staging before production |
| 259 | +- Document any custom modifications in environment files |
| 260 | +- Keep provider contexts and environments in sync |
| 261 | +- Use descriptive comments in configuration files |
| 262 | +- Validate configurations before deployment |
| 263 | + |
| 264 | +This architecture provides clean separation of concerns while maintaining flexibility |
| 265 | +for different deployment scenarios and provider combinations. |
0 commit comments