dcd 0.2.0

Docker Compose Deployment tool for remote servers
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
use super::{
    docker_manager::{DockerManager, HealthCheckResult, SshDockerManager},
    firewall::{PortConfig, Protocol, UfwManager},
    sync::{EnvFileManager, FileSync, SyncPlan},
    types::{
        ComposeExec, DeployError, DeployResult, DeployerEvent, DeploymentConfig, DeploymentStatus,
    },
    DCD_ENV_FILE,
};
use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Duration;
use tokio::sync::mpsc;

pub struct Deployer<'a> {
    config: DeploymentConfig,
    executor: &'a mut (dyn ComposeExec + Send),
    resolved_remote_dir: PathBuf,
    progress_sender: Option<mpsc::Sender<DeployerEvent>>,
}

impl<'a> Deployer<'a> {
    const HEALTH_CHECK_RETRIES: u32 = 5;
    const MAX_STARTING_ATTEMPTS: u32 = 15;
    const HEALTH_CHECK_DELAY: Duration = Duration::from_secs(10);

    pub fn new(
        config: DeploymentConfig,
        executor: &'a mut (dyn ComposeExec + Send),
        progress_sender: Option<mpsc::Sender<DeployerEvent>>,
    ) -> Self {
        // Determine the final remote directory path
        let resolved_remote_dir = match &config.remote_dir {
            Some(user_path) => {
                tracing::debug!(
                    "Using user-provided remote directory: {}",
                    user_path.display()
                );
                user_path.clone()
            }
            None => {
                // Extract project name from config.project_dir
                let project_name = config.project_dir.file_name()
                    .map(|name| name.to_string_lossy().into_owned())
                    .unwrap_or_else(|| {
                        tracing::warn!("Could not determine project directory name from '{}', using 'default_project'", config.project_dir.display());
                        "default_project".to_string() // Fallback name
                    });
                let default_path = PathBuf::from(format!("/opt/{}", project_name));
                tracing::info!(
                    "No --workdir provided. Using default remote directory: {}",
                    default_path.display()
                );
                default_path
            }
        };

        tracing::debug!(
            "Deployer created with project_dir: '{}', resolved_remote_dir: '{}'",
            config.project_dir.display(),
            resolved_remote_dir.display()
        );
        Self {
            config,
            executor,
            resolved_remote_dir,
            progress_sender,
        }
    }

    /// Helper to send progress events if a sender exists.
    async fn send_event(&self, event: DeployerEvent) {
        if let Some(sender) = &self.progress_sender {
            if let Err(e) = sender.send(event.clone()).await {
                // Log error if sending fails (receiver likely dropped in CLI)
                tracing::warn!("Failed to send progress event: {}", e);
            }
        }
    }

    /// Main deployment method
    pub async fn deploy(&mut self) -> DeployResult<DeploymentStatus> {
        let mut status = DeploymentStatus::new();

        tracing::info!("🚀 Starting deployment process...");
        self.send_event(DeployerEvent::StepStarted(
            "Starting Deployment".to_string(),
        ))
        .await;

        // Step 1: Prepare environment
        tracing::info!("Step 1: Preparing remote environment...");
        self.send_event(DeployerEvent::StepStarted(
            "Preparing environment".to_string(),
        ))
        .await;
        if let Err(e) = self.prepare_environment(&mut status).await {
            self.send_event(DeployerEvent::StepFailed(
                "Preparing environment".to_string(),
                e.to_string(),
            ))
            .await;
            return Err(e);
        }
        self.send_event(DeployerEvent::StepCompleted(
            "Preparing environment".to_string(),
        ))
        .await;

        // Step 2: Sync files
        tracing::info!("Step 2: Synchronizing project files...");
        self.send_event(DeployerEvent::StepStarted(
            "Synchronizing files".to_string(),
        ))
        .await;
        if let Err(e) = self.sync_files(&mut status).await {
            self.send_event(DeployerEvent::StepFailed(
                "Synchronizing files".to_string(),
                e.to_string(),
            ))
            .await;
            return Err(e);
        }
        self.send_event(DeployerEvent::StepCompleted(
            "Synchronizing files".to_string(),
        ))
        .await;

        // Step 3: Configure firewall
        tracing::info!("Step 3: Configuring firewall (UFW)...");
        self.send_event(DeployerEvent::StepStarted(
            "Configuring firewall".to_string(),
        ))
        .await;
        if let Err(e) = self.configure_firewall(&mut status).await {
            self.send_event(DeployerEvent::StepFailed(
                "Configuring firewall".to_string(),
                e.to_string(),
            ))
            .await;
            return Err(e);
        }
        self.send_event(DeployerEvent::StepCompleted(
            "Configuring firewall".to_string(),
        ))
        .await;

        // Step 4: Deploy services
        tracing::info!("Step 4: Deploying services using Docker Compose...");
        self.send_event(DeployerEvent::StepStarted("Deploying services".to_string()))
            .await;
        if let Err(e) = self.deploy_services(&mut status).await {
            self.send_event(DeployerEvent::StepFailed(
                "Deploying services".to_string(),
                e.to_string(),
            ))
            .await;
            return Err(e);
        }
        self.send_event(DeployerEvent::StepCompleted(
            "Deploying services".to_string(),
        ))
        .await;

        Ok(status)
    }

    pub async fn destroy(
        &mut self,
        remove_volumes: bool,
        remove_images: bool,
        force: bool,
    ) -> DeployResult<DeploymentStatus> {
        let mut status = DeploymentStatus::new();
        self.send_event(DeployerEvent::StepStarted(
            "Initializing destruction...".to_string(),
        ))
        .await;
        tracing::info!("🔥 Starting destruction process...");

        // Clone the sender before creating the manager which borrows self.executor mutably
        let cloned_sender = self.progress_sender.clone();

        // Create Docker manager
        tracing::debug!("Initializing Docker manager for destruction.");
        // Build list of remote compose and env files (basenames)
        let compose_files = self
            .config
            .compose_files
            .iter()
            .map(|p| PathBuf::from(p.file_name().expect("Invalid compose file path")))
            .collect::<Vec<PathBuf>>();
        let mut env_files = self
            .config
            .env_files
            .iter()
            .map(|p| PathBuf::from(p.file_name().expect("Invalid env file path")))
            .collect::<Vec<PathBuf>>();
        // Include generated .env.dcd if present
        let dcd_path = self.config.project_dir.join(DCD_ENV_FILE);
        if dcd_path.exists() {
            env_files.push(PathBuf::from(DCD_ENV_FILE));
        }
        let mut docker_manager = SshDockerManager::new(
            self.executor,
            self.resolved_remote_dir.clone(),
            compose_files,
            env_files,
        )
        .await?;

        // Check if any services are running
        tracing::info!("Checking for running services...");
        let services_running = docker_manager.has_running_services().await?;

        // If services are running and force is false, return error
        if services_running && !force {
            tracing::warn!("Services are running. Destruction aborted. Use --force to override.");
            status.message = "Services are still running. Use --force to destroy anyway.".into();
            return Err(DeployError::Deployment(status.message.clone()));
        } else if services_running && force {
            tracing::info!(
                "Force flag enabled. Proceeding with destruction despite running services."
            );
        }

        // Stop and remove containers
        tracing::info!(
            "Stopping services, removing containers{}{}...",
            if remove_volumes { ", volumes" } else { "" },
            if remove_images { ", images" } else { "" }
        );
        // Use the cloned sender
        if let Some(sender) = &cloned_sender {
            let _ = sender
                .send(DeployerEvent::StepStarted(
                    "Stopping and removing containers/networks...".to_string(),
                ))
                .await;
        }
        docker_manager
            .compose_down(remove_volumes, remove_images)
            .await?;
        if let Some(sender) = &cloned_sender {
            let _ = sender
                .send(DeployerEvent::StepCompleted(
                    "Containers and networks removed.".to_string(),
                ))
                .await;
        }

        let mut removal_details = Vec::new();
        if remove_volumes {
            removal_details.push("volumes");
        }
        if remove_images {
            removal_details.push("images");
        }

        // If removing volumes, also remove the remote project directory
        if remove_volumes {
            let remote_dir_str = self.resolved_remote_dir.display().to_string();
            // Use the cloned sender
            if let Some(sender) = &cloned_sender {
                let _ = sender
                    .send(DeployerEvent::StepStarted(format!(
                        "Removing remote directory: {}",
                        remote_dir_str
                    )))
                    .await;
            }
            tracing::info!("Removing remote project directory: {}", remote_dir_str);
            let rm_cmd = format!("rm -rf {}", remote_dir_str);
            let rm_result = self.executor.execute_command(&rm_cmd).await.map_err(|e| {
                DeployError::Deployment(format!("Failed to remove directory: {}", e))
            })?;
            if !rm_result.is_success() {
                let error_msg = rm_result.output.to_stderr_string()?;
                tracing::error!(
                    "Failed to remove remote directory '{}': {}",
                    remote_dir_str,
                    error_msg
                );
                // Log error but don't fail the whole destroy operation, as compose down succeeded
                status.message = format!("Deployment destroyed (containers{}, images{}), but failed to remove project directory: {}", 
                    if remove_volumes {"+volumes"} else {""}, 
                    if remove_images {"+images"} else {""}, 
                    error_msg);
            } else {
                removal_details.push("project directory");
                // Use the cloned sender
                if let Some(sender) = &cloned_sender {
                    let _ = sender
                        .send(DeployerEvent::StepCompleted(
                            "Remote directory removed.".to_string(),
                        ))
                        .await;
                }
            }
        }

        status.message = format!(
            "Deployment destroyed successfully (removed: {}).",
            if removal_details.is_empty() {
                "containers only".to_string()
            } else {
                removal_details.join(", ")
            }
        );

        self.send_event(DeployerEvent::StepCompleted(
            "Destruction complete.".to_string(),
        ))
        .await;
        Ok(status)
    }

    /// Prepare environment (env files, directories)
    async fn prepare_environment(&mut self, status: &mut DeploymentStatus) -> DeployResult<()> {
        tracing::debug!("Initializing environment file manager.");
        // Create env file manager
        let env_manager =
            EnvFileManager::new(self.config.consumed_env.clone(), &self.config.project_dir);

        // Generate .env.dcd if we have consumed environment variables
        if env_manager.has_env_vars() {
            tracing::info!("Generating {} file locally...", DCD_ENV_FILE);
            env_manager.generate_dcd_env().await?;
            tracing::debug!(
                "{} generated at: {}",
                DCD_ENV_FILE,
                env_manager.get_dcd_env_path().display()
            );
            status.env_changed = true;
        } else {
            tracing::debug!(
                "No consumed environment variables, skipping {} generation.",
                DCD_ENV_FILE
            );
        }

        Ok(())
    }

    /// Synchronize all required files
    async fn sync_files(&mut self, status: &mut DeploymentStatus) -> DeployResult<()> {
        let mut sync_plan = SyncPlan::new();
        tracing::debug!("Initializing file synchronization plan.");
        // Keep track of top-level project directories/files already added to the plan
        // to avoid duplicates when multiple files from the same dir are referenced.
        let mut synced_top_level_paths: HashSet<PathBuf> = HashSet::new();

        // Add compose files
        for file in &self.config.compose_files {
            let remote_path =
                self.resolved_remote_dir
                    .join(file.file_name().ok_or_else(|| {
                        DeployError::Configuration("Invalid compose file name".into())
                    })?);
            tracing::debug!(
                "Adding compose file to sync plan: '{}' -> '{}'",
                file.display(),
                remote_path.display()
            );
            sync_plan.add_compose_file(file, remote_path);
        }

        // Add env files
        for file in &self.config.env_files {
            let remote_path = self.resolved_remote_dir.join(
                file.file_name()
                    .ok_or_else(|| DeployError::Configuration("Invalid env file name".into()))?,
            );
            tracing::debug!(
                "Adding env file to sync plan: '{}' -> '{}'",
                file.display(),
                remote_path.display()
            );
            sync_plan.add_env_file(file, remote_path);
        }

        // Add .env.dcd if it exists
        let dcd_env = self.config.project_dir.join(DCD_ENV_FILE);
        if dcd_env.exists() {
            tracing::debug!(
                "Adding generated {} to sync plan: '{}' -> '{}'",
                DCD_ENV_FILE,
                dcd_env.display(),
                self.resolved_remote_dir.join(DCD_ENV_FILE).display()
            );
            sync_plan.add_env_file(&dcd_env, self.resolved_remote_dir.join(DCD_ENV_FILE));
        }

        // Add referenced files
        for path in &self.config.local_references {
            tracing::debug!("Processing local reference: '{}'", path.display());
            // Try to make the path relative to the resolved project directory
            match path.strip_prefix(&self.config.project_dir) {
                Ok(relative_path) => {
                    // Path is inside the project. Check if it exists locally.
                    if !path.exists() {
                        tracing::warn!(
                            "Local reference path inside project directory does not exist, skipping: {}",
                            path.display()
                        );
                        continue;
                    }

                    // Get the first component of the relative path (e.g., "traefik" from "traefik/config/file.yml")
                    if let Some(first_component) = relative_path.components().next() {
                        let top_level_component = first_component.as_os_str();
                        let top_level_path = self.config.project_dir.join(top_level_component);
                        let remote_top_level_path =
                            self.resolved_remote_dir.join(top_level_component);
                        tracing::debug!(
                            "Identified top-level component '{}' for reference '{}'",
                            top_level_component.to_string_lossy(),
                            path.display()
                        );

                        // Only add if we haven't added this top-level path already
                        if synced_top_level_paths.insert(top_level_path.clone()) {
                            // Add the top-level directory containing the reference to the sync plan.
                            // Always sync as a directory based on the user request.
                            tracing::debug!(
                                "Adding reference directory to sync plan: '{}' -> '{}'",
                                top_level_path.display(),
                                remote_top_level_path.display()
                            );
                            sync_plan.add_reference(&top_level_path, remote_top_level_path, true);
                        }
                    } // else: relative_path was empty or unusual, ignore.
                }
                Err(_) => {
                    // Path is outside the project directory, log and skip syncing
                    // We don't need to check path.exists() for external paths.
                    tracing::info!(
                        "External reference '{}' found, will not be synced by dcd.",
                        path.display()
                    );
                }
            }
        }

        // Perform synchronization
        tracing::info!("Executing file synchronization...");
        let mut file_sync = FileSync::new(self.executor, self.resolved_remote_dir.clone());
        let sync_status = file_sync.sync_files(&sync_plan).await?;

        // Update deployment status
        status.files_changed = !sync_status.files_synced.is_empty();
        tracing::debug!(
            "Sync results: {} files synced, {} skipped, {} failed.",
            sync_status.files_synced.len(),
            sync_status.files_skipped.len(),
            sync_status.files_failed.len()
        );

        if !sync_status.files_failed.is_empty() {
            let failed_files: Vec<_> = sync_status
                .files_failed
                .iter()
                .map(|(path, _)| path.display().to_string())
                .collect();
            status.message = format!("Failed to sync files: {}", failed_files.join(", "));
            tracing::error!(
                "File synchronization failed for: {}",
                failed_files.join(", ")
            );
            return Err(DeployError::FileSync(status.message.clone()));
        }

        Ok(())
    }

    /// Configure firewall rules
    async fn configure_firewall(&mut self, status: &mut DeploymentStatus) -> DeployResult<()> {
        if self.config.exposed_ports.is_empty() {
            tracing::info!("No exposed ports found in configuration, skipping firewall setup.");
            return Ok(());
        }

        tracing::debug!("Initializing UFW manager.");
        let mut ufw = UfwManager::new(self.executor);

        // Convert exposed ports to firewall config
        let port_configs: Vec<PortConfig> = self
            .config
            .exposed_ports
            .iter()
            .map(|port| PortConfig {
                port: port.target,
                protocol: Protocol::from(port.protocol.as_deref().unwrap_or("tcp")),
                description: format!("Docker service port {}", port.published),
            })
            .collect();

        tracing::info!(
            "Applying firewall rules for {} port(s)...",
            port_configs.len()
        );
        tracing::debug!("Port configurations to apply: {:?}", port_configs);
        // Configure ports
        ufw.configure_ports(&port_configs).await?;

        // TODO: check why i don't pass this check
        // Verify port accessibility
        tracing::info!("Verifying firewall rules...");
        for config in &port_configs {
            tracing::debug!("Verifying port {}/{}", config.port, config.protocol);
            if !ufw.verify_port(config.port, &config.protocol).await? {
                status.message = format!("Port {} is not accessible", config.port);
                status.ports_changed = true;
                tracing::warn!(
                    "Verification failed: Port {}/{} is not accessible after configuration.",
                    config.port,
                    config.protocol
                );
                // Decide if this should be a hard error or just a warning in status
            }
        }

        Ok(())
    }

    /// Deploy services using docker-compose
    async fn deploy_services(&mut self, status: &mut DeploymentStatus) -> DeployResult<()> {
        tracing::debug!("Initializing Docker manager for service deployment.");
        // Build list of remote compose and env files (basenames)
        let compose_files = self
            .config
            .compose_files
            .iter()
            .map(|p| PathBuf::from(p.file_name().expect("Invalid compose file path")))
            .collect::<Vec<PathBuf>>();
        let mut env_files = self
            .config
            .env_files
            .iter()
            .map(|p| PathBuf::from(p.file_name().expect("Invalid env file path")))
            .collect::<Vec<PathBuf>>();
        // Include generated .env.dcd if present
        let dcd_path = self.config.project_dir.join(DCD_ENV_FILE);
        if dcd_path.exists() {
            env_files.push(PathBuf::from(DCD_ENV_FILE));
        }
        let mut docker_manager = SshDockerManager::new(
            self.executor,
            self.resolved_remote_dir.clone(),
            compose_files,
            env_files,
        )
        .await?;

        tracing::info!("Ensuring Docker is installed on remote host...");
        docker_manager
            .ensure_docker_installed()
            .await
            .map_err(|e| {
                tracing::error!("Docker installation check failed: {}", e);
                e
            })?;

        tracing::info!("Ensuring Docker Compose is installed and compatible...");
        docker_manager
            .ensure_docker_compose_installed()
            .await
            .map_err(|e| {
                tracing::error!("Docker Compose installation check failed: {}", e);
                e
            })?;

        // Start services
        tracing::info!("Running 'docker compose up -d' ...");
        docker_manager.compose_up().await?;

        // Clone the sender before the loop to avoid borrow conflicts with docker_manager
        let cloned_sender = self.progress_sender.clone();
        // Helper closure: Captures cloned_sender by reference.
        // The async move block takes ownership of 'event' and the reference to cloned_sender.
        let send_event_local = |event: DeployerEvent| {
            // Capture by reference here
            let sender_ref = &cloned_sender;
            async move {
                // Move 'event' and 'sender_ref'
                if let Some(sender) = sender_ref.as_ref() {
                    // Use Option::as_ref() to get Option<&Sender>
                    if let Err(e) = sender.send(event).await {
                        // Send the moved event using &Sender
                        tracing::warn!("Failed to send progress event (local): {}", e);
                    }
                }
            }
        };

        tracing::info!("Checking health of deployed services...");
        let mut attempts = 0;

        loop {
            attempts += 1;
            tracing::info!(
                "Health check attempt {}/{}...",
                attempts,
                Self::HEALTH_CHECK_RETRIES
            );
            send_event_local(DeployerEvent::HealthCheckAttempt(
                attempts,
                Self::HEALTH_CHECK_RETRIES,
            ))
            .await;

            match docker_manager.verify_services_healthy().await {
                Ok(HealthCheckResult::Healthy) => {
                    tracing::info!("✅ Services are healthy.");
                    send_event_local(DeployerEvent::HealthCheckStatus(
                        "All services are healthy".to_string(),
                    ))
                    .await;
                    status.services_healthy = true;
                    break;
                }
                Ok(HealthCheckResult::NoServices) => {
                    tracing::warn!("Health check: No services found.");
                    send_event_local(DeployerEvent::HealthCheckStatus(
                        "No services found".to_string(),
                    ))
                    .await;
                    status.services_healthy = false;
                    status.message = "No services were found after deployment.".into();
                    break;
                }
                Ok(HealthCheckResult::Failed(failed_services))
                    if attempts < Self::HEALTH_CHECK_RETRIES =>
                {
                    tracing::warn!(
                        "Services not healthy yet. Found {} unhealthy service(s). Retrying in {:?}...", 
                        failed_services.len(),
                        Self::HEALTH_CHECK_DELAY
                    );
                    send_event_local(DeployerEvent::HealthCheckStatus(format!(
                        "{} unhealthy service(s), retrying...",
                        failed_services.len()
                    )))
                    .await;
                    tokio::time::sleep(Self::HEALTH_CHECK_DELAY).await;
                }
                Ok(HealthCheckResult::Failed(failed_services)) => {
                    // Final attempt with unhealthy services
                    status.services_healthy = false;

                    // Create a detailed message about unhealthy services
                    let service_details: Vec<String> = failed_services
                        .iter()
                        .map(|s| {
                            format!(
                                "{} (state: {}, health: {})",
                                s.name,
                                s.state,
                                if s.health.is_empty() {
                                    "no health check"
                                } else {
                                    &s.health
                                }
                            )
                        })
                        .collect();

                    status.message = format!(
                        "Definitively unhealthy services found after {} attempts: {}.",
                        attempts,
                        service_details.join("; ")
                    );

                    let event_msg = format!(
                        "Health check failed: {} unhealthy service(s)",
                        failed_services.len()
                    );
                    send_event_local(DeployerEvent::HealthCheckStatus(event_msg)).await;

                    tracing::error!(
                        "❌ Health check failed (terminal state): {}",
                        status.message
                    );
                    break;
                }
                Ok(HealthCheckResult::Starting(starting_services))
                    if attempts < Self::MAX_STARTING_ATTEMPTS =>
                {
                    tracing::info!(
                        "Health check: {} service(s) still starting. Waiting longer (attempt {}/{})...",
                        starting_services.len(),
                        attempts,
                        Self::MAX_STARTING_ATTEMPTS
                    );
                    send_event_local(DeployerEvent::HealthCheckStatus(format!(
                        "{} service(s) still starting...",
                        starting_services.len()
                    )))
                    .await;
                    tokio::time::sleep(Self::HEALTH_CHECK_DELAY).await;
                }
                Ok(HealthCheckResult::Starting(starting_services)) => {
                    // Max starting attempts reached
                    status.services_healthy = false;
                    let service_details: Vec<String> = starting_services
                        .iter()
                        .map(|s| format!("{} (state: {}, health: {})", s.name, s.state, s.health))
                        .collect();

                    status.message = format!(
                        "Services still in 'starting' state after extended timeout ({} attempts): {}.",
                        attempts,
                        service_details.join("; ")
                    );

                    send_event_local(DeployerEvent::HealthCheckStatus(format!(
                        "Timeout: {} service(s) still starting",
                        starting_services.len()
                    )))
                    .await;

                    tracing::error!("❌ Health check failed: {}", status.message);
                    break;
                }
                Err(e) => {
                    status.services_healthy = false;
                    status.message = format!("Error checking service health: {}", e);
                    send_event_local(DeployerEvent::HealthCheckStatus(
                        "Error checking service health".to_string(),
                    ))
                    .await;
                    tracing::error!("❌ Health check error: {}", e);
                    break;
                }
            }
        }

        Ok(())
    }

    /// Get current deployment status
    pub async fn get_status(&mut self) -> DeployResult<DeploymentStatus> {
        tracing::info!("Checking current deployment status...");
        self.send_event(DeployerEvent::StepStarted(
            "Initializing status check...".to_string(),
        ))
        .await;

        // Clone the sender before creating the manager which borrows self.executor mutably
        let cloned_sender = self.progress_sender.clone();

        tracing::debug!("Initializing Docker manager for status check.");
        // Use the cloned sender
        if let Some(sender) = &cloned_sender {
            let _ = sender
                .send(DeployerEvent::StepStarted(
                    "Connecting to Docker...".to_string(),
                ))
                .await;
        }
        // Build list of remote compose and env files (basenames)
        let compose_files = self
            .config
            .compose_files
            .iter()
            .map(|p| PathBuf::from(p.file_name().expect("Invalid compose file path")))
            .collect::<Vec<PathBuf>>();
        let env_files = self
            .config
            .env_files
            .iter()
            .map(|p| PathBuf::from(p.file_name().expect("Invalid env file path")))
            .collect::<Vec<PathBuf>>();
        let mut docker_manager = SshDockerManager::new(
            self.executor,
            self.resolved_remote_dir.clone(),
            compose_files,
            env_files,
        )
        .await?;
        if let Some(sender) = &cloned_sender {
            let _ = sender
                .send(DeployerEvent::StepCompleted(
                    "Connected to Docker.".to_string(),
                ))
                .await;
        }

        let mut status = DeploymentStatus::new();

        // Check services health
        tracing::info!("Checking service health...");
        // Use the cloned sender
        if let Some(sender) = &cloned_sender {
            let _ = sender
                .send(DeployerEvent::StepStarted(
                    "Checking service health...".to_string(),
                ))
                .await;
        }
        match docker_manager.verify_services_healthy().await? {
            HealthCheckResult::Healthy => {
                status.services_healthy = true;
                tracing::info!("Service health status: Healthy");
            }
            HealthCheckResult::NoServices => {
                status.services_healthy = false;
                status.message = "No services found.".into();
                tracing::warn!("Service health status: No services found");
            }
            HealthCheckResult::Failed(failed_services) => {
                status.services_healthy = false;

                // Create a detailed message about unhealthy services
                let service_details: Vec<String> = failed_services
                    .iter()
                    .map(|s| {
                        format!(
                            "{} (state: {}, health: {})",
                            s.name,
                            s.state,
                            if s.health.is_empty() {
                                "no health check"
                            } else {
                                &s.health
                            }
                        )
                    })
                    .collect();

                status.message = format!("Failed services: {}.", service_details.join("; "));

                tracing::warn!("Service health status: Failed - {}", status.message);
            }
            HealthCheckResult::Starting(starting_services) => {
                status.services_healthy = false;

                let service_details: Vec<String> = starting_services
                    .iter()
                    .map(|s| format!("{} (state: {}, health: {})", s.name, s.state, s.health))
                    .collect();

                status.message =
                    format!("Services still starting: {}.", service_details.join("; "));

                tracing::info!("Service health status: Starting - {}", status.message);
            }
        }
        // Use the cloned sender for the final event within the scope of docker_manager
        if let Some(sender) = &cloned_sender {
            let _ = sender
                .send(DeployerEvent::StepCompleted(
                    "Service health checked.".to_string(),
                ))
                .await;
        }

        Ok(status)
    }
}