Skip to content

Commit

Permalink
Use recipe image version as fallback if there are errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpinder committed Feb 3, 2024
1 parent afe4d36 commit 670b333
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl BuildCommand {
trace!("recipe: {recipe:#?}");

// Get values for image
let tags = recipe.generate_tags()?;
let tags = recipe.generate_tags();
let image_name = self.generate_full_image_name(&recipe)?;
let first_image_name = if self.archive.is_some() {
image_name.to_string()
Expand Down Expand Up @@ -271,7 +271,7 @@ impl BuildCommand {
trace!("BuildCommand::build_image()");
let recipe: Recipe = serde_yaml::from_str(fs::read_to_string(&self.recipe)?.as_str())?;

let tags = recipe.generate_tags()?;
let tags = recipe.generate_tags();

let image_name = self.generate_full_image_name(&recipe)?;

Expand Down
47 changes: 34 additions & 13 deletions src/module_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
process::{self, Command},
};

use anyhow::{bail, Result};
use anyhow::Result;
use chrono::Local;
use indexmap::IndexMap;
use log::{debug, error, info, trace, warn};
Expand Down Expand Up @@ -46,12 +46,12 @@ pub struct Recipe<'a> {
}

impl<'a> Recipe<'a> {
pub fn generate_tags(&self) -> Result<Vec<String>> {
pub fn generate_tags(&self) -> Vec<String> {
trace!("Recipe::generate_tags()");
trace!("Generating image tags for {}", &self.name);

let mut tags: Vec<String> = Vec::new();
let image_version = self.get_os_version()?;
let image_version = self.get_os_version();
let timestamp = Local::now().format("%Y%m%d").to_string();

if let (Ok(commit_branch), Ok(default_branch), Ok(commit_sha), Ok(pipeline_source)) = (
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<'a> Recipe<'a> {
debug!("Finished generating tags!");
debug!("Tags: {tags:#?}");

Ok(tags)
tags
}

/// # Parse a recipe file
Expand Down Expand Up @@ -149,31 +149,52 @@ impl<'a> Recipe<'a> {
})
}

fn get_os_version(&self) -> Result<String> {
fn get_os_version(&self) -> String {
trace!("Recipe::get_os_version()");
check_command_exists("skopeo")?;

if check_command_exists("skopeo").is_err() {
warn!("The 'skopeo' command doesn't exist, falling back to version defined in recipe");
return self.image_version.to_string();
}

let base_image = self.base_image.as_ref();
let image_version = self.image_version.as_ref();

info!("Retrieving information from {base_image}:{image_version}, this will take a bit");

let output = Command::new("skopeo")
let output = match Command::new("skopeo")
.arg("inspect")
.arg(format!("docker://{base_image}:{image_version}"))
.output()?;
.output()
{
Err(_) => {
warn!(
"Issue running the 'skopeo' command, falling back to version defined in recipe"
);
return self.image_version.to_string();
}
Ok(output) => output,
};

if !output.status.success() {
bail!("Failed to get image information for {base_image}:{image_version}");
warn!("Failed to get image information for {base_image}:{image_version}, falling back to version defined in recipe");
return self.image_version.to_string();
}

let inspection: ImageInspection =
serde_json::from_str(String::from_utf8(output.stdout)?.as_str())?;
let inspection: ImageInspection = match serde_json::from_str(
String::from_utf8_lossy(&output.stdout).as_ref(),
) {
Err(_) => {
warn!("Issue deserializing 'skopeo' output, falling back to version defined in recipe");
return self.image_version.to_string();
}
Ok(inspection) => inspection,
};

Ok(inspection.get_version().unwrap_or_else(|| {
inspection.get_version().unwrap_or_else(|| {
warn!("Version label does not exist on image, using version in recipe");
image_version.to_string()
}))
})
}
}

Expand Down

0 comments on commit 670b333

Please sign in to comment.