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

netdog: Support versioning in net.toml #2281

Merged
merged 1 commit into from
Jul 29, 2022
Merged
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
14 changes: 7 additions & 7 deletions sources/api/netdog/src/interface_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use std::ops::Deref;

/// InterfaceName can only be created from a string that contains a valid network interface name.
/// Validation is handled in the `TryFrom` implementation below.
#[derive(Debug, Eq, PartialEq, Hash, Deserialize)]
#[serde(try_from = "&str")]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
#[serde(try_from = "String")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we now deserialize the majority of the network config to a toml::Value first (to handle the versions), this is stored as a String inside of the toml::Value, hence the change from &str to String.

pub(crate) struct InterfaceName {
inner: String,
}

impl TryFrom<&str> for InterfaceName {
impl TryFrom<String> for InterfaceName {
type Error = error::Error;

fn try_from(input: &str) -> Result<Self> {
fn try_from(input: String) -> Result<Self> {
// Rust does not treat all Unicode line terminators as starting a new line, so we check for
// specific characters here, rather than just counting from lines().
// https://en.wikipedia.org/wiki/Newline#Unicode
Expand Down Expand Up @@ -67,11 +67,11 @@ impl TryFrom<&str> for InterfaceName {
}
}

impl TryFrom<String> for InterfaceName {
impl TryFrom<&str> for InterfaceName {
type Error = error::Error;

fn try_from(input: String) -> Result<Self> {
Self::try_from(input.as_ref())
fn try_from(input: &str) -> Result<Self> {
Self::try_from(input.to_string())
}
}

Expand Down
13 changes: 6 additions & 7 deletions sources/api/netdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use dns_lookup::lookup_addr;
use envy;
use ipnet::IpNet;
use lazy_static::lazy_static;
use net_config::NetConfig;
use rand::seq::SliceRandom;
use rand::thread_rng;
use regex::Regex;
Expand All @@ -53,7 +52,6 @@ use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
use std::str::FromStr;
use wicked::WickedInterface;

static RESOLV_CONF: &str = "/etc/resolv.conf";
static KERNEL_HOSTNAME: &str = "/proc/sys/kernel/hostname";
Expand Down Expand Up @@ -321,11 +319,11 @@ fn generate_hostname() -> Result<()> {
/// Generate configuration for network interfaces.
fn generate_net_config() -> Result<()> {
let maybe_net_config = if Path::exists(Path::new(DEFAULT_NET_CONFIG_FILE)) {
NetConfig::from_path(DEFAULT_NET_CONFIG_FILE).context(error::NetConfigParseSnafu {
net_config::from_path(DEFAULT_NET_CONFIG_FILE).context(error::NetConfigParseSnafu {
path: DEFAULT_NET_CONFIG_FILE,
})?
} else {
NetConfig::from_command_line(KERNEL_CMDLINE).context(error::NetConfigParseSnafu {
net_config::from_command_line(KERNEL_CMDLINE).context(error::NetConfigParseSnafu {
path: KERNEL_CMDLINE,
})?
};
Expand All @@ -338,14 +336,15 @@ fn generate_net_config() -> Result<()> {
return Ok(());
}
};

let primary_interface = net_config
.primary_interface()
.context(error::GetPrimaryInterfaceSnafu)?;
write_primary_interface(primary_interface)?;

for (name, config) in net_config.interfaces {
let wicked_interface = WickedInterface::from_config(name, config);
wicked_interface
let wicked_interfaces = net_config.into_wicked_interfaces();
for interface in wicked_interfaces {
interface
.write_config_file()
.context(error::InterfaceConfigWriteSnafu)?;
}
Expand Down
Loading