Skip to content

Commit f96a447

Browse files
committed
wip: fix build
1 parent ecd9e1a commit f96a447

File tree

9 files changed

+86
-129
lines changed

9 files changed

+86
-129
lines changed

goldboot/src/builder/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use self::{fabricators::Fabricator, os::Os};
33
use crate::builder::os::BuildImage;
44
use crate::cli::cmd::Commands;
55
use crate::library::ImageLibrary;
6+
use crate::size;
67

78
use anyhow::{Result, anyhow, bail};
89
use byte_unit::Byte;
@@ -90,7 +91,11 @@ impl Builder {
9091
pub fn run(&mut self, cli: Commands) -> Result<()> {
9192
self.start_time = Some(SystemTime::now());
9293

93-
let qcow_size: u64 = self.elements.iter().map(|element| 0 /*TODO*/).sum();
94+
let qcow_size: u64 = self
95+
.elements
96+
.iter()
97+
.map(|element| -> u64 { size!(element).into() })
98+
.sum();
9499

95100
match cli {
96101
Commands::Build {

goldboot/src/builder/options/size.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ impl Validate for Size {
4848
}
4949
}
5050

51+
impl Into<u64> for Size {
52+
fn into(self) -> u64 {
53+
// Assume Size was validated previously
54+
self.0
55+
.parse::<Byte>()
56+
.expect("Size was not validated")
57+
.as_u64()
58+
}
59+
}
60+
5161
#[cfg(test)]
5262
mod tests {
5363
use super::*;
@@ -56,19 +66,8 @@ mod tests {
5666
fn test_valid_sizes() {
5767
// Test various valid size formats
5868
let sizes = vec![
59-
"16G",
60-
"16GB",
61-
"512M",
62-
"512MB",
63-
"1T",
64-
"1TB",
65-
"2048K",
66-
"2048KB",
67-
"1024",
68-
"1 GB",
69-
"1.5 GB",
70-
"100 MiB",
71-
"16GiB",
69+
"16G", "16GB", "512M", "512MB", "1T", "1TB", "2048K", "2048KB", "1024", "1 GB",
70+
"1.5 GB", "100 MiB", "16GiB",
7271
];
7372

7473
for size_str in sizes {
@@ -85,10 +84,10 @@ mod tests {
8584
fn test_invalid_sizes() {
8685
// Test various invalid size formats
8786
let invalid_sizes = vec![
88-
"", // Empty string
89-
"abc", // No numbers
90-
"G16", // Unit before number
91-
"16X", // Invalid unit
87+
"", // Empty string
88+
"abc", // No numbers
89+
"G16", // Unit before number
90+
"16X", // Invalid unit
9291
"hello world", // Completely invalid
9392
];
9493

@@ -106,16 +105,10 @@ mod tests {
106105
fn test_zero_size() {
107106
// Test that zero size is rejected
108107
let size = Size("0".to_string());
109-
assert!(
110-
size.validate().is_err(),
111-
"Expected zero size to be invalid"
112-
);
108+
assert!(size.validate().is_err(), "Expected zero size to be invalid");
113109

114110
let size = Size("0GB".to_string());
115-
assert!(
116-
size.validate().is_err(),
117-
"Expected zero size to be invalid"
118-
);
111+
assert!(size.validate().is_err(), "Expected zero size to be invalid");
119112
}
120113

121114
#[test]
@@ -132,12 +125,7 @@ mod tests {
132125
#[test]
133126
fn test_large_sizes() {
134127
// Test large sizes
135-
let sizes = vec![
136-
"1000T",
137-
"1000TB",
138-
"1PB",
139-
"100000GB",
140-
];
128+
let sizes = vec!["1000T", "1000TB", "1PB", "100000GB"];
141129

142130
for size_str in sizes {
143131
let size = Size(size_str.to_string());

goldboot/src/builder/os/alpine_linux/mod.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use anyhow::Result;
22
use goldboot_image::ImageArch;
33
use serde::{Deserialize, Serialize};
4+
use smart_default::SmartDefault;
45
use std::fmt::Display;
56
use strum::{Display, EnumIter, IntoEnumIterator};
67
use validator::Validate;
78

89
use crate::{
910
builder::{
1011
Builder,
11-
options::{hostname::Hostname, iso::Iso, unix_account::RootPassword},
12+
options::{hostname::Hostname, iso::Iso, size::Size, unix_account::RootPassword},
1213
qemu::{OsCategory, QemuBuilder},
1314
},
1415
cli::prompt::Prompt,
@@ -18,31 +19,21 @@ use crate::{
1819
use super::BuildImage;
1920

2021
/// Produces [Alpine Linux](https://www.alpinelinux.org) images.
21-
#[derive(Clone, Serialize, Deserialize, Validate, Debug, goldboot_macros::Prompt)]
22+
#[derive(Clone, Serialize, Deserialize, Validate, Debug, SmartDefault, goldboot_macros::Prompt)]
2223
pub struct AlpineLinux {
24+
pub size: Size,
2325
pub edition: AlpineEdition,
2426
#[serde(flatten)]
2527
pub hostname: Hostname,
2628
pub release: AlpineRelease,
2729
pub root_password: RootPassword,
30+
#[default(Iso {
31+
url: "https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/x86_64/alpine-standard-3.19.1-x86_64.iso".parse().unwrap(),
32+
checksum: Some("sha256:12addd7d4154df1caf5f258b80ad72e7a724d33e75e6c2e6adc1475298d47155".to_string()),
33+
})]
2834
pub iso: Iso,
2935
}
3036

31-
impl Default for AlpineLinux {
32-
fn default() -> Self {
33-
Self {
34-
edition: AlpineEdition::default(),
35-
hostname: Hostname::default(),
36-
release: AlpineRelease::default(),
37-
root_password: RootPassword::default(),
38-
iso: Iso {
39-
url: "https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/x86_64/alpine-standard-3.19.1-x86_64.iso".parse().unwrap(),
40-
checksum: Some("sha256:12addd7d4154df1caf5f258b80ad72e7a724d33e75e6c2e6adc1475298d47155".to_string()),
41-
},
42-
}
43-
}
44-
}
45-
4637
impl BuildImage for AlpineLinux {
4738
fn build(&self, worker: &Builder) -> Result<()> {
4839
let mut qemu = QemuBuilder::new(&worker, OsCategory::Linux)

goldboot/src/builder/os/arch_linux/archinstall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl From<&super::ArchLinux> for ArchinstallConfig {
249249
no_pkg_lookups: false,
250250
ntp: true,
251251
offline: false,
252-
packages: value.packages.clone().unwrap_or_default().packages,
252+
packages: value.packages.clone().unwrap_or_default().0,
253253
parallel_downloads: 0,
254254
profile_config: None,
255255
silent: true,

goldboot/src/builder/os/debian/mod.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::{Result, bail};
22
use goldboot_image::ImageArch;
33
use serde::{Deserialize, Serialize};
4+
use smart_default::SmartDefault;
45
use std::io::{BufRead, BufReader};
56
use strum::{Display, EnumIter, IntoEnumIterator};
67
use validator::Validate;
@@ -9,7 +10,9 @@ use crate::{
910
builder::{
1011
Builder,
1112
http::HttpServer,
12-
options::{hostname::Hostname, iso::Iso, unix_account::RootPassword},
13+
options::{
14+
arch::Arch, hostname::Hostname, iso::Iso, size::Size, unix_account::RootPassword,
15+
},
1316
qemu::{OsCategory, QemuBuilder},
1417
},
1518
cli::prompt::Prompt,
@@ -24,31 +27,22 @@ use super::BuildImage;
2427
///
2528
/// Upstream: https://www.debian.org
2629
/// Maintainer: cilki
27-
#[derive(Clone, Serialize, Deserialize, Validate, Debug, goldboot_macros::Prompt)]
30+
#[derive(Clone, Serialize, Deserialize, Validate, Debug, SmartDefault, goldboot_macros::Prompt)]
2831
pub struct Debian {
32+
#[default(Arch(ImageArch::Amd64))]
33+
pub arch: Arch,
34+
pub size: Size,
2935
pub edition: DebianEdition,
30-
3136
#[serde(flatten)]
3237
pub hostname: Option<Hostname>,
3338
pub root_password: RootPassword,
34-
39+
#[default(Iso {
40+
url: "http://example.com".parse().unwrap(),
41+
checksum: None,
42+
})]
3543
pub iso: Iso,
3644
}
3745

38-
impl Default for Debian {
39-
fn default() -> Self {
40-
Self {
41-
root_password: RootPassword::default(),
42-
edition: DebianEdition::default(),
43-
hostname: Some(Hostname::default()),
44-
iso: Iso {
45-
url: "https://example.com".parse().unwrap(),
46-
checksum: None,
47-
},
48-
}
49-
}
50-
}
51-
5246
impl BuildImage for Debian {
5347
fn build(&self, worker: &Builder) -> Result<()> {
5448
let mut qemu = QemuBuilder::new(&worker, OsCategory::Linux)

goldboot/src/builder/os/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,16 @@ pub mod nix;
2626
pub mod windows_10;
2727
pub mod windows_11;
2828

29-
/// Macro to access fields from the inner variant of an Os enum.
30-
///
31-
/// Usage: `inner!(os).size` or `inner!(os).hostname`
32-
///
33-
/// This generates a match statement that extracts the inner variant
34-
/// and allows accessing its fields.
3529
#[macro_export]
36-
macro_rules! inner {
30+
macro_rules! size {
3731
($os:expr) => {
38-
match $os {
39-
Os::AlpineLinux(inner)
40-
| Os::ArchLinux(inner)
41-
| Os::Debian(inner)
42-
| Os::Nix(inner)
43-
| Os::Windows10(inner)
44-
| Os::Windows11(inner) => inner,
32+
match $os.clone() {
33+
Os::AlpineLinux(inner) => inner.size,
34+
Os::ArchLinux(inner) => inner.size,
35+
Os::Debian(inner) => inner.size,
36+
Os::Nix(inner) => inner.size,
37+
Os::Windows10(inner) => inner.size,
38+
Os::Windows11(inner) => inner.size,
4539
}
4640
};
4741
}

goldboot/src/builder/os/nix/mod.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use anyhow::Result;
22
use goldboot_image::ImageArch;
33
use serde::{Deserialize, Serialize};
4+
use smart_default::SmartDefault;
45
use std::{collections::HashMap, path::PathBuf};
56
use validator::Validate;
67

78
use crate::{
89
builder::{
910
Builder,
10-
options::iso::Iso,
11+
options::{arch::Arch, iso::Iso, size::Size},
1112
qemu::{OsCategory, QemuBuilder},
1213
},
1314
cli::prompt::Prompt,
@@ -23,30 +24,26 @@ use super::BuildImage;
2324
///
2425
/// Upstream: https://www.nixos.org
2526
/// Maintainer: cilki
26-
#[derive(Clone, Serialize, Deserialize, Validate, Debug, goldboot_macros::Prompt)]
27+
#[derive(Clone, Serialize, Deserialize, Validate, Debug, SmartDefault, goldboot_macros::Prompt)]
2728
pub struct Nix {
29+
#[default(Arch(ImageArch::Amd64))]
30+
pub arch: Arch,
31+
pub size: Size,
32+
2833
/// Path to /etc/nixos/configuration.nix
34+
#[default(ConfigurationPath("configuration.nix".parse().unwrap()))]
2935
pub configuration: ConfigurationPath,
3036

3137
/// Path to /etc/nixos/hardware-configuration.nix
3238
pub hardware_configuration: Option<ConfigurationPath>,
3339

40+
#[default(Iso {
41+
url: "http://example.com".parse().unwrap(),
42+
checksum: None,
43+
})]
3444
pub iso: Iso,
3545
}
3646

37-
impl Default for Nix {
38-
fn default() -> Self {
39-
Self {
40-
configuration: ConfigurationPath(PathBuf::from("configuration.nix")),
41-
hardware_configuration: None,
42-
iso: Iso {
43-
url: "https://example.com".parse().unwrap(),
44-
checksum: None,
45-
},
46-
}
47-
}
48-
}
49-
5047
impl BuildImage for Nix {
5148
fn build(&self, worker: &Builder) -> Result<()> {
5249
let mut qemu = QemuBuilder::new(&worker, OsCategory::Linux)

goldboot/src/builder/os/windows_10/mod.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use anyhow::Result;
44
use goldboot_image::ImageArch;
55
use serde::{Deserialize, Serialize};
66
use serde_win_unattend::*;
7+
use smart_default::SmartDefault;
78
use tracing::debug;
89
use validator::Validate;
910

1011
use crate::{
1112
builder::{
1213
Builder,
13-
options::{hostname::Hostname, iso::Iso},
14+
options::{arch::Arch, hostname::Hostname, iso::Iso, size::Size},
1415
qemu::{OsCategory, QemuBuilder},
1516
},
1617
cli::prompt::Prompt,
@@ -23,32 +24,24 @@ use super::BuildImage;
2324
///
2425
/// Upstream: https://microsoft.com
2526
/// Maintainer: cilki
26-
#[derive(Clone, Serialize, Deserialize, Validate, Debug, goldboot_macros::Prompt)]
27+
#[derive(Clone, Serialize, Deserialize, Validate, Debug, SmartDefault, goldboot_macros::Prompt)]
2728
pub struct Windows10 {
29+
#[default(Arch(ImageArch::Amd64))]
30+
pub arch: Arch,
31+
pub size: Size,
2832
#[serde(flatten)]
2933
pub hostname: Hostname,
3034

3135
// username: String,
3236

3337
// password: String,
38+
#[default(Iso {
39+
url: "http://example.com".parse().unwrap(),
40+
checksum: None,
41+
})]
3442
iso: Iso,
3543
}
3644

37-
impl Default for Windows10 {
38-
fn default() -> Self {
39-
// TODO? https://github.com/pbatard/Fido
40-
Self {
41-
hostname: Hostname::default(),
42-
// username: todo!(),
43-
// password: todo!(),
44-
iso: Iso {
45-
url: "https://example.com".parse().unwrap(),
46-
checksum: None,
47-
},
48-
}
49-
}
50-
}
51-
5245
impl BuildImage for Windows10 {
5346
fn build(&self, worker: &Builder) -> Result<()> {
5447
let unattended = UnattendXml {

0 commit comments

Comments
 (0)