Skip to content

Commit 41d1902

Browse files
committed
docs: reorganize template documentation into centralized structure
- Move docs/contributing/templates.md to docs/contributing/templates/tera.md - Move docs/technical/template-system-architecture.md to docs/contributing/templates/template-system-architecture.md - Move templates/ansible/README.md to docs/contributing/templates/ansible.md - Create docs/contributing/templates/README.md with comprehensive overview and navigation - Update all documentation references across the project (AGENTS.md, docs/README.md, etc.) - Update internal cross-references in moved files This centralizes all template-related documentation in one location (docs/contributing/templates/) instead of being scattered across multiple directories, improving discoverability and maintainability.
1 parent 3306178 commit 41d1902

File tree

12 files changed

+177
-20
lines changed

12 files changed

+177
-20
lines changed

AGENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ These principles should guide all development decisions, code reviews, and featu
121121
- Git clients: GitHub Desktop, GitKraken, etc.
122122
- CI/CD: Any automated commits or merges
123123

124-
6. **Before working with Tera templates**: Read [`docs/contributing/templates.md`](docs/contributing/templates.md) for correct variable syntax - use `{{ variable }}` not `{ { variable } }`. Tera template files have the `.tera` extension.
124+
6. **Before working with Tera templates**: Read [`docs/contributing/templates/tera.md`](docs/contributing/templates/tera.md) for correct variable syntax - use `{{ variable }}` not `{ { variable } }`. Tera template files have the `.tera` extension.
125125

126-
7. **When adding new Ansible playbooks**: Read [`docs/contributing/templates.md`](docs/contributing/templates.md) for the complete guide. **CRITICAL**: Static playbooks (without `.tera` extension) must be registered in `src/infrastructure/external_tools/ansible/template/renderer/project_generator.rs` in the `copy_static_templates` method, otherwise they won't be copied to the build directory and Ansible will fail with "playbook not found" error.
126+
7. **When adding new Ansible playbooks**: Read [`docs/contributing/templates/tera.md`](docs/contributing/templates/tera.md) for the complete guide. **CRITICAL**: Static playbooks (without `.tera` extension) must be registered in `src/infrastructure/external_tools/ansible/template/renderer/project_generator.rs` in the `copy_static_templates` method, otherwise they won't be copied to the build directory and Ansible will fail with "playbook not found" error.
127127

128128
8. **When handling errors in code**: Read [`docs/contributing/error-handling.md`](docs/contributing/error-handling.md) for error handling principles. Prefer explicit enum errors over anyhow for better pattern matching and user experience. Make errors clear, include sufficient context for traceability, and ensure they are actionable with specific fix instructions.
129129

@@ -144,7 +144,7 @@ These principles should guide all development decisions, code reviews, and featu
144144

145145
15. **When creating new environment variables**: Read [`docs/contributing/environment-variables-naming.md`](docs/contributing/environment-variables-naming.md) for comprehensive guidance on naming conventions (condition-based vs action-based), decision frameworks, and best practices. Also review [`docs/decisions/environment-variable-prefix.md`](docs/decisions/environment-variable-prefix.md) to ensure all project environment variables use the `TORRUST_TD_` prefix for proper namespacing and avoiding conflicts with system or user variables.
146146

147-
16. **When adding new templates**: Read [`docs/technical/template-system-architecture.md`](docs/technical/template-system-architecture.md) to understand the Project Generator pattern. The `templates/` directory contains source templates. Dynamic templates (`.tera`) are automatically processed, but static files must be explicitly registered in their respective `ProjectGenerator` to be copied to the build directory.
147+
16. **When adding new templates**: Read [`docs/contributing/templates/template-system-architecture.md`](docs/contributing/templates/template-system-architecture.md) to understand the Project Generator pattern. The `templates/` directory contains source templates. Dynamic templates (`.tera`) are automatically processed, but static files must be explicitly registered in their respective `ProjectGenerator` to be copied to the build directory.
148148

149149
17. **When writing unit tests** (CRITICAL for test quality): Read [`docs/contributing/testing/unit-testing.md`](docs/contributing/testing/unit-testing.md) and follow the behavior-driven naming convention. **NEVER use the `test_` prefix** for test function names. Always use the `it_should_{expected_behavior}_when_{condition}` or `it_should_{expected_behavior}_given_{state}` pattern. This ensures tests clearly document the behavior being validated and the conditions under which it occurs. Example: `it_should_return_error_when_username_is_invalid()` instead of `test_invalid_username()`. Test names should follow the three-part structure (What-When-Then) and be descriptive enough that the test's purpose is clear without reading the code.
150150

@@ -224,7 +224,7 @@ The project has comprehensive documentation organized in the [`docs/`](docs/) di
224224
| Understand a decision | [`docs/decisions/README.md`](docs/decisions/README.md) |
225225
| Plan a new feature | [`docs/features/README.md`](docs/features/README.md) |
226226
| Fix external tool issues | [`docs/external-issues/README.md`](docs/external-issues/README.md) |
227-
| Work with templates | [`docs/contributing/templates.md`](docs/contributing/templates.md) |
227+
| Work with templates | [`docs/contributing/templates/`](docs/contributing/templates/) |
228228
| Handle errors properly | [`docs/contributing/error-handling.md`](docs/contributing/error-handling.md) |
229229
| Handle output properly | [`docs/contributing/output-handling.md`](docs/contributing/output-handling.md) |
230230
| Organize Rust modules | [`docs/contributing/module-organization.md`](docs/contributing/module-organization.md) |

docs/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Welcome to the Torrust Tracker Deployer documentation! This index helps you quic
1515
| Handle errors properly | [`contributing/error-handling.md`](contributing/error-handling.md) - Explicit enums, actionable messages |
1616
| Handle output properly | [`contributing/output-handling.md`](contributing/output-handling.md) - **CRITICAL** UserOutput, never `println!` |
1717
| Organize Rust code | [`contributing/module-organization.md`](contributing/module-organization.md) - **CRITICAL** import conventions |
18-
| Work with templates | [`contributing/templates.md`](contributing/templates.md) - **CRITICAL** Tera syntax, registration |
18+
| Work with templates | [`contributing/templates/`](contributing/templates/) - **CRITICAL** Tera syntax, registration |
1919
| Write unit tests | [`contributing/testing/unit-testing.md`](contributing/testing/unit-testing.md) - Naming conventions (no `test_` prefix!) |
2020
| Run E2E tests | [`e2e-testing/README.md`](e2e-testing/README.md) - Quick start, test suites |
2121
| Understand a past decision | [`decisions/README.md`](decisions/README.md) - 30+ ADRs indexed |
@@ -83,7 +83,7 @@ docs/
8383
| Understand a decision | [`decisions/README.md`](decisions/README.md) |
8484
| Plan a new feature | [`features/README.md`](features/README.md) |
8585
| Fix external tool issues | [`external-issues/README.md`](external-issues/README.md) |
86-
| Work with templates | [`contributing/templates.md`](contributing/templates.md) |
86+
| Work with templates | [`contributing/templates/`](contributing/templates/) |
8787
| Handle errors properly | [`contributing/error-handling.md`](contributing/error-handling.md) |
8888
| Handle output properly | [`contributing/output-handling.md`](contributing/output-handling.md) |
8989
| Organize Rust modules | [`contributing/module-organization.md`](contributing/module-organization.md) |
@@ -120,7 +120,7 @@ Essential guides: DDD layer placement, module organization, error handling, temp
120120
- [`contributing/README.md`](contributing/README.md) - Quick reference guide to all contribution documentation
121121
- [`contributing/ddd-layer-placement.md`](contributing/ddd-layer-placement.md) - **CRITICAL**: Rules for placing code in correct DDD layers
122122
- [`contributing/module-organization.md`](contributing/module-organization.md) - **CRITICAL**: Module organization and Rust import patterns
123-
- [`contributing/templates.md`](contributing/templates.md) - **CRITICAL**: Tera template syntax and registration
123+
- [`contributing/templates/`](contributing/templates/) - **CRITICAL**: Tera template syntax and registration
124124
- [`contributing/output-handling.md`](contributing/output-handling.md) - **CRITICAL**: Output handling with UserOutput (never `println!`)
125125
- [`contributing/error-handling.md`](contributing/error-handling.md) - Error handling principles
126126
- [`contributing/commit-process.md`](contributing/commit-process.md) - Commit process and pre-commit checks
@@ -212,7 +212,7 @@ Ansible, LXD, OpenTofu, SSH keys - installation, setup, and usage
212212
<details>
213213
<summary><strong>🔧 Technical Deep Dives (2 documents) (Click to Expand)</strong></summary>
214214

215-
- [`technical/template-system-architecture.md`](technical/template-system-architecture.md) - **CRITICAL**: Template system architecture and Project Generator pattern
215+
- [`contributing/templates/template-system-architecture.md`](contributing/templates/template-system-architecture.md) - **CRITICAL**: Template system architecture and Project Generator pattern
216216
- [`technical/type-erasure-pattern.md`](technical/type-erasure-pattern.md) - Type erasure pattern for Environment states
217217

218218
</details>

docs/contributing/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This guide will help you understand our development practices and contribution w
1616
| Error handling principles | [error-handling.md](./error-handling.md) |
1717
| Output handling with UserOutput | [output-handling.md](./output-handling.md) |
1818
| Secret handling (sensitive data) | [secret-handling.md](./secret-handling.md) |
19-
| Working with Tera templates | [templates.md](./templates.md) |
19+
| Working with Tera templates | [templates/](./templates/) |
2020
| Environment variables naming | [environment-variables-naming.md](./environment-variables-naming.md) |
2121
| Debugging techniques | [debugging.md](./debugging.md) |
2222
| Spell checking and dictionaries | [spelling.md](./spelling.md) |

docs/contributing/pr-review-guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Verify contributors followed these project conventions:
4343

4444
### Templates (if applicable)
4545

46-
- [ ] **Tera templates use correct syntax**: `{{ variable }}` not `{ { variable } }` (see [templates.md](./templates.md))
46+
- [ ] **Tera templates use correct syntax**: `{{ variable }}` not `{ { variable } }` (see [templates/tera.md](./templates/tera.md))
4747
- [ ] **Static Ansible playbooks** (without `.tera` extension) are registered in `src/infrastructure/external_tools/ansible/template/renderer/project_generator.rs`
4848

4949
## 🚩 Quick Red Flags
@@ -174,7 +174,7 @@ If you notice patterns that should be added to this guide, please:
174174
- [Branching Conventions](./branching.md) - Branch naming rules
175175
- [Commit Process](./commit-process.md) - Commit message format
176176
- [Testing Guide](./testing/) - Testing conventions and patterns
177-
- [Templates Guide](./templates.md) - Working with Tera templates
177+
- [Templates Guide](./templates/) - Working with Tera templates
178178
- [Known Issues](./known-issues.md) - Expected behaviors and workarounds
179179

180180
### Development Principles
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Template Documentation
2+
3+
This directory contains documentation about the template system used in the Torrust Tracker Deployer project.
4+
5+
## 📚 Documentation Overview
6+
7+
The template system uses a **double indirection** approach with embedded templates that are extracted and then rendered to build directories. Understanding this system is essential when working with infrastructure configurations.
8+
9+
### Core Documentation
10+
11+
- **[Template System Architecture](template-system-architecture.md)** - **CRITICAL**: Technical architecture overview
12+
13+
- Double indirection pattern (embedded → external → build)
14+
- Project Generator pattern (Orchestrator/Worker)
15+
- Two-phase processing (dynamic rendering + static copying)
16+
- Wrapper types, Renderer types, and Project Generators
17+
- Read this first to understand the overall system design
18+
19+
- **[Tera Template Syntax](tera.md)** - **CRITICAL**: Working with Tera templates
20+
21+
- Correct Tera variable syntax: `{{ variable }}` not `{ { variable } }`
22+
- Static vs dynamic playbooks
23+
- Adding new Ansible playbooks (registration required)
24+
- Using centralized variables pattern
25+
- Common mistakes and troubleshooting
26+
- Read this when creating or modifying `.tera` files
27+
28+
- **[Ansible Templates](ansible.md)** - Ansible-specific documentation
29+
- Available playbooks and their purpose
30+
- Usage order for typical deployments
31+
- CI/Testing considerations (firewall, container limitations)
32+
- Variables pattern and template processing
33+
- Read this when working with Ansible infrastructure
34+
35+
## 🎯 Quick Reference
36+
37+
### When to Read What
38+
39+
| Task | Read This |
40+
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
41+
| Understanding template architecture | [Template System Architecture](template-system-architecture.md) |
42+
| Creating/modifying `.tera` files | [Tera Template Syntax](tera.md) |
43+
| Adding new Ansible playbooks | [Tera Template Syntax](tera.md#-adding-new-ansible-playbooks) |
44+
| Working with Ansible infrastructure | [Ansible Templates](ansible.md) |
45+
| Understanding Project Generator pattern | [Template System Architecture](template-system-architecture.md#-project-generator-pattern-orchestratorworker) |
46+
47+
## ⚠️ Critical Rules
48+
49+
### 1. Tera Variable Syntax
50+
51+
Always use correct Tera syntax:
52+
53+
```yaml
54+
# ✅ CORRECT
55+
{{ variable_name }}
56+
57+
# ❌ WRONG - Spaces inside braces
58+
{ { variable_name } }
59+
```
60+
61+
### 2. Static Template Registration
62+
63+
Static templates (without `.tera` extension) **MUST be registered** in the Project Generator:
64+
65+
```rust
66+
// In copy_static_templates() method
67+
for playbook in &[
68+
"install-docker.yml",
69+
"your-new-playbook.yml", // ← ADD HERE
70+
] {
71+
// ...
72+
}
73+
```
74+
75+
**Without registration**: Ansible will fail with "playbook not found" error.
76+
77+
### 3. Centralized Variables Pattern
78+
79+
For Ansible playbooks, prefer the centralized variables pattern:
80+
81+
- Add variables to `templates/ansible/variables.yml.tera`
82+
- Reference via `vars_files: [variables.yml]` in static playbooks
83+
- Do NOT create new `.tera` templates unless necessary
84+
85+
## 🏗️ Template Structure
86+
87+
```text
88+
templates/
89+
├── ansible/ Ansible playbooks and configuration
90+
├── docker-compose/ Docker Compose configurations
91+
├── grafana/ Grafana monitoring dashboards
92+
├── prometheus/ Prometheus monitoring configuration
93+
├── tofu/ OpenTofu (Terraform) infrastructure
94+
└── tracker/ Torrust Tracker configuration
95+
```
96+
97+
## 📦 Template Types
98+
99+
### Dynamic Templates (.tera)
100+
101+
- **Extension**: `.tera`
102+
- **Processing**: Variable substitution using Tera engine
103+
- **Examples**: `inventory.yml.tera`, `variables.yml.tera`, `env.tera`
104+
- **Registration**: Automatic (discovered by extension)
105+
106+
### Static Templates
107+
108+
- **Extension**: `.yml`, `.cfg`, etc. (no `.tera`)
109+
- **Processing**: Direct file copy
110+
- **Examples**: `install-docker.yml`, `ansible.cfg`, `docker-compose.yml`
111+
- **Registration**: **Required** - must be added to Project Generator's copy list
112+
113+
## 🔄 Template Flow
114+
115+
```text
116+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
117+
│ Embedded │ │ External │ │ Build │
118+
│ Templates │───▶│ Templates │───▶│ Directory │
119+
│ (in binary) │ │ (data/templates) │ │ (build/) │
120+
└─────────────────┘ └──────────────────┘ └─────────────────┘
121+
│ │ │
122+
Compile Time Runtime Extraction Runtime Rendering
123+
```
124+
125+
## 📝 Related Documentation
126+
127+
- **[DDD Layer Placement](../ddd-layer-placement.md)** - Where template-related code belongs
128+
- **[Module Organization](../module-organization.md)** - How to organize template code
129+
- **[E2E Testing](../../e2e-testing/README.md)** - Testing template generation
130+
- **[AGENTS.md](../../../AGENTS.md)** - AI assistant instructions for templates
131+
132+
## 🎓 Learning Path
133+
134+
For new contributors working with templates:
135+
136+
1. **Start**: Read [Template System Architecture](template-system-architecture.md) for the big picture
137+
2. **Practice**: Read [Tera Template Syntax](tera.md) and try modifying existing templates
138+
3. **Apply**: Read [Ansible Templates](ansible.md) when working with infrastructure
139+
4. **Extend**: Follow the guides to add new templates and playbooks
140+
141+
## 🐛 Common Issues
142+
143+
### Problem: Prettier adds spaces in Tera variables
144+
145+
**Solution**: Add `*.tera` to `.prettierignore` or disable formatting for `.tera` files.
146+
147+
### Problem: Ansible playbook not found error
148+
149+
**Solution**: Static playbooks must be registered in `copy_static_templates()` method.
150+
151+
### Problem: Variables not resolved in output
152+
153+
**Solution**: Use `.tera` extension for files needing variable substitution.
154+
155+
## 📄 Beta Status Notice
156+
157+
The template system is currently in beta. Implementation details, APIs, and internal structure may change. These docs focus on core concepts and best practices rather than specific implementation details that may evolve.
File renamed without changes.

docs/technical/template-system-architecture.md renamed to docs/contributing/templates/template-system-architecture.md

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

33
Technical documentation for contributors working with the template rendering system.
44

5-
> **See Also**: For practical guidance on working with templates, see [Tera Template Variable Syntax](../contributing/templates.md).
5+
> **See Also**: For practical guidance on working with templates, see [Tera Template Variable Syntax](tera.md).
66
77
## 🏗️ System Overview
88

@@ -45,7 +45,7 @@ The system operates through two levels of indirection to balance portability wit
4545
- **Examples**: Infrastructure definitions, Ansible playbooks (`install-docker.yml`, `configure-security-updates.yml`)
4646
- **Use Case**: Configuration files that don't need variable substitution
4747
- **Registration**: **Must be explicitly registered** in the template renderer's copy list
48-
- **Guide**: See [`docs/contributing/templates.md`](../contributing/templates.md#-adding-new-ansible-playbooks) for adding new static Ansible playbooks
48+
- **Guide**: See [Tera Template Variable Syntax - Adding New Ansible Playbooks](tera.md#-adding-new-ansible-playbooks) for adding new static Ansible playbooks
4949

5050
### Dynamic Templates (Tera)
5151

0 commit comments

Comments
 (0)