Skip to content

Commit

Permalink
wip: reorganize crates
Browse files Browse the repository at this point in the history
  • Loading branch information
cilki committed Nov 12, 2023
1 parent 9daf9c9 commit cca0620
Show file tree
Hide file tree
Showing 74 changed files with 272 additions and 129 deletions.
103 changes: 99 additions & 4 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ strip = true
[workspace]
members = [
"goldboot",
"goldboot-build",
"goldboot-image",
"goldboot-linux",
"goldboot-registry",
]
13 changes: 13 additions & 0 deletions goldboot-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "goldboot-build"
description = "Support for building goldboot images"
version = "0.0.1"
edition = "2021"
license = "AGPL-3.0-only"
authors = ["Tyler Cook"]
homepage = "https://goldboot.fossable.org"
repository = "https://github.com/fossable/goldboot"

[dependencies]

[dev-dependencies]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
135 changes: 135 additions & 0 deletions goldboot-build/src/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use crate::progress::ProgressBar;
use log::{debug, info};
use sha1::{Digest, Sha1};
use sha2::{Sha256, Sha512};
use simple_error::bail;
use std::{
error::Error,
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
};

///! Contains general-purpose sources for use in templates.
pub mod iso;

/// All builds start with a single `Source` which provides the initial image
/// to be subjected to further customizations.
pub trait Source {
fn load(&self) -> Result<String, Box<dyn Error>>;
}

/// A cache for source installation media like ISOs.
pub struct SourceCache {
pub directory: PathBuf,
}

impl SourceCache {
/// Get the default source cache.
pub fn default() -> Result<Self, Box<dyn Error>> {
let directory = if cfg!(target_os = "linux") {
PathBuf::from(format!(
"/home/{}/.cache/goldboot/sources",
whoami::username()
))
} else if cfg!(target_os = "macos") {
PathBuf::from(format!(
"/Users/{}/.cache/goldboot/sources",
whoami::username()
))
} else if cfg!(target_os = "windows") {
PathBuf::from(format!(
"C:/Users/{}/AppData/Local/goldboot/cache/sources",
whoami::username()
))
} else {
bail!("Unsupported platform");
};

// Make sure it exists before we return
std::fs::create_dir_all(&directory)?;

Ok(Self { directory })
}

pub fn get(&self, url: String, checksum: &str) -> Result<String, Box<dyn Error>> {
let id = hex::encode(Sha1::new().chain_update(&url).finalize());
let path = self.directory.join(id);

// Delete file if the checksum doesn't match
if path.is_file() {
if !verify_checksum(path.to_string_lossy().to_string(), checksum).is_ok() {
info!("Deleting corrupt cached file");
std::fs::remove_file(&path)?;
}
}

if !path.is_file() {
// Check for local URL
if !url.starts_with("http") && Path::new(&url).is_file() {
return Ok(url);
}

// Try to download it
let rs = reqwest::blocking::get(&url)?;
if rs.status().is_success() {
let length = rs.content_length().ok_or("Failed to get content length")?;
let mut file = File::create(&path)?;

info!("Saving install media");
ProgressBar::Download.copy(&mut rs, &mut file, length)?;
} else {
bail!("Failed to download");
}

verify_checksum(path.to_string_lossy().to_string(), checksum)?;
}

Ok(path.to_string_lossy().to_string())
}

fn verify_checksum(path: String, checksum: &str) -> Result<(), Box<dyn Error>> {
// "None" shortcut
if checksum == "none" {
return Ok(());
}

let c: Vec<&str> = checksum.split(":").collect();
if c.len() != 2 {
bail!("Invalid checksum: {}", checksum);
}

let mut file = File::open(&path)?;

let hash = match c[0] {
"sha1" | "SHA1" => {
info!("Computing SHA1 checksum");
let mut hasher = Sha1::new();
ProgressBar::Hash.copy(&mut file, &mut hasher, std::fs::metadata(&path)?.len())?;
hex::encode(hasher.finalize())
}
"sha256" | "SHA256" => {
info!("Computing SHA256 checksum");
let mut hasher = Sha256::new();
ProgressBar::Hash.copy(&mut file, &mut hasher, std::fs::metadata(&path)?.len())?;
hex::encode(hasher.finalize())
}
"sha512" | "SHA512" => {
info!("Computing SHA512 checksum");
let mut hasher = Sha512::new();
ProgressBar::Hash.copy(&mut file, &mut hasher, std::fs::metadata(&path)?.len())?;
hex::encode(hasher.finalize())
}
_ => bail!("Unsupported hash"),
};

debug!("Computed: {}", &hash);
debug!("Expected: {}", &c[1]);

if hash != c[1] {
bail!("Hash mismatch");
}

Ok(())
}
}
File renamed without changes.
File renamed without changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{build::BuildWorker, cache::MediaCache, provisioners::*, qemu::QemuArgs, templates::*};
use crate::{build::BuildWorker, provisioners::*, qemu::QemuArgs, templates::*};
use log::info;
use serde::{Deserialize, Serialize};
use simple_error::bail;
Expand All @@ -11,6 +11,7 @@ use validator::Validate;
#[derive(Clone, Serialize, Deserialize, Validate, Debug)]
pub struct ArchLinuxTemplate {
pub source: sources::ArchSource,
pub installer: installer::ArchLinuxInstaller,
pub provisioners: Option<Vec<provisioners::ArchProvisioner>>,
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions goldboot-image/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "goldboot-image"
description = "Defines the goldboot image format"
version = "0.0.1"
edition = "2021"
license = "AGPL-3.0-only"
authors = ["Tyler Cook"]
homepage = "https://goldboot.fossable.org"
repository = "https://github.com/fossable/goldboot"

[dependencies]
aes-gcm = { version = "0.10.3", features = ["std"] }
binrw = "0.13.1"

[dev-dependencies]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions goldboot-linux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
license = "AGPL-3.0-only"
authors = ["Tyler Cook"]
readme = "README.md"
homepage = "https://goldboot.org"
repository = "https://github.com/goldboot/goldboot/"
homepage = "https://goldboot.fossable.org"
repository = "https://github.com/fossable/goldboot"

[dependencies]
block-utils = "0.11.1"
Expand Down
6 changes: 3 additions & 3 deletions goldboot-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2021"
license = "AGPL-3.0-only"
authors = ["Tyler Cook"]
readme = "README.md"
homepage = "https://goldboot.org"
repository = "https://github.com/goldboot/goldboot/"
homepage = "https://goldboot.fossable.org"
repository = "https://github.com/fossable/goldboot"

[dependencies]
env_logger = "0.10.0"
goldboot = { path="../goldboot", version = "0.0.1" }
goldboot-build = { path="../goldboot-build", version = "0.0.1" }
log = { version = "0.4.20", default-features = false }
reqwest = { version = "0.11.22", features = ["stream"] }
rustls = "0.21.8"
Expand Down
1 change: 1 addition & 0 deletions goldboot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ reqwest = { version = "0.11.22", features = ["stream", "blocking", "json"] }
rust-embed = "8.0.0"
serde = { version = "1.0.190", features = ["derive"] }
serde_json = "1.0.108"
serde_win_unattend = "0.1.0"
serde_yaml = "0.9.27"
sha1 = "0.10.6"
sha2 = "0.10.8"
Expand Down
Loading

0 comments on commit cca0620

Please sign in to comment.