|
| 1 | +# Environment Configuration Templates |
| 2 | + |
| 3 | +This directory contains environment-specific configuration templates that are processed |
| 4 | +during deployment to generate the final configuration files. |
| 5 | + |
| 6 | +## Files |
| 7 | + |
| 8 | +- `local.env.tpl` - Local development environment template |
| 9 | +- `production.env.tpl` - Production environment template (requires manual setup) |
| 10 | + |
| 11 | +## Template Processing |
| 12 | + |
| 13 | +Templates use environment variable substitution (`envsubst`) to generate final |
| 14 | +configuration files: |
| 15 | + |
| 16 | +```bash |
| 17 | +# Templates are processed like this: |
| 18 | +envsubst < local.env.tpl > local.env |
| 19 | +envsubst < production.env.tpl > production.env # (after manual setup) |
| 20 | +``` |
| 21 | + |
| 22 | +## Critical Deployment Behavior |
| 23 | + |
| 24 | +### The Git Archive Issue |
| 25 | + |
| 26 | +**IMPORTANT:** When you modify templates in this folder and run E2E tests, the tests |
| 27 | +might fail if they depend on the new values. This happens due to how the application |
| 28 | +deployment process works: |
| 29 | + |
| 30 | +1. **Infrastructure Provisioning**: New VM is created |
| 31 | +2. **Code Deployment**: Git archive is copied to VM (`git archive HEAD`) |
| 32 | +3. **Configuration Generation**: Templates are processed on the VM |
| 33 | + |
| 34 | +### The Problem |
| 35 | + |
| 36 | +**`git archive` only includes committed changes, not your working tree changes.** |
| 37 | + |
| 38 | +This means: |
| 39 | + |
| 40 | +- ✅ If you modify templates and **commit** them, E2E tests will use the new values |
| 41 | +- ❌ If you modify templates but **don't commit** them, E2E tests will use the old |
| 42 | + committed values |
| 43 | + |
| 44 | +### Example Scenario |
| 45 | + |
| 46 | +```bash |
| 47 | +# 1. You modify local.env.tpl to change TRACKER_ADMIN_TOKEN |
| 48 | +vim infrastructure/config/environments/local.env.tpl |
| 49 | + |
| 50 | +# 2. You run E2E tests without committing |
| 51 | +make test-e2e # ❌ FAILS - Uses old token from git archive |
| 52 | + |
| 53 | +# 3. You commit your changes |
| 54 | +git add infrastructure/config/environments/local.env.tpl |
| 55 | +git commit -m "update token" |
| 56 | + |
| 57 | +# 4. You run E2E tests again |
| 58 | +make test-e2e # ✅ PASSES - Uses new token from git archive |
| 59 | +``` |
| 60 | + |
| 61 | +## Why Git Archive? |
| 62 | + |
| 63 | +The deployment process uses `git archive` for several important reasons: |
| 64 | + |
| 65 | +### Development Benefits |
| 66 | + |
| 67 | +- **Clean Deployment**: Only committed, tested changes are deployed |
| 68 | +- **Excludes Local Files**: Doesn't copy `.env` files, build artifacts, or local storage |
| 69 | +- **Reproducible**: Same git commit always produces the same deployment |
| 70 | +- **Fast**: Compressed archive transfer is faster than full directory sync |
| 71 | + |
| 72 | +### Production Safety |
| 73 | + |
| 74 | +- **Version Control**: Only committed code reaches production |
| 75 | +- **No Accidental Deployments**: Prevents deploying uncommitted debug code or secrets |
| 76 | +- **Audit Trail**: Clear link between deployments and git commits |
| 77 | +- **Rollback Capability**: Easy to redeploy any previous commit |
| 78 | + |
| 79 | +## Best Practices |
| 80 | + |
| 81 | +### For Development (E2E Testing) |
| 82 | + |
| 83 | +1. **Always commit template changes before running E2E tests**: |
| 84 | + |
| 85 | + ```bash |
| 86 | + git add infrastructure/config/environments/ |
| 87 | + git commit -m "update configuration templates" |
| 88 | + make test-e2e |
| 89 | + ``` |
| 90 | + |
| 91 | +2. **Check git status before testing**: |
| 92 | + |
| 93 | + ```bash |
| 94 | + git status # Should show "working tree clean" |
| 95 | + make test-e2e |
| 96 | + ``` |
| 97 | + |
| 98 | +### For Production Deployment |
| 99 | + |
| 100 | +1. **Never modify templates directly in production** |
| 101 | +2. **Always test changes in development first** |
| 102 | +3. **Use proper git workflow** (feature branches, reviews, etc.) |
| 103 | +4. **Verify configuration after deployment** |
| 104 | + |
| 105 | +## Alternative Approaches Considered |
| 106 | + |
| 107 | +### Option 1: Copy Working Tree |
| 108 | + |
| 109 | +```bash |
| 110 | +# Instead of: git archive HEAD | tar -xz |
| 111 | +rsync -av --exclude='.git' . vm:/path/ |
| 112 | +``` |
| 113 | + |
| 114 | +**Pros**: Includes uncommitted changes |
| 115 | + |
| 116 | +**Cons**: |
| 117 | + |
| 118 | +- Copies local secrets and build artifacts |
| 119 | +- No version control guarantee |
| 120 | +- Inconsistent between development and production |
| 121 | +- Larger transfer size |
| 122 | + |
| 123 | +### Option 2: Separate Config Management |
| 124 | + |
| 125 | +```bash |
| 126 | +# Keep templates separate from code deployment |
| 127 | +scp infrastructure/config/environments/*.tpl vm:/path/ |
| 128 | +``` |
| 129 | + |
| 130 | +**Pros**: Templates can be updated independently |
| 131 | + |
| 132 | +**Cons**: |
| 133 | + |
| 134 | +- More complex deployment process |
| 135 | +- Configuration and code can get out of sync |
| 136 | +- Additional deployment step to fail |
| 137 | + |
| 138 | +## Current Choice: Git Archive |
| 139 | + |
| 140 | +We chose to keep `git archive` because: |
| 141 | + |
| 142 | +1. **Production Safety**: Ensures only committed code is deployed |
| 143 | +2. **Consistency**: Same process for development and production |
| 144 | +3. **Simplicity**: Single deployment artifact |
| 145 | +4. **Version Control**: Clear audit trail of what was deployed |
| 146 | + |
| 147 | +The trade-off is that **developers must commit template changes before E2E testing**, |
| 148 | +but this is actually a good practice that ensures: |
| 149 | + |
| 150 | +- Template changes are reviewed and tested |
| 151 | +- No accidental deployment of uncommitted changes |
| 152 | +- Clear history of configuration changes |
| 153 | + |
| 154 | +## Troubleshooting |
| 155 | + |
| 156 | +### E2E Tests Fail After Template Changes |
| 157 | + |
| 158 | +1. **Check if changes are committed**: |
| 159 | + |
| 160 | + ```bash |
| 161 | + git status infrastructure/config/environments/ |
| 162 | + ``` |
| 163 | + |
| 164 | +2. **If uncommitted, commit them**: |
| 165 | + |
| 166 | + ```bash |
| 167 | + git add infrastructure/config/environments/ |
| 168 | + git commit -m "update: configuration templates for testing" |
| 169 | + ``` |
| 170 | + |
| 171 | +3. **Re-run tests**: |
| 172 | + |
| 173 | + ```bash |
| 174 | + make test-e2e |
| 175 | + ``` |
| 176 | + |
| 177 | +### Configuration Not Updated After Deployment |
| 178 | + |
| 179 | +1. **Verify the git archive contains your changes**: |
| 180 | + |
| 181 | + ```bash |
| 182 | + git archive HEAD -- infrastructure/config/environments/ | tar -tz |
| 183 | + ``` |
| 184 | + |
| 185 | +2. **Check template processing on VM**: |
| 186 | + |
| 187 | + ```bash |
| 188 | + ssh torrust@$VM_IP 'cd torrust-tracker-demo && cat infrastructure/config/environments/local.env' |
| 189 | + ``` |
| 190 | + |
| 191 | +3. **Verify generated configuration**: |
| 192 | + |
| 193 | + ```bash |
| 194 | + ssh torrust@$VM_IP 'cd torrust-tracker-demo && cat application/.env' |
| 195 | + ``` |
| 196 | + |
| 197 | +## Security Notes |
| 198 | + |
| 199 | +- **Never commit production secrets** - Use placeholder values in templates |
| 200 | +- **Review template changes** - Configuration changes can affect security |
| 201 | +- **Test thoroughly** - Configuration errors can break the entire application |
| 202 | +- **Backup production configs** - Before deploying configuration changes |
| 203 | + |
| 204 | +## Related Documentation |
| 205 | + |
| 206 | +- [Deployment Guide](../../../docs/guides/integration-testing-guide.md) |
| 207 | +- [Twelve-Factor App Methodology](../../../docs/guides/integration-testing-guide.md#twelve-factor-compliance) |
| 208 | +- [Configuration Management ADR](../../../docs/adr/004-configuration-approach-files-vs-environment-variables.md) |
0 commit comments