feat: Add logging

This commit is contained in:
Gerald Pinder 2023-12-18 00:17:15 -05:00
parent f437bdaffa
commit b83cf574b8
5 changed files with 376 additions and 96 deletions

185
Cargo.lock generated
View file

@ -61,7 +61,7 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -71,7 +71,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"windows-sys", "windows-sys 0.48.0",
] ]
[[package]] [[package]]
@ -92,6 +92,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@ -143,7 +149,7 @@ dependencies = [
"js-sys", "js-sys",
"num-traits", "num-traits",
"wasm-bindgen", "wasm-bindgen",
"windows-targets", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -187,6 +193,16 @@ dependencies = [
"clap_derive", "clap_derive",
] ]
[[package]]
name = "clap-verbosity-flag"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c90e95e5bd4e8ac34fa6f37c774b0c6f8ed06ea90c79931fd448fcf941a9767"
dependencies = [
"clap",
"log",
]
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.4.4" version = "4.4.4"
@ -264,6 +280,19 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "env_logger"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]] [[package]]
name = "envmnt" name = "envmnt"
version = "0.8.4" version = "0.8.4"
@ -280,6 +309,16 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -341,7 +380,7 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
"ignore", "ignore",
"walkdir", "walkdir",
] ]
@ -364,6 +403,12 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]] [[package]]
name = "humansize" name = "humansize"
version = "2.1.3" version = "2.1.3"
@ -373,6 +418,12 @@ dependencies = [
"libm", "libm",
] ]
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.57" version = "0.1.57"
@ -433,6 +484,17 @@ dependencies = [
"hashbrown 0.14.0", "hashbrown 0.14.0",
] ]
[[package]]
name = "is-terminal"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
@ -466,6 +528,12 @@ version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4" checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4"
[[package]]
name = "linux-raw-sys"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.20" version = "0.4.20"
@ -680,6 +748,19 @@ version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]]
name = "rustix"
version = "0.38.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3"
dependencies = [
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "rusty-hook" name = "rusty-hook"
version = "0.11.2" version = "0.11.2"
@ -816,6 +897,15 @@ dependencies = [
"unic-segment", "unic-segment",
] ]
[[package]]
name = "termcolor"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.48" version = "1.0.48"
@ -869,6 +959,9 @@ dependencies = [
"cfg-if", "cfg-if",
"chrono", "chrono",
"clap", "clap",
"clap-verbosity-flag",
"env_logger",
"log",
"rusty-hook", "rusty-hook",
"serde", "serde",
"serde_json", "serde_json",
@ -1069,7 +1162,7 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -1078,7 +1171,16 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
] ]
[[package]] [[package]]
@ -1087,13 +1189,28 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.48.5",
"windows_i686_gnu", "windows_i686_gnu 0.48.5",
"windows_i686_msvc", "windows_i686_msvc 0.48.5",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
] ]
[[package]] [[package]]
@ -1102,38 +1219,80 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

View file

@ -15,6 +15,9 @@ anyhow = "1.0.75"
cfg-if = "1.0.0" cfg-if = "1.0.0"
chrono = "0.4.31" chrono = "0.4.31"
clap = { version = "4.4.4", features = ["derive"] } clap = { version = "4.4.4", features = ["derive"] }
clap-verbosity-flag = "2.1.1"
env_logger = "0.10.1"
log = "0.4.20"
serde = { version = "1.0.188", features = ["derive"] } serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107" serde_json = "1.0.107"
serde_yaml = "0.9.25" serde_yaml = "0.9.25"

View file

@ -1,7 +1,10 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::process;
use anyhow::Result; use anyhow::Result;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use clap_verbosity_flag::{InfoLevel, Verbosity};
use log::{error, info, trace};
use ublue_rs::{self}; use ublue_rs::{self};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -9,6 +12,9 @@ use ublue_rs::{self};
struct UblueArgs { struct UblueArgs {
#[command(subcommand)] #[command(subcommand)]
command: CommandArgs, command: CommandArgs,
#[clap(flatten)]
verbosity: Verbosity<InfoLevel>,
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
@ -79,13 +85,26 @@ enum CommandArgs {
fn main() -> Result<()> { fn main() -> Result<()> {
let args = UblueArgs::parse(); let args = UblueArgs::parse();
env_logger::builder()
.filter_level(args.verbosity.log_level_filter())
.init();
trace!("{args:#?}");
match args.command { match args.command {
CommandArgs::Template { CommandArgs::Template {
recipe, recipe,
containerfile, containerfile,
output, output,
} => { } => {
ublue_rs::template_file(&recipe, containerfile.as_ref(), output.as_ref())?; info!("Templating for recipe at {}", recipe.display());
if let Err(e) =
ublue_rs::template_file(&recipe, containerfile.as_ref(), output.as_ref())
{
error!("Failed to template file: {e}");
process::exit(1);
}
} }
#[cfg(feature = "init")] #[cfg(feature = "init")]
CommandArgs::Init { dir } => { CommandArgs::Init { dir } => {
@ -106,19 +125,29 @@ fn main() -> Result<()> {
username, username,
password, password,
} => { } => {
ublue_rs::template_file( info!("Templating for recipe at {}", recipe.display());
if let Err(e) = ublue_rs::template_file(
&recipe, &recipe,
containerfile.as_ref(), containerfile.as_ref(),
Some(&PathBuf::from("Containerfile")), Some(&PathBuf::from("Containerfile")),
)?; ) {
ublue_rs::build::build_image( error!("Failed to template file: {e}");
process::exit(1);
}
info!("Building image for recipe at {}", recipe.display());
if let Err(e) = ublue_rs::build::build_image(
&recipe, &recipe,
registry.as_ref(), registry.as_ref(),
registry_path.as_ref(), registry_path.as_ref(),
username.as_ref(), username.as_ref(),
password.as_ref(), password.as_ref(),
push, push,
)?; ) {
error!("Failed to build image: {e}");
process::exit(1);
}
} }
} }
Ok(()) Ok(())

View file

@ -2,11 +2,15 @@ use std::{env, fs, path::Path, process::Command};
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use chrono::Local; use chrono::Local;
use log::{debug, info, trace, warn};
use crate::module_recipe::Recipe; use crate::module_recipe::Recipe;
fn check_command_exists(command: &str) -> Result<()> { fn check_command_exists(command: &str) -> Result<()> {
eprintln!("Checking if {command} exists..."); debug!("Checking if {command} exists");
trace!("ublue_rs::build::check_command_exists({command})");
trace!("command -v {command}");
match Command::new("command") match Command::new("command")
.arg("-v") .arg("-v")
.arg(command) .arg(command)
@ -14,7 +18,7 @@ fn check_command_exists(command: &str) -> Result<()> {
.success() .success()
{ {
true => { true => {
eprintln!("Command {command} does exist"); debug!("Command {command} does exist");
Ok(()) Ok(())
} }
false => Err(anyhow!( false => Err(anyhow!(
@ -24,48 +28,53 @@ fn check_command_exists(command: &str) -> Result<()> {
} }
fn generate_tags(recipe: &Recipe) -> Vec<String> { fn generate_tags(recipe: &Recipe) -> Vec<String> {
eprintln!("Generating image tags for {}", &recipe.name); debug!("Generating image tags for {}", &recipe.name);
trace!("ublue_rs::build::generate_tags({recipe:?})");
let mut tags: Vec<String> = Vec::new(); let mut tags: Vec<String> = Vec::new();
let image_version = recipe.image_version; let image_version = recipe.image_version;
let timestamp = Local::now().format("%Y%m%d").to_string(); let timestamp = Local::now().format("%Y%m%d").to_string();
if env::var("CI").is_ok() { if env::var("CI").is_ok() {
eprintln!("Detected running in Gitlab, pulling information from CI variables..."); warn!("Detected running in Gitlab, pulling information from CI variables");
if let (Ok(mr_iid), Ok(pipeline_source)) = ( if let (Ok(mr_iid), Ok(pipeline_source)) = (
env::var("CI_MERGE_REQUEST_IID"), env::var("CI_MERGE_REQUEST_IID"),
env::var("CI_PIPELINE_SOURCE"), env::var("CI_PIPELINE_SOURCE"),
) { ) {
trace!("CI_MERGE_REQUEST_IID={mr_iid}, CI_PIPELINE_SOURCE={pipeline_source}");
if pipeline_source == "merge_request_event" { if pipeline_source == "merge_request_event" {
eprintln!("Running in a MR..."); debug!("Running in a MR");
tags.push(format!("{mr_iid}-{image_version}")); tags.push(format!("{mr_iid}-{image_version}"));
} }
} }
if let Ok(commit_sha) = env::var("CI_COMMIT_SHORT_SHA") { if let Ok(commit_sha) = env::var("CI_COMMIT_SHORT_SHA") {
trace!("CI_COMMIT_SHORT_SHA={commit_sha}");
tags.push(format!("{commit_sha}-{image_version}")); tags.push(format!("{commit_sha}-{image_version}"));
} }
if let (Ok(commit_branch), Ok(default_branch)) = if let (Ok(commit_branch), Ok(default_branch)) = (
(env::var("CI_COMMIT_BRANCH"), env::var("CI_DEFAULT_BRANCH")) env::var("CI_COMMIT_REF_NAME"),
{ env::var("CI_DEFAULT_BRANCH"),
) {
trace!("CI_COMMIT_REF_NAME={commit_branch}, CI_DEFAULT_BRANCH={default_branch}");
if default_branch != commit_branch { if default_branch != commit_branch {
eprintln!("Running on branch {commit_branch}..."); debug!("Running on branch {commit_branch}");
tags.push(format!("br-{commit_branch}-{image_version}")); tags.push(format!("br-{commit_branch}-{image_version}"));
} else { } else {
eprintln!("Running on the default branch..."); debug!("Running on the default branch");
tags.push(image_version.to_string()); tags.push(image_version.to_string());
tags.push(format!("{image_version}-{timestamp}")); tags.push(format!("{image_version}-{timestamp}"));
tags.push(timestamp.to_string()); tags.push(timestamp.to_string());
} }
} }
} else { } else {
eprintln!("Running locally..."); warn!("Running locally");
tags.push(format!("{image_version}-local")); tags.push(format!("{image_version}-local"));
} }
eprintln!("Finished generating tags!"); info!("Finished generating tags!");
eprintln!("Tags: {tags:?}"); trace!("Tags: {tags:#?}");
tags tags
} }
@ -74,7 +83,9 @@ fn login(
username: Option<&String>, username: Option<&String>,
password: Option<&String>, password: Option<&String>,
) -> Result<()> { ) -> Result<()> {
eprintln!("Attempting to login to the registry"); info!("Attempting to login to the registry");
trace!("ublue_rs::build::login({registry:?}, {username:?}, [MASKED])");
let registry = match registry { let registry = match registry {
Some(registry) => registry.to_owned(), Some(registry) => registry.to_owned(),
None => env::var("CI_REGISTRY")?, None => env::var("CI_REGISTRY")?,
@ -90,6 +101,7 @@ fn login(
None => env::var("CI_REGISTRY_PASSWORD")?, None => env::var("CI_REGISTRY_PASSWORD")?,
}; };
trace!("buildah login -u {username} -p [MASKED] {registry}");
match Command::new("buildah") match Command::new("buildah")
.arg("login") .arg("login")
.arg("-u") .arg("-u")
@ -100,10 +112,11 @@ fn login(
.status()? .status()?
.success() .success()
{ {
true => eprintln!("Buildah login success at {registry} for user {username}!"), true => info!("Buildah login success at {registry} for user {username}!"),
false => return Err(anyhow!("Failed to login for buildah!")), false => return Err(anyhow!("Failed to login for buildah!")),
} }
trace!("cosign login -u {username} -p [MASKED] {registry}");
match Command::new("cosign") match Command::new("cosign")
.arg("login") .arg("login")
.arg("-u") .arg("-u")
@ -114,7 +127,7 @@ fn login(
.status()? .status()?
.success() .success()
{ {
true => eprintln!("Cosign login success at {registry} for user {username}!"), true => info!("Cosign login success at {registry} for user {username}!"),
false => return Err(anyhow!("Failed to login for cosign!")), false => return Err(anyhow!("Failed to login for cosign!")),
} }
@ -127,22 +140,27 @@ fn generate_full_image_name(
registry_path: Option<&String>, registry_path: Option<&String>,
push: bool, push: bool,
) -> Result<String> { ) -> Result<String> {
eprintln!("Generating full image name"); info!("Generating full image name");
trace!(
"ublue_rs::build::generate_full_image_name({recipe:#?}, {registry:?}, {registry_path:?})"
);
let image_name = recipe.name.as_str(); let image_name = recipe.name.as_str();
let image_name = if env::var("CI").is_ok() { let image_name = if env::var("CI").is_ok() {
eprintln!("Detected running in Gitlab CI..."); warn!("Detected running in Gitlab CI");
if let (Ok(registry), Ok(project_namespace), Ok(project_name)) = ( if let (Ok(registry), Ok(project_namespace), Ok(project_name)) = (
env::var("CI_REGISTRY"), env::var("CI_REGISTRY"),
env::var("CI_PROJECT_NAMESPACE"), env::var("CI_PROJECT_NAMESPACE"),
env::var("CI_PROJECT_NAME"), env::var("CI_PROJECT_NAME"),
) { ) {
trace!("CI_REGISTRY={registry}, CI_PROJECT_NAMESPACE={project_namespace}, CI_PROJECT_NAME={project_name}");
format!("{registry}/{project_namespace}/{project_name}/{image_name}") format!("{registry}/{project_namespace}/{project_name}/{image_name}")
} else { } else {
bail!("Unable to generate image name for Gitlab CI env!") bail!("Unable to generate image name for Gitlab CI env!")
} }
} else { } else {
eprintln!("Detected running locally..."); warn!("Detected running locally");
if let (Some(registry), Some(registry_path)) = (registry, registry_path) { if let (Some(registry), Some(registry_path)) = (registry, registry_path) {
format!( format!(
"{}/{}/{image_name}", "{}/{}/{image_name}",
@ -157,44 +175,52 @@ fn generate_full_image_name(
} }
}; };
eprintln!("Using image name {image_name}"); info!("Using image name {image_name}");
Ok(image_name) Ok(image_name)
} }
fn build(image_name: &str, tags: &[String], push: bool) -> Result<()> { fn build(image_name: &str, tags: &[String], push: bool) -> Result<()> {
trace!("ublue_rs::build::build({image_name}, {tags:#?}, {push})");
let mut tags_iter = tags.iter(); let mut tags_iter = tags.iter();
let first_tag = tags_iter let first_tag = tags_iter
.next() .next()
.ok_or(anyhow!("We got here with no tags!?"))?; .ok_or(anyhow!("We got here with no tags!?"))?;
let mut build = Command::new("buildah") let full_image = format!("{image_name}:{first_tag}");
trace!("buildah build -t {full_image}");
let status = Command::new("buildah")
.arg("build") .arg("build")
.arg("-t") .arg("-t")
.arg(format!("{image_name}:{first_tag}")) .arg(&full_image)
.spawn()?; .status()?;
let status = build.wait()?;
if status.success() { if status.success() {
eprintln!("Successfully built {image_name}"); info!("Successfully built {image_name}");
} else { } else {
bail!("Failed to build {image_name}"); bail!("Failed to build {image_name}");
} }
if tags.len() > 1 { if tags.len() > 1 {
eprintln!("Tagging all images..."); debug!("Tagging all images");
for tag in tags_iter {
eprintln!("Tagging {image_name} with {tag}");
let mut child = Command::new("buildah")
.arg("tag")
.arg(format!("{image_name}:{first_tag}"))
.arg(format!("{image_name}:{tag}"))
.spawn()?;
if child.wait()?.success() { for tag in tags_iter {
eprintln!("Successfully tagged {image_name}:{tag}!"); debug!("Tagging {image_name} with {tag}");
let tag_image = format!("{image_name}:{tag}");
trace!("buildah tag {full_image} {tag_image}");
let status = Command::new("buildah")
.arg("tag")
.arg(&full_image)
.arg(&tag_image)
.status()?;
if status.success() {
info!("Successfully tagged {image_name}:{tag}!");
} else { } else {
bail!("Failed to tag image {image_name}:{tag}"); bail!("Failed to tag image {image_name}:{tag}");
} }
@ -202,16 +228,20 @@ fn build(image_name: &str, tags: &[String], push: bool) -> Result<()> {
} }
if push { if push {
eprintln!("Pushing all images..."); debug!("Pushing all images");
for tag in tags.iter() { for tag in tags.iter() {
eprintln!("Pushing image {image_name}:{tag}..."); debug!("Pushing image {image_name}:{tag}");
let mut child = Command::new("buildah")
.arg("push")
.arg(format!("{image_name}:{tag}"))
.spawn()?;
if child.wait()?.success() { let tag_image = format!("{image_name}:{tag}");
eprintln!("Successfully pushed {image_name}:{tag}!")
trace!("buildah push {tag_image}");
let status = Command::new("buildah")
.arg("push")
.arg(&tag_image)
.status()?;
if status.success() {
info!("Successfully pushed {image_name}:{tag}!")
} else { } else {
bail!("Failed to push image {image_name}:{tag}"); bail!("Failed to push image {image_name}:{tag}");
} }
@ -224,7 +254,11 @@ fn build(image_name: &str, tags: &[String], push: bool) -> Result<()> {
} }
fn sign_images(image_name: &str, tag: &str) -> Result<()> { fn sign_images(image_name: &str, tag: &str) -> Result<()> {
trace!("ublue_rs::build::sign_images({image_name}, {tag})");
if env::var("SIGSTORE_ID_TOKEN").is_ok() && env::var("CI").is_ok() { if env::var("SIGSTORE_ID_TOKEN").is_ok() && env::var("CI").is_ok() {
debug!("SIGSTORE_ID_TOKEN detected, signing image");
if let ( if let (
Ok(project_url), Ok(project_url),
Ok(default_branch), Ok(default_branch),
@ -238,48 +272,62 @@ fn sign_images(image_name: &str, tag: &str) -> Result<()> {
env::var("CI_SERVER_PROTOCOL"), env::var("CI_SERVER_PROTOCOL"),
env::var("CI_SERVER_HOST"), env::var("CI_SERVER_HOST"),
) { ) {
trace!("CI_PROJECT_URL={project_url}, CI_DEFAULT_BRANCH={default_branch}, CI_COMMIT_REF_NAME={commit_branch}, CI_SERVER_PROTOCOL={server_protocol}, CI_SERVER_HOST={server_host}");
if default_branch == commit_branch { if default_branch == commit_branch {
eprintln!("Retrieving image digest..."); debug!("On default branch, retrieving image digest");
let image_name_tag = format!("{image_name}:{tag}");
let image_url = format!("docker://{image_name_tag}");
trace!("skopeo inspect --format='{{.Digest}}' {image_url}");
let image_digest = String::from_utf8( let image_digest = String::from_utf8(
Command::new("skopeo") Command::new("skopeo")
.arg("inspect") .arg("inspect")
.arg("--format='{{.Digest}}'") .arg("--format='{{.Digest}}'")
.arg(format!("docker://{image_name}:{tag}")) .arg(&image_url)
.output()? .output()?
.stdout, .stdout,
)?; )?;
eprintln!("Signing image: {image_name}@{image_digest}"); let image_digest = format!("{image_name}@{image_digest}");
let mut child = Command::new("cosign") info!("Signing image: {image_digest}");
trace!("cosign sign {image_digest}");
let status = Command::new("cosign")
.arg("sign") .arg("sign")
.arg(format!("{image_name}@{image_digest}")) .arg(&image_digest)
.spawn()?; .status()?;
if child.wait()?.success() { if status.success() {
eprintln!("Successfully signed image!"); info!("Successfully signed image!");
} else { } else {
bail!("Failed to sign image: {image_name}@{image_digest}"); bail!("Failed to sign image: {image_digest}");
} }
let mut child = Command::new("cosign") let cert_ident =
format!("{project_url}//.gitlab-ci.yml@refs/heads/{default_branch}");
let cert_oidc = format!("{server_protocol}://{server_host}");
trace!("cosign verify --certificate-identity {cert_ident}");
let status = Command::new("cosign")
.arg("verify") .arg("verify")
.arg("--certificate-identity") .arg("--certificate-identity")
.arg(format!( .arg(&cert_ident)
"{project_url}//.gitlab-ci.yml@refs/heads/{default_branch}"
))
.arg("--certificate-oidc-issuer") .arg("--certificate-oidc-issuer")
.arg(format!("{server_protocol}://{server_host}")) .arg(&cert_oidc)
.arg(format!("{image_name}:{tag}")) .arg(&image_name_tag)
.spawn()?; .status()?;
if !child.wait()?.success() { if !status.success() {
eprintln!("Failed to verify image!"); bail!("Failed to verify image!");
} }
} }
} }
} else { } else {
eprintln!("No SIGSTORE_ID_TOKEN detected, not signing image"); debug!("No SIGSTORE_ID_TOKEN detected, not signing image");
} }
Ok(()) Ok(())
@ -293,6 +341,7 @@ pub fn build_image(
password: Option<&String>, password: Option<&String>,
push: bool, push: bool,
) -> Result<()> { ) -> Result<()> {
trace!("ublue_rs::build_image({recipe:?}, {registry:?}, {registry_path:?}, {username:?}, [MASKED], {push})");
check_command_exists("buildah")?; check_command_exists("buildah")?;
if push { if push {
check_command_exists("cosign")?; check_command_exists("cosign")?;
@ -310,7 +359,7 @@ pub fn build_image(
} }
build(&image_name, &tags, push)?; build(&image_name, &tags, push)?;
eprintln!("Build complete!"); info!("Build complete!");
Ok(()) Ok(())
} }

View file

@ -18,40 +18,58 @@ pub mod module_recipe;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::{self, read_to_string}, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use anyhow::Result; use anyhow::Result;
use log::{debug, info, trace};
use module_recipe::Recipe; use module_recipe::Recipe;
use tera::{Context, Tera}; use tera::{Context, Tera};
pub const DEFAULT_CONTAINERFILE: &str = include_str!("../templates/Containerfile.tera"); pub const DEFAULT_CONTAINERFILE: &str = include_str!("../templates/Containerfile.tera");
fn setup_tera(recipe: &Path, containerfile: Option<&PathBuf>) -> Result<(Tera, Context)> { fn setup_tera(recipe: &Path, containerfile: Option<&PathBuf>) -> Result<(Tera, Context)> {
let recipe_de = serde_yaml::from_str::<Recipe>(fs::read_to_string(recipe)?.as_str())?; trace!("ublue_rs::setup_tera({recipe:?}, {containerfile:?})");
debug!("Deserializing recipe");
let recipe_de = serde_yaml::from_str::<Recipe>(fs::read_to_string(recipe)?.as_str())?;
trace!("recipe_de: {recipe_de:#?}");
debug!("Building context");
let mut context = Context::from_serialize(recipe_de)?; let mut context = Context::from_serialize(recipe_de)?;
trace!("add to context 'recipe': {recipe:?}");
context.insert("recipe", &recipe); context.insert("recipe", &recipe);
let mut tera = Tera::default(); let mut tera = Tera::default();
match containerfile { match containerfile {
Some(containerfile) => { Some(containerfile) => {
tera.add_raw_template("Containerfile", &read_to_string(containerfile)?)? debug!("Using {} as the template", containerfile.display());
tera.add_raw_template("Containerfile", &fs::read_to_string(containerfile)?)?
} }
None => tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?, None => tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?,
} }
debug!("Registering function `print_containerfile`");
tera.register_function( tera.register_function(
"print_containerfile", "print_containerfile",
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> { |args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
trace!("tera fn print_containerfile({args:#?})");
match args.get("containerfile") { match args.get("containerfile") {
Some(v) => match v.as_str() { Some(v) => match v.as_str() {
Some(containerfile) => Ok(read_to_string(format!( Some(containerfile) => {
"config/containerfiles/{containerfile}/Containerfile" debug!("Loading containerfile contents for {containerfile}");
))?
.into()), let path = format!("config/containerfiles/{containerfile}/Containerfile");
let path = Path::new(path.as_str());
let file = fs::read_to_string(path)?;
trace!("Containerfile contents {}:\n{file}", path.display());
Ok(file.into())
}
None => Err("Arg containerfile wasn't a string".into()), None => Err("Arg containerfile wasn't a string".into()),
}, },
None => Err("Needs the argument 'containerfile'".into()), None => Err("Needs the argument 'containerfile'".into()),
@ -59,33 +77,41 @@ fn setup_tera(recipe: &Path, containerfile: Option<&PathBuf>) -> Result<(Tera, C
}, },
); );
debug!("Registering function `print_module_context`");
tera.register_function( tera.register_function(
"print_module_context", "print_module_context",
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> { |args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
trace!("tera fn print_module_context({args:#?})");
match args.get("module") { match args.get("module") {
Some(v) => Ok(match serde_json::to_string(v) { Some(v) => match serde_json::to_string(v) {
Ok(s) => s, Ok(s) => Ok(s.into()),
Err(_) => "Unable to serialize".into(), Err(e) => Err(format!("Unable to serialize: {e}").into()),
} },
.into()),
None => Err("Needs the argument 'module'".into()), None => Err("Needs the argument 'module'".into()),
} }
}, },
); );
debug!("Registering function `get_module_from_file`");
tera.register_function( tera.register_function(
"get_module_from_file", "get_module_from_file",
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> { |args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
trace!("tera fn get_module_from_file({args:#?})");
match args.get("file") { match args.get("file") {
Some(v) => { Some(v) => {
let file = match v.as_str() { let file = match v.as_str() {
Some(s) => s, Some(s) => s,
None => return Err("Property 'from-file' must be a string".into()), None => return Err("Property 'from-file' must be a string".into()),
}; };
trace!("from-file: {file}");
match serde_yaml::from_str::<tera::Value>( match serde_yaml::from_str::<tera::Value>(
read_to_string(format!("config/{file}"))?.as_str(), fs::read_to_string(format!("config/{file}"))?.as_str(),
) { ) {
Ok(context) => Ok(context), Ok(context) => {
trace!("context: {context}");
Ok(context)
}
Err(_) => Err(format!("Unable to deserialize file {file}").into()), Err(_) => Err(format!("Unable to deserialize file {file}").into()),
} }
} }
@ -102,13 +128,27 @@ pub fn template_file(
containerfile: Option<&PathBuf>, containerfile: Option<&PathBuf>,
output: Option<&PathBuf>, output: Option<&PathBuf>,
) -> Result<()> { ) -> Result<()> {
trace!("ublue_rs::template_file({recipe:?}, {containerfile:?}, {output:?})");
debug!("Setting up tera");
let (tera, context) = setup_tera(recipe, containerfile)?; let (tera, context) = setup_tera(recipe, containerfile)?;
trace!("tera: {tera:#?}");
trace!("context: {context:#?}");
debug!("Rendering Containerfile");
let output_str = tera.render("Containerfile", &context)?; let output_str = tera.render("Containerfile", &context)?;
if let Some(output) = output { if let Some(output) = output {
debug!("Templating to file {}", output.display());
trace!("Containerfile:\n{output_str}");
std::fs::write(output, output_str)?; std::fs::write(output, output_str)?;
} else { } else {
debug!("Templating to stdout");
println!("{output_str}"); println!("{output_str}");
} }
info!("Finished templating Containerfile");
Ok(()) Ok(())
} }