Skip to content

Commit

Permalink
fix: Use lenient_semver for build drivers version check to handle pre…
Browse files Browse the repository at this point in the history
…-release versions
  • Loading branch information
gmpinder committed Feb 11, 2025
1 parent e88c756 commit 003e473
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 26 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ comlexr = "1"
indexmap = { version = "2", features = ["serde"] }
indicatif = { version = "0.17", features = ["improved_unicode"] }
log = "0.4"
miette = "7"
nix = { version = "0.29" }
oci-distribution = { version = "0.11", default-features = false }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
miette = "7"
rstest = "0.18"
semver = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
3 changes: 1 addition & 2 deletions process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ path = "process.rs"
anyhow = "1"
blue-build-utils = { version = "=0.9.6", path = "../utils" }
indicatif-log-bridge = "0.2"
lenient_semver = "0.4"
log4rs = { version = "1", features = ["background_rotation"] }
nu-ansi-term = { version = "0.50", features = ["gnu_legacy"] }
once_cell = "1"
Expand All @@ -36,7 +35,7 @@ miette.workspace = true
nix = { workspace = true, features = ["signal", "user"] }
oci-distribution.workspace = true
reqwest.workspace = true
semver = { workspace = true, features = ["serde"] }
semver.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
Expand Down
14 changes: 8 additions & 6 deletions process/drivers/buildah_driver.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{io::Write, process::Stdio};

use blue_build_utils::credentials::Credentials;
use blue_build_utils::{credentials::Credentials, semver::Version};
use colored::Colorize;
use comlexr::cmd;
use log::{debug, error, info, trace};
use miette::{bail, miette, IntoDiagnostic, Result};
use semver::Version;
use serde::Deserialize;

use crate::{drivers::types::Platform, logging::CommandLogging};
Expand Down Expand Up @@ -36,10 +35,13 @@ impl DriverVersion for BuildahDriver {
fn version() -> Result<Version> {
trace!("BuildahDriver::version()");

trace!("buildah version --json");
let output = cmd!("buildah", "version", "--json")
.output()
.into_diagnostic()?;
let output = {
let c = cmd!("buildah", "version", "--json");
trace!("{c:?}");
c
}
.output()
.into_diagnostic()?;

let version_json: BuildahVersionJson = serde_json::from_slice(&output.stdout)
.inspect_err(|e| error!("{e}: {}", String::from_utf8_lossy(&output.stdout)))
Expand Down
4 changes: 3 additions & 1 deletion process/drivers/docker_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
use blue_build_utils::{
constants::{BB_BUILDKIT_CACHE_GHA, DOCKER_HOST, GITHUB_ACTIONS},
credentials::Credentials,
semver::Version,
string_vec,
};
use cached::proc_macro::cached;
Expand All @@ -17,7 +18,6 @@ use log::{debug, info, trace, warn};
use miette::{bail, IntoDiagnostic, Result};
use oci_distribution::Reference;
use once_cell::sync::Lazy;
use semver::Version;
use serde::Deserialize;
use tempfile::TempDir;

Expand Down Expand Up @@ -124,6 +124,8 @@ impl DriverVersion for DockerDriver {
const VERSION_REQ: &'static str = ">=23";

fn version() -> Result<Version> {
trace!("DockerDriver::version()");

let output = {
let c = cmd!("docker", "version", "-f", "json");
trace!("{c:?}");
Expand Down
14 changes: 8 additions & 6 deletions process/drivers/podman_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use std::{
time::Duration,
};

use blue_build_utils::credentials::Credentials;
use blue_build_utils::{credentials::Credentials, semver::Version};
use cached::proc_macro::cached;
use colored::Colorize;
use comlexr::{cmd, pipe};
use indicatif::{ProgressBar, ProgressStyle};
use log::{debug, error, info, trace};
use miette::{bail, miette, IntoDiagnostic, Report, Result};
use oci_distribution::Reference;
use semver::Version;
use serde::Deserialize;
use tempfile::TempDir;

Expand Down Expand Up @@ -114,10 +113,13 @@ impl DriverVersion for PodmanDriver {
fn version() -> Result<Version> {
trace!("PodmanDriver::version()");

trace!("podman version -f json");
let output = cmd!("podman", "version", "-f", "json")
.output()
.into_diagnostic()?;
let output = {
let c = cmd!("podman", "version", "-f", "json");
trace!("{c:?}");
c
}
.output()
.into_diagnostic()?;

let version_json: PodmanVersionJson = serde_json::from_slice(&output.stdout)
.inspect_err(|e| error!("{e}: {}", String::from_utf8_lossy(&output.stdout)))
Expand Down
4 changes: 2 additions & 2 deletions process/drivers/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::{
process::{ExitStatus, Output},
};

use blue_build_utils::{constants::COSIGN_PUB_PATH, retry, string_vec};
use blue_build_utils::{constants::COSIGN_PUB_PATH, retry, semver::Version, string_vec};
use log::{debug, info, trace};
use miette::{bail, Context, IntoDiagnostic, Result};
use oci_distribution::Reference;
use semver::{Version, VersionReq};
use semver::VersionReq;

use crate::drivers::{functions::get_private_key, types::CiDriverType, Driver};

Expand Down
11 changes: 7 additions & 4 deletions process/drivers/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, env};

use blue_build_utils::constants::{GITHUB_ACTIONS, GITLAB_CI, IMAGE_VERSION_LABEL};
use blue_build_utils::{
constants::{GITHUB_ACTIONS, GITLAB_CI, IMAGE_VERSION_LABEL},
semver::Version,
};
use clap::ValueEnum;
use log::trace;
use serde::Deserialize;
Expand Down Expand Up @@ -233,9 +236,9 @@ impl ImageMetadata {
pub fn get_version(&self) -> Option<u64> {
Some(
self.labels
.get(IMAGE_VERSION_LABEL)?
.as_str()
.and_then(|v| lenient_semver::parse(v).ok())?
.get(IMAGE_VERSION_LABEL)
.map(ToOwned::to_owned)
.and_then(|v| serde_json::from_value::<Version>(v).ok())?
.major,
)
}
Expand Down
2 changes: 2 additions & 0 deletions utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ constcat = "0.6"
directories = "6"
docker_credential = "1"
format_serde_error = "0.3"
lenient_semver = "0.4"
process_control = { version = "4", features = ["crossbeam-channel"] }
which = "7"

Expand All @@ -24,6 +25,7 @@ clap = { workspace = true, features = ["derive", "env"] }
comlexr.workspace = true
log.workspace = true
miette.workspace = true
semver.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
Expand Down
1 change: 1 addition & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod command_output;
pub mod constants;
pub mod credentials;
mod macros;
pub mod semver;
pub mod syntax_highlighting;
#[cfg(feature = "test")]
pub mod test_utils;
Expand Down
31 changes: 31 additions & 0 deletions utils/src/semver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use serde::{de::Error, Deserialize};

#[derive(Debug)]
pub struct Version(semver::Version);

impl std::ops::Deref for Version {
type Target = semver::Version;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl<'de> Deserialize<'de> for Version {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let ver = String::deserialize(deserializer)?;
lenient_semver::parse(&ver)
.ok()
.map(Self)
.ok_or_else(|| D::Error::custom(format!("Failed to deserialize version {ver}")))
}
}

0 comments on commit 003e473

Please sign in to comment.