-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.rs
More file actions
367 lines (329 loc) · 13.6 KB
/
Copy pathhandler.rs
File metadata and controls
367 lines (329 loc) · 13.6 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//! Destroy command handler implementation
use std::sync::Arc;
use tracing::{info, instrument};
use super::errors::DestroyCommandHandlerError;
use crate::application::command_handlers::common::StepResult;
use crate::application::steps::DestroyInfrastructureStep;
use crate::domain::environment::repository::{EnvironmentRepository, TypedEnvironmentRepository};
use crate::domain::environment::{Destroyed, Environment};
use crate::shared::error::Traceable;
/// `DestroyCommandHandler` orchestrates the complete infrastructure destruction workflow
///
/// The `DestroyCommandHandler` orchestrates the complete infrastructure teardown workflow.
///
/// This command handler handles all steps required to destroy infrastructure:
/// 1. Destroy infrastructure via `OpenTofu`
/// 2. Clean up state files
/// 3. Transition environment to `Destroyed` state
///
/// # State Management
///
/// The command handler integrates with the type-state pattern for environment lifecycle:
/// - Accepts `Environment<S>` (any state) as input via environment name lookup
/// - Transitions to `Environment<Destroying>` at start
/// - Returns `Environment<Destroyed>` on success
/// - Transitions to `Environment<DestroyFailed>` on error
///
/// State is persisted after each transition using the injected repository.
///
/// # Idempotency
///
/// The destroy operation is idempotent. Running it multiple times on the same
/// environment will:
/// - Succeed if the infrastructure is already destroyed
/// - Report appropriate status to the user
/// - Not fail due to missing resources
pub struct DestroyCommandHandler {
pub(crate) repository: TypedEnvironmentRepository,
pub(crate) clock: Arc<dyn crate::shared::Clock>,
}
impl DestroyCommandHandler {
/// Create a new `DestroyCommandHandler`
#[must_use]
pub fn new(
repository: Arc<dyn EnvironmentRepository>,
clock: Arc<dyn crate::shared::Clock>,
) -> Self {
Self {
repository: TypedEnvironmentRepository::new(repository),
clock,
}
}
/// Execute the complete destruction workflow
///
/// # Arguments
///
/// * `env_name` - The name of the environment to destroy
///
/// # Returns
///
/// Returns the destroyed environment
///
/// # Errors
///
/// Returns an error if any step in the destruction workflow fails:
/// * Environment not found or cannot be loaded
/// * Environment is in an invalid state for destruction
/// * `OpenTofu` destroy fails
/// * Unable to persist the destroyed state
///
/// On error, the environment transitions to `DestroyFailed` state and is persisted.
#[instrument(
name = "destroy_command",
skip_all,
fields(
command_type = "destroy",
environment = %env_name
)
)]
pub fn execute(
&self,
env_name: &crate::domain::environment::name::EnvironmentName,
) -> Result<crate::domain::environment::Environment<Destroyed>, DestroyCommandHandlerError>
{
use crate::domain::environment::state::AnyEnvironmentState;
info!(
command = "destroy",
environment = %env_name,
"Starting complete infrastructure destruction workflow"
);
// 1. Load the environment from storage
let environment = self
.repository
.inner()
.load(env_name)
.map_err(DestroyCommandHandlerError::StatePersistence)?;
// 2. Check if environment exists
let environment = environment.ok_or_else(|| {
DestroyCommandHandlerError::StatePersistence(
crate::domain::environment::repository::RepositoryError::NotFound,
)
})?;
// 3. Check if environment is already destroyed
if let AnyEnvironmentState::Destroyed(env) = environment {
info!(
command = "destroy",
environment = %env_name,
"Environment is already destroyed"
);
return Ok(env);
}
// 4. Capture start time before transitioning to Destroying state
let started_at = self.clock.now();
// 5. Get the build directory from the environment context (before consuming environment)
let opentofu_build_dir = environment.tofu_build_dir();
// 6. Transition to Destroying state
// Since we have AnyEnvironmentState, we need to match on it and call start_destroying on the typed environment
let destroying_env = match environment {
AnyEnvironmentState::Created(env) => env.start_destroying(),
AnyEnvironmentState::Provisioning(env) => env.start_destroying(),
AnyEnvironmentState::Provisioned(env) => env.start_destroying(),
AnyEnvironmentState::Configuring(env) => env.start_destroying(),
AnyEnvironmentState::Configured(env) => env.start_destroying(),
AnyEnvironmentState::Releasing(env) => env.start_destroying(),
AnyEnvironmentState::Released(env) => env.start_destroying(),
AnyEnvironmentState::Running(env) => env.start_destroying(),
AnyEnvironmentState::Destroying(env) => env, // Already destroying
AnyEnvironmentState::ProvisionFailed(env) => env.start_destroying(),
AnyEnvironmentState::ConfigureFailed(env) => env.start_destroying(),
AnyEnvironmentState::ReleaseFailed(env) => env.start_destroying(),
AnyEnvironmentState::RunFailed(env) => env.start_destroying(),
AnyEnvironmentState::DestroyFailed(env) => env.start_destroying(),
AnyEnvironmentState::Destroyed(_) => {
unreachable!("Already handled Destroyed state above")
}
};
// 7. Persist intermediate state
self.repository.save_destroying(&destroying_env)?;
// 8. Create OpenTofu client with correct build directory
let opentofu_client = Arc::new(crate::adapters::tofu::client::OpenTofuClient::new(
opentofu_build_dir,
));
// 9. Execute destruction steps with explicit step tracking
match Self::execute_destruction_with_tracking(&destroying_env, &opentofu_client) {
Ok(()) => {
// Transition to Destroyed state
let destroyed = destroying_env.destroyed();
// Persist final state
self.repository.save_destroyed(&destroyed)?;
info!(
command = "destroy",
environment = %destroyed.name(),
"Infrastructure destruction completed successfully"
);
Ok(destroyed)
}
Err((e, current_step)) => {
// Transition to error state with structured context
let context =
self.build_failure_context(&destroying_env, &e, current_step, started_at);
let failed = destroying_env.destroy_failed(context);
// Persist error state
self.repository.save_destroy_failed(&failed)?;
Err(e)
}
}
}
// pub(crate) helper methods for testing business logic
/// Check if infrastructure should be destroyed
///
/// Determines whether to attempt infrastructure destruction based on whether
/// the `OpenTofu` build directory exists. If the directory doesn't exist, it means
/// no infrastructure was ever provisioned (e.g., environment in Created state).
///
/// # Arguments
///
/// * `environment` - The environment being destroyed
///
/// # Returns
///
/// Returns `true` if infrastructure destruction should be attempted, `false` otherwise
pub(crate) fn should_destroy_infrastructure(
environment: &Environment<crate::domain::environment::Destroying>,
) -> bool {
let tofu_build_dir = environment.tofu_build_dir();
tofu_build_dir.exists()
}
/// Clean up state files during environment destruction
///
/// Removes the data and build directories for the environment.
/// This is called as part of the destruction workflow.
///
/// # Arguments
///
/// * `env` - The environment being destroyed
///
/// # Errors
///
/// Returns an error if state file cleanup fails
pub(crate) fn cleanup_state_files<S>(
env: &Environment<S>,
) -> Result<(), DestroyCommandHandlerError> {
let data_dir = env.data_dir();
let build_dir = env.build_dir();
// Remove data directory if it exists
if data_dir.exists() {
std::fs::remove_dir_all(data_dir).map_err(|source| {
DestroyCommandHandlerError::StateCleanupFailed {
path: data_dir.clone(),
source,
}
})?;
info!(
command = "destroy",
path = %data_dir.display(),
"Removed state directory"
);
}
// Remove build directory if it exists
if build_dir.exists() {
std::fs::remove_dir_all(build_dir).map_err(|source| {
DestroyCommandHandlerError::StateCleanupFailed {
path: build_dir.clone(),
source,
}
})?;
info!(
command = "destroy",
path = %build_dir.display(),
"Removed build directory"
);
}
Ok(())
}
// Private helper methods
/// Execute the destruction steps with step tracking
///
/// This method executes all destruction steps while tracking which step is currently
/// being executed. If an error occurs, it returns both the error and the step that
/// was being executed, enabling accurate failure context generation.
///
/// # Errors
///
/// Returns a tuple of (error, `current_step`) if any destruction step fails
fn execute_destruction_with_tracking(
environment: &crate::domain::environment::Environment<
crate::domain::environment::Destroying,
>,
opentofu_client: &Arc<crate::adapters::tofu::client::OpenTofuClient>,
) -> StepResult<(), DestroyCommandHandlerError, crate::domain::environment::state::DestroyStep>
{
use crate::domain::environment::state::DestroyStep;
// Step 1: Conditionally destroy infrastructure via OpenTofu
// Only attempt infrastructure destruction if infrastructure was provisioned
if Self::should_destroy_infrastructure(environment) {
info!(
environment = %environment.name(),
"Destroying provisioned infrastructure"
);
Self::destroy_infrastructure(opentofu_client)
.map_err(|e| (e, DestroyStep::DestroyInfrastructure))?;
} else {
info!(
environment = %environment.name(),
"Skipping infrastructure destruction (environment was never provisioned)"
);
}
// Step 2: Clean up state files
Self::cleanup_state_files(environment).map_err(|e| (e, DestroyStep::CleanupStateFiles))?;
Ok(())
}
/// Build structured failure context for destroy command errors
///
/// Creates a comprehensive `DestroyFailureContext` containing all relevant
/// metadata about the failure including step, timing, error classification,
/// and trace file location.
///
/// # Arguments
///
/// * `environment` - The environment being destroyed (for trace directory path)
/// * `error` - The destroy error that occurred
/// * `current_step` - The step that was executing when the error occurred
/// * `started_at` - The timestamp when destruction execution started
///
/// # Returns
///
/// A `DestroyFailureContext` with all failure metadata and trace file path
fn build_failure_context(
&self,
_environment: &crate::domain::environment::Environment<
crate::domain::environment::Destroying,
>,
error: &DestroyCommandHandlerError,
current_step: crate::domain::environment::state::DestroyStep,
started_at: chrono::DateTime<chrono::Utc>,
) -> crate::domain::environment::state::DestroyFailureContext {
use crate::application::command_handlers::common::failure_context::build_base_failure_context;
use crate::domain::environment::state::DestroyFailureContext;
// Step that failed is directly provided - no reverse engineering needed
let failed_step = current_step;
// Get error kind from the error itself (errors are self-describing)
let error_kind = error.error_kind();
// Build base failure context using common helper
let base = build_base_failure_context(&self.clock, started_at, error.to_string());
// Build handler-specific context
// Note: Trace file generation not implemented for destroy yet
DestroyFailureContext {
failed_step,
error_kind,
base,
}
}
/// Destroy the infrastructure using `OpenTofu`
///
/// Executes the `OpenTofu` destroy workflow to remove all managed infrastructure.
///
/// # Arguments
///
/// * `opentofu_client` - The `OpenTofu` client configured with the correct build directory
///
/// # Errors
///
/// Returns an error if `OpenTofu` destroy fails
fn destroy_infrastructure(
opentofu_client: &Arc<crate::adapters::tofu::client::OpenTofuClient>,
) -> Result<(), DestroyCommandHandlerError> {
DestroyInfrastructureStep::new(Arc::clone(opentofu_client)).execute()?;
Ok(())
}
}