2017-06-09 22:50:51 -04:00
|
|
|
/*
|
2018-02-28 23:13:33 -05:00
|
|
|
* Minio Cloud Storage, (C) 2017, 2018 Minio, Inc.
|
2017-06-09 22:50:51 -04:00
|
|
|
*
|
|
|
|
* 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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-02-02 21:18:52 -05:00
|
|
|
"net"
|
2017-06-09 22:50:51 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-29 17:38:26 -04:00
|
|
|
"strconv"
|
2017-06-09 22:50:51 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-07-12 17:12:40 -04:00
|
|
|
etcd "github.com/coreos/etcd/clientv3"
|
2018-09-20 17:56:32 -04:00
|
|
|
dns2 "github.com/miekg/dns"
|
2017-06-09 22:50:51 -04:00
|
|
|
"github.com/minio/cli"
|
2018-08-17 15:52:14 -04:00
|
|
|
"github.com/minio/minio/cmd/crypto"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2017-10-31 14:54:32 -04:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2018-02-02 21:18:52 -05:00
|
|
|
"github.com/minio/minio/pkg/dns"
|
2018-05-11 15:02:30 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio-go/pkg/set"
|
2017-06-09 22:50:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Check for updates and print a notification message
|
|
|
|
func checkUpdate(mode string) {
|
2017-12-15 15:33:42 -05:00
|
|
|
// Its OK to ignore any errors during doUpdate() here.
|
|
|
|
if updateMsg, _, currentReleaseTime, latestReleaseTime, err := getUpdateInfo(2*time.Second, mode); err == nil {
|
|
|
|
if globalInplaceUpdateDisabled {
|
2018-04-10 12:37:14 -04:00
|
|
|
logger.StartupMessage(updateMsg)
|
2017-12-15 15:33:42 -05:00
|
|
|
} else {
|
2018-04-10 12:37:14 -04:00
|
|
|
logger.StartupMessage(prepareUpdateMessage("Run `minio update`", latestReleaseTime.Sub(currentReleaseTime)))
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 18:55:06 -04:00
|
|
|
// Load logger targets based on user's configuration
|
|
|
|
func loadLoggers() {
|
|
|
|
if globalServerConfig.Logger.Console.Enabled {
|
|
|
|
// Enable console logging
|
|
|
|
logger.AddTarget(logger.NewConsole())
|
|
|
|
}
|
|
|
|
for _, l := range globalServerConfig.Logger.HTTP {
|
|
|
|
if l.Enabled {
|
|
|
|
// Enable http logging
|
|
|
|
logger.AddTarget(logger.NewHTTP(l.Endpoint, NewCustomHTTPTransport()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
func handleCommonCmdArgs(ctx *cli.Context) {
|
2018-02-28 23:13:33 -05:00
|
|
|
|
|
|
|
var configDir string
|
|
|
|
|
2018-08-06 13:26:40 -04:00
|
|
|
switch {
|
|
|
|
case ctx.IsSet("config-dir"):
|
2018-02-28 23:13:33 -05:00
|
|
|
configDir = ctx.String("config-dir")
|
2018-08-06 13:26:40 -04:00
|
|
|
case ctx.GlobalIsSet("config-dir"):
|
2018-02-28 23:13:33 -05:00
|
|
|
configDir = ctx.GlobalString("config-dir")
|
|
|
|
// cli package does not expose parent's "config-dir" option. Below code is workaround.
|
|
|
|
if configDir == "" || configDir == getConfigDir() {
|
|
|
|
if ctx.Parent().GlobalIsSet("config-dir") {
|
|
|
|
configDir = ctx.Parent().GlobalString("config-dir")
|
|
|
|
}
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
2018-08-06 13:26:40 -04:00
|
|
|
default:
|
2018-02-28 23:13:33 -05:00
|
|
|
// Neither local nor global config-dir option is provided. In this case, try to use
|
|
|
|
// default config directory.
|
|
|
|
configDir = getConfigDir()
|
2017-06-09 22:50:51 -04:00
|
|
|
if configDir == "" {
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.FatalIf(errors.New("missing option"), "config-dir option must be provided")
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
2018-02-28 23:13:33 -05:00
|
|
|
}
|
2017-06-09 22:50:51 -04:00
|
|
|
|
2018-02-28 23:13:33 -05:00
|
|
|
if configDir == "" {
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.FatalIf(errors.New("empty directory"), "Configuration directory cannot be empty")
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
2018-02-28 23:13:33 -05:00
|
|
|
|
|
|
|
// Disallow relative paths, figure out absolute paths.
|
|
|
|
configDirAbs, err := filepath.Abs(configDir)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.FatalIf(err, "Unable to fetch absolute path for config directory %s", configDir)
|
2018-02-28 23:13:33 -05:00
|
|
|
setConfigDir(configDirAbs)
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-27 23:36:17 -04:00
|
|
|
// Parses the given compression exclude list `extensions` or `content-types`.
|
|
|
|
func parseCompressIncludes(includes []string) ([]string, error) {
|
|
|
|
for _, e := range includes {
|
|
|
|
if len(e) == 0 {
|
|
|
|
return nil, uiErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type (%s) cannot be empty", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return includes, nil
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
func handleCommonEnvVars() {
|
2018-09-27 23:36:17 -04:00
|
|
|
compressEnvDelimiter := ","
|
2017-06-09 22:50:51 -04:00
|
|
|
// Start profiler if env is set.
|
|
|
|
if profiler := os.Getenv("_MINIO_PROFILER"); profiler != "" {
|
2018-09-18 19:46:35 -04:00
|
|
|
var err error
|
|
|
|
globalProfiler, err = startProfiler(profiler, "")
|
|
|
|
logger.FatalIf(err, "Unable to setup a profiler")
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := os.Getenv("MINIO_ACCESS_KEY")
|
|
|
|
secretKey := os.Getenv("MINIO_SECRET_KEY")
|
|
|
|
if accessKey != "" && secretKey != "" {
|
2017-10-31 14:54:32 -04:00
|
|
|
cred, err := auth.CreateCredentials(accessKey, secretKey)
|
2018-05-08 22:04:36 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(uiErrInvalidCredentials(err), "Unable to validate credentials inherited from the shell environment")
|
|
|
|
}
|
2017-06-09 22:50:51 -04:00
|
|
|
|
|
|
|
// credential Envs are set globally.
|
|
|
|
globalIsEnvCreds = true
|
|
|
|
globalActiveCred = cred
|
|
|
|
}
|
|
|
|
|
|
|
|
if browser := os.Getenv("MINIO_BROWSER"); browser != "" {
|
2018-06-06 21:10:51 -04:00
|
|
|
browserFlag, err := ParseBoolFlag(browser)
|
2017-06-09 22:50:51 -04:00
|
|
|
if err != nil {
|
2018-09-20 17:56:32 -04:00
|
|
|
logger.Fatal(uiErrInvalidBrowserValue(nil).Msg("Unknown value `%s`", browser), "Invalid MINIO_BROWSER environment variable")
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// browser Envs are set globally, this does not represent
|
|
|
|
// if browser is turned off or on.
|
|
|
|
globalIsEnvBrowser = true
|
|
|
|
globalIsBrowserEnabled = bool(browserFlag)
|
|
|
|
}
|
2017-10-24 22:04:51 -04:00
|
|
|
|
2018-01-05 14:24:31 -05:00
|
|
|
traceFile := os.Getenv("MINIO_HTTP_TRACE")
|
|
|
|
if traceFile != "" {
|
|
|
|
var err error
|
|
|
|
globalHTTPTraceFile, err = os.OpenFile(traceFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.FatalIf(err, "error opening file %s", traceFile)
|
2018-01-05 14:24:31 -05:00
|
|
|
}
|
2017-11-14 19:56:24 -05:00
|
|
|
|
2018-02-02 21:18:52 -05:00
|
|
|
etcdEndpointsEnv, ok := os.LookupEnv("MINIO_ETCD_ENDPOINTS")
|
|
|
|
if ok {
|
|
|
|
etcdEndpoints := strings.Split(etcdEndpointsEnv, ",")
|
|
|
|
var err error
|
2018-04-23 14:05:56 -04:00
|
|
|
globalEtcdClient, err = etcd.New(etcd.Config{
|
2018-07-12 17:12:40 -04:00
|
|
|
Endpoints: etcdEndpoints,
|
|
|
|
DialTimeout: defaultDialTimeout,
|
|
|
|
DialKeepAliveTime: defaultDialKeepAlive,
|
2018-02-02 21:18:52 -05:00
|
|
|
})
|
|
|
|
logger.FatalIf(err, "Unable to initialize etcd with %s", etcdEndpoints)
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:20:22 -04:00
|
|
|
globalDomainName, globalIsEnvDomainName = os.LookupEnv("MINIO_DOMAIN")
|
2018-09-20 17:56:32 -04:00
|
|
|
if globalDomainName != "" {
|
|
|
|
if _, ok := dns2.IsDomainName(globalDomainName); !ok {
|
|
|
|
logger.Fatal(uiErrInvalidDomainValue(nil).Msg("Unknown value `%s`", globalDomainName), "Invalid MINIO_DOMAIN environment variable")
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 21:20:22 -04:00
|
|
|
|
2018-05-11 15:02:30 -04:00
|
|
|
minioEndpointsEnv, ok := os.LookupEnv("MINIO_PUBLIC_IPS")
|
|
|
|
if ok {
|
|
|
|
minioEndpoints := strings.Split(minioEndpointsEnv, ",")
|
|
|
|
globalDomainIPs = set.NewStringSet()
|
|
|
|
for i, ip := range minioEndpoints {
|
|
|
|
if net.ParseIP(ip) == nil {
|
|
|
|
logger.FatalIf(errInvalidArgument, "Unable to initialize Minio server with invalid MINIO_PUBLIC_IPS[%d]: %s", i, ip)
|
|
|
|
}
|
|
|
|
globalDomainIPs.Add(ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if globalDomainName != "" && !globalDomainIPs.IsEmpty() && globalEtcdClient != nil {
|
2018-02-02 21:18:52 -05:00
|
|
|
var err error
|
2018-05-11 15:02:30 -04:00
|
|
|
globalDNSConfig, err = dns.NewCoreDNS(globalDomainName, globalDomainIPs, globalMinioPort, globalEtcdClient)
|
2018-02-02 21:18:52 -05:00
|
|
|
logger.FatalIf(err, "Unable to initialize DNS config for %s.", globalDomainName)
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:14:06 -04:00
|
|
|
if drives := os.Getenv("MINIO_CACHE_DRIVES"); drives != "" {
|
2018-03-29 17:38:26 -04:00
|
|
|
driveList, err := parseCacheDrives(strings.Split(drives, cacheEnvDelimiter))
|
2018-05-08 22:04:36 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Unable to parse MINIO_CACHE_DRIVES value (%s)", drives)
|
|
|
|
}
|
2018-03-28 17:14:06 -04:00
|
|
|
globalCacheDrives = driveList
|
|
|
|
globalIsDiskCacheEnabled = true
|
|
|
|
}
|
2018-05-08 22:04:36 -04:00
|
|
|
|
2018-03-28 17:14:06 -04:00
|
|
|
if excludes := os.Getenv("MINIO_CACHE_EXCLUDE"); excludes != "" {
|
2018-03-29 17:38:26 -04:00
|
|
|
excludeList, err := parseCacheExcludes(strings.Split(excludes, cacheEnvDelimiter))
|
2018-05-08 22:04:36 -04:00
|
|
|
if err != nil {
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.Fatal(err, "Unable to parse MINIO_CACHE_EXCLUDE value (`%s`)", excludes)
|
2018-05-08 22:04:36 -04:00
|
|
|
}
|
2018-03-28 17:14:06 -04:00
|
|
|
globalCacheExcludes = excludeList
|
|
|
|
}
|
2018-05-08 22:04:36 -04:00
|
|
|
|
2018-03-28 17:14:06 -04:00
|
|
|
if expiryStr := os.Getenv("MINIO_CACHE_EXPIRY"); expiryStr != "" {
|
2018-03-29 17:38:26 -04:00
|
|
|
expiry, err := strconv.Atoi(expiryStr)
|
2018-05-08 22:04:36 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(uiErrInvalidCacheExpiryValue(err), "Unable to parse MINIO_CACHE_EXPIRY value (`%s`)", expiryStr)
|
|
|
|
}
|
2018-03-28 17:14:06 -04:00
|
|
|
globalCacheExpiry = expiry
|
|
|
|
}
|
2018-03-29 17:38:26 -04:00
|
|
|
|
2018-06-25 13:24:12 -04:00
|
|
|
if maxUseStr := os.Getenv("MINIO_CACHE_MAXUSE"); maxUseStr != "" {
|
|
|
|
maxUse, err := strconv.Atoi(maxUseStr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(uiErrInvalidCacheMaxUse(err), "Unable to parse MINIO_CACHE_MAXUSE value (`%s`)", maxUseStr)
|
|
|
|
}
|
|
|
|
// maxUse should be a valid percentage.
|
|
|
|
if maxUse > 0 && maxUse <= 100 {
|
|
|
|
globalCacheMaxUse = maxUse
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 15:33:42 -05:00
|
|
|
// In place update is true by default if the MINIO_UPDATE is not set
|
|
|
|
// or is not set to 'off', if MINIO_UPDATE is set to 'off' then
|
|
|
|
// in-place update is off.
|
|
|
|
globalInplaceUpdateDisabled = strings.EqualFold(os.Getenv("MINIO_UPDATE"), "off")
|
2017-12-22 06:28:13 -05:00
|
|
|
|
|
|
|
// Validate and store the storage class env variables only for XL/Dist XL setups
|
|
|
|
if globalIsXL {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Check for environment variables and parse into storageClass struct
|
2017-12-26 23:36:16 -05:00
|
|
|
if ssc := os.Getenv(standardStorageClassEnv); ssc != "" {
|
2017-12-22 06:28:13 -05:00
|
|
|
globalStandardStorageClass, err = parseStorageClass(ssc)
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.FatalIf(err, "Invalid value set in environment variable %s", standardStorageClassEnv)
|
2017-12-22 06:28:13 -05:00
|
|
|
}
|
|
|
|
|
2017-12-26 23:36:16 -05:00
|
|
|
if rrsc := os.Getenv(reducedRedundancyStorageClassEnv); rrsc != "" {
|
2017-12-22 06:28:13 -05:00
|
|
|
globalRRStorageClass, err = parseStorageClass(rrsc)
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.FatalIf(err, "Invalid value set in environment variable %s", reducedRedundancyStorageClassEnv)
|
2017-12-22 06:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validation is done after parsing both the storage classes. This is needed because we need one
|
|
|
|
// storage class value to deduce the correct value of the other storage class.
|
|
|
|
if globalRRStorageClass.Scheme != "" {
|
2018-02-01 02:30:07 -05:00
|
|
|
err = validateParity(globalStandardStorageClass.Parity, globalRRStorageClass.Parity)
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.FatalIf(err, "Invalid value set in environment variable %s", reducedRedundancyStorageClassEnv)
|
2017-12-22 06:28:13 -05:00
|
|
|
globalIsStorageClass = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalStandardStorageClass.Scheme != "" {
|
2018-02-01 02:30:07 -05:00
|
|
|
err = validateParity(globalStandardStorageClass.Parity, globalRRStorageClass.Parity)
|
2018-05-09 18:11:24 -04:00
|
|
|
logger.FatalIf(err, "Invalid value set in environment variable %s", standardStorageClassEnv)
|
2017-12-22 06:28:13 -05:00
|
|
|
globalIsStorageClass = true
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 19:44:45 -04:00
|
|
|
|
|
|
|
// Get WORM environment variable.
|
2018-06-06 21:10:51 -04:00
|
|
|
if worm := os.Getenv("MINIO_WORM"); worm != "" {
|
|
|
|
wormFlag, err := ParseBoolFlag(worm)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(uiErrInvalidWormValue(nil).Msg("Unknown value `%s`", worm), "Unable to validate MINIO_WORM environment variable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// worm Envs are set globally, this does not represent
|
|
|
|
// if worm is turned off or on.
|
|
|
|
globalIsEnvWORM = true
|
|
|
|
globalWORMEnabled = bool(wormFlag)
|
|
|
|
}
|
2018-08-17 15:52:14 -04:00
|
|
|
|
|
|
|
kmsConf, err := crypto.NewVaultConfig()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Unable to initialize hashicorp vault")
|
|
|
|
}
|
|
|
|
if kmsConf.Vault.Endpoint != "" {
|
|
|
|
kms, err := crypto.NewVault(kmsConf)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Unable to initialize KMS")
|
|
|
|
}
|
|
|
|
globalKMS = kms
|
|
|
|
globalKMSKeyID = kmsConf.Vault.Key.Name
|
|
|
|
globalKMSConfig = kmsConf
|
|
|
|
}
|
2018-09-27 23:36:17 -04:00
|
|
|
|
|
|
|
if compress := os.Getenv("MINIO_COMPRESS"); compress != "" {
|
|
|
|
globalIsCompressionEnabled = strings.EqualFold(compress, "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
compressExtensions := os.Getenv("MINIO_COMPRESS_EXTENSIONS")
|
|
|
|
compressMimeTypes := os.Getenv("MINIO_COMPRESS_MIMETYPES")
|
|
|
|
if compressExtensions != "" || compressMimeTypes != "" {
|
|
|
|
globalIsEnvCompression = true
|
|
|
|
if compressExtensions != "" {
|
|
|
|
extensions, err := parseCompressIncludes(strings.Split(compressExtensions, compressEnvDelimiter))
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Invalid MINIO_COMPRESS_EXTENSIONS value (`%s`)", extensions)
|
|
|
|
}
|
|
|
|
globalCompressExtensions = extensions
|
|
|
|
}
|
|
|
|
if compressMimeTypes != "" {
|
|
|
|
contenttypes, err := parseCompressIncludes(strings.Split(compressMimeTypes, compressEnvDelimiter))
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(err, "Invalid MINIO_COMPRESS_MIMETYPES value (`%s`)", contenttypes)
|
|
|
|
}
|
|
|
|
globalCompressMimeTypes = contenttypes
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 22:50:51 -04:00
|
|
|
}
|