osvm 0.8.3

OpenSVM CLI tool for managing SVM nodes and deployments
Documentation
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Rollback validation and health verification
//!
//! This module provides comprehensive validation after rollback operations
//! to ensure system integrity and functionality.

use super::{CheckResult, DiagnosticError};
use crate::utils::self_repair::{
    system_deps::SystemDependencyManager, user_deps::UserDependencyManager,
};
use std::time::Instant;

/// Rollback validation result
#[derive(Debug, Clone)]
pub struct RollbackValidationResult {
    pub success: bool,
    pub critical_checks: Vec<CheckResult>,
    pub warning_checks: Vec<CheckResult>,
    pub validation_time_ms: u64,
    pub issues_found: Vec<String>,
    pub recommendations: Vec<String>,
}

/// Rollback validator for comprehensive system health verification
pub struct RollbackValidator {
    system_manager: Option<SystemDependencyManager>,
    user_manager: UserDependencyManager,
}

impl RollbackValidator {
    /// Create a new rollback validator
    pub fn new() -> Self {
        let system_manager = SystemDependencyManager::new().ok();
        let user_manager = UserDependencyManager::new();

        Self {
            system_manager,
            user_manager,
        }
    }

    /// Validate that rollback was successful and system is functional
    pub async fn validate_rollback_success(
        &self,
    ) -> Result<RollbackValidationResult, DiagnosticError> {
        let start_time = Instant::now();
        let mut critical_checks = Vec::new();
        let mut warning_checks = Vec::new();
        let mut issues_found = Vec::new();
        let mut recommendations = Vec::new();

        println!("🔍 Validating rollback success...");

        // Critical system functionality checks
        critical_checks.push(self.check_package_manager_functional().await);
        critical_checks.push(self.check_file_system_integrity().await);
        critical_checks.push(self.check_system_services_running().await);
        critical_checks.push(self.check_basic_commands_working().await);

        // Warning-level checks
        warning_checks.push(self.check_rust_toolchain_functional().await);
        warning_checks.push(self.check_solana_cli_functional().await);
        warning_checks.push(self.check_network_connectivity().await);
        warning_checks.push(self.check_configuration_integrity().await);

        // Analyze results
        let critical_failures: Vec<_> = critical_checks
            .iter()
            .filter(|check| !check.passed)
            .collect();

        let warning_failures: Vec<_> = warning_checks
            .iter()
            .filter(|check| !check.passed)
            .collect();

        // Determine overall success
        let success = critical_failures.is_empty();

        // Collect issues and recommendations
        for check in &critical_failures {
            issues_found.push(format!("CRITICAL: {}", check.message));
            if let Some(details) = &check.details {
                recommendations.push(format!("Fix {}: {}", check.name, details));
            }
        }

        for check in &warning_failures {
            issues_found.push(format!("WARNING: {}", check.message));
        }

        if success {
            if warning_failures.is_empty() {
                println!("✅ Rollback validation passed - System fully functional");
            } else {
                println!(
                    "⚠️  Rollback validation passed with warnings - Core functionality restored"
                );
                recommendations.push("Some non-critical components may need attention".to_string());
            }
        } else {
            println!("❌ Rollback validation failed - Critical system issues detected");
            recommendations.push(
                "Manual intervention may be required to restore full functionality".to_string(),
            );
            recommendations
                .push("Consider restoring from an earlier snapshot if available".to_string());
        }

        let validation_time = start_time.elapsed().as_millis() as u64;

        Ok(RollbackValidationResult {
            success,
            critical_checks,
            warning_checks,
            validation_time_ms: validation_time,
            issues_found,
            recommendations,
        })
    }

    /// Check if package manager is functional
    async fn check_package_manager_functional(&self) -> CheckResult {
        let start = Instant::now();

        if let Some(ref system_manager) = self.system_manager {
            // Try to get package manager info
            let package_info = system_manager.get_package_manager_info();

            // Try to check for updates (non-destructive operation)
            match system_manager.check_system_updates().await {
                Ok(_) => CheckResult {
                    name: "Package Manager".to_string(),
                    passed: true,
                    message: format!("Package manager functional: {}", package_info),
                    details: Some("Successfully queried package database".to_string()),
                    execution_time_ms: start.elapsed().as_millis() as u64,
                },
                Err(e) => CheckResult {
                    name: "Package Manager".to_string(),
                    passed: false,
                    message: "Package manager not functional".to_string(),
                    details: Some(format!("Error: {}", e)),
                    execution_time_ms: start.elapsed().as_millis() as u64,
                },
            }
        } else {
            CheckResult {
                name: "Package Manager".to_string(),
                passed: false,
                message: "Package manager not available".to_string(),
                details: Some("Could not initialize package manager".to_string()),
                execution_time_ms: start.elapsed().as_millis() as u64,
            }
        }
    }

    /// Check file system integrity
    async fn check_file_system_integrity(&self) -> CheckResult {
        let start = Instant::now();
        let mut issues = Vec::new();

        // Check critical directories
        let critical_dirs = ["/tmp", "/var", "/etc"];

        for dir in &critical_dirs {
            if !std::path::Path::new(dir).exists() {
                issues.push(format!("Missing critical directory: {}", dir));
            }
        }

        // Check user directories
        if let Some(home) = dirs::home_dir() {
            let user_dirs = [home.join(".config"), home.join(".local")];

            for dir in &user_dirs {
                if !dir.exists() {
                    // Try to create it
                    if let Err(e) = std::fs::create_dir_all(dir) {
                        issues.push(format!(
                            "Cannot create user directory {}: {}",
                            dir.display(),
                            e
                        ));
                    }
                }
            }
        }

        CheckResult {
            name: "File System Integrity".to_string(),
            passed: issues.is_empty(),
            message: if issues.is_empty() {
                "File system integrity OK".to_string()
            } else {
                format!("File system issues detected: {}", issues.len())
            },
            details: if issues.is_empty() {
                Some("All critical directories present and accessible".to_string())
            } else {
                Some(issues.join("; "))
            },
            execution_time_ms: start.elapsed().as_millis() as u64,
        }
    }

    /// Check that system services are running
    async fn check_system_services_running(&self) -> CheckResult {
        let start = Instant::now();

        // Check if basic system commands work
        let test_commands = [
            ("ps", vec!["--version"]),
            ("ls", vec!["--version"]),
            ("cat", vec!["--version"]),
        ];

        let mut working_commands = 0;
        let total_commands = test_commands.len();

        for (cmd, args) in &test_commands {
            match tokio::process::Command::new(cmd).args(args).output().await {
                Ok(output) if output.status.success() => {
                    working_commands += 1;
                }
                _ => {
                    // Command failed
                }
            }
        }

        let passed = working_commands == total_commands;

        CheckResult {
            name: "System Services".to_string(),
            passed,
            message: if passed {
                "Essential system commands functional".to_string()
            } else {
                format!(
                    "Some system commands not working ({}/{})",
                    working_commands, total_commands
                )
            },
            details: Some(format!(
                "Tested basic commands: ps, ls, cat - {}/{} working",
                working_commands, total_commands
            )),
            execution_time_ms: start.elapsed().as_millis() as u64,
        }
    }

    /// Check that basic commands are working
    async fn check_basic_commands_working(&self) -> CheckResult {
        let start = Instant::now();

        // Test shell and basic command execution
        match tokio::process::Command::new("sh")
            .arg("-c")
            .arg("echo 'system_check' | wc -l")
            .output()
            .await
        {
            Ok(output) if output.status.success() => {
                let output_str = String::from_utf8_lossy(&output.stdout);
                if output_str.trim() == "1" {
                    CheckResult {
                        name: "Basic Commands".to_string(),
                        passed: true,
                        message: "Shell and basic commands working".to_string(),
                        details: Some("Successfully executed shell pipeline".to_string()),
                        execution_time_ms: start.elapsed().as_millis() as u64,
                    }
                } else {
                    CheckResult {
                        name: "Basic Commands".to_string(),
                        passed: false,
                        message: "Shell pipeline returned unexpected result".to_string(),
                        details: Some(format!("Expected '1', got '{}'", output_str.trim())),
                        execution_time_ms: start.elapsed().as_millis() as u64,
                    }
                }
            }
            Ok(output) => CheckResult {
                name: "Basic Commands".to_string(),
                passed: false,
                message: "Shell command failed".to_string(),
                details: Some(format!(
                    "Exit code: {}, stderr: {}",
                    output.status.code().unwrap_or(-1),
                    String::from_utf8_lossy(&output.stderr)
                )),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
            Err(e) => CheckResult {
                name: "Basic Commands".to_string(),
                passed: false,
                message: "Could not execute shell command".to_string(),
                details: Some(format!("Error: {}", e)),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
        }
    }

    /// Check Rust toolchain functionality
    async fn check_rust_toolchain_functional(&self) -> CheckResult {
        let start = Instant::now();

        if let Some(ref system_manager) = self.system_manager {
            match system_manager.is_rust_installed().await {
                Ok(true) => {
                    // Test if Rust can compile a simple program
                    match self.test_rust_compilation().await {
                        Ok(true) => CheckResult {
                            name: "Rust Toolchain".to_string(),
                            passed: true,
                            message: "Rust toolchain functional".to_string(),
                            details: Some("Successfully compiled test program".to_string()),
                            execution_time_ms: start.elapsed().as_millis() as u64,
                        },
                        Ok(false) => CheckResult {
                            name: "Rust Toolchain".to_string(),
                            passed: false,
                            message: "Rust compilation test failed".to_string(),
                            details: Some(
                                "Rust is installed but cannot compile programs".to_string(),
                            ),
                            execution_time_ms: start.elapsed().as_millis() as u64,
                        },
                        Err(e) => CheckResult {
                            name: "Rust Toolchain".to_string(),
                            passed: false,
                            message: "Could not test Rust compilation".to_string(),
                            details: Some(format!("Error: {}", e)),
                            execution_time_ms: start.elapsed().as_millis() as u64,
                        },
                    }
                }
                Ok(false) => CheckResult {
                    name: "Rust Toolchain".to_string(),
                    passed: false,
                    message: "Rust is not installed".to_string(),
                    details: Some("Rust toolchain not found".to_string()),
                    execution_time_ms: start.elapsed().as_millis() as u64,
                },
                Err(e) => CheckResult {
                    name: "Rust Toolchain".to_string(),
                    passed: false,
                    message: "Could not check Rust installation".to_string(),
                    details: Some(format!("Error: {}", e)),
                    execution_time_ms: start.elapsed().as_millis() as u64,
                },
            }
        } else {
            CheckResult {
                name: "Rust Toolchain".to_string(),
                passed: false,
                message: "System manager not available".to_string(),
                details: Some("Cannot check Rust without system manager".to_string()),
                execution_time_ms: start.elapsed().as_millis() as u64,
            }
        }
    }

    /// Test Rust compilation
    async fn test_rust_compilation(
        &self,
    ) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
        // Create a simple test program
        let test_program = r#"fn main() { println!("test"); }"#;
        let temp_file = "/tmp/osvm_rust_test.rs";

        // Write test program
        tokio::fs::write(temp_file, test_program).await?;

        // Try to compile it
        let output = tokio::process::Command::new("rustc")
            .arg(temp_file)
            .arg("-o")
            .arg("/tmp/osvm_rust_test")
            .output()
            .await?;

        // Clean up
        let _ = tokio::fs::remove_file(temp_file).await;
        let _ = tokio::fs::remove_file("/tmp/osvm_rust_test").await;

        Ok(output.status.success())
    }

    /// Check Solana CLI functionality
    async fn check_solana_cli_functional(&self) -> CheckResult {
        let start = Instant::now();

        match self.user_manager.is_solana_cli_installed().await {
            Ok(true) => {
                // Test if Solana CLI responds to commands
                match tokio::process::Command::new("solana")
                    .arg("--version")
                    .output()
                    .await
                {
                    Ok(output) if output.status.success() => {
                        let version = String::from_utf8_lossy(&output.stdout);
                        CheckResult {
                            name: "Solana CLI".to_string(),
                            passed: true,
                            message: "Solana CLI functional".to_string(),
                            details: Some(format!("Version: {}", version.trim())),
                            execution_time_ms: start.elapsed().as_millis() as u64,
                        }
                    }
                    Ok(output) => CheckResult {
                        name: "Solana CLI".to_string(),
                        passed: false,
                        message: "Solana CLI not responding".to_string(),
                        details: Some(format!(
                            "Error: {}",
                            String::from_utf8_lossy(&output.stderr)
                        )),
                        execution_time_ms: start.elapsed().as_millis() as u64,
                    },
                    Err(e) => CheckResult {
                        name: "Solana CLI".to_string(),
                        passed: false,
                        message: "Could not execute Solana CLI".to_string(),
                        details: Some(format!("Error: {}", e)),
                        execution_time_ms: start.elapsed().as_millis() as u64,
                    },
                }
            }
            Ok(false) => CheckResult {
                name: "Solana CLI".to_string(),
                passed: false,
                message: "Solana CLI is not installed".to_string(),
                details: Some("Solana CLI not found in PATH".to_string()),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
            Err(e) => CheckResult {
                name: "Solana CLI".to_string(),
                passed: false,
                message: "Could not check Solana CLI".to_string(),
                details: Some(format!("Error: {}", e)),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
        }
    }

    /// Check network connectivity
    async fn check_network_connectivity(&self) -> CheckResult {
        let start = Instant::now();

        // Simple connectivity test using ping
        match tokio::process::Command::new("ping")
            .arg("-c")
            .arg("1")
            .arg("-W")
            .arg("5")
            .arg("8.8.8.8")
            .output()
            .await
        {
            Ok(output) if output.status.success() => CheckResult {
                name: "Network Connectivity".to_string(),
                passed: true,
                message: "Network connectivity OK".to_string(),
                details: Some("Successfully pinged 8.8.8.8".to_string()),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
            Ok(_) => CheckResult {
                name: "Network Connectivity".to_string(),
                passed: false,
                message: "Network connectivity failed".to_string(),
                details: Some("Could not ping 8.8.8.8".to_string()),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
            Err(e) => CheckResult {
                name: "Network Connectivity".to_string(),
                passed: false,
                message: "Could not test network connectivity".to_string(),
                details: Some(format!("Error: {}", e)),
                execution_time_ms: start.elapsed().as_millis() as u64,
            },
        }
    }

    /// Check configuration integrity
    async fn check_configuration_integrity(&self) -> CheckResult {
        let start = Instant::now();

        let config_dir = self.user_manager.get_solana_config_dir();
        let osvm_config_dir = self.user_manager.get_osvm_config_dir();

        let mut issues = Vec::new();

        // Check if configuration directories are accessible
        if config_dir.exists() && !config_dir.is_dir() {
            issues.push("Solana config path exists but is not a directory".to_string());
        }

        if osvm_config_dir.exists() && !osvm_config_dir.is_dir() {
            issues.push("OSVM config path exists but is not a directory".to_string());
        }

        // Test write permissions
        let test_file = osvm_config_dir.join("write_test.tmp");
        if let Err(e) = std::fs::create_dir_all(&osvm_config_dir) {
            issues.push(format!("Cannot create OSVM config directory: {}", e));
        } else if let Err(e) = std::fs::write(&test_file, "test") {
            issues.push(format!("Cannot write to OSVM config directory: {}", e));
        } else {
            // Clean up test file
            let _ = std::fs::remove_file(&test_file);
        }

        CheckResult {
            name: "Configuration Integrity".to_string(),
            passed: issues.is_empty(),
            message: if issues.is_empty() {
                "Configuration integrity OK".to_string()
            } else {
                format!("Configuration issues detected: {}", issues.len())
            },
            details: if issues.is_empty() {
                Some("All configuration directories accessible".to_string())
            } else {
                Some(issues.join("; "))
            },
            execution_time_ms: start.elapsed().as_millis() as u64,
        }
    }
}

impl Default for RollbackValidator {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience function for direct rollback validation
pub async fn validate_rollback_success() -> Result<(), DiagnosticError> {
    let validator = RollbackValidator::new();
    let result = validator.validate_rollback_success().await?;

    if result.success {
        Ok(())
    } else {
        Err(DiagnosticError::ValidationFailed(format!(
            "Rollback validation failed: {:?}",
            result.issues_found
        )))
    }
}

/// Display rollback validation results
pub fn display_rollback_results(result: &RollbackValidationResult) {
    println!("\n🔍 ROLLBACK VALIDATION RESULTS");
    println!("==============================");

    if result.success {
        println!("✅ Overall Status: PASSED");
    } else {
        println!("❌ Overall Status: FAILED");
    }

    println!("⏱️  Validation Time: {}ms", result.validation_time_ms);

    if !result.critical_checks.is_empty() {
        println!("\n🔧 CRITICAL CHECKS:");
        for check in &result.critical_checks {
            let status = if check.passed { "" } else { "" };
            println!("  {} {}: {}", status, check.name, check.message);
        }
    }

    if !result.warning_checks.is_empty() {
        println!("\n⚠️  WARNING CHECKS:");
        for check in &result.warning_checks {
            let status = if check.passed { "" } else { "⚠️ " };
            println!("  {} {}: {}", status, check.name, check.message);
        }
    }

    if !result.issues_found.is_empty() {
        println!("\n🚨 ISSUES FOUND:");
        for issue in &result.issues_found {
            println!("{}", issue);
        }
    }

    if !result.recommendations.is_empty() {
        println!("\n💡 RECOMMENDATIONS:");
        for recommendation in &result.recommendations {
            println!("{}", recommendation);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rollback_validator_creation() {
        let validator = RollbackValidator::new();
        // Should not panic during creation
    }

    #[tokio::test]
    async fn test_basic_commands_check() {
        let validator = RollbackValidator::new();
        let result = validator.check_basic_commands_working().await;

        // This should generally pass on most systems
        println!("Basic commands check: {}", result.message);
    }

    #[tokio::test]
    async fn test_file_system_integrity_check() {
        let validator = RollbackValidator::new();
        let result = validator.check_file_system_integrity().await;

        // This should generally pass on healthy systems
        println!("File system integrity: {}", result.message);
    }

    #[test]
    fn test_rollback_validation_result() {
        let result = RollbackValidationResult {
            success: true,
            critical_checks: Vec::new(),
            warning_checks: Vec::new(),
            validation_time_ms: 100,
            issues_found: Vec::new(),
            recommendations: Vec::new(),
        };

        assert!(result.success);
        assert_eq!(result.validation_time_ms, 100);
    }
}