-
Notifications
You must be signed in to change notification settings - Fork 106
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
gabriel-samfira
wants to merge
2
commits into
coreos:main
Choose a base branch
from
gabriel-samfira:write-short-form-hostname
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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(); | ||
|
@@ -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" | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.