Skip to content

Commit

Permalink
netdog: Support versioning in net.toml
Browse files Browse the repository at this point in the history
This change adds the ability to handle additional versions of `net.toml`
as the project continues to add features to network configuration.  A
top level `NetConfig` type has been removed in favor of a few traits.
Each new version of config is expected to implement these traits, the
most important of which converts the network configuration into
`WickedInterface` structs which are suitable for serializing directly to
file.

As part of this change, the `net_config` module has been lightly
refactored and split into submodules to make the pieces easier to use
among new versions.
  • Loading branch information
zmrow committed Jul 20, 2022
1 parent b539d40 commit 1a32d2c
Show file tree
Hide file tree
Showing 8 changed files with 701 additions and 617 deletions.
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")]
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

0 comments on commit 1a32d2c

Please sign in to comment.