|
1 | | -# Environment Configuration |
| 1 | +# Environment Configuration Files |
2 | 2 |
|
3 | | -This directory contains the environment configuration system for the Torrust Tracker Demo. |
| 3 | +This directory contains **user-generated environment configuration files** that are |
| 4 | +specific to your deployment. |
4 | 5 |
|
5 | | -## Files Overview |
| 6 | +## ⚠️ Important Notes |
6 | 7 |
|
7 | | -### Templates and Configuration |
| 8 | +- **Files in this directory are git-ignored** - they won't be committed to the repository |
| 9 | +- **Contains sensitive information** - passwords, API tokens, domain names specific to your setup |
| 10 | +- **Make backups** - these files are crucial for your deployment and need to be backed up separately |
8 | 11 |
|
9 | | -- **`base.env.tpl`** - Single base template for all environments (uses variable substitution) |
10 | | -- **`local.defaults`** - Default values for local development environment |
11 | | -- **`production.defaults`** - Default values for production environment template |
| 12 | +## File Purpose |
12 | 13 |
|
13 | | -### Generated Files (Git-Ignored) |
| 14 | +These files are generated from templates in `../templates/environments/` and contain: |
14 | 15 |
|
15 | | -- **`local.env`** - Generated local environment configuration (regenerated automatically) |
16 | | -- **`production.env`** - Generated production environment configuration (manual secrets required) |
| 16 | +- Database passwords and connection strings |
| 17 | +- API tokens and authentication secrets |
| 18 | +- Domain names and SSL configuration |
| 19 | +- Provider-specific settings (server types, regions, etc.) |
| 20 | +- Environment-specific configuration values |
17 | 21 |
|
18 | | -## How It Works |
| 22 | +## File Naming Convention |
19 | 23 |
|
20 | | -### Twelve-Factor Compliance |
| 24 | +Environment files follow the pattern: `{environment}-{provider}.env` |
21 | 25 |
|
22 | | -This system follows twelve-factor app principles by: |
| 26 | +Examples: |
23 | 27 |
|
24 | | -1. **Single Source of Truth**: One base template (`base.env.tpl`) for all environments |
25 | | -2. **Environment-Specific Configuration**: Default files define environment-specific values |
26 | | -3. **Separation of Concerns**: Configuration (defaults) separated from code (scripts) |
27 | | -4. **Version Control**: Default files are tracked, generated files with secrets are ignored |
| 28 | +- `development-libvirt.env` - Development environment using libvirt provider |
| 29 | +- `staging-hetzner.env` - Staging environment using Hetzner Cloud provider |
| 30 | +- `production-hetzner.env` - Production environment using Hetzner Cloud provider |
28 | 31 |
|
29 | | -## Template Processing |
| 32 | +## Creating Environment Files |
30 | 33 |
|
31 | | -Templates use environment variable substitution (`envsubst`) to generate final |
32 | | -configuration files: |
| 34 | +1. **Use templates**: Copy from `../templates/environments/{environment}.env.tpl` |
| 35 | +2. **Use generation scripts**: Run `infrastructure/scripts/configure-env.sh {environment}` |
| 36 | +3. **Follow naming convention**: Always include the provider suffix |
33 | 37 |
|
34 | | -```bash |
35 | | -# Templates are processed like this: |
36 | | -envsubst < local.env.tpl > local.env |
37 | | -envsubst < production.env.tpl > production.env # (after manual setup) |
38 | | -``` |
39 | | - |
40 | | -## Critical Deployment Behavior |
41 | | - |
42 | | -### The Git Archive Issue |
43 | | - |
44 | | -**IMPORTANT:** When you modify templates in this folder and run E2E tests, the tests |
45 | | -might fail if they depend on the new values. This happens due to how the application |
46 | | -deployment process works: |
47 | | - |
48 | | -1. **Infrastructure Provisioning**: New VM is created |
49 | | -2. **Code Deployment**: Git archive is copied to VM (`git archive HEAD`) |
50 | | -3. **Configuration Generation**: Templates are processed on the VM |
51 | | - |
52 | | -### The Problem |
53 | | - |
54 | | -**`git archive` only includes committed changes, not your working tree changes.** |
55 | | - |
56 | | -This means: |
57 | | - |
58 | | -- ✅ If you modify templates and **commit** them, E2E tests will use the new values |
59 | | -- ❌ If you modify templates but **don't commit** them, E2E tests will use the old |
60 | | - committed values |
61 | | - |
62 | | -### Example Scenario |
63 | | - |
64 | | -```bash |
65 | | -# 1. You modify local.env.tpl to change TRACKER_ADMIN_TOKEN |
66 | | -vim infrastructure/config/environments/local.env.tpl |
67 | | - |
68 | | -# 2. You run E2E tests without committing |
69 | | -make test-e2e # ❌ FAILS - Uses old token from git archive |
70 | | - |
71 | | -# 3. You commit your changes |
72 | | -git add infrastructure/config/environments/local.env.tpl |
73 | | -git commit -m "update token" |
74 | | - |
75 | | -# 4. You run E2E tests again |
76 | | -make test-e2e # ✅ PASSES - Uses new token from git archive |
77 | | -``` |
78 | | - |
79 | | -## Why Git Archive? |
80 | | - |
81 | | -The deployment process uses `git archive` for several important reasons: |
82 | | - |
83 | | -### Development Benefits |
84 | | - |
85 | | -- **Clean Deployment**: Only committed, tested changes are deployed |
86 | | -- **Excludes Local Files**: Doesn't copy `.env` files, build artifacts, or local storage |
87 | | -- **Reproducible**: Same git commit always produces the same deployment |
88 | | -- **Fast**: Compressed archive transfer is faster than full directory sync |
89 | | - |
90 | | -### Production Safety |
91 | | - |
92 | | -- **Version Control**: Only committed code reaches production |
93 | | -- **No Accidental Deployments**: Prevents deploying uncommitted debug code or secrets |
94 | | -- **Audit Trail**: Clear link between deployments and git commits |
95 | | -- **Rollback Capability**: Easy to redeploy any previous commit |
96 | | - |
97 | | -## Best Practices |
98 | | - |
99 | | -### For Development (E2E Testing) |
100 | | - |
101 | | -1. **Always commit template changes before running E2E tests**: |
102 | | - |
103 | | - ```bash |
104 | | - git add infrastructure/config/environments/ |
105 | | - git commit -m "update configuration templates" |
106 | | - make test-e2e |
107 | | - ``` |
108 | | - |
109 | | -2. **Check git status before testing**: |
110 | | - |
111 | | - ```bash |
112 | | - git status # Should show "working tree clean" |
113 | | - make test-e2e |
114 | | - ``` |
115 | | - |
116 | | -### For Production Deployment |
117 | | - |
118 | | -1. **Never modify templates directly in production** |
119 | | -2. **Always test changes in development first** |
120 | | -3. **Use proper git workflow** (feature branches, reviews, etc.) |
121 | | -4. **Verify configuration after deployment** |
122 | | - |
123 | | -## Alternative Approaches Considered |
124 | | - |
125 | | -### Option 1: Copy Working Tree |
126 | | - |
127 | | -```bash |
128 | | -# Instead of: git archive HEAD | tar -xz |
129 | | -rsync -av --exclude='.git' . vm:/path/ |
130 | | -``` |
131 | | - |
132 | | -**Pros**: Includes uncommitted changes |
133 | | - |
134 | | -**Cons**: |
135 | | - |
136 | | -- Copies local secrets and build artifacts |
137 | | -- No version control guarantee |
138 | | -- Inconsistent between development and production |
139 | | -- Larger transfer size |
140 | | - |
141 | | -### Option 2: Separate Config Management |
| 38 | +## Security Best Practices |
142 | 39 |
|
143 | | -```bash |
144 | | -# Keep templates separate from code deployment |
145 | | -scp infrastructure/config/environments/*.tpl vm:/path/ |
146 | | -``` |
147 | | - |
148 | | -**Pros**: Templates can be updated independently |
149 | | - |
150 | | -**Cons**: |
151 | | - |
152 | | -- More complex deployment process |
153 | | -- Configuration and code can get out of sync |
154 | | -- Additional deployment step to fail |
155 | | - |
156 | | -## Current Choice: Git Archive |
157 | | - |
158 | | -We chose to keep `git archive` because: |
159 | | - |
160 | | -1. **Production Safety**: Ensures only committed code is deployed |
161 | | -2. **Consistency**: Same process for development and production |
162 | | -3. **Simplicity**: Single deployment artifact |
163 | | -4. **Version Control**: Clear audit trail of what was deployed |
164 | | - |
165 | | -The trade-off is that **developers must commit template changes before E2E testing**, |
166 | | -but this is actually a good practice that ensures: |
167 | | - |
168 | | -- Template changes are reviewed and tested |
169 | | -- No accidental deployment of uncommitted changes |
170 | | -- Clear history of configuration changes |
171 | | - |
172 | | -## Troubleshooting |
173 | | - |
174 | | -### E2E Tests Fail After Template Changes |
175 | | - |
176 | | -1. **Check if changes are committed**: |
177 | | - |
178 | | - ```bash |
179 | | - git status infrastructure/config/environments/ |
180 | | - ``` |
181 | | - |
182 | | -2. **If uncommitted, commit them**: |
183 | | - |
184 | | - ```bash |
185 | | - git add infrastructure/config/environments/ |
186 | | - git commit -m "update: configuration templates for testing" |
187 | | - ``` |
188 | | - |
189 | | -3. **Re-run tests**: |
190 | | - |
191 | | - ```bash |
192 | | - make test-e2e |
193 | | - ``` |
194 | | - |
195 | | -### Configuration Not Updated After Deployment |
196 | | - |
197 | | -1. **Verify the git archive contains your changes**: |
| 40 | +- ✅ **Never commit** these files to git (they're ignored automatically) |
| 41 | +- ✅ **Make regular backups** of your environment files |
| 42 | +- ✅ **Use strong passwords** for all secrets and tokens |
| 43 | +- ✅ **Restrict file permissions**: `chmod 600 *.env` |
| 44 | +- ❌ **Don't share** environment files in public channels |
| 45 | +- ❌ **Don't store** in cloud storage without encryption |
198 | 46 |
|
199 | | - ```bash |
200 | | - git archive HEAD -- infrastructure/config/environments/ | tar -tz |
201 | | - ``` |
202 | | - |
203 | | -2. **Check template processing on VM**: |
204 | | - |
205 | | - ```bash |
206 | | - ssh torrust@$VM_IP 'cd torrust-tracker-demo && cat infrastructure/config/environments/local.env' |
207 | | - ``` |
208 | | - |
209 | | -3. **Verify generated configuration**: |
210 | | - |
211 | | - ```bash |
212 | | - ssh torrust@$VM_IP 'cd torrust-tracker-demo && cat application/.env' |
213 | | - ``` |
214 | | - |
215 | | -## Default Files System (New Approach) |
216 | | - |
217 | | -### Configuration Architecture |
218 | | - |
219 | | -The environment configuration system now uses a single base template with external default files: |
220 | | - |
221 | | -- **`base.env.tpl`**: Single template with variable placeholders (`${VARIABLE_NAME}`) |
222 | | -- **`local.defaults`**: Default values for local development |
223 | | -- **`production.defaults`**: Default placeholder values for production |
224 | | - |
225 | | -### Benefits |
226 | | - |
227 | | -1. **DRY Principle**: Single source of truth for all environment variables |
228 | | -2. **Maintainability**: Add variables once in base template, define values in defaults |
229 | | -3. **Version Control**: Default values are tracked and can be customized |
230 | | -4. **Consistency**: Same template processing logic for all environments |
231 | | - |
232 | | -### Usage |
| 47 | +## Backup Recommendations |
233 | 48 |
|
234 | 49 | ```bash |
235 | | -# Generate local environment (uses local.defaults) |
236 | | -./infrastructure/scripts/configure-env.sh local |
237 | | - |
238 | | -# Generate production template (uses production.defaults) |
239 | | -./infrastructure/scripts/configure-env.sh production |
| 50 | +# Create encrypted backup of all environment files |
| 51 | +tar -czf env-backup-$(date +%Y%m%d).tar.gz *.env |
| 52 | +gpg --symmetric --cipher-algo AES256 env-backup-$(date +%Y%m%d).tar.gz |
| 53 | +rm env-backup-$(date +%Y%m%d).tar.gz |
240 | 54 |
|
241 | | -# Generate secure production secrets |
242 | | -./infrastructure/scripts/configure-env.sh generate-secrets |
| 55 | +# Store the .gpg file in a secure location |
243 | 56 | ``` |
244 | 57 |
|
245 | | -### Customizing Defaults |
246 | | - |
247 | | -Edit the `.defaults` files to change environment-specific values: |
248 | | - |
249 | | -```bash |
250 | | -# Change local development domain |
251 | | -vim infrastructure/config/environments/local.defaults |
252 | | - |
253 | | -# Change production backup retention |
254 | | -vim infrastructure/config/environments/production.defaults |
| 58 | +## Recovery |
| 59 | + |
| 60 | +If you lose environment files: |
| 61 | + |
| 62 | +1. **From templates**: Regenerate using `infrastructure/scripts/configure-env.sh` |
| 63 | +2. **From backups**: Restore from your encrypted backups |
| 64 | +3. **Manual creation**: Copy from `../templates/environments/` and fill in values |
| 65 | + |
| 66 | +## Directory Structure |
| 67 | + |
| 68 | +```text |
| 69 | +infrastructure/config/ |
| 70 | +├── environments/ # ← User-generated files (YOU ARE HERE) |
| 71 | +│ ├── .gitignore # Ignores *.env files |
| 72 | +│ ├── README.md # This file |
| 73 | +│ ├── development-libvirt.env # Your development config |
| 74 | +│ ├── staging-hetzner.env # Your staging config |
| 75 | +│ └── production-hetzner.env # Your production config |
| 76 | +└── templates/ |
| 77 | + └── environments/ # Template files (.tpl) |
| 78 | + ├── README.md # Template documentation |
| 79 | + ├── base.env.tpl # Base template |
| 80 | + ├── production.env.tpl # Production template |
| 81 | + └── staging.env.tpl # Staging template |
255 | 82 | ``` |
256 | 83 |
|
257 | | -The next time you run configuration generation, your changes will be applied. |
258 | | - |
259 | | -## Security Notes |
260 | | - |
261 | | -- **Never commit production secrets** - Use placeholder values in templates |
262 | | -- **Review template changes** - Configuration changes can affect security |
263 | | -- **Test thoroughly** - Configuration errors can break the entire application |
264 | | -- **Backup production configs** - Before deploying configuration changes |
265 | | - |
266 | | -## Related Documentation |
267 | | - |
268 | | -- [Deployment Guide](../../../docs/guides/integration-testing-guide.md) |
269 | | -- [Twelve-Factor App Methodology](../../../docs/guides/integration-testing-guide.md#twelve-factor-compliance) |
270 | | -- [Configuration Management ADR](../../../docs/adr/004-configuration-approach-files-vs-environment-variables.md) |
| 84 | +For template documentation and usage instructions, see: `../templates/environments/README.md` |
0 commit comments