-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcaddy.rs
More file actions
120 lines (106 loc) · 3.48 KB
/
caddy.rs
File metadata and controls
120 lines (106 loc) · 3.48 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
//! Caddy service release steps
//!
//! This module contains all steps required to release the Caddy service:
//! - Configuration template rendering
//! - Configuration deployment to remote
//!
//! All steps are optional and only execute if HTTPS is configured.
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::DeployCaddyConfigStep;
use crate::application::steps::rendering::RenderCaddyTemplatesStep;
use crate::domain::environment::state::ReleaseStep;
use crate::domain::environment::{Environment, Releasing};
use crate::domain::template::TemplateManager;
/// Release the Caddy service (if HTTPS enabled)
///
/// Executes all steps required to release Caddy:
/// 1. Render configuration templates
/// 2. Deploy configuration to remote
///
/// If HTTPS is not configured, all steps are skipped.
///
/// # Errors
///
/// Returns a tuple of (error, step) if any Caddy step fails
#[allow(clippy::result_large_err)]
pub fn release(
environment: &Environment<Releasing>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
// Check if HTTPS is configured
if environment.context().user_inputs.https().is_none() {
info!(
command = "release",
service = "caddy",
status = "skipped",
"HTTPS not configured - skipping all Caddy steps"
);
return Ok(());
}
render_templates(environment)?;
deploy_config_to_remote(environment)?;
Ok(())
}
/// Render Caddy configuration templates
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::RenderCaddyTemplates`) if rendering fails
#[allow(clippy::result_large_err)]
fn render_templates(
environment: &Environment<Releasing>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::RenderCaddyTemplates;
let template_manager = Arc::new(TemplateManager::new(environment.templates_dir()));
let step = RenderCaddyTemplatesStep::new(
Arc::new(environment.clone()),
template_manager,
environment.build_dir().clone(),
);
step.execute().map_err(|e| {
(
ReleaseCommandHandlerError::TemplateRendering {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
info!(
command = "release",
step = %current_step,
"Caddy configuration templates rendered successfully"
);
Ok(())
}
/// Deploy Caddy configuration to the remote host
///
/// # Errors
///
/// Returns a tuple of (error, `ReleaseStep::DeployCaddyConfigToRemote`) if deployment fails
#[allow(clippy::result_large_err)]
fn deploy_config_to_remote(
environment: &Environment<Releasing>,
) -> StepResult<(), ReleaseCommandHandlerError, ReleaseStep> {
let current_step = ReleaseStep::DeployCaddyConfigToRemote;
DeployCaddyConfigStep::new(ansible_client(environment))
.execute()
.map_err(|e| {
(
ReleaseCommandHandlerError::CaddyConfigDeployment {
message: e.to_string(),
source: Box::new(e),
},
current_step,
)
})?;
info!(
command = "release",
step = %current_step,
"Caddy configuration deployed to remote successfully"
);
Ok(())
}