-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracker.rs
More file actions
252 lines (223 loc) · 7.62 KB
/
Copy pathtracker.rs
File metadata and controls
252 lines (223 loc) · 7.62 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Tracker service release steps
//!
//! This module contains all steps required to release the Tracker service:
//! - Storage directory creation
//! - Database initialization
//! - Configuration template rendering
//! - Configuration deployment to remote
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::info;
use super::common::ansible_client;
use crate::application::command_handlers::common::StepResult;
use crate::application::command_handlers::release::errors::ReleaseCommandHandlerError;
use crate::application::steps::application::{
CreateTrackerStorageStep, DeployTrackerConfigStep, InitTrackerDatabaseStep,
};
use crate::application::steps::rendering::RenderTrackerTemplatesStep;
use crate::application::traits::CommandProgressListener;
use crate::domain::environment::state::ReleaseStep;
use crate::domain::environment::{Environment, Releasing};
use crate::shared::SystemClock;
/// Release the Tracker service
///
/// Executes all steps required to release the Tracker:
/// 1. Create storage directories
/// 2. Initialize database
/// 3. Render configuration templates
/// 4. Deploy configuration to remote
///
/// # Arguments
///
/// * `environment` - The environment in Releasing state
/// * `listener` - Optional progress listener for detail and debug reporting
///
/// # Errors
///
/// Returns a tuple of (error, step) if any tracker step fails
#[allow(clippy::result_large_err)]
pub fn release(
environment: &Environment<Releasing>,
listener: Option<&dyn CommandProgressListener>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
create_storage(environment, listener)?;
init_database(environment, listener)?;
let tracker_build_dir = render_templates(environment, listener)?;
deploy_config_to_remote(environment, &tracker_build_dir, listener)?;
Ok(())
}
/// Create tracker storage directories on the remote host
///
/// # Arguments
///
/// * `environment` - The environment in Releasing state
/// * `listener` - Optional progress listener for detail and debug reporting
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::CreateTrackerStorage`) if creation fails
#[allow(clippy::result_large_err)]
fn create_storage(
environment: &Environment<Releasing>,
listener: Option<&dyn CommandProgressListener>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::CreateTrackerStorage;
if let Some(l) = listener {
l.on_debug(&format!(
"Ansible working directory: {}",
environment.ansible_build_dir().display()
));
l.on_debug("Executing playbook: ansible-playbook create-tracker-storage.yml");
}
CreateTrackerStorageStep::new(ansible_client(environment))
.execute()
.map_err(|e| {
(
ReleaseCommandHandlerError::TrackerStorageCreation {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
if let Some(l) = listener {
l.on_detail("Creating storage directories: /opt/torrust/storage/tracker/{lib,log,etc}");
}
info!(
command = "release",
step = %current_step,
"Tracker storage directories created successfully"
);
Ok(())
}
/// Initialize tracker database on the remote host
///
/// # Arguments
///
/// * `environment` - The environment in Releasing state
/// * `listener` - Optional progress listener for detail and debug reporting
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::InitTrackerDatabase`) if initialization fails
#[allow(clippy::result_large_err)]
fn init_database(
environment: &Environment<Releasing>,
listener: Option<&dyn CommandProgressListener>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::InitTrackerDatabase;
if let Some(l) = listener {
l.on_debug("Executing playbook: ansible-playbook init-tracker-database.yml");
}
InitTrackerDatabaseStep::new(ansible_client(environment))
.execute()
.map_err(|e| {
(
ReleaseCommandHandlerError::TrackerDatabaseInit {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
if let Some(l) = listener {
l.on_detail("Initializing database: tracker.db");
}
info!(
command = "release",
step = %current_step,
"Tracker database initialized successfully"
);
Ok(())
}
/// Render Tracker configuration templates to the build directory
///
/// # Arguments
///
/// * `environment` - The environment in Releasing state
/// * `listener` - Optional progress listener for detail and debug reporting
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::RenderTrackerTemplates`) if rendering fails
#[allow(clippy::result_large_err)]
fn render_templates(
environment: &Environment<Releasing>,
listener: Option<&dyn CommandProgressListener>,
) -> StepResult<PathBuf, ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::RenderTrackerTemplates;
if let Some(l) = listener {
l.on_debug(&format!(
"Template source: {}/tracker/",
environment.templates_dir().display()
));
}
let clock = Arc::new(SystemClock);
let step = RenderTrackerTemplatesStep::new(
Arc::new(environment.clone()),
environment.templates_dir(),
environment.build_dir().clone(),
clock,
);
let tracker_build_dir = step.execute().map_err(|e| {
(
ReleaseCommandHandlerError::TemplateRendering {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
if let Some(l) = listener {
l.on_detail("Rendering tracker.toml from template");
l.on_debug(&format!("Template output: {}", tracker_build_dir.display()));
}
info!(
command = "release",
tracker_build_dir = %tracker_build_dir.display(),
"Tracker configuration templates rendered successfully"
);
Ok(tracker_build_dir)
}
/// Deploy tracker configuration to the remote host via Ansible
///
/// # Arguments
///
/// * `environment` - The environment in Releasing state
/// * `tracker_build_dir` - Path to the rendered tracker configuration
/// * `listener` - Optional progress listener for detail and debug reporting
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::DeployTrackerConfigToRemote`) if deployment fails
#[allow(clippy::result_large_err)]
fn deploy_config_to_remote(
environment: &Environment<Releasing>,
tracker_build_dir: &Path,
listener: Option<&dyn CommandProgressListener>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::DeployTrackerConfigToRemote;
if let Some(l) = listener {
l.on_debug("Executing playbook: ansible-playbook deploy-tracker-config.yml");
}
DeployTrackerConfigStep::new(ansible_client(environment), tracker_build_dir.to_path_buf())
.execute()
.map_err(|e| {
(
ReleaseCommandHandlerError::TrackerConfigDeployment {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
if let Some(l) = listener {
l.on_detail("Deploying config to /opt/torrust/storage/tracker/etc/tracker.toml");
}
info!(
command = "release",
step = %current_step,
"Tracker configuration deployed successfully"
);
Ok(())
}