2016-02-19 17:42:51 -05:00
|
|
|
package flags
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/cliconfig"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-12-29 19:27:12 -05:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2015-05-05 00:18:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-19 17:42:51 -05:00
|
|
|
// DefaultTrustKeyFile is the default filename for the trust key
|
|
|
|
DefaultTrustKeyFile = "key.json"
|
|
|
|
// DefaultCaFile is the default filename for the CA pem file
|
|
|
|
DefaultCaFile = "ca.pem"
|
|
|
|
// DefaultKeyFile is the default filename for the key pem file
|
|
|
|
DefaultKeyFile = "key.pem"
|
|
|
|
// DefaultCertFile is the default filename for the cert pem file
|
|
|
|
DefaultCertFile = "cert.pem"
|
|
|
|
// TLSVerifyKey is the default flag name for the tls verification option
|
|
|
|
TLSVerifyKey = "tlsverify"
|
2015-05-05 00:18:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
|
|
|
|
dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
|
|
|
|
)
|
|
|
|
|
2016-04-21 17:51:28 -04:00
|
|
|
// CommonFlags are flags common to both the client and the daemon.
|
|
|
|
type CommonFlags struct {
|
|
|
|
FlagSet *flag.FlagSet
|
|
|
|
PostParse func()
|
|
|
|
|
|
|
|
Debug bool
|
|
|
|
Hosts []string
|
|
|
|
LogLevel string
|
|
|
|
TLS bool
|
|
|
|
TLSVerify bool
|
|
|
|
TLSOptions *tlsconfig.Options
|
|
|
|
TrustKey string
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
// InitCommonFlags initializes flags common to both client and daemon
|
2016-04-21 17:51:28 -04:00
|
|
|
func InitCommonFlags() *CommonFlags {
|
|
|
|
var commonFlags = &CommonFlags{FlagSet: new(flag.FlagSet)}
|
2016-02-19 17:42:51 -05:00
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
if dockerCertPath == "" {
|
|
|
|
dockerCertPath = cliconfig.ConfigDir()
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
commonFlags.PostParse = func() { postParseCommon(commonFlags) }
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
cmd := commonFlags.FlagSet
|
|
|
|
|
|
|
|
cmd.BoolVar(&commonFlags.Debug, []string{"D", "-debug"}, false, "Enable debug mode")
|
|
|
|
cmd.StringVar(&commonFlags.LogLevel, []string{"l", "-log-level"}, "info", "Set the logging level")
|
|
|
|
cmd.BoolVar(&commonFlags.TLS, []string{"-tls"}, false, "Use TLS; implied by --tlsverify")
|
|
|
|
cmd.BoolVar(&commonFlags.TLSVerify, []string{"-tlsverify"}, dockerTLSVerify, "Use TLS and verify the remote")
|
|
|
|
|
|
|
|
// TODO use flag flag.String([]string{"i", "-identity"}, "", "Path to libtrust key file")
|
|
|
|
|
|
|
|
var tlsOptions tlsconfig.Options
|
|
|
|
commonFlags.TLSOptions = &tlsOptions
|
2016-02-19 17:42:51 -05:00
|
|
|
cmd.StringVar(&tlsOptions.CAFile, []string{"-tlscacert"}, filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
|
|
|
|
cmd.StringVar(&tlsOptions.CertFile, []string{"-tlscert"}, filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
|
|
|
|
cmd.StringVar(&tlsOptions.KeyFile, []string{"-tlskey"}, filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
|
2015-05-05 00:18:28 -04:00
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
cmd.Var(opts.NewNamedListOptsRef("hosts", &commonFlags.Hosts, opts.ValidateHost), []string{"H", "-host"}, "Daemon socket(s) to connect to")
|
2016-02-19 17:42:51 -05:00
|
|
|
return commonFlags
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
|
2016-04-21 17:51:28 -04:00
|
|
|
func postParseCommon(commonFlags *CommonFlags) {
|
2015-05-05 00:18:28 -04:00
|
|
|
cmd := commonFlags.FlagSet
|
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
SetDaemonLogLevel(commonFlags.LogLevel)
|
2015-05-05 00:18:28 -04:00
|
|
|
|
|
|
|
// Regardless of whether the user sets it to true or false, if they
|
|
|
|
// specify --tlsverify at all then we need to turn on tls
|
2016-02-19 17:42:51 -05:00
|
|
|
// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
|
|
|
|
// to check that here as well
|
|
|
|
if cmd.IsSet("-"+TLSVerifyKey) || commonFlags.TLSVerify {
|
2015-05-05 00:18:28 -04:00
|
|
|
commonFlags.TLS = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !commonFlags.TLS {
|
|
|
|
commonFlags.TLSOptions = nil
|
|
|
|
} else {
|
|
|
|
tlsOptions := commonFlags.TLSOptions
|
|
|
|
tlsOptions.InsecureSkipVerify = !commonFlags.TLSVerify
|
|
|
|
|
|
|
|
// Reset CertFile and KeyFile to empty string if the user did not specify
|
|
|
|
// the respective flags and the respective default files were not found.
|
|
|
|
if !cmd.IsSet("-tlscert") {
|
|
|
|
if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
|
|
|
|
tlsOptions.CertFile = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !cmd.IsSet("-tlskey") {
|
|
|
|
if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
|
|
|
|
tlsOptions.KeyFile = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 14:16:07 -05:00
|
|
|
|
2016-02-19 17:42:51 -05:00
|
|
|
// SetDaemonLogLevel sets the logrus logging level
|
|
|
|
// TODO: this is a bad name, it applies to the client as well.
|
|
|
|
func SetDaemonLogLevel(logLevel string) {
|
2016-01-19 14:16:07 -05:00
|
|
|
if logLevel != "" {
|
|
|
|
lvl, err := logrus.ParseLevel(logLevel)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
logrus.SetLevel(lvl)
|
|
|
|
} else {
|
|
|
|
logrus.SetLevel(logrus.InfoLevel)
|
|
|
|
}
|
|
|
|
}
|