-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracker.rs
More file actions
184 lines (163 loc) · 5.41 KB
/
Copy pathtracker.rs
File metadata and controls
184 lines (163 loc) · 5.41 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
//! 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::domain::environment::state::ReleaseStep;
use crate::domain::environment::{Environment, Releasing};
use crate::domain::template::TemplateManager;
/// 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
///
/// # Errors
///
/// Returns a tuple of (error, step) if any tracker step fails
#[allow(clippy::result_large_err)]
pub fn release(
environment: &Environment<Releasing>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
create_storage(environment)?;
init_database(environment)?;
let tracker_build_dir = render_templates(environment)?;
deploy_config_to_remote(environment, &tracker_build_dir)?;
Ok(())
}
/// Create tracker storage directories on the remote host
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::CreateTrackerStorage`) if creation fails
#[allow(clippy::result_large_err)]
fn create_storage(
environment: &Environment<Releasing>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::CreateTrackerStorage;
CreateTrackerStorageStep::new(ansible_client(environment))
.execute()
.map_err(|e| {
(
ReleaseCommandHandlerError::TrackerStorageCreation {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
info!(
command = "release",
step = %current_step,
"Tracker storage directories created successfully"
);
Ok(())
}
/// Initialize tracker database on the remote host
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::InitTrackerDatabase`) if initialization fails
#[allow(clippy::result_large_err)]
fn init_database(
environment: &Environment<Releasing>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::InitTrackerDatabase;
InitTrackerDatabaseStep::new(ansible_client(environment))
.execute()
.map_err(|e| {
(
ReleaseCommandHandlerError::TrackerDatabaseInit {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
info!(
command = "release",
step = %current_step,
"Tracker database initialized successfully"
);
Ok(())
}
/// Render Tracker configuration templates to the build directory
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::RenderTrackerTemplates`) if rendering fails
#[allow(clippy::result_large_err)]
fn render_templates(
environment: &Environment<Releasing>,
) -> StepResult<PathBuf, ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::RenderTrackerTemplates;
let template_manager = Arc::new(TemplateManager::new(environment.templates_dir()));
let step = RenderTrackerTemplatesStep::new(
Arc::new(environment.clone()),
template_manager,
environment.build_dir().clone(),
);
let tracker_build_dir = step.execute().map_err(|e| {
(
ReleaseCommandHandlerError::TemplateRendering {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
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
///
/// # 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,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::DeployTrackerConfigToRemote;
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,
)
})?;
info!(
command = "release",
step = %current_step,
"Tracker configuration deployed successfully"
);
Ok(())
}