@@ -7,8 +7,10 @@ import (
7
7
"strings"
8
8
)
9
9
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"
12
14
13
15
func getIpv4Address () (string , error ) {
14
16
return ExecCmd ("hostname" , "-I" )
@@ -26,59 +28,129 @@ func getDomain() (string, error) {
26
28
return ExecCmd ("hostname" , "-d" )
27
29
}
28
30
31
+ func setHostname (hostName string ) (string , error ) {
32
+ return ExecCmd ("hostname" , hostName )
33
+ }
34
+
29
35
// 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
+ }
35
48
}
36
49
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 )
44
53
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
+ }
45
62
}
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 )
47
71
48
72
return nil
49
73
}
50
74
51
- func updateIpv6HostName (hostName string , domain string , file string ,
75
+ func updateHostsFile (hostName string , domain string , file string ,
52
76
getIpv4Address func () (string , error ),
53
77
readFile func (filename string ) ([]byte , error ),
54
78
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 )
56
86
b , err := readFile (file )
57
87
if err != nil {
58
88
return err
59
89
}
60
90
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 )
63
106
if err != nil {
64
107
return err
65
108
}
66
- if ! strings .HasPrefix (domain , "." ) {
67
- domain = "." + domain
109
+
110
+ _ , err = setHostname (hostName )
111
+ if err != nil {
112
+ return err
68
113
}
69
- ipv6hostString := address + " " + hostName + domain + " " + hostName
70
- log .Printf ("[updateIpv6HostName] ipv6hostString: %s" , ipv6hostString )
71
114
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 " )
73
131
var filteredLines = make ([]string , 0 )
74
132
for _ , line := range lines {
75
- if ! strings .Contains (line , address ) {
133
+ if ! strings .Contains (line , "HOSTNAME=" ) && len ( line ) > 0 {
76
134
filteredLines = append (filteredLines , line )
77
135
}
78
136
}
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 )
82
154
if err != nil {
83
155
return err
84
156
}
0 commit comments