Skip to content

Commit db1fb29

Browse files
committed
feat: implement template-based configuration system
- Add Tera template engine for dynamic configuration rendering - Migrate config/ → templates/ with template variables ({{ansible_host}}, {{ansible_ssh_private_key_file}}) - Implement 4-stage E2E workflow: static templates → infrastructure → runtime templates → execution - Add comprehensive template system with type-safe wrappers and early error detection - Remove direct file modification during E2E tests (preserves git working tree) - Add build/ directory for generated runtime configs (git-ignored) - Update documentation and workflows to use new template system - Add 21 comprehensive tests (17 unit tests + 4 integration tests) The template system ensures clean separation between templates (git-tracked) and runtime configurations (git-ignored), preventing git working tree modifications during E2E tests.
1 parent e1855e1 commit db1fb29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1492
-110
lines changed

.github/copilot-instructions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ This is a deployment infrastructure proof-of-concept for the Torrust ecosystem.
1414
## 📁 Key Directories
1515

1616
- `src/` - Rust source code and binaries
17-
- `config/ansible/` - Ansible playbooks and inventory
18-
- `config/tofu/` - OpenTofu/Terraform configurations
17+
- `templates/ansible/` - Ansible playbook templates
18+
- `templates/tofu/` - OpenTofu/Terraform configuration templates
19+
- `build/` - Generated runtime configurations (git-ignored)
1920
- `docs/` - Project documentation
2021

2122
## 🔧 Essential Rules

.github/workflows/test-e2e.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: E2E Tests
22

33
# NOTE: This workflow uses CI-specific approaches like 'sudo chmod 666' on the LXD socket
44
# and 'sudo' with LXD commands. These approaches are NOT recommended for local development.
5-
# For local use, follow the proper group membership approach documented in config/tofu/lxd/README.md
5+
# For local use, follow the proper group membership approach documented in templates/tofu/lxd/README.md
66

77
on:
88
push:
@@ -59,7 +59,7 @@ jobs:
5959

6060
- name: Get test outputs (on success)
6161
if: success()
62-
working-directory: config/tofu/lxd
62+
working-directory: build/tofu/lxd
6363
run: |
6464
echo "=== Infrastructure Outputs ==="
6565
sudo -E tofu output || echo "No outputs available"
@@ -77,7 +77,7 @@ jobs:
7777
sudo lxc list || echo "LXC list failed"
7878
7979
echo "=== OpenTofu State ==="
80-
cd config/tofu/lxd
80+
cd build/tofu/lxd
8181
sudo -E tofu show || echo "No state to show"
8282
8383
echo "=== System Resources ==="
@@ -89,7 +89,7 @@ jobs:
8989
9090
- name: Cleanup infrastructure (always run)
9191
if: always()
92-
working-directory: config/tofu/lxd
92+
working-directory: build/tofu/lxd
9393
run: |
9494
echo "Cleaning up test infrastructure..."
9595
# Use sudo for CI environment cleanup

.github/workflows/test-lxd-provision.yml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Test LXD Container Provisioning
22

33
# NOTE: This workflow uses CI-specific approaches like 'sudo chmod 666' on the LXD socket
44
# and 'sudo' with LXD commands. These approaches are NOT recommended for local development.
5-
# For local use, follow the proper group membership approach documented in config/tofu/lxd/README.md
5+
# For local use, follow the proper group membership approach documented in templates/tofu/lxd/README.md
66

77
on:
88
push:
@@ -26,6 +26,22 @@ jobs:
2626
- name: Install OpenTofu
2727
run: ./scripts/setup/install-opentofu.sh
2828

29+
- name: Setup Rust toolchain and build template system
30+
uses: dtolnay/rust-toolchain@stable
31+
with:
32+
toolchain: stable
33+
34+
- name: Cache Rust dependencies
35+
uses: Swatinem/rust-cache@v2
36+
37+
- name: Render template configurations
38+
run: |
39+
# Build the template system and render the static templates
40+
cargo build --release
41+
# For this workflow, we need static templates only (no runtime variables)
42+
mkdir -p build
43+
cp -r templates/* build/
44+
2945
- name: Verify installations
3046
run: |
3147
sudo lxc version
@@ -38,19 +54,19 @@ jobs:
3854
lxc list
3955
4056
- name: Initialize OpenTofu
41-
working-directory: config/tofu/lxd
57+
working-directory: build/tofu/lxd
4258
run: tofu init
4359

4460
- name: Validate OpenTofu configuration
45-
working-directory: config/tofu/lxd
61+
working-directory: build/tofu/lxd
4662
run: tofu validate
4763

4864
- name: Plan deployment
49-
working-directory: config/tofu/lxd
65+
working-directory: build/tofu/lxd
5066
run: tofu plan
5167

5268
- name: Apply configuration
53-
working-directory: config/tofu/lxd
69+
working-directory: build/tofu/lxd
5470
run: |
5571
# Use tofu without sudo since socket permissions are set up
5672
# NOTE: For local development, use "sg lxd -c 'tofu apply'" instead
@@ -114,12 +130,12 @@ jobs:
114130
lxc exec torrust-vm -- systemctl status ssh || echo "ssh service check failed"
115131
116132
- name: Get container outputs
117-
working-directory: config/tofu/lxd
133+
working-directory: build/tofu/lxd
118134
run: tofu output
119135

120136
- name: Cleanup
121137
if: always()
122-
working-directory: config/tofu/lxd
138+
working-directory: build/tofu/lxd
123139
run: |
124140
echo "Cleaning up container..."
125141
# Use tofu without sudo since socket permissions are set up

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Thumbs.db
5050
target/
5151
Cargo.lock
5252

53+
# Template build directory (runtime-generated configs)
54+
build/
55+
5356
# Meson build directory
5457
builddir/
5558

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ path = "src/bin/linter.rs"
2626

2727
[dependencies]
2828
tokio = { version = "1.0", features = [ "full" ] }
29-
serde_json = "1.0"
3029
anyhow = "1.0"
3130
clap = { version = "4.0", features = [ "derive" ] }
3231
regex = "1.0"
32+
serde = { version = "1.0", features = [ "derive" ] }
33+
serde_json = "1.0"
3334
tempfile = "3.0"
35+
tera = "1.0"
3436
torrust-linting = { path = "packages/linting" }

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ creating VMs that support cloud-init both locally (development) and in CI enviro
1818

1919
This repository uses LXD containers for virtualization:
2020

21-
### ☁️ **LXD Containers (`config/tofu/lxd/`)** - **OFFICIAL**
21+
### ☁️ **LXD Containers (`templates/tofu/lxd/`)** - **OFFICIAL**
2222

2323
- **Technology**: System containers with cloud-init support
2424
- **Status**: ✅ Official provider - Guaranteed GitHub Actions compatibility
2525
- **Best for**: CI/CD environments, fast provisioning, local development
2626
- **Requirements**: No special virtualization needed
2727

28-
**[📖 See detailed documentation →](config/tofu/lxd/README.md)**
28+
**[📖 See detailed documentation →](templates/tofu/lxd/README.md)**
2929

3030
## � Provider Comparison
3131

@@ -136,7 +136,7 @@ If you prefer manual deployment instead of using the E2E tests:
136136

137137
```bash
138138
# Navigate to LXD configuration
139-
cd config/tofu/lxd
139+
cd templates/tofu/lxd
140140

141141
# Initialize and deploy
142142
tofu init && tofu apply
@@ -242,10 +242,13 @@ The repository includes comprehensive GitHub Actions workflows for CI testing:
242242
│ │ └── meson-removal.md # Decision to remove Meson build system
243243
│ ├── documentation.md # Documentation organization guide
244244
│ └── vm-providers.md # Provider comparison for this project
245-
├── config/
246-
│ ├── tofu/
247-
│ │ └── lxd/ # LXD container configuration
248-
│ └── ansible/ # Ansible configuration management
245+
├── templates/ # 📁 Template configurations (git-tracked)
246+
│ ├── tofu/ # 🏗️ OpenTofu/Terraform templates
247+
│ │ └── lxd/ # LXD container template configuration
248+
│ └── ansible/ # 🤖 Ansible playbook templates
249+
├── build/ # 📁 Generated runtime configs (git-ignored)
250+
│ ├── tofu/ # 🏗️ Runtime OpenTofu configs
251+
│ └── ansible/ # 🤖 Runtime Ansible configs
249252
├── scripts/ # Development and utility scripts
250253
│ └── setup/ # Setup scripts for dependencies
251254
├── src/ # Rust source code

docs/documentation.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ docs/
110110
- `vm-providers.md` - Comparison of VM providers for this project
111111
- Project-specific usage patterns and workflows
112112

113-
### 📁 Configuration Documentation (`config/*/README.md`)
113+
### 📁 Configuration Documentation (`templates/*/README.md`)
114114

115115
**Purpose**: Documentation for specific configurations within the project.
116116

@@ -123,8 +123,8 @@ docs/
123123

124124
**Examples**:
125125

126-
- `config/tofu/lxd/README.md` - How to use the LXD OpenTofu configuration
127-
- `config/ansible/README.md` - How to use the Ansible playbooks
126+
- `templates/tofu/lxd/README.md` - How to use the LXD OpenTofu configuration
127+
- `templates/ansible/README.md` - How to use the Ansible playbooks
128128

129129
## 🎯 Guidelines for Contributors
130130

@@ -138,7 +138,7 @@ docs/
138138

139139
- If it's specific to how this project works
140140

141-
3. **Configuration documentation**`config/*/README.md`
141+
3. **Configuration documentation**`templates/*/README.md`
142142

143143
- If it's about a specific configuration or setup
144144

@@ -171,7 +171,7 @@ Install OpenTofu following the [OpenTofu setup guide](tech-stack/opentofu.md).
171171

172172
<!-- From tech stack to project usage -->
173173

174-
For project-specific usage, see the [LXD configuration guide](../config/tofu/lxd/README.md).
174+
For project-specific usage, see the [LXD configuration guide](../templates/tofu/lxd/README.md).
175175
```
176176

177177
## 🔄 Maintaining Documentation

docs/e2e-testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The E2E tests execute the following steps in production order:
4343

4444
1. **Infrastructure Provisioning**
4545

46-
- Uses OpenTofu configuration from `config/tofu/lxd/`
46+
- Uses OpenTofu configuration from `templates/tofu/lxd/`
4747
- Creates LXD container with Ubuntu and cloud-init configuration
4848

4949
2. **Cloud-init Completion** (`wait-cloud-init.yml`)
@@ -118,7 +118,7 @@ lxc stop torrust-vm
118118
lxc delete torrust-vm
119119

120120
# Or use OpenTofu to clean up
121-
cd config/tofu/lxd
121+
cd build/tofu/lxd
122122
tofu destroy -auto-approve
123123
```
124124

docs/research/ansible-testing-strategy.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ After extensive research and testing (documented in [docker-vs-lxd-ansible-testi
4141

4242
```bash
4343
# Provision LXD container once
44-
cd config/tofu/lxd
44+
cd build/tofu/lxd
4545
tofu apply -auto-approve # ~17.6s initial setup
4646

4747
# Reuse the same VM for multiple playbook tests
@@ -66,7 +66,7 @@ time ansible-playbook deploy-docker-stack.yml # ~22.6s
6666
#### 3. VM Cleanup (When Needed)
6767

6868
```bash
69-
cd config/tofu/lxd
69+
cd build/tofu/lxd
7070
tofu destroy -auto-approve # Clean slate for next test cycle
7171
```
7272

@@ -210,7 +210,7 @@ Based on comprehensive research and performance testing, we have implemented a *
210210

211211
```bash
212212
# One-time setup
213-
cd config/tofu/lxd && tofu apply -auto-approve
213+
cd build/tofu/lxd && tofu apply -auto-approve
214214

215215
# Sequential playbook testing (reusing same VM)
216216
cd ../../ansible

docs/research/docker-vs-lxd-ansible-testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ docker run -d --name torrust-test-container -p 2222:22 torrust-ansible-test
527527
docker run -d --name torrust-enhanced-container -p 2223:22 torrust-ansible-test-enhanced
528528

529529
# Test with LXD
530-
cd config/tofu/lxd
530+
cd templates/tofu/lxd
531531
time tofu apply -auto-approve # 17.6s
532532
cd ../../ansible
533533
time ansible-playbook install-docker.yml # 27.7s

0 commit comments

Comments
 (0)