Skip to content

Commit f50ba39

Browse files
committed
netdog: add commit subcommand
Signed-off-by: Kyle Sessions <kssessio@amazon.com>
1 parent cd6952f commit f50ba39

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

sources/netdog/src/cli/commit.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use crate::cli::{error, Result};
2+
use argh::FromArgs;
3+
use snafu::ResultExt;
4+
use std::io::Read;
5+
use std::path::Path;
6+
7+
/// Validate and commit network configuration from stdin
8+
#[derive(FromArgs, PartialEq, Debug)]
9+
#[argh(subcommand, name = "commit")]
10+
pub struct CommitArgs {}
11+
12+
pub fn run(_args: CommitArgs) -> Result<()> {
13+
// Read content from stdin
14+
let mut content = String::new();
15+
std::io::stdin()
16+
.read_to_string(&mut content)
17+
.context(error::StdinReadSnafu)?;
18+
19+
// Validate content
20+
crate::net_config::deserialize_config(&content).context(error::StdinNetConfigParseSnafu)?;
21+
22+
// Write to file after successful validation
23+
let config_dir = Path::new("/.bottlerocket");
24+
let config_file = config_dir.join("net.toml");
25+
26+
// Ensure directory exists
27+
if !config_dir.exists() {
28+
return error::NetConfigDirMissingSnafu { path: config_dir }.fail();
29+
}
30+
31+
std::fs::write(&config_file, content).context(error::PathReadSnafu { path: &config_file })?;
32+
33+
Ok(())
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
use std::io::Cursor;
40+
41+
#[test]
42+
fn test_commit_invalid_version() {
43+
let invalid_config = "version = 99\n\n[eth0]\ndhcp4 = true";
44+
let mut stdin = Cursor::new(invalid_config.as_bytes());
45+
46+
let mut content = String::new();
47+
stdin.read_to_string(&mut content).unwrap();
48+
49+
let result = crate::net_config::deserialize_config(&content);
50+
51+
assert!(result.is_err());
52+
}
53+
54+
#[test]
55+
fn test_commit_valid_config() {
56+
let valid_config = "version = 3\n\n[enp0s16]\ndhcp4 = true\ndhcp6 = false\nprimary = true\n\n[enp0s17]\ndhcp4 = true\ndhcp6 = false";
57+
let mut stdin = Cursor::new(valid_config.as_bytes());
58+
59+
let mut content = String::new();
60+
stdin.read_to_string(&mut content).unwrap();
61+
62+
let result = crate::net_config::deserialize_config(&content);
63+
64+
assert!(result.is_ok());
65+
}
66+
}

sources/netdog/src/cli/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub(crate) mod commit;
12
pub(crate) mod generate_hostname;
23
pub(crate) mod generate_net_config;
34
pub(crate) mod node_ip;
@@ -10,6 +11,7 @@ use crate::{
1011
DEFAULT_NET_CONFIG_FILE, KERNEL_CMDLINE, PRIMARY_INTERFACE, PRIMARY_MAC_ADDRESS,
1112
PRIMARY_SYSCTL_CONF, SYSCTL_MARKER_FILE, SYSTEMD_SYSCTL, SYS_CLASS_NET, USR_NET_CONFIG_FILE,
1213
};
14+
pub(crate) use commit::CommitArgs;
1315
pub(crate) use generate_hostname::GenerateHostnameArgs;
1416
pub(crate) use generate_net_config::GenerateNetConfigArgs;
1517
pub(crate) use node_ip::NodeIpArgs;
@@ -319,6 +321,15 @@ mod error {
319321
link: PathBuf,
320322
source: io::Error,
321323
},
324+
325+
#[snafu(display("Failed to read from stdin: {}", source))]
326+
StdinRead { source: std::io::Error },
327+
328+
#[snafu(display("Failed to parse network config from stdin: {}", source))]
329+
StdinNetConfigParse { source: net_config::Error },
330+
331+
#[snafu(display("Network config directory '{}' does not exist", path.display()))]
332+
NetConfigDirMissing { path: PathBuf },
322333
}
323334
}
324335

sources/netdog/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct Args {
7070
#[derive(FromArgs, PartialEq, Debug)]
7171
#[argh(subcommand)]
7272
enum SubCommand {
73+
Commit(cli::CommitArgs),
7374
NodeIp(cli::NodeIpArgs),
7475
GenerateHostname(cli::GenerateHostnameArgs),
7576
GenerateNetConfig(cli::GenerateNetConfigArgs),
@@ -81,6 +82,7 @@ enum SubCommand {
8182
async fn run() -> cli::Result<()> {
8283
let args: Args = argh::from_env();
8384
match args.subcommand {
85+
SubCommand::Commit(args) => cli::commit::run(args)?,
8486
SubCommand::NodeIp(_) => cli::node_ip::run()?,
8587
SubCommand::GenerateHostname(_) => cli::generate_hostname::run().await?,
8688
SubCommand::GenerateNetConfig(_) => cli::generate_net_config::run()?,

sources/netdog/src/net_config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ where
103103

104104
/// Deserialize the network config, using the version key to determine which config struct to
105105
/// deserialize into
106-
fn deserialize_config(config_str: &str) -> Result<Box<dyn Interfaces>> {
106+
pub fn deserialize_config(config_str: &str) -> Result<Box<dyn Interfaces>> {
107107
#[derive(Debug, Deserialize)]
108108
struct ConfigToml {
109109
version: u8,

0 commit comments

Comments
 (0)