Skip to content

Commit 1410adb

Browse files
Krisztian Horvathakanto
Krisztian Horvath
authored andcommitted
ability to set custom hostnames
1 parent 13a3cdf commit 1410adb

File tree

4 files changed

+113
-39
lines changed

4 files changed

+113
-39
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.11.2
3+
VERSION=0.12.0
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

+100-28
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"strings"
88
)
99

10-
const DEFAULT_DOMAIN = ".example.com"
11-
const HOST_FILE_NAME = "/etc/hosts"
10+
const EXAMPLE_DOMAIN = ".example.com"
11+
const HOSTS_FILE = "/etc/hosts"
12+
const NETWORK_SYSCONFIG_FILE = "/etc/sysconfig/network"
13+
const HOSTNAME_FILE = "/etc/hostname"
1214

1315
func getIpv4Address() (string, error) {
1416
return ExecCmd("hostname", "-I")
@@ -26,59 +28,129 @@ func getDomain() (string, error) {
2628
return ExecCmd("hostname", "-d")
2729
}
2830

31+
func setHostname(hostName string) (string, error) {
32+
return ExecCmd("hostname", hostName)
33+
}
34+
2935
// This is required due to: https://github.com/saltstack/salt/issues/32719
30-
func ensureIpv6Resolvable(customDomain string) error {
31-
hostname, hostNameErr := getHostName()
32-
log.Printf("[ensureIpv6Resolvable] hostName: %s", hostname)
33-
if hostNameErr != nil {
34-
return hostNameErr
36+
func ensureHostIsResolvable(customHostname *string, customDomain string) error {
37+
var hostName string
38+
if customHostname != nil && len(*customHostname) > 0 {
39+
log.Printf("[ensureHostIsResolvable] use custom hostname: %s", *customHostname)
40+
hostName = *customHostname
41+
} else {
42+
if hn, hostNameErr := getHostName(); hostNameErr != nil {
43+
return hostNameErr
44+
} else {
45+
log.Printf("[ensureHostIsResolvable] default hostName: %s", hn)
46+
hostName = hn
47+
}
3548
}
3649

37-
domain, domainError := getDomain()
38-
log.Printf("[ensureIpv6Resolvable] origin domain: %s", domain)
39-
if customDomain == "" {
40-
if domainError != nil || domain == "" {
41-
domain = DEFAULT_DOMAIN
42-
}
43-
} else {
50+
var domain string
51+
if len(customDomain) > 0 {
52+
log.Printf("[ensureHostIsResolvable] use custom domain: %s", customDomain)
4453
domain = customDomain
54+
} else {
55+
if defaultDomain, domainError := getDomain(); domainError != nil || len(defaultDomain) == 0 {
56+
log.Printf("[ensureHostIsResolvable] default domain is not available, use: %s", EXAMPLE_DOMAIN)
57+
domain = EXAMPLE_DOMAIN
58+
} else {
59+
log.Printf("[ensureHostIsResolvable] use default domain: %s", defaultDomain)
60+
domain = defaultDomain
61+
}
4562
}
46-
updateIpv6HostName(hostname, domain, HOST_FILE_NAME, getIpv4Address, ioutil.ReadFile, ioutil.WriteFile)
63+
64+
if !strings.HasPrefix(domain, ".") {
65+
domain = "." + domain
66+
}
67+
68+
updateHostsFile(hostName, domain, HOSTS_FILE, getIpv4Address, ioutil.ReadFile, ioutil.WriteFile)
69+
updateSysConfig(hostName, domain, NETWORK_SYSCONFIG_FILE, ioutil.ReadFile, ioutil.WriteFile)
70+
updateHostNameFile(hostName, HOSTNAME_FILE, ioutil.WriteFile)
4771

4872
return nil
4973
}
5074

51-
func updateIpv6HostName(hostName string, domain string, file string,
75+
func updateHostsFile(hostName string, domain string, file string,
5276
getIpv4Address func() (string, error),
5377
readFile func(filename string) ([]byte, error),
5478
writeFile func(filename string, data []byte, perm os.FileMode) error) error {
55-
log.Printf("[updateIpv6HostName] hostName: %s, domain: %s", hostName, domain)
79+
80+
ip, err := getIpv4Address()
81+
if err != nil {
82+
return err
83+
}
84+
85+
log.Printf("[updateHostsFile] hostName: %s, domain: %s, ip: %s", hostName, domain, ip)
5686
b, err := readFile(file)
5787
if err != nil {
5888
return err
5989
}
6090
hostsFile := string(b)
61-
log.Printf("[updateIpv6HostName] original hosts file: %s", hostsFile)
62-
address, err := getIpv4Address()
91+
log.Printf("[updateHostsFile] original hosts file: %s", hostsFile)
92+
93+
ipv4HostString := ip + " " + hostName + domain + " " + hostName
94+
log.Printf("[updateHostsFile] ipv4HostString: %s", ipv4HostString)
95+
96+
lines := strings.Split(hostsFile, "\n")
97+
var filteredLines = make([]string, 0)
98+
for _, line := range lines {
99+
if !strings.Contains(line, ip) {
100+
filteredLines = append(filteredLines, line)
101+
}
102+
}
103+
hostsFile = strings.Join(filteredLines, "\n") + "\n" + ipv4HostString
104+
log.Printf("[updateHostsFile] updated hosts file: %s", hostsFile)
105+
err = writeFile(file, []byte(hostsFile), 0644)
63106
if err != nil {
64107
return err
65108
}
66-
if !strings.HasPrefix(domain, ".") {
67-
domain = "." + domain
109+
110+
_, err = setHostname(hostName)
111+
if err != nil {
112+
return err
68113
}
69-
ipv6hostString := address + " " + hostName + domain + " " + hostName
70-
log.Printf("[updateIpv6HostName] ipv6hostString: %s", ipv6hostString)
71114

72-
lines := strings.Split(hostsFile, "\n")
115+
return nil
116+
}
117+
118+
func updateSysConfig(hostName string, domain string, file string,
119+
readFile func(filename string) ([]byte, error),
120+
writeFile func(filename string, data []byte, perm os.FileMode) error) error {
121+
122+
log.Printf("[updateSysConfig] hostname: %s, domain: %s", hostName, domain)
123+
b, err := readFile(file)
124+
if err != nil {
125+
return err
126+
}
127+
sysConfig := string(b)
128+
log.Printf("[updateSysConfig] original sysconfig: %s", sysConfig)
129+
130+
lines := strings.Split(sysConfig, "\n")
73131
var filteredLines = make([]string, 0)
74132
for _, line := range lines {
75-
if !strings.Contains(line, address) {
133+
if !strings.Contains(line, "HOSTNAME=") && len(line) > 0 {
76134
filteredLines = append(filteredLines, line)
77135
}
78136
}
79-
hostsFile = strings.Join(filteredLines, "\n") + "\n" + ipv6hostString
80-
log.Printf("[updateIpv6HostName] updated hosts file: %s", hostsFile)
81-
err = writeFile(file, []byte(hostsFile), 0644)
137+
138+
hostNameString := "HOSTNAME=" + hostName + domain
139+
sysConfig = strings.Join(filteredLines, "\n") + "\n" + hostNameString
140+
log.Printf("[updateSysConfig] updated sysconfig: %s", sysConfig)
141+
err = writeFile(file, []byte(sysConfig), 0644)
142+
if err != nil {
143+
return err
144+
}
145+
146+
return nil
147+
}
148+
149+
func updateHostNameFile(hostName string, file string,
150+
writeFile func(filename string, data []byte, perm os.FileMode) error) error {
151+
152+
log.Printf("[updateHostNameFile] hostname: %s", hostName)
153+
err := writeFile(file, []byte(hostName), 0644)
82154
if err != nil {
83155
return err
84156
}

saltboot/hostname_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestHostsFileWriteRemoveExistingIp(t *testing.T) {
2525
return nil
2626
}
2727

28-
updateIpv6HostName("hostname-1", "example.com", "hosts", getIpv4Address, readFile, writeFile)
28+
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address, readFile, writeFile)
2929

3030
expected := `
3131
127.0.0.1 localhost
@@ -59,7 +59,7 @@ func TestHostsFileWriteRemoveExistingIpNotLastLine(t *testing.T) {
5959
return nil
6060
}
6161

62-
updateIpv6HostName("hostname-1", "example.com", "hosts", getIpv4Address, readFile, writeFile)
62+
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address, readFile, writeFile)
6363

6464
expected := `
6565
127.0.0.1 localhost
@@ -95,7 +95,7 @@ func TestHostsFileWriteRemoveExistingIpMiddleLastLine(t *testing.T) {
9595
return nil
9696
}
9797

98-
updateIpv6HostName("hostname-1", "example.com", "hosts", getIpv4Address, readFile, writeFile)
98+
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address, readFile, writeFile)
9999

100100
expected := `
101101
127.0.0.1 localhost
@@ -131,7 +131,7 @@ func TestHostsFileWriteIpNotPresent(t *testing.T) {
131131
return nil
132132
}
133133

134-
updateIpv6HostName("hostname-1", "example.com", "hosts", getIpv4Address, readFile, writeFile)
134+
updateHostsFile("hostname-1", ".example.com", "hosts", getIpv4Address, readFile, writeFile)
135135

136136
expected := `
137137
127.0.0.1 localhost
@@ -168,7 +168,7 @@ func TestHostsFileWriteExistingWithDefaultDomain(t *testing.T) {
168168
return nil
169169
}
170170

171-
updateIpv6HostName("hostname-1", ".compute.internal", "hosts", getIpv4Address, readFile, writeFile)
171+
updateHostsFile("hostname-1", ".compute.internal", "hosts", getIpv4Address, readFile, writeFile)
172172

173173
expected := `
174174
127.0.0.1 localhost

saltboot/salt.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ type SaltAuth struct {
2727
}
2828

2929
type SaltMaster struct {
30-
Address string `json:"address"`
31-
Auth SaltAuth `json:"auth,omitempty"`
32-
Domain string `json:"domain,omitempty"`
30+
Address string `json:"address"`
31+
Auth SaltAuth `json:"auth,omitempty"`
32+
Hostname *string `json:"hostName,omitempty"`
33+
Domain string `json:"domain,omitempty"`
3334
}
3435

3536
type SaltMinion struct {
@@ -38,6 +39,7 @@ type SaltMinion struct {
3839
Server string `json:"server,omitempty"`
3940
Servers []string `json:"servers,omitempty"`
4041
HostGroup string `json:"hostGroup,omitempty"`
42+
Hostname *string `json:"hostName,omitempty"`
4143
Domain string `json:"domain,omitempty"`
4244
}
4345

@@ -131,7 +133,7 @@ func SaltMinionRunRequestHandler(w http.ResponseWriter, req *http.Request) {
131133
saltMinion.Domain = saltActionRequest.Master.Domain
132134
}
133135

134-
err = ensureIpv6Resolvable(saltMinion.Domain)
136+
err = ensureHostIsResolvable(saltMinion.Hostname, saltMinion.Domain)
135137
if err != nil {
136138
log.Printf("[SaltMinionRunRequestHandler] [ERROR] while hostfile update: %s", err)
137139
}
@@ -226,7 +228,7 @@ func SaltServerRunRequestHandler(w http.ResponseWriter, req *http.Request) {
226228
saltMaster = saltActionRequest.Master
227229
}
228230

229-
ensureIpv6Resolvable(saltMaster.Domain)
231+
ensureHostIsResolvable(saltMaster.Hostname, saltMaster.Domain)
230232
if err != nil {
231233
log.Printf("[SaltServerRunRequestHandler] [ERROR] while hostfile update: %s", err)
232234
}

0 commit comments

Comments
 (0)