-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker_compose_templates.rs
More file actions
232 lines (203 loc) · 7.93 KB
/
docker_compose_templates.rs
File metadata and controls
232 lines (203 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! Docker Compose template rendering step
//!
//! This module provides the `RenderDockerComposeTemplatesStep` which handles rendering
//! of Docker Compose configuration templates to the build directory. This step prepares
//! Docker Compose files for deployment to the remote host.
//!
//! ## Key Features
//!
//! - Template rendering for Docker Compose configurations
//! - Integration with the `DockerComposeTemplateRenderingService` for file generation
//! - Build directory preparation for deployment operations
//! - Comprehensive error handling for template processing
//!
//! ## Usage Context
//!
//! This step is typically executed during the release workflow, after
//! infrastructure provisioning and software installation, to prepare
//! the Docker Compose files for deployment.
//!
//! ## Architecture
//!
//! This step follows the three-level architecture:
//! - **Command** (Level 1): `ReleaseCommandHandler` orchestrates the release workflow
//! - **Step** (Level 2): This `RenderDockerComposeTemplatesStep` handles template rendering
//! - The templates are rendered locally, no remote action is needed
use std::path::PathBuf;
use std::sync::Arc;
use tracing::{info, instrument};
use crate::application::services::rendering::DockerComposeTemplateRenderingService;
use crate::application::services::rendering::DockerComposeTemplateRenderingServiceError;
use crate::domain::environment::Environment;
use crate::shared::clock::Clock;
/// Step that renders Docker Compose templates to the build directory
///
/// This step handles the preparation of Docker Compose configuration files
/// by rendering templates to the build directory. The rendered files are
/// then ready to be deployed to the remote host by the `DeployComposeFilesStep`.
pub struct RenderDockerComposeTemplatesStep<S> {
environment: Arc<Environment<S>>,
templates_dir: PathBuf,
build_dir: PathBuf,
clock: Arc<dyn Clock>,
}
impl<S> RenderDockerComposeTemplatesStep<S> {
/// Creates a new `RenderDockerComposeTemplatesStep`
///
/// # Arguments
///
/// * `environment` - The deployment environment
/// * `templates_dir` - The templates directory
/// * `build_dir` - The build directory where templates will be rendered
/// * `clock` - Clock service for generating template metadata timestamps
#[must_use]
pub fn new(
environment: Arc<Environment<S>>,
templates_dir: PathBuf,
build_dir: PathBuf,
clock: Arc<dyn Clock>,
) -> Self {
Self {
environment,
templates_dir,
build_dir,
clock,
}
}
/// Execute the template rendering step
///
/// This will render Docker Compose templates to the build directory.
///
/// # Returns
///
/// Returns the path to the docker-compose build directory on success.
///
/// # Errors
///
/// Returns an error if:
/// * Template rendering fails
/// * Directory creation fails
/// * File copying fails
#[instrument(
name = "render_docker_compose_templates",
skip_all,
fields(
step_type = "rendering",
template_type = "docker_compose",
build_dir = %self.build_dir.display()
)
)]
pub async fn execute(&self) -> Result<PathBuf, DockerComposeTemplateRenderingServiceError> {
info!(
step = "render_docker_compose_templates",
templates_dir = %self.templates_dir.display(),
build_dir = %self.build_dir.display(),
"Rendering Docker Compose templates"
);
let service = DockerComposeTemplateRenderingService::from_paths(
self.templates_dir.clone(),
self.build_dir.clone(),
self.clock.clone(),
);
let user_inputs = &self.environment.context().user_inputs;
let admin_token = self.environment.admin_token();
let compose_build_dir = service.render(user_inputs, admin_token).await?;
info!(
step = "render_docker_compose_templates",
compose_build_dir = %compose_build_dir.display(),
status = "success",
"Docker Compose templates rendered successfully"
);
Ok(compose_build_dir)
}
}
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use super::*;
use crate::domain::environment::testing::EnvironmentTestBuilder;
use crate::infrastructure::templating::docker_compose::DOCKER_COMPOSE_SUBFOLDER;
use crate::shared::clock::SystemClock;
#[tokio::test]
async fn it_should_create_render_docker_compose_templates_step() {
let templates_dir = TempDir::new().expect("Failed to create templates dir");
let build_dir = TempDir::new().expect("Failed to create build dir");
let (environment, _, _, _temp_dir) =
EnvironmentTestBuilder::new().build_with_custom_paths();
let environment = Arc::new(environment);
let clock = Arc::new(SystemClock);
let step = RenderDockerComposeTemplatesStep::new(
environment.clone(),
templates_dir.path().to_path_buf(),
build_dir.path().to_path_buf(),
clock,
);
assert_eq!(step.build_dir, build_dir.path());
assert_eq!(step.templates_dir, templates_dir.path());
}
#[tokio::test]
async fn it_should_render_templates_from_embedded_sources() {
let templates_dir = TempDir::new().expect("Failed to create templates dir");
let build_dir = TempDir::new().expect("Failed to create build dir");
let (environment, _, _, _temp_dir) =
EnvironmentTestBuilder::new().build_with_custom_paths();
let environment = Arc::new(environment);
let clock = Arc::new(SystemClock);
let step = RenderDockerComposeTemplatesStep::new(
environment,
templates_dir.path().to_path_buf(),
build_dir.path().to_path_buf(),
clock,
);
let result = step.execute().await;
assert!(result.is_ok());
let compose_build_dir = result.unwrap();
assert!(compose_build_dir.join("docker-compose.yml").exists());
}
#[tokio::test]
async fn it_should_render_correct_content() {
let templates_dir = TempDir::new().expect("Failed to create templates dir");
let build_dir = TempDir::new().expect("Failed to create build dir");
let (environment, _, _, _temp_dir) =
EnvironmentTestBuilder::new().build_with_custom_paths();
let environment = Arc::new(environment);
let clock = Arc::new(SystemClock);
let step = RenderDockerComposeTemplatesStep::new(
environment,
templates_dir.path().to_path_buf(),
build_dir.path().to_path_buf(),
clock,
);
let result = step.execute().await;
assert!(result.is_ok());
let output_content = tokio::fs::read_to_string(
build_dir
.path()
.join(DOCKER_COMPOSE_SUBFOLDER)
.join("docker-compose.yml"),
)
.await
.expect("Failed to read output");
// Verify it contains expected content from embedded template
assert!(output_content.contains("torrust/tracker"));
assert!(output_content.contains("./storage/tracker/lib:/var/lib/torrust/tracker"));
// Verify dynamic ports are rendered (default TrackerConfig has 6969 UDP, 7070 HTTP, 1212 API)
assert!(
output_content.contains("6969:6969/udp"),
"Should contain UDP tracker port 6969"
);
assert!(
output_content.contains("7070:7070"),
"Should contain HTTP tracker port 7070"
);
assert!(
output_content.contains("1212:1212"),
"Should contain HTTP API port 1212"
);
// Verify hardcoded ports are NOT present
assert!(
!output_content.contains("6868:6868"),
"Should not contain hardcoded UDP port 6868"
);
}
}