dcd 0.1.9

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
use crate::composer::{
    config::parser::ConfigParser,
    config::ports::PortsParser,
    config::volumes::VolumesParser,
    detection::{detect_compose_command, ComposeCommand, DetectionError},
    errors::ComposerError,
    types::{ComposeFile, ComposerConfig, ComposerOutput, ComposerResult},
    variables::availability::EnvironmentChecker,
    variables::availability::EnvironmentStatus,
    variables::parser::VariablesParser,
};
use crate::executor::CommandExecutor;

use semver::Version;
use std::{fs, path::PathBuf};

pub struct Composer<T: CommandExecutor> {
    executor: T,
    config: ComposerConfig,
    pub compose_command: ComposeCommand,
    pub compose_version: Version,
}

impl<T: CommandExecutor> Composer<T> {
    pub async fn try_new(mut executor: T, mut config: ComposerConfig) -> ComposerResult<Self> {
        // --- Resolve project directory to absolute path ---
        config.project_dir = fs::canonicalize(&config.project_dir).map_err(|e| {
            ComposerError::ConfigurationError(format!(
                "Failed to resolve project directory '{}': {}",
                config.project_dir.display(),
                e
            ))
        })?;
        tracing::debug!(
            "Resolved project directory to: {}",
            config.project_dir.display()
        );
        // --- End resolve project directory ---

        // Validate and handle compose files
        if config.compose_files.is_empty() {
            tracing::debug!("No compose files specified, looking for defaults...");
            // Try docker-compose.yml first, then docker-compose.yaml
            let yml_path = config.project_dir.join("docker-compose.yml");
            let yaml_path = config.project_dir.join("docker-compose.yaml");

            if yml_path.exists() {
                tracing::debug!("Found default docker-compose.yml");
                config.compose_files.push(yml_path);
            } else if yaml_path.exists() {
                tracing::debug!("Found default docker-compose.yaml");
                config.compose_files.push(yaml_path);
            } else {
                return Err(ComposerError::ConfigurationError(
                    "No compose files specified and no default docker-compose.yml or docker-compose.yaml found".to_string()
                ));
            }
        } else {
            // Verify all specified compose files exist
            for file_path in &config.compose_files {
                if !file_path.exists() {
                    return Err(ComposerError::ConfigurationError(format!(
                        "Specified compose file does not exist: {}",
                        file_path.display()
                    )));
                }
            }
            tracing::debug!("All specified compose files exist");
        }

        // Validate and handle env files
        if config.env_files.is_empty() {
            tracing::debug!("No env files specified, looking for default '.env'...");
            let default_env_path = config.project_dir.join(".env");
            if default_env_path.exists() {
                tracing::debug!("Found default .env file");
                config.env_files.push(default_env_path);
            } else {
                tracing::debug!("No default .env file found");
            }
        } else {
            // Verify all specified env files exist
            for file_path in &config.env_files {
                if !file_path.exists() {
                    return Err(ComposerError::ConfigurationError(format!(
                        "Specified env file does not exist: {}",
                        file_path.display()
                    )));
                }
            }
            tracing::debug!("All specified env files exist");
        }

        tracing::debug!("Detecting docker compose command...");
        let (command, version) =
            detect_compose_command(&mut executor)
                .await
                .map_err(|e| match e {
                    // Map detection errors to ComposerError
                    DetectionError::CommandNotFound => ComposerError::CommandNotFound,
                    DetectionError::VersionTooLow {
                        command,
                        version,
                        required,
                    } => ComposerError::VersionTooLow {
                        command,
                        version,
                        required,
                    },
                    DetectionError::CommandFailed(exec_err) => {
                        ComposerError::CommandExecutionError(format!(
                            "Detection command failed: {}",
                            exec_err
                        ))
                    }
                    DetectionError::OutputParsingError(msg) => ComposerError::ParseError(format!(
                        "Detection output parsing failed: {}",
                        msg
                    )),
                    DetectionError::VersionParsingError {
                        version_str,
                        source,
                    } => ComposerError::ConfigurationError(format!(
                        "Version parsing failed for '{}': {}",
                        version_str, source
                    )),
                })?;

        Ok(Self {
            executor,
            config,
            compose_command: command,
            compose_version: version,
        })
    }

    /// Main entry point - analyze docker compose configuration
    pub async fn analyze(&mut self) -> ComposerResult<ComposerOutput> {
        // Step 1: Check environment variables
        let env_status = self.check_environment_variables().await?;

        // If we have missing required variables, return early
        if !env_status.is_valid() {
            return Err(ComposerError::missing_vars(env_status.missing_required));
        }

        // Step 2: Get and parse the full compose config
        let compose_file = self.get_compose_config().await?;

        // Step 3: Extract all required information
        let mut output = self.process_compose_file(&compose_file)?;

        // Add resolved environment variables to output
        output.consumed_env = env_status.get_resolved_variables();

        // Add resolved file lists from the config held by the Composer instance
        output.resolved_compose_files = self.config.compose_files.clone();
        output.resolved_env_files = self.config.env_files.clone();
        output.resolved_project_dir = self.config.project_dir.clone(); // Populate the resolved project dir

        Ok(output)
    }

    /// Check environment variables using docker compose config --variables
    async fn check_environment_variables(&mut self) -> ComposerResult<EnvironmentStatus> {
        // Build the variables command
        let vars_cmd = self.build_compose_command("config --variables")?;
        tracing::debug!("Running command: {}", &vars_cmd);

        // Execute the command
        let result = self
            .executor
            .execute_command(&vars_cmd)
            .await
            .map_err(|e| ComposerError::command_error(e.to_string()))?;

        if !result.is_success() {
            return Err(ComposerError::command_error(
                "Failed to get variables configuration",
            ));
        }

        // Parse variables output
        let variables =
            VariablesParser::parse_variables_output(&result.output.to_stdout_string()?)?;

        // Check environment status
        let mut checker = EnvironmentChecker::new();
        checker
            .check_environment(&variables, &self.config.env_files)
            .await
    }

    /// Get and parse the full docker compose config
    async fn get_compose_config(&mut self) -> ComposerResult<ComposeFile> {
        let config_cmd = self.build_compose_command("config")?;

        let result = self
            .executor
            .execute_command(&config_cmd)
            .await
            .map_err(|e| ComposerError::CommandExecutionError(e.to_string()))?;

        if !result.is_success() {
            return Err(ComposerError::command_error(
                "Failed to get compose configuration",
            ));
        }

        ConfigParser::parse_config(&result.output.to_stdout_string()?)
    }

    /// Process the compose file to extract all required information
    fn process_compose_file(&self, compose_file: &ComposeFile) -> ComposerResult<ComposerOutput> {
        let mut output = ComposerOutput::new();

        // Extract ports
        for service in compose_file.services.values() {
            if let Some(ports) = &service.ports {
                let parsed_ports = PortsParser::parse_ports(ports)?;
                output.exposed_ports.extend(parsed_ports);
            }

            // Extract volumes
            if let Some(volumes) = &service.volumes {
                let parsed_volumes =
                    VolumesParser::parse_volumes(volumes, &self.config.project_dir)?;
                output.volumes.extend(parsed_volumes);
            }
        }

        // Extract local references
        let references = ConfigParser::extract_local_references(compose_file);
        output
            .local_references
            .extend(references.into_iter().map(PathBuf::from));

        Ok(output)
    }

    fn build_compose_command(&self, subcommand: &str) -> ComposerResult<String> {
        let base_cmd = self.compose_command.command_string();
        let mut cmd_parts = base_cmd.split_whitespace().collect::<Vec<&str>>();

        // Add compose files
        for file in &self.config.compose_files {
            cmd_parts.push("-f");
            cmd_parts.push(file.to_str().ok_or_else(|| {
                ComposerError::ConfigurationError("Invalid compose file path".to_string())
            })?);
        }

        // Add env files
        for env_file in &self.config.env_files {
            cmd_parts.push("--env-file");
            cmd_parts.push(env_file.to_str().ok_or_else(|| {
                ComposerError::ConfigurationError("Invalid env file path".to_string())
            })?);
        }

        // Add subcommand
        cmd_parts.extend(subcommand.split_whitespace());

        Ok(cmd_parts.join(" "))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::composer::{
        detection::ComposeCommand,
        types::{PortMapping, Service, VolumeMapping},
    };
    use crate::executor::{CommandResult, ExecutorError};
    use async_trait::async_trait;
    use std::collections::HashMap;
    use std::fs;
    use std::io::Write;
    use tempfile::TempDir;

    struct MockExecutor {
        // Store Result directly to simulate execution errors
        responses: HashMap<String, Result<CommandResult, ExecutorError>>,
        commands: Vec<String>,
    }

    impl MockExecutor {
        fn new() -> Self {
            Self {
                responses: HashMap::new(),
                commands: Vec::new(),
            }
        }

        fn add_response(&mut self, command: &str, result: Result<CommandResult, ExecutorError>) {
            self.responses.insert(command.to_string(), result);
        }

        // Helper to set up standard successful detection (e.g., plugin)
        fn setup_successful_plugin_detection(&mut self) {
            self.add_response(
                "docker compose version --format json",
                create_success_result("{\"version\":\"v2.5.1\"}"),
            );
            // Optionally add a failure for standalone if needed by a specific test
            self.add_response(
                "docker-compose --version",
                Err(ExecutorError::Other("command not found".into())), // Simulate execution error
            );
        }

        // Helper to set up standard successful detection (e.g., standalone)
        fn setup_successful_standalone_detection(&mut self) {
            self.add_response(
                "docker compose version --format json",
                Err(ExecutorError::Other("command not found".into())),
            );
            self.add_response(
                "docker-compose --version",
                create_success_result("docker-compose version v1.29.2, build abcdef"),
            );
        }

        // Helper to set up failed detection (both commands fail)
        fn setup_failed_detection(&mut self) {
            self.add_response(
                "docker compose version --format json",
                Err(ExecutorError::Other("command not found".into())),
            );
            self.add_response(
                "docker-compose --version",
                Err(ExecutorError::Other("command not found".into())),
            );
        }
    }

    #[async_trait]
    impl CommandExecutor for MockExecutor {
        async fn execute_command(&mut self, command: &str) -> Result<CommandResult, ExecutorError> {
            self.commands.push(command.to_string());
            // Clone the result from the map, or return an error if not found
            let response = self.responses.get(command).cloned().ok_or_else(|| {
                ExecutorError::Other(format!("Mock response not found for command: {}", command))
            })?;

            let result = response?; // Propagate potential ExecutorError stored in the map value
            Ok(result)
        }

        async fn close(&mut self) -> Result<(), ExecutorError> {
            Ok(())
        }
    }

    fn create_test_environment() -> (TempDir, ComposerConfig) {
        let temp_dir = TempDir::new().unwrap();
        // Create a docker-compose.yml file in the temp directory
        fs::write(temp_dir.path().join("docker-compose.yml"), "version: '3'").unwrap();

        let config = ComposerConfig {
            project_dir: temp_dir.path().to_path_buf(),
            compose_files: vec![temp_dir.path().join("docker-compose.yml")],
            env_files: vec![],
        };

        (temp_dir, config)
    }

    fn create_env_file(dir: &TempDir, filename: &str, content: &str) -> PathBuf {
        let env_path = dir.path().join(filename);
        let mut file = fs::File::create(&env_path).unwrap();
        write!(file, "{}", content).unwrap();
        env_path
    }

    fn create_success_result(stdout: &str) -> Result<CommandResult, ExecutorError> {
        let mut result = CommandResult::new("mock_command");
        result.output.stdout = stdout.as_bytes().to_vec();
        result.output.exit_code = 0;
        Ok(result)
    }

    #[tokio::test]
    async fn test_composer_try_new_success_plugin() {
        let (temp_dir, config) = create_test_environment();
        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection(); // Setup mock for successful plugin detection

        let composer_result = Composer::try_new(executor, config).await;
        assert!(composer_result.is_ok());
        let composer = composer_result.unwrap();

        assert_eq!(composer.compose_command, ComposeCommand::Plugin);
        assert_eq!(composer.compose_version, Version::parse("2.5.1").unwrap());

        // Test build_compose_command implicitly
        let cmd = composer.build_compose_command("config").unwrap();
        let expected_start = format!(
            "docker compose -f {}",
            temp_dir.path().join("docker-compose.yml").display()
        );
        // let expected_env = format!("--env-file {}", temp_dir.path().join(".env").display()); // No env file in this setup
        assert!(cmd.starts_with(&expected_start));
        // assert!(cmd.contains(&expected_env));
        assert!(cmd.ends_with(" config"));
    }

    #[tokio::test]
    async fn test_composer_try_new_success_standalone() {
        let (_temp_dir, config) = create_test_environment();
        let mut executor = MockExecutor::new();
        executor.setup_successful_standalone_detection(); // Setup mock for successful standalone detection

        let composer_result = Composer::try_new(executor, config).await;
        assert!(composer_result.is_ok());
        let composer = composer_result.unwrap();

        assert_eq!(composer.compose_command, ComposeCommand::Standalone);
        assert_eq!(composer.compose_version, Version::parse("1.29.2").unwrap());
    }

    #[tokio::test]
    async fn test_build_compose_command() {
        // Tests that the build_compose_command method correctly constructs a docker compose command
        // with the appropriate -f and --env-file flags based on the provided configuration
        let (temp_dir, mut config) = create_test_environment();

        // Add an env file
        let env_path = create_env_file(&temp_dir, ".env", "TEST=value");
        config.env_files.push(env_path);

        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection();
        let composer = Composer::try_new(executor, config).await.unwrap();

        let cmd = composer.build_compose_command("config").unwrap();

        assert!(cmd.starts_with("docker compose -f "));
        assert!(cmd.contains(" --env-file "));
        assert!(cmd.ends_with(" config"));
    }

    #[tokio::test]
    async fn test_get_compose_config() {
        let (_temp_dir, config) = create_test_environment();

        // Mock the config output
        let config_output = r#"
services:
  db:
    container_name: postgres
    environment:
      POSTGRES_PASSWORD: password
      POSTGRES_USER: user
    image: postgres:13
    networks:
      default: null
    ports:
      - mode: ingress
        target: 5432
        published: "5432"
        protocol: tcp
    volumes:
      - type: volume
        source: postgres_data
        target: /var/lib/postgresql/data
        volume: {}
networks:
  default:
    name: dcd_default
volumes:
  postgres_data:
    name: dcd_postgres_dat
"#;

        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection(); // Need detection to succeed
                                                      // build_compose_command will construct the command with the absolute path
        let expected_config_cmd = format!(
            "docker compose -f {} config",
            config.compose_files[0].display()
        );
        executor.add_response(&expected_config_cmd, create_success_result(config_output)); // Mock the config command
        let mut composer = Composer::try_new(executor, config).await.unwrap();

        let compose_file = composer.get_compose_config().await.unwrap();

        assert!(compose_file.services.contains_key("db"));
        let db_service = &compose_file.services["db"];
        assert_eq!(db_service.image, Some("postgres:13".to_string()));
        assert_eq!(db_service.container_name, Some("postgres".to_string()));

        // Check ports
        let ports = db_service.ports.as_ref().unwrap();
        assert_eq!(ports.len(), 1);
        assert_eq!(ports[0].published, "5432");
        assert_eq!(ports[0].target, 5432);

        // Check volumes
        let volumes = db_service.volumes.as_ref().unwrap();
        assert_eq!(volumes.len(), 1);
        assert_eq!(volumes[0].source, Some("postgres_data".to_string()));
        assert_eq!(volumes[0].target, "/var/lib/postgresql/data");
    }

    #[tokio::test]
    async fn test_process_compose_file() {
        let (_temp_dir, config) = create_test_environment();

        // Create a compose file with services, ports, and volumes
        let mut services = HashMap::new();

        // Add a service with ports and volumes
        let db_service = Service {
            container_name: Some("postgres".to_string()),
            image: Some("postgres:13".to_string()),
            build: None,
            environment: None,
            ports: Some(vec![PortMapping {
                mode: None,
                target: 5432,
                published: "5432".to_string(),
                protocol: None,
            }]),
            volumes: Some(vec![VolumeMapping {
                r#type: "bind".to_string(),
                source: Some("/local/path".to_string()),
                target: "/container/path".to_string(),
                read_only: Some(false),
            }]),
            configs: None,
            env_file: None,
        };

        services.insert("db".to_string(), db_service);

        let compose_file = ComposeFile {
            services,
            volumes: None,
        };

        // Need a Composer instance, detection doesn't matter for this test function itself
        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection(); // Provide detection mocks
        let composer = Composer::try_new(executor, config).await.unwrap();

        let output = composer.process_compose_file(&compose_file).unwrap();

        // Check extracted ports
        assert_eq!(output.exposed_ports.len(), 1);
        assert_eq!(output.exposed_ports[0].published, "5432");
        assert_eq!(output.exposed_ports[0].target, 5432);

        // Check extracted volumes
        assert_eq!(output.volumes.len(), 1);
        assert_eq!(output.volumes[0].r#type, "bind");
        // Source path should be resolved relative to project_dir
        assert_eq!(output.volumes[0].target, "/container/path");

        // Check local references (from bind mount source)
        assert_eq!(output.local_references.len(), 1);
    }

    #[tokio::test]
    async fn test_composer_try_new_detection_failure() {
        let (_temp_dir, config) = create_test_environment();

        let mut executor = MockExecutor::new();
        executor.setup_failed_detection(); // Setup mock for failed detection

        let result = Composer::try_new(executor, config).await;

        // Should return a CommandNotFound error
        assert!(result.is_err());
        // Check that the error is specifically CommandNotFound
        match result.err().unwrap() {
            ComposerError::CommandExecutionError(_) => {} // Expect CommandExecutionError
            e => panic!("Expected CommandExecutionError, got {:?}", e),
        }
    }

    #[tokio::test]
    async fn test_composer_try_new_version_too_low() {
        let (_temp_dir, config) = create_test_environment();
        let mut executor = MockExecutor::new();
        // Mock plugin detection failing
        executor.add_response(
            "docker compose version --format json",
            Err(ExecutorError::Other("command not found".into())),
        );
        // Mock standalone detection succeeding but with an old version
        executor.add_response(
            "docker-compose --version",
            create_success_result("docker-compose version v1.20.0, build abcdef"),
        );

        let result = Composer::try_new(executor, config).await;
        assert!(result.is_err());
        match result.err().unwrap() {
            ComposerError::VersionTooLow {
                command, version, ..
            } => {
                assert_eq!(command, "docker-compose");
                assert_eq!(version, Version::parse("1.20.0").unwrap());
            }
            e => panic!("Expected VersionTooLow, got {:?}", e),
        }
    }

    #[tokio::test]
    async fn test_composer_try_new_missing_compose_files() {
        // Create temp dir but don't create any compose files
        let temp_dir = TempDir::new().unwrap();

        let config = ComposerConfig {
            project_dir: temp_dir.path().to_path_buf(),
            compose_files: vec![], // Empty compose files list
            env_files: vec![],
        };

        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection();

        let result = Composer::try_new(executor, config).await;
        assert!(result.is_err());
        match result.err().unwrap() {
            ComposerError::ConfigurationError(msg) => {
                assert!(msg.contains("No compose files specified and no default"));
            }
            e => panic!("Expected ConfigurationError, got {:?}", e),
        }
    }

    #[tokio::test]
    async fn test_composer_try_new_nonexistent_compose_file() {
        let temp_dir = TempDir::new().unwrap();

        let config = ComposerConfig {
            project_dir: temp_dir.path().to_path_buf(),
            compose_files: vec![temp_dir.path().join("nonexistent-file.yml")], // File doesn't exist
            env_files: vec![],
        };

        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection();

        let result = Composer::try_new(executor, config).await;
        assert!(result.is_err());
        match result.err().unwrap() {
            ComposerError::ConfigurationError(msg) => {
                assert!(msg.contains("does not exist"));
            }
            e => panic!("Expected ConfigurationError, got {:?}", e),
        }
    }

    #[tokio::test]
    async fn test_composer_try_new_default_compose_file() {
        // Create temp dir with a default docker-compose.yml
        let temp_dir = TempDir::new().unwrap();
        fs::write(temp_dir.path().join("docker-compose.yml"), "version: '3'").unwrap();

        let config = ComposerConfig {
            project_dir: temp_dir.path().to_path_buf(),
            compose_files: vec![], // Empty compose files list - should find default
            env_files: vec![],
        };

        let mut executor = MockExecutor::new();
        executor.setup_successful_plugin_detection();

        // Mock the config command that will be called after detection
        let expected_config_cmd = format!(
            "docker compose -f {} config",
            temp_dir.path().join("docker-compose.yml").display()
        );
        executor.add_response(&expected_config_cmd, create_success_result("services: {}"));

        let result = Composer::try_new(executor, config).await;
        assert!(result.is_ok());
    }
}