-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
74 changed files
with
272 additions
and
129 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.