nuwax-cli 1.0.123

Docker service management and upgrade CLI
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
use crate::docker_service::{DockerServiceError, DockerServiceResult};
use bollard::Docker;
use bollard::models::HealthStatusEnum;
use bollard::query_parameters::{InspectContainerOptions, ListContainersOptions};
use client_core::constants::timeout;
use client_core::container::DockerManager;
use rust_i18n::t;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::time::Duration;
use std::{collections::HashSet, sync::Arc};
use tracing::{debug, error, info, warn};

/// Docker容器重启策略
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RestartPolicy {
    /// 不自动重启 (restart: no)
    No,
    /// 总是重启 (restart: always)
    Always,
    /// 除非手动停止否则重启 (restart: unless-stopped)
    UnlessStopped,
    /// 失败时重启 (restart: on-failure)
    OnFailure,
    /// 失败时重启,最大重试次数 (restart: on-failure:3)
    OnFailureWithRetries(u32),
}

impl FromStr for RestartPolicy {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        RestartPolicy::parse(s).ok_or_else(|| anyhow::anyhow!("Invalid restart policy: {}", s))
    }
}

impl std::fmt::Display for RestartPolicy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::No => "no",
            Self::Always => "always",
            Self::UnlessStopped => "unless-stopped",
            Self::OnFailure => "on-failure",
            Self::OnFailureWithRetries(retries) => return write!(f, "on-failure:{retries}"),
        };
        write!(f, "{}", s)
    }
}

impl RestartPolicy {
    /// 从字符串解析重启策略
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "no" | "false" => Some(Self::No),
            "always" => Some(Self::Always),
            "unless-stopped" => Some(Self::UnlessStopped),
            "on-failure" => Some(Self::OnFailure),
            s if s.starts_with("on-failure:") => {
                if let Ok(retries) = s[11..].parse::<u32>() {
                    Some(Self::OnFailureWithRetries(retries))
                } else {
                    Some(Self::OnFailure)
                }
            }
            _ => None,
        }
    }

    /// 转换为字符串
    pub fn as_str(&self) -> String {
        match self {
            Self::No => "no".to_string(),
            Self::Always => "always".to_string(),
            Self::UnlessStopped => "unless-stopped".to_string(),
            Self::OnFailure => "on-failure".to_string(),
            Self::OnFailureWithRetries(retries) => format!("on-failure:{retries}"),
        }
    }

    /// 判断是否为一次性任务
    pub fn is_oneshot(&self) -> bool {
        matches!(self, Self::No)
    }

    /// 判断是否应该持续运行
    pub fn should_keep_running(&self) -> bool {
        matches!(
            self,
            Self::Always | Self::UnlessStopped | Self::OnFailure | Self::OnFailureWithRetries(_)
        )
    }

    /// 获取显示名称
    pub fn display_name(&self) -> String {
        match self {
            Self::No => t!("restart_policy.no"),
            Self::Always => t!("restart_policy.always"),
            Self::UnlessStopped => t!("restart_policy.unless_stopped"),
            Self::OnFailure => t!("restart_policy.on_failure"),
            Self::OnFailureWithRetries(_) => t!("restart_policy.on_failure_n"),
        }
        .to_string()
    }
}

/// Docker Compose 容器标签信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComposeLabels {
    /// 项目名称
    pub project: Option<String>,
    /// 服务名称
    pub service: Option<String>,
    /// 容器编号
    pub container_number: Option<String>,
    /// 是否为一次性任务
    pub oneoff: Option<bool>,
    /// 配置文件路径
    pub config_files: Option<String>,
    /// 工作目录
    pub working_dir: Option<String>,
}

/// 容器状态
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ContainerStatus {
    /// 运行中
    Running,
    /// 已停止
    Stopped,
    /// 正在启动
    Starting,
    /// 已完成 (一次性任务成功退出)
    Completed,
    /// 未知状态
    Unknown,
}

impl ContainerStatus {
    /// 从ducker的容器状态和退出码解析状态
    pub fn from_ducker_status(running: bool, status: &str, is_oneshot: bool) -> Self {
        if running {
            ContainerStatus::Running
        } else if status.to_lowercase().contains("exited") {
            if is_oneshot {
                // 一次性任务:检查退出码
                if status.contains("(0)") {
                    ContainerStatus::Completed // 成功完成
                } else {
                    ContainerStatus::Stopped // 失败退出
                }
            } else {
                ContainerStatus::Stopped // 持续服务退出都视为异常
            }
        } else if status.to_lowercase().contains("restarting")
            || status.to_lowercase().contains("created")
        {
            ContainerStatus::Starting
        } else {
            ContainerStatus::Unknown
        }
    }

    /// 获取状态的显示名称
    pub fn display_name(&self) -> String {
        match self {
            ContainerStatus::Running => t!("container_status.running").to_string(),
            ContainerStatus::Stopped => t!("container_status.stopped").to_string(),
            ContainerStatus::Starting => t!("container_status.starting").to_string(),
            ContainerStatus::Completed => t!("container_status.completed").to_string(),
            ContainerStatus::Unknown => t!("container_status.unknown").to_string(),
        }
    }
    /// 判断是否运行中
    pub fn is_running(&self) -> bool {
        matches!(self, ContainerStatus::Running)
    }

    /// 判断状态是否健康(运行中或已完成都算健康)
    pub fn is_healthy(&self) -> bool {
        matches!(self, ContainerStatus::Running | ContainerStatus::Completed)
    }

    /// 判断状态是否为过渡状态(需要继续等待)
    pub fn is_transitioning(&self) -> bool {
        matches!(self, ContainerStatus::Starting)
    }

    /// 判断状态是否为失败状态
    pub fn is_failed(&self) -> bool {
        matches!(self, ContainerStatus::Stopped | ContainerStatus::Unknown)
    }
}

/// 容器信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContainerInfo {
    /// 容器名称
    pub name: String,
    /// 容器状态
    pub status: ContainerStatus,
    /// 镜像名称
    pub image: String,
    /// 端口映射
    pub ports: Vec<String>,
    /// 启动时间
    pub uptime: Option<String>,
    /// 健康检查状态
    pub health: Option<HealthStatusEnum>,
    /// 是否为一次性任务
    pub is_oneshot: bool,
    /// 重启策略
    pub restart: Option<RestartPolicy>,
}

impl ContainerInfo {
    /// 判断是否为一次性任务
    /// 仅基于restart策略进行判断,不使用名称匹配
    pub fn is_oneshot(&self) -> bool {
        match &self.restart {
            Some(policy) => policy.is_oneshot(),
            None => {
                // 如果没有restart信息,默认不是一次性任务
                // 这样更安全,避免误判持续服务为一次性任务
                false
            }
        }
    }

    /// 判断是否为持续服务(需要一直运行)
    /// 仅基于restart策略进行判断,不使用名称匹配
    pub fn is_persistent_service(&self) -> bool {
        match &self.restart {
            Some(policy) => policy.should_keep_running(),
            None => {
                // 如果没有restart信息,默认认为是持续服务
                // 这样更安全,避免误判持续服务导致备份时出现问题
                true
            }
        }
    }

    /// 获取restart策略的显示字符串
    pub fn get_restart_display(&self) -> String {
        match &self.restart {
            Some(policy) => policy.as_str(),
            None => t!("restart_policy.unknown").to_string(),
        }
    }
}

/// 服务整体状态
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ServiceStatus {
    /// 所有服务都在运行
    AllRunning,
    /// 部分服务在运行
    PartiallyRunning,
    /// 所有服务都已停止
    AllStopped,
    /// 服务正在启动中
    Starting,
    /// 服务状态未知
    Unknown,
    /// 没有发现容器
    NoContainer,
}

impl ServiceStatus {
    /// 获取状态的显示名称
    pub fn display_name(&self) -> String {
        match self {
            ServiceStatus::AllRunning => t!("service_status.all_running").to_string(),
            ServiceStatus::PartiallyRunning => t!("service_status.partially_running").to_string(),
            ServiceStatus::AllStopped => t!("service_status.all_stopped").to_string(),
            ServiceStatus::Starting => t!("service_status.starting").to_string(),
            ServiceStatus::Unknown => t!("service_status.unknown").to_string(),
            ServiceStatus::NoContainer => t!("service_status.no_container").to_string(),
        }
    }

    /// 判断状态是否健康
    pub fn is_healthy(&self) -> bool {
        matches!(self, ServiceStatus::AllRunning)
    }
}

/// 健康检查报告
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    /// 容器详细信息
    pub containers: Vec<ContainerInfo>,
    /// 运行中的容器数量
    running_count: usize,
    /// 已完成的容器数量 (一次性任务)
    one_shot_count: usize,
    /// 总容器数量
    total_count: usize,
    /// 检查时间
    pub check_time: chrono::DateTime<chrono::Utc>,
    /// 错误信息
    pub errors: Vec<String>,
}

impl HealthReport {
    /// 添加容器信息
    pub fn add_container(&mut self, container: ContainerInfo) {
        self.containers.push(container);
    }

    /// 添加错误信息
    pub fn add_error(&mut self, error: String) {
        self.errors.push(error);
    }

    /// 完成报告并计算整体状态
    pub fn finalize(&self) -> ServiceStatus {
        let healthy_count = self.get_healthy_count();
        let total_count = self.get_total_count();
        let one_shot_count = self.get_one_shot_count();
        let running_count = self.get_running_count();

        if total_count == 0 {
            ServiceStatus::NoContainer
        } else if (healthy_count + one_shot_count) == total_count {
            ServiceStatus::AllRunning
        } else if running_count == 0 {
            ServiceStatus::AllStopped
        } else {
            // 检查是否有正在启动的容器
            let has_starting = self.containers.iter().any(|c| c.status.is_transitioning());
            if has_starting {
                ServiceStatus::Starting
            } else {
                ServiceStatus::PartiallyRunning
            }
        }
    }

    /// 获取运行中的容器列表
    pub fn get_running_containers(&self) -> Vec<&ContainerInfo> {
        self.containers
            .iter()
            .filter(|c| matches!(c.status, ContainerStatus::Running))
            .collect()
    }

    /// 获取已完成的容器列表
    pub fn get_completed_containers(&self) -> Vec<&ContainerInfo> {
        self.containers
            .iter()
            .filter(|c| matches!(c.status, ContainerStatus::Completed))
            .collect()
    }

    /// 获取失败的容器列表
    pub fn get_failed_containers(&self) -> Vec<&ContainerInfo> {
        self.containers
            .iter()
            .filter(|c| c.status.is_failed())
            .collect()
    }

    /// 获取运行中的容器数量 ,不保证一次性的初始化容器
    pub fn get_running_count(&self) -> usize {
        self.containers
            .iter()
            .filter(|c| c.status.is_running())
            .count()
    }

    /// 获取总容器数
    pub fn get_total_count(&self) -> usize {
        self.containers.len()
    }

    /// 获取正在启动的容器列表
    pub fn get_starting_containers(&self) -> Vec<&ContainerInfo> {
        self.containers
            .iter()
            .filter(|c| c.status.is_transitioning())
            .collect()
    }

    /// 获取一次性容器数量
    pub fn get_one_shot_count(&self) -> usize {
        self.containers.iter().filter(|c| c.is_oneshot()).count()
    }

    /// 获取健康容器总数
    pub fn get_healthy_count(&self) -> usize {
        self.containers
            .iter()
            .filter_map(|c| c.health)
            .filter(|&c| c == HealthStatusEnum::HEALTHY)
            .count()
    }

    /// 获取失败容器名称列表
    pub fn get_failed_container_names(&self) -> Vec<String> {
        self.get_failed_containers()
            .iter()
            .map(|c| c.name.clone())
            .collect()
    }

    /// 获取状态摘要字符串
    pub fn get_status_summary(&self) -> String {
        let failed_containers = self.get_failed_container_names();
        let starting_containers: Vec<String> = self
            .get_starting_containers()
            .iter()
            .map(|c| c.name.clone())
            .collect();

        let mut summary = format!(
            "📊 [Healthy: {}/{}] ✅ Running: {} | ✔️ One-shot (init): {} | ❌ Failed: {} | ⏳ Starting: {}",
            self.get_healthy_count(),
            self.get_total_count(),
            self.get_running_count(),
            self.get_one_shot_count(),
            failed_containers.len(),
            starting_containers.len()
        );

        if !failed_containers.is_empty() {
            summary.push_str(&format!(
                " | Failed containers: {}",
                failed_containers.join(", ")
            ));
        }

        if !starting_containers.is_empty() {
            summary.push_str(&format!(" | Starting: {}", starting_containers.join(", ")));
        }

        summary
    }

    /// 检查是否所有服务都健康
    pub fn is_all_healthy(&self) -> bool {
        let healthy_count = self.get_healthy_count();
        let one_shot_count = self.get_one_shot_count();
        let total_count = self.get_total_count();
        healthy_count > 0 && healthy_count == total_count - one_shot_count
    }

    /// 获取所有健康容器(运行中 + 已完成)
    pub fn healthy_containers(&self) -> Vec<&ContainerInfo> {
        self.containers
            .iter()
            .filter(|c| c.status.is_healthy())
            .collect()
    }

    /// 获取总容器数
    pub fn total_containers(&self) -> usize {
        self.containers.len()
    }

    /// 获取失败容器列表(别名,兼容性)
    pub fn failed_containers(&self) -> Vec<&ContainerInfo> {
        self.get_failed_containers()
    }
}

impl Default for HealthReport {
    fn default() -> Self {
        Self {
            containers: Vec::new(),
            running_count: 0,
            one_shot_count: 0,
            total_count: 0,
            check_time: chrono::Utc::now(),
            errors: Vec::new(),
        }
    }
}

/// 健康检查器
pub struct HealthChecker {
    docker_manager: Arc<DockerManager>,
}

impl HealthChecker {
    /// 创建新的健康检查器
    pub fn new(docker_manager: Arc<DockerManager>) -> Self {
        Self { docker_manager }
    }

    /// 获取服务的restart策略
    async fn get_restart_policy(&self, service_name: &str) -> Option<RestartPolicy> {
        if let Ok(service_config) = self.docker_manager.parse_service_config(service_name).await
            && let Some(restart_str) = service_config.restart
        {
            return RestartPolicy::parse(&restart_str);
        }
        None
    }

    /// 执行健康检查
    /// 使用基于Docker Compose标签的精确匹配
    pub async fn health_check(&self) -> DockerServiceResult<HealthReport> {
        info!("🏥 Starting health check...");

        // 获取 docker-compose 项目信息
        let compose_project_name = self.docker_manager.get_compose_project_name();
        let compose_file_path = self
            .docker_manager
            .get_compose_file()
            .to_string_lossy()
            .to_string();

        info!("📋 Docker Compose Project Info:");
        info!("   - Project name: {name}", name = compose_project_name);
        info!("   - Config file: {path}", path = compose_file_path);

        // 创建健康检查报告
        let mut report = HealthReport::default();

        // 获取compose文件中定义的所有服务
        let compose_services = self
            .docker_manager
            .get_compose_service_names()
            .await
            .unwrap_or_else(|e| {
                error!(
                    "Failed to get compose services: {error}",
                    error = e.to_string()
                );
                HashSet::new()
            });

        if compose_services.is_empty() {
            warn!("⚠️ No services defined in compose file");
            return Ok(report);
        }

        info!(
            "🔍 Services defined in compose file: {services}",
            services = format!("{:?}", compose_services)
        );

        // 获取系统中所有容器
        let all_containers = self
            .docker_manager
            .get_all_containers_status()
            .await
            .unwrap_or_else(|e| {
                error!(
                    "Failed to get container status: {error}",
                    error = e.to_string()
                );
                Vec::new()
            });

        info!(
            "📊 Found {count} containers in system",
            count = all_containers.len()
        );

        // 🔧 使用标签精确匹配容器
        let mut found_services = HashSet::new();
        let mut added_containers = HashSet::new();

        // 第一轮:处理正在运行的和已停止的容器
        for service in &all_containers {
            // 🆕 使用标签精确匹配
            if let Some(service_name) = self.get_container_service_name(&service.name).await {
                // 验证是否属于当前项目
                if self
                    .is_container_from_compose_project(
                        &service.name,
                        &compose_project_name,
                        &compose_file_path,
                    )
                    .await
                {
                    // 检查是否在compose文件中定义
                    if compose_services.contains(&service_name) {
                        info!(
                            "✅ Matched compose service: {container} -> {service}",
                            container = service.name,
                            service = service_name
                        );

                        // 🔧 防重复:检查是否已经添加过这个compose服务
                        if added_containers.contains(&service_name) {
                            warn!(
                                "⚠️ Skipping duplicate compose service: {service} (container: {container})",
                                service = service_name,
                                container = service.name
                            );
                            continue;
                        }

                        found_services.insert(service_name.clone());
                        added_containers.insert(service_name.clone());

                        // 检查是否为一次性服务
                        let is_oneshot = self.is_oneshot_service(&service_name).await;

                        // 获取restart策略
                        let restart_policy = self.get_restart_policy(&service_name).await;

                        // 使用增强的状态解析逻辑
                        let status = self.determine_container_status(service, is_oneshot);

                        // 获取容器的健康检查状态
                        let health = self.get_container_health_status(&service.name).await;

                        let container = ContainerInfo {
                            name: service_name.clone(), // 使用compose中定义的服务名
                            status,
                            image: service.image.clone(),
                            ports: service.ports.clone(),
                            uptime: None,
                            health,
                            is_oneshot,
                            restart: restart_policy,
                        };

                        debug!(
                            "📦 Added container: {} (status: {:?}, oneshot: {})",
                            container.name, container.status, is_oneshot
                        );
                        report.add_container(container);
                    } else {
                        // 不在compose文件中定义的容器(可能是历史遗留)
                        warn!(
                            "⏭️ Skipping non-project container: {container} (service: {service})",
                            container = service.name,
                            service = service_name
                        );
                    }
                } else {
                    // 不属于当前项目的容器
                    debug!(
                        "⏭️ Skipping other project container: {container} (project: other)",
                        container = service.name
                    );
                }
            } else {
                // 无法获取服务名称,可能不是compose容器
                debug!(
                    "⏭️ Skipping non-compose container: {container} (no label info)",
                    container = service.name
                );
            }
        }

        info!(
            "📊 Round 1 complete: added {count} containers",
            count = added_containers.len()
        );

        // 为未找到的compose服务创建"已停止"状态的条目
        for service_name in &compose_services {
            if !found_services.contains(service_name) {
                // 🔧 防重复:再次检查是否已经添加过
                if added_containers.contains(service_name) {
                    warn!(
                        "⚠️ Skipping duplicate stopped service: {service}",
                        service = service_name
                    );
                    continue;
                }

                let is_oneshot = self.is_oneshot_service(service_name).await;

                // 获取restart策略
                let restart_policy = self.get_restart_policy(service_name).await;

                let status = if is_oneshot {
                    // 一次性服务未运行通常表示已完成
                    ContainerStatus::Completed
                } else {
                    // 持续服务未运行表示已停止
                    ContainerStatus::Stopped
                };

                let container = ContainerInfo {
                    name: service_name.clone(),
                    status,
                    image: t!("health_check.not_started_label").to_string(),
                    ports: Vec::new(),
                    uptime: None,
                    health: None,
                    is_oneshot,
                    restart: restart_policy,
                };

                info!(
                    "📦 Adding stopped service: {name} (status: {status}, oneshot: {oneshot})",
                    name = container.name,
                    status = format!("{:?}", container.status),
                    oneshot = is_oneshot
                );
                report.add_container(container);
                added_containers.insert(service_name.clone());
            }
        }

        info!(
            "📊 Final stats: compose services={compose}, added containers={containers}",
            compose = compose_services.len(),
            containers = added_containers.len()
        );

        // 生成健康检查摘要
        let summary = format!(
            "{}: {}/{}",
            t!("health_check.complete"),
            report.get_healthy_count(),
            report.get_total_count()
        );

        info!("🎯 {summary}", summary = summary);

        Ok(report)
    }

    /// 智能判断容器状态
    fn determine_container_status(
        &self,
        service: &client_core::container::ServiceInfo,
        is_oneshot: bool,
    ) -> ContainerStatus {
        match service.status {
            client_core::container::ServiceStatus::Running => ContainerStatus::Running,
            client_core::container::ServiceStatus::Stopped => {
                if is_oneshot {
                    // 一次性任务停止通常表示已完成
                    ContainerStatus::Completed
                } else {
                    ContainerStatus::Stopped
                }
            }
            client_core::container::ServiceStatus::Unknown => ContainerStatus::Unknown,
            client_core::container::ServiceStatus::Created => ContainerStatus::Unknown,
            client_core::container::ServiceStatus::Restarting => ContainerStatus::Starting,
        }
    }

    /// 检查服务是否为一次性任务 - 增强版
    async fn is_oneshot_service(&self, service_name: &str) -> bool {
        // 1. 尝试从docker-compose.yml文件解析restart策略
        if let Ok(service_config) = self.docker_manager.parse_service_config(service_name).await
            && let Some(restart_policy) = service_config.restart
        {
            // restart: "no" 表示不自动重启,通常是一次性任务
            if restart_policy == "no" || restart_policy == "false" {
                info!(
                    "Service {service} restart policy: {policy} (oneshot)",
                    service = service_name,
                    policy = restart_policy
                );
                return true;
            }
            // restart: "always" 或 "unless-stopped" 表示应该一直运行
            if restart_policy == "always"
                || restart_policy == "unless-stopped"
                || restart_policy == "on-failure"
            {
                info!(
                    "Service {service} restart policy: {policy} (persistent)",
                    service = service_name,
                    policy = restart_policy
                );
                return false;
            }
        }

        false
    }

    /// 获取容器的Docker Compose标签信息
    /// 使用bollard库直接从Docker API获取容器标签信息
    async fn get_container_labels(&self, container_name: &str) -> Option<ComposeLabels> {
        match Docker::connect_with_socket_defaults() {
            Ok(docker) => {
                // 获取容器列表,查找指定容器
                match docker
                    .list_containers(Some(ListContainersOptions {
                        all: true,
                        ..Default::default()
                    }))
                    .await
                {
                    Ok(containers) => {
                        for container in containers {
                            // 检查容器名称是否匹配
                            if let Some(names) = &container.names {
                                let container_matches = names.iter().any(|name| {
                                    // Docker容器名称通常以/开头,需要去掉
                                    let clean_name = name.strip_prefix('/').unwrap_or(name);
                                    clean_name == container_name
                                });

                                if container_matches {
                                    if let Some(labels) = &container.labels {
                                        return Some(ComposeLabels {
                                            project: labels
                                                .get("com.docker.compose.project")
                                                .cloned(),
                                            service: labels
                                                .get("com.docker.compose.service")
                                                .cloned(),
                                            container_number: labels
                                                .get("com.docker.compose.container-number")
                                                .cloned(),
                                            oneoff: labels
                                                .get("com.docker.compose.oneoff")
                                                .and_then(|v| v.parse::<bool>().ok())
                                                .or_else(|| {
                                                    labels
                                                        .get("com.docker.compose.oneoff")
                                                        .map(|v| v.to_lowercase() == "true")
                                                }),
                                            config_files: labels
                                                .get("com.docker.compose.project.config_files")
                                                .cloned(),
                                            working_dir: labels
                                                .get("com.docker.compose.project.working_dir")
                                                .cloned(),
                                        });
                                    }
                                    return None; // 找到容器但没有标签
                                }
                            }
                        }
                        None // 没有找到匹配的容器
                    }
                    Err(e) => {
                        warn!(
                            "Bollard failed to get container list: {error}",
                            error = e.to_string()
                        );
                        None
                    }
                }
            }
            Err(e) => {
                warn!(
                    "Bollard failed to connect to Docker: {error}",
                    error = e.to_string()
                );
                None
            }
        }
    }

    /// 验证容器是否属于指定的docker-compose项目
    /// 基于标签精确匹配,避免名称匹配的不准确性
    async fn is_container_from_compose_project(
        &self,
        container_name: &str,
        project_name: &str,
        compose_file_path: &str,
    ) -> bool {
        if let Some(labels) = self.get_container_labels(container_name).await {
            // 1. 检查项目名称是否匹配
            if let Some(label_project) = &labels.project {
                if label_project != project_name {
                    info!(
                        "⚠️ Container {container} project mismatch: {label} vs {expected}",
                        container = container_name,
                        label = label_project,
                        expected = project_name
                    );
                    return false;
                }
            } else {
                info!(
                    "⚠️ Container {container} missing project label",
                    container = container_name
                );
                return false;
            }

            // 2. 检查配置文件路径是否匹配(处理相对路径vs绝对路径问题)
            if let Some(label_config_files) = &labels.config_files {
                // 将我们的配置文件路径转换为绝对路径
                let compose_file_absolute =
                    match std::path::Path::new(compose_file_path).canonicalize() {
                        Ok(abs_path) => abs_path.to_string_lossy().to_string(),
                        Err(_) => {
                            // 如果无法获取绝对路径,尝试基于当前目录构建
                            let current_dir = std::env::current_dir().unwrap_or_default();
                            let full_path = current_dir.join(compose_file_path);
                            full_path.to_string_lossy().to_string()
                        }
                    };

                debug!(
                    "🔍 Path comparison: container label path={}, local absolute path={}",
                    label_config_files, compose_file_absolute
                );

                #[cfg(windows)]
                fn normalize_win_path(path: &str) -> &str {
                    if path.starts_with(r"\\?\") {
                        &path[4..]
                    } else {
                        path
                    }
                }

                #[cfg(windows)]
                let matched = normalize_win_path(label_config_files)
                    .eq_ignore_ascii_case(normalize_win_path(&compose_file_absolute));
                #[cfg(not(windows))]
                let matched = label_config_files == &compose_file_absolute;

                if matched {
                    debug!(
                        "✅ Container {container} config path matched",
                        container = container_name
                    );
                    return true;
                } else {
                    debug!(
                        "❌ Container {container} config path mismatch: {label} vs {expected}",
                        container = container_name,
                        label = label_config_files,
                        expected = compose_file_absolute
                    );
                    return false;
                }
            }

            // 3. 如果没有配置文件路径信息,但项目名称匹配,则认为匹配
            info!(
                "⚠️ Container {container} missing config path, but project name matched",
                container = container_name
            );
            true
        } else {
            // 如果无法获取标签,说明不是compose容器
            info!(
                "⚠️ Container {container} cannot get compose labels",
                container = container_name
            );
            false
        }
    }

    /// 根据标签获取容器的服务名称
    async fn get_container_service_name(&self, container_name: &str) -> Option<String> {
        self.get_container_labels(container_name)
            .await
            .and_then(|labels| labels.service)
    }

    /// 获取Docker容器的健康检查状态
    async fn get_container_health_status(&self, container_name: &str) -> Option<HealthStatusEnum> {
        match Docker::connect_with_socket_defaults() {
            Ok(docker) => {
                match docker
                    .inspect_container(container_name, None::<InspectContainerOptions>)
                    .await
                {
                    Ok(container_info) => container_info
                        .state
                        .and_then(|state| state.health.and_then(|health| health.status)),
                    Err(e) => {
                        warn!(
                            "Cannot get health status for container {container}: {error}",
                            container = container_name,
                            error = e.to_string()
                        );
                        None
                    }
                }
            }
            Err(e) => {
                warn!(
                    "Cannot connect to Docker for health check: {error}",
                    error = e.to_string()
                );
                None
            }
        }
    }

    /// 等待服务启动完成 - 智能等待策略
    pub async fn wait_for_services_ready(
        &self,
        check_interval: Duration,
    ) -> DockerServiceResult<HealthReport> {
        use std::time::Instant;

        // 最长检查180秒
        let timeout = Duration::from_secs(timeout::HEALTH_CHECK_TIMEOUT);

        let start_time = Instant::now();

        info!(
            "⏳ Starting service startup check, timeout: {timeout}s",
            timeout = timeout.as_secs()
        );

        loop {
            let elapsed = start_time.elapsed();
            if elapsed >= timeout {
                error!(
                    "⏰ Health check timeout! elapsed: {elapsed}s",
                    elapsed = elapsed.as_secs()
                );
                return Err(DockerServiceError::Timeout {
                    operation: t!("health_check.wait_operation").to_string(),
                    timeout_seconds: timeout.as_secs(),
                });
            }

            // 执行健康检查
            let report = self.health_check().await?;

            // 检查是否所有服务都已就绪
            if report.is_all_healthy() {
                info!(
                    "🎉 All services started! elapsed: {elapsed}s",
                    elapsed = elapsed.as_secs()
                );
                return Ok(report);
            } else {
                info!(
                    "⏳ Services starting... elapsed: {elapsed}s",
                    elapsed = elapsed.as_secs()
                );
                //打印尚未启动成功容器
                let failed_containers = report.failed_containers();
                if !failed_containers.is_empty() {
                    let failed_names: Vec<&str> =
                        failed_containers.iter().map(|c| c.name.as_str()).collect();
                    info!(
                        "⚠️ Not started containers: {names}",
                        names = format!("{:?}", failed_names)
                    );
                }
            }

            tokio::time::sleep(check_interval).await;
        }
    }

    /// 获取服务状态摘要
    pub async fn get_status_summary(&self) -> DockerServiceResult<String> {
        let report = self.health_check().await?;

        let mut summary = format!(
            "{}: {} ({}/{})",
            t!("health_check.service_status"),
            t!("health_check.healthy"),
            report.healthy_containers().len(),
            report.total_containers()
        );

        if !report.errors.is_empty() {
            summary.push_str(&format!(
                "\n{}: {}",
                t!("health_check.errors"),
                report.errors.join(", ")
            ));
        }

        let failed_containers = report.failed_containers();
        if !failed_containers.is_empty() {
            let failed_names: Vec<&str> =
                failed_containers.iter().map(|c| c.name.as_str()).collect();
            summary.push_str(&format!(
                "\n{}: {:?}",
                t!("health_check.failed_containers"),
                failed_names
            ));
        }

        Ok(summary)
    }
}

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

    #[test]
    fn test_health_report() {
        let mut report = HealthReport::default();

        report.add_container(ContainerInfo {
            name: "service1".to_string(),
            status: ContainerStatus::Running,
            image: "test:latest".to_string(),
            ports: vec!["8080:8080".to_string()],
            uptime: None,
            health: None,
            is_oneshot: false,
            restart: Some(RestartPolicy::UnlessStopped),
        });

        report.add_container(ContainerInfo {
            name: "service2".to_string(),
            status: ContainerStatus::Starting,
            image: "test2:latest".to_string(),
            ports: vec![],
            uptime: None,
            health: None,
            is_oneshot: false,
            restart: Some(RestartPolicy::Always),
        });

        assert_eq!(report.finalize(), ServiceStatus::Starting);
        assert_eq!(report.get_running_count(), 1);
        assert_eq!(report.get_total_count(), 2);
    }
}