make user retrieval for Ansible provisioner more robust
Previously, the Ansible provisioner would look for the username from the `USER` environment variable. Unfortunately, this is not always set - particularly in Docker containers. It's very confusing to understand why the error is happening. Switched to using Go's built-in `os/user` package for retrieving the current username. @rickard-von-essen had done this in7369841
, but moved away from it ind59844f
because, at the time, it wasn't possible to use that library with cross-compilation. This was fixed in Go in795e712b72
This commit is contained in:
parent
a8e30bc796
commit
b894c925d1
|
@ -15,6 +15,7 @@ import (
|
|||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
@ -141,7 +142,12 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
|||
}
|
||||
|
||||
if p.config.User == "" {
|
||||
p.config.User = os.Getenv("USER")
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
errs = packer.MultiErrorAppend(errs, err)
|
||||
} else {
|
||||
p.config.User = usr.Username
|
||||
}
|
||||
}
|
||||
if p.config.User == "" {
|
||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("user: could not determine current user from environment."))
|
||||
|
|
|
@ -76,6 +76,16 @@ func TestProvisionerPrepare_Defaults(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
defer os.Remove(playbook_file.Name())
|
||||
|
||||
err = os.Unsetenv("USER")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
err = p.Prepare(config)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProvisionerPrepare_PlaybookFile(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue