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

Write short form hostname #922

Open
wants to merge 2 commits into
base: main
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
7 changes: 5 additions & 2 deletions src/cli/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ pub struct CliMulti {
#[arg(long)]
check_in: bool,
/// The file into which the hostname should be written
#[arg(long = "hostname", value_name = "path")]
#[arg(group = "hostname", long = "hostname", value_name = "path")]
hostname_file: Option<String>,
/// Parse the hostname retrieved from metadata as a short hostname
#[arg(long = "short", requires = "hostname")]
short_hostname: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I prefer your original idea of having --short-hostname=/etc/hostname. Rather than adding a mode switch that controls the behavior of another option, let's just support --hostname and --short-hostname as two output switches that operate independently.

/// The directory into which network units are written
#[arg(long = "network-units", value_name = "path")]
network_units_dir: Option<String>,
Expand Down Expand Up @@ -64,7 +67,7 @@ impl CliMulti {

// write hostname if configured to do so
self.hostname_file
.map_or(Ok(()), |x| metadata.write_hostname(x))
.map_or(Ok(()), |x| metadata.write_hostname(x, self.short_hostname))
.context("writing hostname")?;

// write network units if configured to do so
Expand Down
35 changes: 26 additions & 9 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,21 @@ pub trait MetadataProvider {
Ok(())
}

fn write_hostname(&self, hostname_file_path: String) -> Result<()> {
fn write_hostname(&self, hostname_file_path: String, short: bool) -> Result<()> {
if let Some(mut hostname) = self.hostname()? {
if let Some(maxlen) = max_hostname_len()? {
if short {
// Get the short form hostname (i.e. without the domain name).
// The domain name is usually set as a search domain via DHCP, and
// it can optionally be added as a canonical hostname to /etc/hosts
// as well, as the first value after the local IP address.
//
// Example:
// 127.0.0.1 example.local example
if let Some(idx) = hostname.find('.') {
hostname.truncate(idx);
}
}
if hostname.len() > maxlen {
// Value exceeds the system's maximum hostname length.
// Truncate hostname to the first dot, or to the maximum
Expand Down Expand Up @@ -309,11 +321,11 @@ mod tests {
}

// write specified hostname to a file, then read it back
fn try_write_hostname(hostname: &str) -> String {
fn try_write_hostname(hostname: &str, short: bool) -> String {
let mut temp = NamedTempFile::new().unwrap();
let provider = HostnameMock(hostname.into());
provider
.write_hostname(temp.path().to_str().unwrap().into())
.write_hostname(temp.path().to_str().unwrap().into(), short)
.unwrap();
let mut ret = String::new();
temp.read_to_string(&mut ret).unwrap();
Expand All @@ -330,30 +342,35 @@ mod tests {
.take(maxlen * 2)
.collect::<String>();
// simple hostname
assert_eq!(try_write_hostname("hostname7"), "hostname7");
assert_eq!(try_write_hostname("hostname7", false), "hostname7");
// simple FQDN
assert_eq!(
try_write_hostname("hostname7.example.com"),
try_write_hostname("hostname7.example.com", false),
"hostname7.example.com"
);
// short form hostname
assert_eq!(
try_write_hostname("hostname7.example.com", true),
"hostname7"
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's at least also test short hostnames where the input value doesn't have an FQDN.

// truncated simple hostname
assert_eq!(
try_write_hostname(&long_string[0..maxlen + 10]),
try_write_hostname(&long_string[0..maxlen + 10], false),
long_string[0..maxlen]
);
// truncated FQDN
assert_eq!(
try_write_hostname(&format!("{}.example.com", &long_string[0..maxlen + 5])),
try_write_hostname(&format!("{}.example.com", &long_string[0..maxlen + 5]), false),
long_string[0..maxlen]
);
// truncate to first dot
assert_eq!(
try_write_hostname(&format!("{}.example.com", &long_string[0..maxlen - 5])),
try_write_hostname(&format!("{}.example.com", &long_string[0..maxlen - 5]), false),
long_string[0..maxlen - 5]
);
// truncate to first dot even if we could truncate to second dot
assert_eq!(
try_write_hostname(&format!("{}.example.com", &long_string[0..maxlen - 10])),
try_write_hostname(&format!("{}.example.com", &long_string[0..maxlen - 10]), false),
long_string[0..maxlen - 10]
);
}
Expand Down