Skip to content

Commit 03a137c

Browse files
keykimhmxs
authored andcommitted
BUG-100214 fix issue when multiple network interfaces are present
1 parent 27d6358 commit 03a137c

File tree

5 files changed

+14
-45
lines changed

5 files changed

+14
-45
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BINARY=salt-bootstrap
22

3-
VERSION=0.12.2
3+
VERSION=0.12.3
44
BUILD_TIME=$(shell date +%FT%T)
55
LDFLAGS=-ldflags "-X github.com/hortonworks/salt-bootstrap/saltboot.Version=${VERSION} -X github.com/hortonworks/salt-bootstrap/saltboot.BuildTime=${BUILD_TIME}"
66
GOFILES = $(shell find . -type f -name '*.go')

saltboot/hostname.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ const HOSTNAME_FILE = "/etc/hostname"
1515
var readFile = ioutil.ReadFile
1616
var writeFile = ioutil.WriteFile
1717

18-
func getIpv4Address() (string, error) {
19-
return ExecCmd("hostname", "-I")
20-
}
21-
2218
func getFQDN() (string, error) {
2319
return ExecCmd("hostname", "-f")
2420
}
@@ -41,7 +37,7 @@ func setHostname(hostName string) (string, error) {
4137
}
4238

4339
// This is required due to: https://github.com/saltstack/salt/issues/32719
44-
func ensureHostIsResolvable(customHostname *string, customDomain string) error {
40+
func ensureHostIsResolvable(customHostname *string, customDomain string, ipv4address string) error {
4541
var hostName string
4642
if customHostname != nil && len(*customHostname) > 0 {
4743
log.Printf("[ensureHostIsResolvable] use custom hostname: %s", *customHostname)
@@ -73,7 +69,7 @@ func ensureHostIsResolvable(customHostname *string, customDomain string) error {
7369
domain = "." + domain
7470
}
7571

76-
if err := updateHostsFile(hostName, domain, HOSTS_FILE, getIpv4Address); err != nil {
72+
if err := updateHostsFile(hostName, domain, HOSTS_FILE, ipv4address); err != nil {
7773
log.Printf("[ensureHostIsResolvable] [ERROR] unable to update host file: %s", err.Error())
7874
return err
7975
}
@@ -92,27 +88,22 @@ func ensureHostIsResolvable(customHostname *string, customDomain string) error {
9288
return nil
9389
}
9490

95-
func updateHostsFile(hostName string, domain string, file string, getIpv4Address func() (string, error)) error {
96-
ip, err := getIpv4Address()
97-
if err != nil {
98-
return err
99-
}
100-
101-
log.Printf("[updateHostsFile] hostName: %s, domain: %s, ip: %s", hostName, domain, ip)
91+
func updateHostsFile(hostName string, domain string, file string, ipv4address string) error {
92+
log.Printf("[updateHostsFile] hostName: %s, domain: %s, ip: %s", hostName, domain, ipv4address)
10293
b, err := readFile(file)
10394
if err != nil {
10495
return err
10596
}
10697
hostsFile := string(b)
10798
log.Printf("[updateHostsFile] original hosts file: %s", hostsFile)
10899

109-
ipv4HostString := ip + " " + hostName + domain + " " + hostName
100+
ipv4HostString := ipv4address + " " + hostName + domain + " " + hostName
110101
log.Printf("[updateHostsFile] ipv4HostString: %s", ipv4HostString)
111102

112103
lines := strings.Split(hostsFile, "\n")
113104
var filteredLines = make([]string, 0)
114105
for _, line := range lines {
115-
if !strings.Contains(line, ip) {
106+
if !strings.Contains(line, ipv4address) {
116107
filteredLines = append(filteredLines, line)
117108
}
118109
}

saltboot/hostname_test.go

+5-25
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ func init() {
1818
}
1919

2020
func TestHostsFileWriteRemoveExistingIp(t *testing.T) {
21-
getIpv4Address := func() (string, error) {
22-
return "10.0.0.1", nil
23-
}
24-
2521
readFile = func(filename string) ([]byte, error) {
2622
hostsFile := `
2723
127.0.0.1 localhost
@@ -43,7 +39,7 @@ func TestHostsFileWriteRemoveExistingIp(t *testing.T) {
4339
writeFile = emptyWriteFile
4440
}()
4541

46-
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address)
42+
updateHostsFile("hostname-1", ".example.com", "hosts", "10.0.0.1")
4743

4844
expected := `
4945
127.0.0.1 localhost
@@ -57,10 +53,6 @@ func TestHostsFileWriteRemoveExistingIp(t *testing.T) {
5753
}
5854

5955
func TestHostsFileWriteRemoveExistingIpNotLastLine(t *testing.T) {
60-
getIpv4Address := func() (string, error) {
61-
return "10.0.0.1", nil
62-
}
63-
6456
readFile = func(filename string) ([]byte, error) {
6557
hostsFile := `
6658
127.0.0.1 localhost
@@ -83,7 +75,7 @@ func TestHostsFileWriteRemoveExistingIpNotLastLine(t *testing.T) {
8375
writeFile = emptyWriteFile
8476
}()
8577

86-
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address)
78+
updateHostsFile("hostname-1", ".example.com", "hosts", "10.0.0.1")
8779

8880
expected := `
8981
127.0.0.1 localhost
@@ -98,10 +90,6 @@ func TestHostsFileWriteRemoveExistingIpNotLastLine(t *testing.T) {
9890
}
9991

10092
func TestHostsFileWriteRemoveExistingIpMiddleLastLine(t *testing.T) {
101-
getIpv4Address := func() (string, error) {
102-
return "10.0.0.1", nil
103-
}
104-
10593
readFile = func(filename string) ([]byte, error) {
10694
hostsFile := `
10795
127.0.0.1 localhost
@@ -125,7 +113,7 @@ func TestHostsFileWriteRemoveExistingIpMiddleLastLine(t *testing.T) {
125113
writeFile = emptyWriteFile
126114
}()
127115

128-
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address)
116+
updateHostsFile("hostname-1", ".example.com", "hosts", "10.0.0.1")
129117

130118
expected := `
131119
127.0.0.1 localhost
@@ -141,10 +129,6 @@ func TestHostsFileWriteRemoveExistingIpMiddleLastLine(t *testing.T) {
141129
}
142130

143131
func TestHostsFileWriteIpNotPresent(t *testing.T) {
144-
getIpv4Address := func() (string, error) {
145-
return "10.0.0.1", nil
146-
}
147-
148132
readFile = func(filename string) ([]byte, error) {
149133
hostsFile := `
150134
127.0.0.1 localhost
@@ -167,7 +151,7 @@ func TestHostsFileWriteIpNotPresent(t *testing.T) {
167151
writeFile = emptyWriteFile
168152
}()
169153

170-
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address)
154+
updateHostsFile("hostname-1", ".example.com", "hosts", "10.0.0.1")
171155

172156
expected := `
173157
127.0.0.1 localhost
@@ -183,10 +167,6 @@ func TestHostsFileWriteIpNotPresent(t *testing.T) {
183167
}
184168

185169
func TestHostsFileWriteExistingWithDefaultDomain(t *testing.T) {
186-
getIpv4Address := func() (string, error) {
187-
return "10.0.0.1", nil
188-
}
189-
190170
readFile = func(filename string) ([]byte, error) {
191171
hostsFile := `
192172
127.0.0.1 localhost
@@ -210,7 +190,7 @@ func TestHostsFileWriteExistingWithDefaultDomain(t *testing.T) {
210190
writeFile = emptyWriteFile
211191
}()
212192

213-
updateHostsFile("hostname-1", ".compute.internal", "hosts", getIpv4Address)
193+
updateHostsFile("hostname-1", ".compute.internal", "hosts", "10.0.0.1")
214194

215195
expected := `
216196
127.0.0.1 localhost

saltboot/salt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func SaltMinionRunRequestHandler(w http.ResponseWriter, req *http.Request) {
133133
saltMinion.Domain = saltActionRequest.Master.Domain
134134
}
135135

136-
err = ensureHostIsResolvable(saltMinion.Hostname, saltMinion.Domain)
136+
err = ensureHostIsResolvable(saltMinion.Hostname, saltMinion.Domain, saltMinion.Address)
137137
if err != nil {
138138
log.Printf("[SaltMinionRunRequestHandler] [ERROR] while hostfile update: %s", err.Error())
139139
}
@@ -227,7 +227,7 @@ func SaltServerRunRequestHandler(w http.ResponseWriter, req *http.Request) {
227227
saltMaster = saltActionRequest.Master
228228
}
229229

230-
if err := ensureHostIsResolvable(saltMaster.Hostname, saltMaster.Domain); err != nil {
230+
if err := ensureHostIsResolvable(saltMaster.Hostname, saltMaster.Domain, saltMaster.Address); err != nil {
231231
log.Printf("[SaltServerRunRequestHandler] [ERROR] while hostfile update: %s", err.Error())
232232
}
233233

saltboot/salt_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ func TestSaltMinionRunRequestHandler(t *testing.T) {
111111
checkExecutedCommands([]string{
112112
"hostname -s",
113113
"hostname -d",
114-
"hostname -I",
115114
"hostname ",
116115
"grep SUSE /etc/issue",
117116
"ps aux",
@@ -165,7 +164,6 @@ func TestSaltServerRunRequestHandler(t *testing.T) {
165164
checkExecutedCommands([]string{
166165
"hostname -s",
167166
"hostname -d",
168-
"hostname -I",
169167
"hostname ",
170168
"grep SUSE /etc/issue",
171169
"grep saltuser /etc/passwd",

0 commit comments

Comments
 (0)