Skip to content

Commit

Permalink
Save always the hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 24, 2020
1 parent c375617 commit e0c1f8d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/lib/y2network/sysconfig/hostname_writer.rb
Expand Up @@ -34,15 +34,13 @@ def write(hostname, old_hostname)
return if old_hostname && hostname == old_hostname

update_sysconfig_dhcp(hostname, old_hostname)
update_hostname(hostname) if hostname.save_hostname?
update_hostname(hostname) if hostname.static != old_hostname.static
end

private

# @return [String] Hostname executable
HOSTNAME_PATH = "/etc/hostname".freeze
# @return [String] Sendmail update script (included in "sendmail" package)
SENDMAIL_UPDATE_PATH = "/usr/lib/sendmail.d/update".freeze

# @param hostname [Y2Network::Hostname] Hostname configuration
# @param old_hostname [Y2Network::Hostname] Old Hostname configuration
Expand All @@ -69,19 +67,25 @@ def update_sysconfig_dhcp(hostname, old_hostname)
end
end

# Sets the hostname
# Sets the system hostname
#
# @param hostname [Y2Network::Hostname] Hostname configuration
def update_hostname(hostname)
hostname = hostname.static
# 1) when user asked for erasing hostname from /etc/hostname, we keep runtime as it is
# 2) we will write whatever user wants even FQDN - no changes under the hood
Yast::Execute.on_target!("/usr/bin/hostname", hostname) if !hostname.empty?
Yast::SCR.Write(
Yast::Path.new(".target.string"),
HOSTNAME_PATH,
hostname.empty? ? "" : hostname + "\n"
)

Yast.import "Stage"
if Yast::Stage.initial
# 1) when user asked for erasing hostname from /etc/hostname, we keep runtime as it is
# 2) we will write whatever user wants even FQDN - no changes under the hood
Yast::Execute.on_target!("/usr/bin/hostname", hostname) if !hostname.empty?
Yast::SCR.Write(
Yast::Path.new(".target.string"),
HOSTNAME_PATH,
hostname.empty? ? "" : hostname + "\n"
)
else
Yast::Execute.on_target!("/usr/bin/hostnamectl", "set-hostname", "--static", hostname)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/y2network/hostname_writer_test.rb
Expand Up @@ -44,6 +44,7 @@

before(:each) do
allow(subject).to receive(:update_sysconfig_dhcp).and_return(nil)
allow(Yast::Stage).to receive(:initial).and_return(true)
end

context "when updating hostname" do
Expand Down

0 comments on commit e0c1f8d

Please sign in to comment.