rewrite minio runtime checks

This commit is contained in:
Anand Babu (AB) Periasamy
2016-02-15 17:56:56 +05:30
parent e73301944a
commit bbca70e13b
21 changed files with 1574 additions and 202 deletions

View File

@@ -1,67 +0,0 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package user
import (
"os"
"os/user"
"runtime"
"strconv"
)
// Current is a portable implementation to determine the current user.
// Golang's user.Current does not work reliably under docker or 32bit linux
//
// Two issues this code handles :-
//
// Docker Container - For static binaries NSS library will not be a part of the static binary hence user.Current() fails.
// Linux Intel 32 bit - CGO is not enabled so it will not link with NSS library.
//
func Current() (*user.User, error) {
if os.Getenv("DOCKERIMAGE") == "1" {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
return &user.User{Uid: "0", Gid: "0", Username: "root", Name: "root", HomeDir: wd}, nil
}
if runtime.GOARCH == "386" {
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
return &user.User{
Uid: strconv.Itoa(os.Getuid()),
Gid: strconv.Itoa(os.Getgid()),
Username: os.Getenv("USER"),
Name: os.Getenv("USER"),
HomeDir: os.Getenv("HOME"),
}, nil
}
}
user, e := user.Current()
if e != nil {
return nil, e
}
return user, nil
}
// HomeDir - return current home directory.
func HomeDir() (string, error) {
user, err := Current()
if err != nil {
return "", err
}
return user.HomeDir, nil
}

View File

@@ -1,41 +0,0 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package user_test
import (
"testing"
"github.com/minio/minio/pkg/user"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestUser(c *C) {
_, err := user.Current()
c.Assert(err, IsNil)
}
func (s *MySuite) TestHomeDir(c *C) {
_, err := user.HomeDir()
c.Assert(err, IsNil)
}

View File

@@ -17,9 +17,9 @@
package xl
import (
"os/user"
"path/filepath"
"github.com/minio/go-homedir"
"github.com/minio/minio/pkg/probe"
"github.com/minio/minio/pkg/quick"
)
@@ -29,11 +29,11 @@ func getXLConfigPath() (string, *probe.Error) {
if customConfigPath != "" {
return customConfigPath, nil
}
u, err := user.Current()
homeDir, err := homedir.Dir()
if err != nil {
return "", probe.NewError(err)
}
xlConfigPath := filepath.Join(u.HomeDir, ".minio", "xl.json")
xlConfigPath := filepath.Join(homeDir, ".minio", "xl.json")
return xlConfigPath, nil
}