bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Rollback system for installer steps (#116)
//!
//! Implements per-step rollback with state restoration following Toyota Way
//! principles (Jidoka - stop and fix problems immediately).
//!
//! # Example
//!
//! ```bash
//! # Execute with automatic rollback on failure
//! bashrs installer run ./my-installer --rollback-on-failure
//!
//! # Manual rollback to specific step
//! bashrs installer rollback ./my-installer --to step-3
//!
//! # View rollback plan
//! bashrs installer rollback ./my-installer --dry-run
//! ```

use crate::models::{Error, Result};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Rollback action types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RollbackAction {
    /// Execute a shell command to undo the step
    Command(String),

    /// Restore a file from backup
    RestoreFile {
        original_path: PathBuf,
        backup_path: PathBuf,
    },

    /// Remove a file that was created
    RemoveFile(PathBuf),

    /// Remove a directory that was created
    RemoveDirectory(PathBuf),

    /// Restore package state (reinstall/remove packages)
    RestorePackages {
        install: Vec<String>,
        remove: Vec<String>,
    },

    /// Restore service state
    RestoreService {
        name: String,
        was_enabled: bool,
        was_running: bool,
    },

    /// Restore user/group membership
    RestoreUserGroup {
        user: String,
        group: String,
        was_member: bool,
    },

    /// No rollback action needed (idempotent step)
    None,
}

impl RollbackAction {
    /// Create a command-based rollback
    pub fn command(cmd: &str) -> Self {
        Self::Command(cmd.to_string())
    }

    /// Create a file restoration rollback
    pub fn restore_file(original: impl AsRef<Path>, backup: impl AsRef<Path>) -> Self {
        Self::RestoreFile {
            original_path: original.as_ref().to_path_buf(),
            backup_path: backup.as_ref().to_path_buf(),
        }
    }

    /// Create a file removal rollback
    pub fn remove_file(path: impl AsRef<Path>) -> Self {
        Self::RemoveFile(path.as_ref().to_path_buf())
    }

    /// Check if this is a no-op rollback
    pub fn is_none(&self) -> bool {
        matches!(self, Self::None)
    }

    /// Get a human-readable description of the rollback action
    pub fn description(&self) -> String {
        match self {
            Self::Command(cmd) => format!("Execute: {}", truncate(cmd, 60)),
            Self::RestoreFile {
                original_path,
                backup_path,
            } => format!(
                "Restore {} from {}",
                original_path.display(),
                backup_path.display()
            ),
            Self::RemoveFile(path) => format!("Remove file: {}", path.display()),
            Self::RemoveDirectory(path) => format!("Remove directory: {}", path.display()),
            Self::RestorePackages { install, remove } => describe_restore_packages(install, remove),
            Self::RestoreService {
                name,
                was_enabled,
                was_running,
            } => describe_restore_service(name, *was_enabled, *was_running),
            Self::RestoreUserGroup {
                user,
                group,
                was_member,
            } => describe_restore_user_group(user, group, *was_member),
            Self::None => "No action required".to_string(),
        }
    }
}

/// Describe package restore action
fn describe_restore_packages(install: &[String], remove: &[String]) -> String {
    let mut parts = Vec::new();
    if !install.is_empty() {
        parts.push(format!("Reinstall: {}", install.join(", ")));
    }
    if !remove.is_empty() {
        parts.push(format!("Remove: {}", remove.join(", ")));
    }
    parts.join("; ")
}

/// Describe service restore action
fn describe_restore_service(name: &str, was_enabled: bool, was_running: bool) -> String {
    let enabled = if was_enabled { "enable" } else { "disable" };
    let running = if was_running { "start" } else { "stop" };
    format!("Service {}: {}, {}", name, enabled, running)
}

/// Describe user/group restore action
fn describe_restore_user_group(user: &str, group: &str, was_member: bool) -> String {
    if was_member {
        format!("Add {} back to group {}", user, group)
    } else {
        format!("Remove {} from group {}", user, group)
    }
}

/// Step rollback plan
#[derive(Debug, Clone)]
pub struct StepRollback {
    /// Step ID this rollback applies to
    pub step_id: String,

    /// Step name for display
    pub step_name: String,

    /// Ordered list of rollback actions (executed in reverse order)
    pub actions: Vec<RollbackAction>,

    /// State files backed up before this step
    pub state_files: Vec<StateFileBackup>,

    /// Whether this step was completed successfully
    pub completed: bool,

    /// Error message if step failed
    pub error: Option<String>,
}

impl StepRollback {
    /// Create a new step rollback plan
    pub fn new(step_id: &str, step_name: &str) -> Self {
        Self {
            step_id: step_id.to_string(),
            step_name: step_name.to_string(),
            actions: Vec::new(),
            state_files: Vec::new(),
            completed: false,
            error: None,
        }
    }

    /// Add a rollback action
    pub fn add_action(&mut self, action: RollbackAction) {
        if !action.is_none() {
            self.actions.push(action);
        }
    }

    /// Add a state file backup
    pub fn add_state_file(&mut self, backup: StateFileBackup) {
        self.state_files.push(backup);
    }

    /// Mark step as completed
    pub fn mark_completed(&mut self) {
        self.completed = true;
    }

    /// Mark step as failed with error
    pub fn mark_failed(&mut self, error: &str) {
        self.completed = false;
        self.error = Some(error.to_string());
    }

    /// Check if rollback is needed
    pub fn needs_rollback(&self) -> bool {
        !self.completed && !self.actions.is_empty()
    }

    /// Get rollback actions in reverse order (LIFO)
    pub fn rollback_actions(&self) -> impl Iterator<Item = &RollbackAction> {
        self.actions.iter().rev()
    }
}

/// Backup of a state file
#[derive(Debug, Clone)]
pub struct StateFileBackup {
    /// Original file path
    pub original_path: PathBuf,

    /// Backup file path
    pub backup_path: PathBuf,

    /// SHA256 hash of original content
    pub content_hash: String,

    /// Whether the file existed before the step
    pub existed: bool,

    /// Timestamp when backup was created
    pub backed_up_at: u64,
}

impl StateFileBackup {
    /// Create a new state file backup record
    pub fn new(
        original: impl AsRef<Path>,
        backup: impl AsRef<Path>,
        content_hash: &str,
        existed: bool,
    ) -> Self {
        Self {
            original_path: original.as_ref().to_path_buf(),
            backup_path: backup.as_ref().to_path_buf(),
            content_hash: content_hash.to_string(),
            existed,
            backed_up_at: current_timestamp(),
        }
    }
}

/// Rollback manager for an installer run
#[derive(Debug)]
pub struct RollbackManager {
    /// Directory to store backups
    backup_dir: PathBuf,

    /// Step rollback plans in execution order
    steps: Vec<StepRollback>,

    /// Index by step ID for quick lookup
    step_index: HashMap<String, usize>,

    /// Whether automatic rollback on failure is enabled
    auto_rollback: bool,
}

impl RollbackManager {
    /// Create a new rollback manager
    pub fn new(backup_dir: impl AsRef<Path>) -> Result<Self> {
        let backup_dir = backup_dir.as_ref().to_path_buf();

        // Create backup directory if it doesn't exist
        if !backup_dir.exists() {
            std::fs::create_dir_all(&backup_dir).map_err(|e| {
                Error::Io(std::io::Error::new(
                    e.kind(),
                    format!("Failed to create backup directory: {}", e),
                ))
            })?;
        }

        Ok(Self {
            backup_dir,
            steps: Vec::new(),
            step_index: HashMap::new(),
            auto_rollback: true,
        })
    }

    /// Enable or disable automatic rollback on failure
    pub fn set_auto_rollback(&mut self, enabled: bool) {
        self.auto_rollback = enabled;
    }

    /// Check if auto rollback is enabled
    pub fn is_auto_rollback(&self) -> bool {
        self.auto_rollback
    }

    /// Get backup directory
    pub fn backup_dir(&self) -> &Path {
        &self.backup_dir
    }

    /// Register a step for rollback tracking
    ///
    /// # Panics
    ///
    /// This function should never panic as it always accesses a valid index.
    #[allow(clippy::expect_used)]
    pub fn register_step(&mut self, step_id: &str, step_name: &str) -> &mut StepRollback {
        let index = self.steps.len();
        self.steps.push(StepRollback::new(step_id, step_name));
        self.step_index.insert(step_id.to_string(), index);
        // SAFETY: We just pushed an element, so the index is valid
        self.steps.get_mut(index).expect("just pushed element")
    }

    /// Get a step rollback plan by ID
    pub fn get_step(&self, step_id: &str) -> Option<&StepRollback> {
        self.step_index
            .get(step_id)
            .and_then(|&idx| self.steps.get(idx))
    }

    /// Get a mutable step rollback plan by ID
    pub fn get_step_mut(&mut self, step_id: &str) -> Option<&mut StepRollback> {
        self.step_index
            .get(step_id)
            .copied()
            .and_then(|idx| self.steps.get_mut(idx))
    }

    /// Backup a file before modification
    pub fn backup_file(
        &mut self,
        step_id: &str,
        path: impl AsRef<Path>,
    ) -> Result<StateFileBackup> {
        let path = path.as_ref();
        let existed = path.exists();

        let backup_name = format!(
            "{}-{}-{}",
            step_id,
            path.file_name().and_then(|n| n.to_str()).unwrap_or("file"),
            current_timestamp()
        );
        let backup_path = self.backup_dir.join(&backup_name);

        let content_hash = if existed {
            // Copy file to backup location
            std::fs::copy(path, &backup_path).map_err(|e| {
                Error::Io(std::io::Error::new(
                    e.kind(),
                    format!("Failed to backup file {}: {}", path.display(), e),
                ))
            })?;

            // Compute hash of original content
            let content = std::fs::read(path).map_err(|e| {
                Error::Io(std::io::Error::new(
                    e.kind(),
                    format!("Failed to read file for hashing: {}", e),
                ))
            })?;
            compute_hash(&content)
        } else {
            "nonexistent".to_string()
        };

        let backup = StateFileBackup::new(path, &backup_path, &content_hash, existed);

        // Add to step's state files
        if let Some(step) = self.get_step_mut(step_id) {
            step.add_state_file(backup.clone());

            // Add restore action
            if existed {
                step.add_action(RollbackAction::restore_file(path, &backup_path));
            } else {
                step.add_action(RollbackAction::remove_file(path));
            }
        }

        Ok(backup)
    }

    /// Get steps that need rollback (in reverse execution order)
    pub fn steps_needing_rollback(&self) -> impl Iterator<Item = &StepRollback> {
        self.steps.iter().rev().filter(|s| s.needs_rollback())
    }

    /// Get all completed steps (in reverse order for rollback)
    pub fn completed_steps_reverse(&self) -> impl Iterator<Item = &StepRollback> {
        self.steps.iter().rev().filter(|s| s.completed)
    }

    /// Generate a rollback plan from a specific step
    pub fn plan_rollback_from(&self, from_step: &str) -> Result<RollbackPlan> {
        let from_idx = self.step_index.get(from_step).ok_or_else(|| {
            Error::Validation(format!(
                "Step '{}' not found in rollback manager",
                from_step
            ))
        })?;

        // Collect steps from the specified step back to the beginning
        let steps_to_rollback: Vec<_> = self
            .steps
            .get(..=*from_idx)
            .unwrap_or(&[])
            .iter()
            .rev()
            .filter(|s| s.completed || s.needs_rollback())
            .cloned()
            .collect();

        Ok(RollbackPlan {
            steps: steps_to_rollback,
            backup_dir: self.backup_dir.clone(),
        })
    }

    /// Generate a rollback plan for all failed steps
    pub fn plan_rollback_failed(&self) -> RollbackPlan {
        let steps_to_rollback: Vec<_> = self
            .steps
            .iter()
            .rev()
            .filter(|s| s.needs_rollback())
            .cloned()
            .collect();

        RollbackPlan {
            steps: steps_to_rollback,
            backup_dir: self.backup_dir.clone(),
        }
    }

    /// Get count of steps
    pub fn step_count(&self) -> usize {
        self.steps.len()
    }

    /// Get count of completed steps
    pub fn completed_count(&self) -> usize {
        self.steps.iter().filter(|s| s.completed).count()
    }

    /// Get count of failed steps
    pub fn failed_count(&self) -> usize {
        self.steps
            .iter()
            .filter(|s| !s.completed && s.error.is_some())
            .count()
    }
}

include!("rollback_rollbackplan.rs");