Skip to main content

client_core/
upgrade.rs

1use crate::{
2    api::ApiClient,
3    config::AppConfig,
4    database::Database,
5    upgrade_strategy::{UpgradeStrategy, UpgradeStrategyManager},
6};
7use anyhow::Result;
8use std::{path::PathBuf, sync::Arc};
9use tracing::{debug, info};
10
11/// 升级管理器
12#[derive(Debug, Clone)]
13pub struct UpgradeManager {
14    config: Arc<AppConfig>,
15    #[allow(dead_code)]
16    config_path: PathBuf,
17    api_client: Arc<ApiClient>,
18    #[allow(dead_code)]
19    database: Arc<Database>,
20}
21
22/// 升级选项
23#[derive(Debug, Clone, Default)]
24pub struct UpgradeOptions {
25    pub skip_backup: bool,
26    pub force: bool,
27    pub use_incremental: bool,
28    pub backup_dir: Option<PathBuf>,
29    pub download_only: bool,
30}
31
32pub type ProgressCallback = Box<dyn Fn(UpgradeStep, &str) + Send + Sync>;
33
34#[derive(Debug, Clone)]
35pub enum UpgradeStep {
36    CheckingUpdates,
37    CreatingBackup,
38    StoppingServices,
39    DownloadingUpdate,
40    ExtractingUpdate,
41    LoadingImages,
42    StartingServices,
43    VerifyingServices,
44    CleaningUp,
45    Completed,
46    Failed(String),
47}
48
49#[derive(Debug)]
50pub struct UpgradeResult {
51    pub success: bool,
52    pub from_version: String,
53    pub to_version: String,
54    pub error: Option<String>,
55    pub backup_id: Option<i64>,
56}
57
58impl UpgradeManager {
59    pub fn new(
60        config: Arc<AppConfig>,
61        config_path: PathBuf,
62        api_client: Arc<ApiClient>,
63        database: Arc<Database>,
64    ) -> Self {
65        Self {
66            config,
67            config_path,
68            api_client,
69            database,
70        }
71    }
72
73    /// 检查docker应用升级策略
74    pub async fn check_for_updates(&self, force_full: bool) -> Result<UpgradeStrategy> {
75        info!("Checking for service updates...");
76        let current_version = &self.config.get_docker_versions();
77        debug!("Current version: {}", current_version);
78        let enhanced_service_manifest = self.api_client.get_enhanced_service_manifest().await?;
79
80        let upgrade_strategy_manager = UpgradeStrategyManager::new(
81            current_version.to_string(),
82            force_full,
83            enhanced_service_manifest,
84        );
85        let upgrade_strategy: UpgradeStrategy = upgrade_strategy_manager.determine_strategy()?;
86
87        Ok(upgrade_strategy)
88    }
89}