Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: clarify answers.toml format #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 127 additions & 120 deletions proxmox-auto-installer/src/answer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{collections::BTreeMap, net::IpAddr};
// BTreeMap is used to store filters as the order of the filters will be stable, compared to
// storing them in a HashMap

// ----- Start of `answers.toml` format definition -----

#[derive(Clone, Deserialize, Debug)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct Answer {
Expand All @@ -34,6 +36,48 @@ pub struct Global {
pub root_ssh_keys: Vec<String>,
}

#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum KeyboardLayout {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved here from bottom so that Global is fully defined after line 67.

De,
DeCh,
Dk,
EnGb,
EnUs,
Es,
Fi,
Fr,
FrBe,
FrCa,
FrCh,
Hu,
Is,
It,
Jp,
Lt,
Mk,
Nl,
No,
Pl,
Pt,
PtBr,
Se,
Si,
Tr,
}

// This is the format in `answers.toml` because `Network` is constructed from `NetworkInAnswer`.
#[derive(Clone, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct NetworkInAnswer {
#[serde(default)]
pub source: NetworkConfigMode,
pub cidr: Option<CidrAddress>,
pub dns: Option<IpAddr>,
pub gateway: Option<IpAddr>,
pub filter: Option<BTreeMap<String, String>>,
}

#[derive(Clone, Deserialize, Debug, Default, PartialEq)]
#[serde(deny_unknown_fields)]
enum NetworkConfigMode {
Expand All @@ -44,23 +88,87 @@ enum NetworkConfigMode {
FromAnswer,
}

#[derive(Clone, Deserialize, Debug)]
// This is the format in `answers.toml` because `Disks` is constructed from `DiskSetup`.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct NetworkInAnswer {
pub struct DiskSetup {
pub filesystem: Filesystem,
#[serde(default)]
pub source: NetworkConfigMode,
pub cidr: Option<CidrAddress>,
pub dns: Option<IpAddr>,
pub gateway: Option<IpAddr>,
pub disk_list: Vec<String>,
pub filter: Option<BTreeMap<String, String>>,
pub filter_match: Option<FilterMatch>,
pub btrfs: Option<BtrfsOptions>,
pub lvm: Option<LvmOptions>,
pub zfs: Option<ZfsOptions>,
}

#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub enum Filesystem {
Btrfs,
Ext4,
Xfs,
Zfs,
}

#[derive(Clone, Deserialize, Debug, PartialEq, ValueEnum)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub enum FilterMatch {
Any,
All,
}

#[derive(Clone, Copy, Default, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct BtrfsOptions {
pub hdsize: Option<f64>,
pub raid: Option<BtrfsRaidLevel>,
}

#[derive(Clone, Copy, Default, Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LvmOptions {
pub hdsize: Option<f64>,
pub swapsize: Option<f64>,
pub maxroot: Option<f64>,
pub maxvz: Option<f64>,
pub minfree: Option<f64>,
}

#[derive(Clone, Copy, Default, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ZfsOptions {
pub raid: Option<ZfsRaidLevel>,
pub ashift: Option<usize>,
pub arc_max: Option<usize>,
pub checksum: Option<ZfsChecksumOption>,
pub compress: Option<ZfsCompressOption>,
pub copies: Option<usize>,
pub hdsize: Option<f64>,
}

// ----- End of `answers.toml` format definition -----
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this line, there is no "external API" anymore.


#[derive(Clone, Deserialize, Debug)]
#[serde(try_from = "NetworkInAnswer", deny_unknown_fields)]
pub struct Network {
pub network_settings: NetworkSettings,
}

#[derive(Clone, Debug)]
pub enum NetworkSettings {
FromDhcp,
Manual(NetworkManual),
}

#[derive(Clone, Debug)]
pub struct NetworkManual {
pub cidr: CidrAddress,
pub dns: IpAddr,
pub gateway: IpAddr,
pub filter: BTreeMap<String, String>,
}

impl TryFrom<NetworkInAnswer> for Network {
type Error = &'static str;

Expand Down Expand Up @@ -108,33 +216,6 @@ impl TryFrom<NetworkInAnswer> for Network {
}
}

#[derive(Clone, Debug)]
pub enum NetworkSettings {
FromDhcp,
Manual(NetworkManual),
}

#[derive(Clone, Debug)]
pub struct NetworkManual {
pub cidr: CidrAddress,
pub dns: IpAddr,
pub gateway: IpAddr,
pub filter: BTreeMap<String, String>,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DiskSetup {
pub filesystem: Filesystem,
#[serde(default)]
pub disk_list: Vec<String>,
pub filter: Option<BTreeMap<String, String>>,
pub filter_match: Option<FilterMatch>,
pub zfs: Option<ZfsOptions>,
pub lvm: Option<LvmOptions>,
pub btrfs: Option<BtrfsOptions>,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(try_from = "DiskSetup", deny_unknown_fields)]
pub struct Disks {
Expand All @@ -144,6 +225,19 @@ pub struct Disks {
pub fs_options: FsOptions,
}

#[derive(Clone, Debug)]
pub enum DiskSelection {
Selection(Vec<String>),
Filter(BTreeMap<String, String>),
}

#[derive(Clone, Debug)]
pub enum FsOptions {
BTRFS(BtrfsOptions),
LVM(LvmOptions),
ZFS(ZfsOptions),
}

impl TryFrom<DiskSetup> for Disks {
type Error = &'static str;

Expand Down Expand Up @@ -215,91 +309,4 @@ impl TryFrom<DiskSetup> for Disks {
}
}

#[derive(Clone, Debug)]
pub enum FsOptions {
LVM(LvmOptions),
ZFS(ZfsOptions),
BTRFS(BtrfsOptions),
}

#[derive(Clone, Debug)]
pub enum DiskSelection {
Selection(Vec<String>),
Filter(BTreeMap<String, String>),
}
#[derive(Clone, Deserialize, Debug, PartialEq, ValueEnum)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub enum FilterMatch {
Any,
All,
}

#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub enum Filesystem {
Ext4,
Xfs,
Zfs,
Btrfs,
}

#[derive(Clone, Copy, Default, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ZfsOptions {
pub raid: Option<ZfsRaidLevel>,
pub ashift: Option<usize>,
pub arc_max: Option<usize>,
pub checksum: Option<ZfsChecksumOption>,
pub compress: Option<ZfsCompressOption>,
pub copies: Option<usize>,
pub hdsize: Option<f64>,
}

#[derive(Clone, Copy, Default, Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LvmOptions {
pub hdsize: Option<f64>,
pub swapsize: Option<f64>,
pub maxroot: Option<f64>,
pub maxvz: Option<f64>,
pub minfree: Option<f64>,
}

#[derive(Clone, Copy, Default, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct BtrfsOptions {
pub hdsize: Option<f64>,
pub raid: Option<BtrfsRaidLevel>,
}

#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum KeyboardLayout {
De,
DeCh,
Dk,
EnGb,
EnUs,
Es,
Fi,
Fr,
FrBe,
FrCa,
FrCh,
Hu,
Is,
It,
Jp,
Lt,
Mk,
Nl,
No,
Pl,
Pt,
PtBr,
Se,
Si,
Tr,
}

serde_plain::derive_display_from_serialize!(KeyboardLayout);
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"country": "at",
"dns": "192.168.1.254",
"domain": "testinstall",
"existing_storage_auto_rename": 1,
"filesys": "ext4",
"gateway": "192.168.1.1",
"hdsize": 223.57088470458984,
"lvm_auto_rename": 1,
"hostname": "pveauto",
"keymap": "de",
"mailto": "[email protected]",
Expand Down