Skip to content

Commit

Permalink
Merge pull request #517 from mchf/bnc1039532-hosts
Browse files Browse the repository at this point in the history
Fixed updating of /etc/hosts during installation
  • Loading branch information
mchf committed May 30, 2017
2 parents 21787c2 + 48b472d commit 641e67f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
9 changes: 9 additions & 0 deletions package/yast2-network.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
-------------------------------------------------------------------
Thu May 25 09:27:09 UTC 2017 - mfilka@suse.com

- bnc#1039532
- do not write hostname into /etc/hosts duplicitly for one IP
- configure static IPs without own hostname to resolve to system
wide hostname during installation
- 3.2.28

-------------------------------------------------------------------
Mon May 22 13:19:22 UTC 2017 - gsouza@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-network.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-network
Version: 3.2.27
Version: 3.2.28
Release: 0
BuildArch: noarch

Expand Down
19 changes: 15 additions & 4 deletions src/modules/Host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def Update(oldhn, newhn, ip)
return true if [nil, ""].include?(newhn)

nick = Hostname.SplitFQ(newhn)[0] || ""
nick = nick.empty? ? [] : [nick]
nick = nick.empty? || nick == newhn ? [] : [nick]
hosts = @hosts.host(ip)
if hosts.empty?
@hosts.add_entry(ip, newhn, nick)
Expand Down Expand Up @@ -276,6 +276,9 @@ def Summary
summary
end

# Creates a list os static ips present in the system
#
# @return [Array<string>] list of ip addresses
def StaticIPs
NetworkInterfaces.Read
devs = NetworkInterfaces.Locate("BOOTPROTO", "static")
Expand All @@ -289,14 +292,22 @@ def StaticIPs
static_ips
end

# if we have a static address,
# make sure /etc/hosts resolves it to our, bnc#664929
# Configure system to resolve static ips without hostname to system wide hostname
#
# It is expected to be used during installation only. If user configures static
# ips during installation and do not assign them particular hostname, then such
# ips are configuret to resolve to the system wide hostname (see Hostname module,
# /etc/HOSTNAME)
#
# Originally implemented as a fix for bnc#664929, later extended for bnc#1039532
def ResolveHostnameToStaticIPs
static_ips = StaticIPs()
# reject those static ips which have particular hostnames already configured
static_ips = StaticIPs().reject { |sip| @hosts.include_ip?(sip) }
return if static_ips.empty?

fqhostname = Hostname.MergeFQ(DNS.hostname, DNS.domain)

# assign system wide hostname to a static ip without particular hostname
static_ips.each { |sip| Update(fqhostname, fqhostname, sip) }

nil
Expand Down
44 changes: 36 additions & 8 deletions test/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@

expect(content.lines).to include("10.100.128.72 pepa pepa2 newname.suse.cz newname\n")
end

it "raises an error when empty ip is provided" do
expect { Yast::Host.Update("oldhostname", "newhostname", "") }
.to raise_error(ArgumentError, instance_of(String))
end

it "raises an error when nil ip is provided" do
expect { Yast::Host.Update("oldhostname", "newhostname", nil) }
.to raise_error(ArgumentError, instance_of(String))
end

it "doesn't write entry with duplicate hostname" do
ip = "1.1.1.1"
hostname = "linux"

Yast::Host.Update(hostname, hostname, ip)
expect(Yast::Host.name_map[ip]).not_to eql ["#{hostname} #{hostname}"]
end
end

describe ".EnsureHostnameResolvable" do
Expand Down Expand Up @@ -246,17 +264,27 @@

Yast::Host.ResolveHostnameToStaticIPs
end
end

describe ".Update" do
it "raises an error when empty ip is provided" do
expect { Yast::Host.Update("oldhostname", "newhostname", "") }
.to raise_error(ArgumentError, instance_of(String))
it "doesn't call .Update when an IP already has a hostname" do
hostname = "linux"

Yast::Host.Update(hostname, hostname, static_ips[0])

expect(Yast::Host)
.not_to receive(:Update)
.with(fqhostname, fqhostname, static_ips[0])

Yast::Host.ResolveHostnameToStaticIPs
end

it "raises an error when nil ip is provided" do
expect { Yast::Host.Update("oldhostname", "newhostname", nil) }
.to raise_error(ArgumentError, instance_of(String))
it "doesn't call .Update when an IP already has a hostname" do
hostname = "linux"

Yast::Host.Update(hostname, hostname, static_ips[0])

expect(Yast::Host.ResolveHostnameToStaticIPs)
.not_to receive(:Update)
.with(fqhostname, fqhostname, static_ips[0])
end
end

Expand Down

0 comments on commit 641e67f

Please sign in to comment.