Skip to content

Commit

Permalink
Make rubocop happy
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 2, 2019
1 parent 93d566d commit 9e4f360
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 8 additions & 6 deletions library/network/src/lib/y2network/config_reader/hostname.rb
Expand Up @@ -27,13 +27,15 @@

module Y2Network
module ConfigReader
# This class is reponsible of reading the current Hostname or the one
# proposed / configured through linuxrc in case of an installation.
class Hostname
include Yast::Logger

def self.from_system
# In installation (standard, or AutoYaST one), prefer /etc/install.inf
# (because HOSTNAME comes with netcfg.rpm already, #144687)
if (Yast::Mode.installation || Yast::Mode.autoinst) && File.exists?("/etc/install.inf")
if (Yast::Mode.installation || Yast::Mode.autoinst) && File.exist?("/etc/install.inf")
fqhostname = read_hostname_from_install_inf
end

Expand All @@ -53,7 +55,7 @@ def read_hostname_from_install_inf

# if the name is actually IP, try to resolve it (bnc#556613, bnc#435649)
if Yast::IP.Check(install_inf_hostname)
fqhostname = ResolveIP(install_inf_hostname)
fqhostname = hosts_hostname_for(install_inf_hostname)
log.info("Got #{fqhostname} after resolving IP from install.inf")
else
fqhostname = install_inf_hostname
Expand All @@ -62,11 +64,11 @@ def read_hostname_from_install_inf
fqhostname
end

# Resolve IP to canonical hostname
# Get the canonical hostname from hosts for a given IP address
#
# @param [String] ip given IP address
# @return resolved canonical hostname (FQDN) for given IP or empty string in case of failure.
def ResolveIP(ip)
def hosts_hostname_for(ip)
getent = Yast::SCR.Execute(path(".target.bash_output"), "/usr/bin/getent hosts #{ip.shellescape}")
exit_code = getent.fetch("exit", -1)

Expand All @@ -76,15 +78,15 @@ def ResolveIP(ip)
return ""
end

GetHostnameFromGetent(getent.fetch("stdout", ""))
hostname_from_getent(getent.fetch("stdout", ""))
end

# Handles input as one line of getent output. Returns first hostname found
# on the line (= canonical hostname).
#
# @param [String] line in /etc/hosts format
# @return canonical hostname from given line
def GetHostnameFromGetent(line)
def hostname_from_getent(line)
# line is expected same format as is used in /etc/hosts without additional
# comments (getent removes comments from the end).
#
Expand Down
3 changes: 1 addition & 2 deletions library/network/test/y2network/hostname_test.rb
Expand Up @@ -22,7 +22,7 @@
require "y2network/hostname"

describe Y2Network::Hostname do
let(:hostname) { described_class.new(fqdn: "server.example.org")}
let(:hostname) { described_class.new(fqdn: "server.example.org") }

describe "#short" do
it "returns the short part of the hostname" do
Expand All @@ -42,4 +42,3 @@
end
end
end

0 comments on commit 9e4f360

Please sign in to comment.