Merge pull request #21820 from estesp/lazy-init-useradd
Lazy init useradd and remove init()
This commit is contained in:
commit
0a8becd7df
1 changed files with 10 additions and 10 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// add a user and/or group to Linux /etc/passwd, /etc/group using standard
|
// add a user and/or group to Linux /etc/passwd, /etc/group using standard
|
||||||
|
@ -16,6 +17,7 @@ import (
|
||||||
// useradd -r -s /bin/false <username>
|
// useradd -r -s /bin/false <username>
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
once sync.Once
|
||||||
userCommand string
|
userCommand string
|
||||||
|
|
||||||
cmdTemplates = map[string]string{
|
cmdTemplates = map[string]string{
|
||||||
|
@ -31,15 +33,6 @@ var (
|
||||||
userMod = "usermod"
|
userMod = "usermod"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
// set up which commands are used for adding users/groups dependent on distro
|
|
||||||
if _, err := resolveBinary("adduser"); err == nil {
|
|
||||||
userCommand = "adduser"
|
|
||||||
} else if _, err := resolveBinary("useradd"); err == nil {
|
|
||||||
userCommand = "useradd"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveBinary(binname string) (string, error) {
|
func resolveBinary(binname string) (string, error) {
|
||||||
binaryPath, err := exec.LookPath(binname)
|
binaryPath, err := exec.LookPath(binname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -94,7 +87,14 @@ func AddNamespaceRangesUser(name string) (int, int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func addUser(userName string) error {
|
func addUser(userName string) error {
|
||||||
|
once.Do(func() {
|
||||||
|
// set up which commands are used for adding users/groups dependent on distro
|
||||||
|
if _, err := resolveBinary("adduser"); err == nil {
|
||||||
|
userCommand = "adduser"
|
||||||
|
} else if _, err := resolveBinary("useradd"); err == nil {
|
||||||
|
userCommand = "useradd"
|
||||||
|
}
|
||||||
|
})
|
||||||
if userCommand == "" {
|
if userCommand == "" {
|
||||||
return fmt.Errorf("Cannot add user; no useradd/adduser binary found")
|
return fmt.Errorf("Cannot add user; no useradd/adduser binary found")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue