Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidestep HOME env var for juju-db snap testing #150

Merged
merged 1 commit into from
May 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ var (
storageEngineMongoVersion = mongo32

installedMongod mongodCache

homeDir string
)

const (
Expand All @@ -69,12 +67,6 @@ const (
DefaultMongoPassword = "conn-from-name-secret"
)

func init() {
// Some juju test suites overwrite env vars.
// When we stop doing that, we can move this.
homeDir, _ = os.UserHomeDir()
}

// Certs holds the certificates and keys required to make a secure
// SSL connection.
type Certs struct {
Expand Down Expand Up @@ -180,6 +172,32 @@ func generatePEM(path string, serverCert *x509.Certificate, serverKey *rsa.Priva
return nil
}

// getHome for robust detection of HOME directory for use on Linux with
// snaps.
func getHome() (string, error) {
targetUID := strconv.Itoa(os.Getuid())
passwd, err := ioutil.ReadFile("/etc/passwd")
if err != nil {
return "", errors.Trace(err)
}
lines := strings.Split(string(passwd), "\n")
for _, line := range lines {
passwdEntry := strings.Split(line, ":")
if len(passwdEntry) != 7 {
// Invalid passwd entry.
continue
}
uidEntry := passwdEntry[2]
if uidEntry != targetUID {
// Not the user we are looking for.
continue
}
home := passwdEntry[5]
return home, nil
}
return "", errors.NotFoundf("UNIX user %s", targetUID)
}

// Start starts a MongoDB server in a temporary directory.
func (inst *MgoInstance) Start(certs *Certs) error {
var err error
Expand All @@ -191,8 +209,12 @@ func (inst *MgoInstance) Start(certs *Certs) error {
dbdir := ""

// Check for snap confined mongod.
if mongopath == "/snap/bin/juju-db.mongod" {
base := path.Join(homeDir, "snap/juju-db/current/tmp")
if runtime.GOOS == "linux" && mongopath == "/snap/bin/juju-db.mongod" {
home, err := getHome()
if err != nil {
return errors.Annotatef(err, "failed to find HOME directory")
}
base := path.Join(home, "snap/juju-db/current/tmp")
err = os.Mkdir(base, 0755)
if os.IsExist(err) {
// do nothing
Expand Down Expand Up @@ -443,9 +465,8 @@ func getMongod() (string, error) {
paths = append(paths, path)
}

if homeDir == "" {
logger.Debugf("no home directory found, skipping /snap/bin/juju-db.mongod")
} else {
if runtime.GOOS == "linux" {
// Snaps are only supported on linux for now.
paths = append(paths, "/snap/bin/juju-db.mongod")
}

Expand Down