Skip to content

Commit 1ac902d

Browse files
akantoKrisztian Horvath
authored and
Krisztian Horvath
committed
CLOUD-67831 Avoid timeouts when IPV6 is disabled saltstack/salt#32719
1 parent 126eb24 commit 1ac902d

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
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.3.1
3+
VERSION=0.3.2
44
BUILD_TIME=$(shell date +%FT%T)
55
LDFLAGS=-ldflags "-X github.com/sequenceiq/salt-bootstrap/saltboot.Version=${VERSION} -X github.com/sequenceiq/salt-bootstrap/saltboot.BuildTime=${BUILD_TIME}"
66

saltboot/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (clients *Clients) DistributeHostnameRequest(user string, pass string) (res
3535

3636
func ClientHostnameHandler(w http.ResponseWriter, req *http.Request) {
3737
log.Printf("[ClientHostnameHandler] get FQDN")
38-
fqdn, err := ExecCmd("hostname", "-f")
38+
fqdn, err := getHostName()
3939
if err != nil {
4040
log.Printf("[ClientHostnameHandler] failed to retrieve FQDN")
4141
model.Response{Status: err.Error(), StatusCode: http.StatusInternalServerError}.WriteHttp(w)

saltboot/hostname.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package saltboot
2+
3+
import (
4+
"io/ioutil"
5+
"strings"
6+
"log"
7+
)
8+
9+
const DEFAULT_DOMAIN = ".example.com"
10+
const HOST_FILE_NAME = "/etc/hosts"
11+
12+
func getIpv4Address() (string, error) {
13+
return ExecCmd("hostname", "-i")
14+
}
15+
16+
func getHostName() (string, error) {
17+
return ExecCmd("hostname", "-f")
18+
}
19+
20+
// This is required due to: https://github.com/saltstack/salt/issues/32719
21+
func ensureIpv6Resolvable(domain string) (error) {
22+
fqdn, err := getHostName()
23+
log.Printf("[ensureIpv6Resolvable] fqdn: %s", fqdn)
24+
if err != nil {
25+
return err
26+
}
27+
if !strings.Contains(fqdn, ".") {
28+
// only fqdn does not contain domain
29+
if domain != "" {
30+
updateIpv6HostName(fqdn, domain)
31+
} else {
32+
//if there is no domain, we need to add one since ambari fails without domain, actually it does nothing just hangs..
33+
updateIpv6HostName(fqdn, DEFAULT_DOMAIN)
34+
}
35+
} else {
36+
if domain != "" {
37+
hostName := strings.Split(fqdn, ".")[0]
38+
updateIpv6HostName(hostName, domain)
39+
}
40+
}
41+
42+
return nil
43+
}
44+
45+
func updateIpv6HostName(hostName string, domain string) (error) {
46+
log.Printf("[updateIpv6HostName] hostName: %s, domain: %s", hostName, domain)
47+
b, err := ioutil.ReadFile(HOST_FILE_NAME)
48+
if err != nil {
49+
return err
50+
}
51+
hostfile := string(b)
52+
log.Printf("[updateIpv6HostName] original hostfile: %s", hostfile)
53+
address, err := getIpv4Address()
54+
if err != nil {
55+
return err
56+
}
57+
ipv6hostString := address + " " + hostName + domain + " " + hostName
58+
log.Printf("[updateIpv6HostName] ipv6hostString: %s", ipv6hostString)
59+
if !strings.Contains(hostfile, address) {
60+
hostfile = hostfile + "\n" + ipv6hostString
61+
log.Printf("[updateIpv6HostName] updated hostfile: %s", hostfile)
62+
err = ioutil.WriteFile(HOST_FILE_NAME, []byte(hostfile), 0644)
63+
if err != nil {
64+
return err
65+
}
66+
}
67+
68+
return nil
69+
}
70+

saltboot/salt.go

+16
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ type SaltAuth struct {
2626
type SaltMaster struct {
2727
Address string `json:"address"`
2828
Auth SaltAuth `json:"auth,omitempty"`
29+
Domain string `json:"domain,omitempty"`
2930
}
3031

3132
type SaltMinion struct {
3233
Address string `json:"address"`
3334
Roles []string `json:"roles,omitempty"`
3435
Server string `json:"server,omitempty"`
3536
HostGroup string `json:"hostGroup,omitempty"`
37+
Domain string `json:"domain,omitempty"`
3638
}
3739

3840
type SaltPillar struct {
@@ -70,12 +72,16 @@ func (r SaltActionRequest) distributeAction(user string, pass string) (result []
7072
if minion.Server == "" {
7173
minion.Server = r.Master.Address
7274
}
75+
if minion.Domain == "" {
76+
minion.Domain = r.Master.Domain
77+
}
7378
minionPayload = append(minionPayload, minion)
7479
}
7580

7681
for res := range DistributePayload(targets, minionPayload, SaltMinionEp + "/" + r.Action, user, pass) {
7782
result = append(result, res)
7883
}
84+
7985
if len(r.Master.Address) > 0 {
8086
result = append(result, <-DistributePayload([]string{r.Master.Address}, []Payload{r.Master}, SaltServerEp + "/" + r.Action, user, pass))
8187
}
@@ -95,6 +101,11 @@ func SaltMinionRunRequestHandler(w http.ResponseWriter, req *http.Request) {
95101
}
96102
log.Printf("[SaltMinionRunRequestHandler] received json: %s", saltMinion.AsByteArray())
97103

104+
err = ensureIpv6Resolvable(saltMinion.Domain)
105+
if err != nil {
106+
log.Printf("[ERROR] while hostfile update: %s", err)
107+
}
108+
98109
grainConfig := GrainConfig{Roles: saltMinion.Roles, HostGroup: saltMinion.HostGroup}
99110
grainYaml, err := yaml.Marshal(grainConfig)
100111
var resp model.Response
@@ -170,6 +181,11 @@ func SaltServerRunRequestHandler(w http.ResponseWriter, req *http.Request) {
170181
return
171182
}
172183

184+
ensureIpv6Resolvable(saltMaster.Domain)
185+
if err != nil {
186+
log.Printf("[ERROR] while hostfile update: %s", err)
187+
}
188+
173189
resp, err := CreateUser(saltMaster)
174190
resp.WriteHttp(w)
175191
if err != nil {

0 commit comments

Comments
 (0)