Skip to content

Commit 27d6358

Browse files
keykimhmxs
authored andcommitted
support SUSE linux
1 parent 7d250ad commit 27d6358

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
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.1
3+
VERSION=0.12.2
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/cmdExecutor.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package saltboot
22

33
import (
4+
"errors"
5+
"fmt"
46
"log"
57
"os/exec"
68
"strings"
79
)
810

9-
const (
10-
ENV_TYPE = "SALT_BOOTSTRAP_ENV_TYPE"
11-
)
12-
1311
var commandExecutor = func(executable string, args ...string) ([]byte, error) {
1412
return exec.Command(executable, args...).CombinedOutput()
1513
}
1614

1715
func ExecCmd(executable string, args ...string) (outStr string, err error) {
18-
log.Printf("[cmdExecutor] Execute command: %s %s", executable, strings.Join(args, " "))
16+
command := executable + " " + strings.Join(args, " ")
17+
log.Printf("[cmdExecutor] Execute command: %s", command)
1918
out, e := commandExecutor(executable, args...)
2019
if e != nil {
21-
err = e
20+
err = errors.New(fmt.Sprintf("Failed to execute command: '%s', err: %s", command, e.Error()))
2221
}
2322
if out != nil {
2423
outStr = strings.TrimSpace(string(out))

saltboot/hostname.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
const EXAMPLE_DOMAIN = ".example.com"
1010
const HOSTS_FILE = "/etc/hosts"
1111
const NETWORK_SYSCONFIG_FILE = "/etc/sysconfig/network"
12+
const NETWORK_SYSCONFIG_FILE_SUSE = "/etc/sysconfig/network/config"
1213
const HOSTNAME_FILE = "/etc/hostname"
1314

1415
var readFile = ioutil.ReadFile
@@ -23,7 +24,12 @@ func getFQDN() (string, error) {
2324
}
2425

2526
func getHostName() (string, error) {
26-
return ExecCmd("hostname", "-s")
27+
out, err := ExecCmd("hostname", "-s")
28+
if err != nil {
29+
log.Printf("[getHostName] hostname -s returned an error, fallback to simple hostname command, err: %s", err.Error())
30+
out, err = ExecCmd("hostname")
31+
}
32+
return out, err
2733
}
2834

2935
func getDomain() (string, error) {
@@ -71,7 +77,11 @@ func ensureHostIsResolvable(customHostname *string, customDomain string) error {
7177
log.Printf("[ensureHostIsResolvable] [ERROR] unable to update host file: %s", err.Error())
7278
return err
7379
}
74-
if err := updateSysConfig(hostName, domain, NETWORK_SYSCONFIG_FILE); err != nil {
80+
networkSysConfig := NETWORK_SYSCONFIG_FILE
81+
if isOs(SUSE) {
82+
networkSysConfig = NETWORK_SYSCONFIG_FILE_SUSE
83+
}
84+
if err := updateSysConfig(hostName, domain, networkSysConfig); err != nil {
7585
log.Printf("[ensureHostIsResolvable] [ERROR] unable to update sys config: %s", err.Error())
7686
return err
7787
}
@@ -128,7 +138,7 @@ func updateSysConfig(hostName string, domain string, file string) error {
128138
return err
129139
}
130140
sysConfig := string(b)
131-
log.Printf("[updateSysConfig] original sysconfig: %s", sysConfig)
141+
log.Printf("[updateSysConfig] original sysconfig %s: %s", file, sysConfig)
132142

133143
lines := strings.Split(sysConfig, "\n")
134144
var filteredLines = make([]string, 0)
@@ -140,21 +150,19 @@ func updateSysConfig(hostName string, domain string, file string) error {
140150

141151
hostNameString := "HOSTNAME=" + hostName + domain
142152
sysConfig = strings.Join(filteredLines, "\n") + "\n" + hostNameString
143-
log.Printf("[updateSysConfig] updated sysconfig: %s", sysConfig)
153+
log.Printf("[updateSysConfig] updated sysconfig %s: %s", file, sysConfig)
144154
err = writeFile(file, []byte(sysConfig), 0644)
145155
if err != nil {
146156
return err
147157
}
148-
149158
return nil
150159
}
151160

152161
func updateHostNameFile(hostName string, file string) error {
153-
log.Printf("[updateHostNameFile] hostname: %s", hostName)
162+
log.Printf("[updateHostNameFile] hostname: %s, file: %s", hostName, file)
154163
err := writeFile(file, []byte(hostName), 0644)
155164
if err != nil {
156165
return err
157166
}
158-
159167
return nil
160168
}

saltboot/salt_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func TestSaltMinionRunRequestHandler(t *testing.T) {
6767
defer func() { watchCommands = false }()
6868

6969
tempDirName, _ := ioutil.TempDir("", "saltminionruntest")
70-
defer os.RemoveAll(tempDirName)
7170

7271
request := SaltActionRequest{
7372
Master: SaltMaster{Address: "address"},
@@ -106,13 +105,15 @@ func TestSaltMinionRunRequestHandler(t *testing.T) {
106105
if err != nil {
107106
t.Errorf("couldn't unmarshall grain yaml: %s", err)
108107
}
108+
os.RemoveAll(tempDirName)
109109
}()
110110

111111
checkExecutedCommands([]string{
112112
"hostname -s",
113113
"hostname -d",
114114
"hostname -I",
115115
"hostname ",
116+
"grep SUSE /etc/issue",
116117
"ps aux",
117118
"/bin/systemctl start salt-minion",
118119
"/bin/systemctl enable salt-minion",
@@ -166,9 +167,11 @@ func TestSaltServerRunRequestHandler(t *testing.T) {
166167
"hostname -d",
167168
"hostname -I",
168169
"hostname ",
170+
"grep SUSE /etc/issue",
169171
"grep saltuser /etc/passwd",
170172
"grep Ubuntu /etc/issue",
171173
"grep Debian /etc/issue",
174+
"grep SUSE /etc/issue",
172175
"^adduser --no-create-home -G wheel -s /sbin/nologin --password \\$6\\$([a-zA-Z\\$0-9/.]+) saltuser",
173176
"ps aux",
174177
"/bin/systemctl start salt-master",

saltboot/user.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88

99
"github.com/hortonworks/salt-bootstrap/saltboot/model"
1010
"github.com/kless/osutil/user/crypt/sha512_crypt"
11+
"strings"
1112
)
1213

1314
const (
1415
SALT_USER = "saltuser"
1516
UBUNTU = "Ubuntu"
1617
DEBIAN = "Debian"
18+
SUSE = "SUSE"
1719
)
1820

1921
func init() {
@@ -34,7 +36,7 @@ func CreateUser(saltMaster SaltMaster) (resp model.Response, err error) {
3436

3537
result := "Create user: OK"
3638

37-
//saltUser, _ := user.Lookup(SALT_USER) //requires cgo
39+
// saltUser, _ := user.Lookup(SALT_USER) //requires cgo
3840
out, err := ExecCmd("grep", SALT_USER, "/etc/passwd")
3941

4042
if len(out) == 0 || err != nil {
@@ -50,8 +52,12 @@ func CreateUser(saltMaster SaltMaster) (resp model.Response, err error) {
5052
return model.Response{ErrorText: err.Error(), StatusCode: http.StatusInternalServerError}, err
5153
}
5254

53-
if isDebianOrUbuntu() {
55+
if shouldUseUserAdd() {
5456
result, err = ExecCmd("groupadd", "-r", "wheel")
57+
if err != nil && strings.Contains(err.Error(), "exit status 9") {
58+
log.Printf("[CreateUser] ignore group exists error: %s", err.Error())
59+
err = nil
60+
}
5561
if err == nil {
5662
result, err = ExecCmd("useradd", "--no-create-home", "-G", "wheel", "-s", "/sbin/nologin", "--password", hash, SALT_USER)
5763
}
@@ -71,8 +77,8 @@ func CreateUser(saltMaster SaltMaster) (resp model.Response, err error) {
7177
return resp, nil
7278
}
7379

74-
func isDebianOrUbuntu() bool {
75-
return isOs(UBUNTU) || isOs(DEBIAN)
80+
func shouldUseUserAdd() bool {
81+
return isOs(UBUNTU) || isOs(DEBIAN) || isOs(SUSE)
7682
}
7783

7884
func isOs(os string) bool {

saltboot/user_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestCreateUser(t *testing.T) {
1818
"grep saltuser /etc/passwd",
1919
"grep Ubuntu /etc/issue",
2020
"grep Debian /etc/issue",
21+
"grep SUSE /etc/issue",
2122
"^adduser --no-create-home -G wheel -s /sbin/nologin --password \\$6\\$([a-zA-Z\\$0-9/.]+) saltuser",
2223
}, t)
2324
}

0 commit comments

Comments
 (0)