zlayer-builder 0.10.76

Dockerfile parsing and buildah-based container image building
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
//! Buildah installation and discovery
//!
//! This module provides functionality to find existing buildah installations
//! or provide helpful error messages for installing buildah on various platforms.

use std::path::{Path, PathBuf};
use std::process::Stdio;

use tokio::process::Command;
use tracing::{debug, info, trace, warn};

/// Minimum required buildah version
const MIN_BUILDAH_VERSION: &str = "1.28.0";

/// Buildah installation manager
///
/// Handles finding existing buildah installations and providing
/// installation guidance when buildah is not found.
#[derive(Debug, Clone)]
pub struct BuildahInstaller {
    /// Where to store downloaded buildah binary (for future use)
    install_dir: PathBuf,
    /// Minimum required buildah version
    min_version: &'static str,
}

/// Information about a discovered buildah installation
#[derive(Debug, Clone)]
pub struct BuildahInstallation {
    /// Path to buildah binary
    pub path: PathBuf,
    /// Installed version
    pub version: String,
}

/// Errors that can occur during buildah installation/discovery
#[derive(Debug, thiserror::Error)]
pub enum InstallError {
    /// Buildah was not found on the system
    #[error("Buildah not found. {}", install_instructions())]
    NotFound,

    /// Buildah version is too old
    #[error("Buildah version {found} is below minimum required version {required}")]
    VersionTooOld {
        /// The version that was found
        found: String,
        /// The minimum required version
        required: String,
    },

    /// Platform is not supported
    #[error("Unsupported platform: {os}/{arch}")]
    UnsupportedPlatform {
        /// Operating system
        os: String,
        /// CPU architecture
        arch: String,
    },

    /// Download failed (for future binary download support)
    #[error("Download failed: {0}")]
    DownloadFailed(String),

    /// IO error during installation
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Failed to parse version output
    #[error("Failed to parse buildah version output: {0}")]
    VersionParse(String),

    /// Failed to execute buildah
    #[error("Failed to execute buildah: {0}")]
    ExecutionFailed(String),
}

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

impl BuildahInstaller {
    /// Create installer with default paths
    ///
    /// User install directory: `~/.zlayer/bin/`
    /// System install directory: `/usr/local/lib/zlayer/`
    #[must_use]
    pub fn new() -> Self {
        let install_dir = default_install_dir();
        Self {
            install_dir,
            min_version: MIN_BUILDAH_VERSION,
        }
    }

    /// Create with custom install directory
    #[must_use]
    pub fn with_install_dir(dir: PathBuf) -> Self {
        Self {
            install_dir: dir,
            min_version: MIN_BUILDAH_VERSION,
        }
    }

    /// Get the install directory
    #[must_use]
    pub fn install_dir(&self) -> &Path {
        &self.install_dir
    }

    /// Get the minimum required version
    #[must_use]
    pub fn min_version(&self) -> &str {
        self.min_version
    }

    /// Find existing buildah installation
    ///
    /// Searches for buildah in the following locations (in order):
    /// 1. PATH environment variable
    /// 2. `~/.zlayer/bin/buildah`
    /// 3. `/usr/local/lib/zlayer/buildah`
    /// 4. `/usr/bin/buildah`
    /// 5. `/usr/local/bin/buildah`
    pub async fn find_existing(&self) -> Option<BuildahInstallation> {
        // Search paths in priority order
        let search_paths = get_search_paths(&self.install_dir);

        for path in search_paths {
            trace!("Checking for buildah at: {}", path.display());

            if path.exists() && path.is_file() {
                match Self::get_version(&path).await {
                    Ok(version) => {
                        info!("Found buildah at {} (version {})", path.display(), version);
                        return Some(BuildahInstallation { path, version });
                    }
                    Err(e) => {
                        debug!(
                            "Found buildah at {} but couldn't get version: {}",
                            path.display(),
                            e
                        );
                    }
                }
            }
        }

        // Also try using `which` to find buildah in PATH
        if let Some(path) = find_in_path("buildah").await {
            match Self::get_version(&path).await {
                Ok(version) => {
                    info!(
                        "Found buildah in PATH at {} (version {})",
                        path.display(),
                        version
                    );
                    return Some(BuildahInstallation { path, version });
                }
                Err(e) => {
                    debug!(
                        "Found buildah in PATH at {} but couldn't get version: {}",
                        path.display(),
                        e
                    );
                }
            }
        }

        None
    }

    /// Check if buildah is installed and meets version requirements
    ///
    /// Returns the installation if found and valid, otherwise returns an error.
    ///
    /// # Errors
    ///
    /// Returns an error if buildah is not found or if the version is below the minimum.
    pub async fn check(&self) -> Result<BuildahInstallation, InstallError> {
        let installation = self.find_existing().await.ok_or(InstallError::NotFound)?;

        // Check version meets minimum requirements
        if !version_meets_minimum(&installation.version, self.min_version) {
            return Err(InstallError::VersionTooOld {
                found: installation.version,
                required: self.min_version.to_string(),
            });
        }

        Ok(installation)
    }

    /// Get buildah version from a binary
    ///
    /// Runs `buildah --version` and parses the output.
    /// Expected format: "buildah version 1.33.0 (image-spec 1.0.2-dev, runtime-spec 1.0.2-dev)"
    ///
    /// # Errors
    ///
    /// Returns an error if the binary cannot be executed or the version output cannot be parsed.
    pub async fn get_version(path: &Path) -> Result<String, InstallError> {
        let output = Command::new(path)
            .arg("--version")
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .await
            .map_err(|e| InstallError::ExecutionFailed(e.to_string()))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(InstallError::ExecutionFailed(format!(
                "buildah --version failed: {}",
                stderr.trim()
            )));
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        parse_version(&stdout)
    }

    /// Ensure buildah is available (find existing or return helpful error)
    ///
    /// This is the primary entry point for ensuring buildah is available.
    /// If buildah is not found, it returns an error with installation instructions.
    ///
    /// # Errors
    ///
    /// Returns an error if buildah is not found or the version is insufficient.
    pub async fn ensure(&self) -> Result<BuildahInstallation, InstallError> {
        // First try to find existing installation
        match self.check().await {
            Ok(installation) => {
                info!(
                    "Using buildah {} at {}",
                    installation.version,
                    installation.path.display()
                );
                Ok(installation)
            }
            Err(InstallError::VersionTooOld { found, required }) => {
                warn!(
                    "Found buildah {} but minimum required version is {}",
                    found, required
                );
                Err(InstallError::VersionTooOld { found, required })
            }
            Err(InstallError::NotFound) => {
                #[cfg(target_os = "macos")]
                debug!("Buildah not available on macOS; will use native sandbox builder");
                #[cfg(not(target_os = "macos"))]
                warn!("Buildah not found on system");
                Err(InstallError::NotFound)
            }
            Err(e) => Err(e),
        }
    }

    /// Download buildah binary for current platform
    ///
    /// Currently returns an error with installation instructions.
    /// Future versions may download static binaries from GitHub releases.
    ///
    /// # Errors
    ///
    /// Returns an error if the platform is unsupported or automatic download is unavailable.
    #[allow(clippy::unused_async)]
    pub async fn download(&self) -> Result<BuildahInstallation, InstallError> {
        let (os, arch) = current_platform();

        // Check if platform is supported
        if !is_platform_supported() {
            return Err(InstallError::UnsupportedPlatform {
                os: os.to_string(),
                arch: arch.to_string(),
            });
        }

        // For now, return helpful error with installation instructions
        // Future: Download static binary from GitHub releases
        Err(InstallError::DownloadFailed(format!(
            "Automatic download not yet implemented. {}",
            install_instructions()
        )))
    }
}

/// Get the current platform (OS and architecture)
#[must_use]
pub fn current_platform() -> (&'static str, &'static str) {
    let os = std::env::consts::OS; // "linux", "macos", "windows"
    let arch = std::env::consts::ARCH; // "x86_64", "aarch64"
    (os, arch)
}

/// Check if the current platform is supported for buildah
///
/// Buildah is primarily a Linux tool, though it can work on macOS
/// through virtualization.
#[must_use]
pub fn is_platform_supported() -> bool {
    let (os, arch) = current_platform();
    matches!((os, arch), ("linux" | "macos", "x86_64" | "aarch64"))
}

/// Get installation instructions for the current platform
#[must_use]
pub fn install_instructions() -> String {
    let (os, _arch) = current_platform();

    match os {
        "linux" => {
            // Try to detect the Linux distribution
            if let Some(distro) = detect_linux_distro() {
                match distro.as_str() {
                    "ubuntu" | "debian" | "pop" | "mint" | "elementary" => {
                        "Install with: sudo apt install buildah".to_string()
                    }
                    "fedora" | "rhel" | "centos" | "rocky" | "alma" => {
                        "Install with: sudo dnf install buildah".to_string()
                    }
                    "arch" | "manjaro" | "endeavouros" => {
                        "Install with: sudo pacman -S buildah".to_string()
                    }
                    "opensuse" | "suse" => "Install with: sudo zypper install buildah".to_string(),
                    "alpine" => "Install with: sudo apk add buildah".to_string(),
                    "gentoo" => "Install with: sudo emerge app-containers/buildah".to_string(),
                    "void" => "Install with: sudo xbps-install buildah".to_string(),
                    _ => "Install buildah using your distribution's package manager.\n\
                         Common commands:\n\
                         - Debian/Ubuntu: sudo apt install buildah\n\
                         - Fedora/RHEL: sudo dnf install buildah\n\
                         - Arch: sudo pacman -S buildah\n\
                         - openSUSE: sudo zypper install buildah"
                        .to_string(),
                }
            } else {
                "Install buildah using your distribution's package manager.\n\
                 Common commands:\n\
                 - Debian/Ubuntu: sudo apt install buildah\n\
                 - Fedora/RHEL: sudo dnf install buildah\n\
                 - Arch: sudo pacman -S buildah\n\
                 - openSUSE: sudo zypper install buildah"
                    .to_string()
            }
        }
        "macos" => "Buildah is not required on macOS -- ZLayer uses the native \
             sandbox builder instead. If you prefer buildah, install with: \
             brew install buildah (requires a Linux VM for container operations)."
            .to_string(),
        "windows" => "Buildah is not natively supported on Windows.\n\
             Consider using WSL2 with a Linux distribution and installing buildah there."
            .to_string(),
        _ => format!("Buildah is not supported on {os}. Use a Linux system."),
    }
}

// ============================================================================
// Internal Helper Functions
// ============================================================================

/// Get the default install directory for `ZLayer` binaries
fn default_install_dir() -> PathBuf {
    zlayer_paths::ZLayerDirs::system_default().bin()
}

/// Get all paths to search for buildah
fn get_search_paths(install_dir: &Path) -> Vec<PathBuf> {
    let mut paths = vec![
        // Custom install directory
        install_dir.join("buildah"),
        // User ZLayer directory
        zlayer_paths::ZLayerDirs::system_default()
            .bin()
            .join("buildah"),
        // System ZLayer directory
        PathBuf::from("/usr/local/lib/zlayer/buildah"),
        // Standard system paths
        PathBuf::from("/usr/bin/buildah"),
        PathBuf::from("/usr/local/bin/buildah"),
        PathBuf::from("/bin/buildah"),
    ];

    // Deduplicate while preserving order
    let mut seen = std::collections::HashSet::new();
    paths.retain(|p| seen.insert(p.clone()));

    paths
}

/// Find a binary in PATH using the `which` command
async fn find_in_path(binary: &str) -> Option<PathBuf> {
    let output = Command::new("which")
        .arg(binary)
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .output()
        .await
        .ok()?;

    if output.status.success() {
        let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
        if !path.is_empty() {
            return Some(PathBuf::from(path));
        }
    }

    // Also try `command -v` as a fallback (works in more shells)
    let output = Command::new("sh")
        .args(["-c", &format!("command -v {binary}")])
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .output()
        .await
        .ok()?;

    if output.status.success() {
        let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
        if !path.is_empty() {
            return Some(PathBuf::from(path));
        }
    }

    None
}

/// Parse version from buildah --version output
///
/// Expected formats:
/// - "buildah version 1.33.0 (image-spec 1.0.2-dev, runtime-spec 1.0.2-dev)"
/// - "buildah version 1.33.0"
/// - "buildah version 1.34.0-dev"
fn parse_version(output: &str) -> Result<String, InstallError> {
    // Look for "buildah version X.Y.Z" pattern
    let output = output.trim();

    // Try to extract version after "version"
    if let Some(pos) = output.to_lowercase().find("version") {
        let after_version = &output[pos + "version".len()..].trim_start();

        // Extract the version number (digits, dots, and optional suffix like -dev, -rc1)
        // Version format: X.Y.Z or X.Y.Z-suffix
        let version: String = after_version
            .chars()
            .take_while(|c| c.is_ascii_alphanumeric() || *c == '.' || *c == '-')
            .collect();

        // Clean up: trim trailing hyphens if any
        let version = version.trim_end_matches('-');

        if !version.is_empty() && version.contains('.') {
            return Ok(version.to_string());
        }
    }

    Err(InstallError::VersionParse(format!(
        "Could not parse version from: {output}"
    )))
}

/// Check if a version string meets the minimum requirement
///
/// Uses simple semantic version comparison.
fn version_meets_minimum(version: &str, minimum: &str) -> bool {
    let parse_version = |s: &str| -> Option<Vec<u32>> {
        // Strip any trailing non-numeric parts (like "-dev")
        let clean = s.split('-').next()?;
        clean
            .split('.')
            .map(|p| p.parse::<u32>().ok())
            .collect::<Option<Vec<_>>>()
    };

    let Some(version_parts) = parse_version(version) else {
        return false;
    };

    let Some(minimum_parts) = parse_version(minimum) else {
        return true; // If we can't parse minimum, assume it's met
    };

    // Compare version parts
    for (v, m) in version_parts.iter().zip(minimum_parts.iter()) {
        match v.cmp(m) {
            std::cmp::Ordering::Greater => return true,
            std::cmp::Ordering::Less => return false,
            std::cmp::Ordering::Equal => {}
        }
    }

    // If all compared parts are equal, check if version has at least as many parts
    version_parts.len() >= minimum_parts.len()
}

/// Detect the Linux distribution
fn detect_linux_distro() -> Option<String> {
    // Try /etc/os-release first (most modern distros)
    if let Ok(contents) = std::fs::read_to_string("/etc/os-release") {
        for line in contents.lines() {
            if let Some(id) = line.strip_prefix("ID=") {
                return Some(id.trim_matches('"').to_lowercase());
            }
        }
    }

    // Try /etc/lsb-release as fallback
    if let Ok(contents) = std::fs::read_to_string("/etc/lsb-release") {
        for line in contents.lines() {
            if let Some(id) = line.strip_prefix("DISTRIB_ID=") {
                return Some(id.trim_matches('"').to_lowercase());
            }
        }
    }

    None
}

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

    #[test]
    fn test_parse_version_full() {
        let output = "buildah version 1.33.0 (image-spec 1.0.2-dev, runtime-spec 1.0.2-dev)";
        let version = parse_version(output).unwrap();
        assert_eq!(version, "1.33.0");
    }

    #[test]
    fn test_parse_version_simple() {
        let output = "buildah version 1.28.0";
        let version = parse_version(output).unwrap();
        assert_eq!(version, "1.28.0");
    }

    #[test]
    fn test_parse_version_with_newline() {
        let output = "buildah version 1.33.0\n";
        let version = parse_version(output).unwrap();
        assert_eq!(version, "1.33.0");
    }

    #[test]
    fn test_parse_version_dev() {
        let output = "buildah version 1.34.0-dev";
        let version = parse_version(output).unwrap();
        assert_eq!(version, "1.34.0-dev");
    }

    #[test]
    fn test_parse_version_invalid() {
        let output = "some random output";
        assert!(parse_version(output).is_err());
    }

    #[test]
    fn test_version_meets_minimum_equal() {
        assert!(version_meets_minimum("1.28.0", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_greater_patch() {
        assert!(version_meets_minimum("1.28.1", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_greater_minor() {
        assert!(version_meets_minimum("1.29.0", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_greater_major() {
        assert!(version_meets_minimum("2.0.0", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_less_patch() {
        assert!(!version_meets_minimum("1.27.5", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_less_minor() {
        assert!(!version_meets_minimum("1.20.0", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_less_major() {
        assert!(!version_meets_minimum("0.99.0", "1.28.0"));
    }

    #[test]
    fn test_version_meets_minimum_with_dev_suffix() {
        assert!(version_meets_minimum("1.34.0-dev", "1.28.0"));
    }

    #[test]
    fn test_current_platform() {
        let (os, arch) = current_platform();
        // Just verify it returns non-empty strings
        assert!(!os.is_empty());
        assert!(!arch.is_empty());
    }

    #[test]
    fn test_is_platform_supported() {
        // This test will pass on Linux and macOS x86_64/aarch64
        let (os, arch) = current_platform();
        let supported = is_platform_supported();

        match (os, arch) {
            ("linux" | "macos", "x86_64" | "aarch64") => assert!(supported),
            _ => assert!(!supported),
        }
    }

    #[test]
    fn test_install_instructions_not_empty() {
        let instructions = install_instructions();
        assert!(!instructions.is_empty());
    }

    #[test]
    fn test_default_install_dir() {
        let dir = default_install_dir();
        // Should end with bin
        assert!(dir.to_string_lossy().contains("bin") || dir.to_string_lossy().contains("zlayer"));
    }

    #[test]
    fn test_get_search_paths_not_empty() {
        let install_dir = PathBuf::from("/tmp/zlayer-test");
        let paths = get_search_paths(&install_dir);
        assert!(!paths.is_empty());
        // First path should be in our custom install dir
        assert!(paths[0].starts_with("/tmp/zlayer-test"));
    }

    #[test]
    fn test_installer_creation() {
        let installer = BuildahInstaller::new();
        assert_eq!(installer.min_version(), MIN_BUILDAH_VERSION);
    }

    #[test]
    fn test_installer_with_custom_dir() {
        let custom_dir = PathBuf::from("/custom/path");
        let installer = BuildahInstaller::with_install_dir(custom_dir.clone());
        assert_eq!(installer.install_dir(), custom_dir);
    }

    #[tokio::test]
    async fn test_find_in_path_nonexistent() {
        // This binary should not exist
        let result = find_in_path("this_binary_should_not_exist_12345").await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_find_in_path_exists() {
        // 'sh' should exist on any Unix system
        let result = find_in_path("sh").await;
        assert!(result.is_some());
    }

    // Integration test - only runs if buildah is installed
    #[tokio::test]
    #[ignore = "requires buildah to be installed"]
    async fn test_find_existing_buildah() {
        let installer = BuildahInstaller::new();
        let result = installer.find_existing().await;
        assert!(result.is_some());
        let installation = result.unwrap();
        assert!(installation.path.exists());
        assert!(!installation.version.is_empty());
    }

    #[tokio::test]
    #[ignore = "requires buildah to be installed"]
    async fn test_check_buildah() {
        let installer = BuildahInstaller::new();
        let result = installer.check().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    #[ignore = "requires buildah to be installed"]
    async fn test_ensure_buildah() {
        let installer = BuildahInstaller::new();
        let result = installer.ensure().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    #[ignore = "requires buildah to be installed"]
    async fn test_get_version() {
        let installer = BuildahInstaller::new();
        if let Some(installation) = installer.find_existing().await {
            let version = BuildahInstaller::get_version(&installation.path).await;
            assert!(version.is_ok());
            let version = version.unwrap();
            assert!(version.contains('.'));
        }
    }
}