2016-02-19 22:42:51 +00:00
|
|
|
package flags
|
2015-05-05 04:18:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/cliconfig"
|
|
|
|
"github.com/docker/docker/opts"
|
2015-12-30 00:27:12 +00:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2016-06-21 20:42:47 +00:00
|
|
|
"github.com/spf13/pflag"
|
2015-05-05 04:18:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-19 22:42:51 +00: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"
|
2016-06-21 20:42:47 +00:00
|
|
|
// FlagTLSVerify is the flag name for the tls verification option
|
|
|
|
FlagTLSVerify = "tlsverify"
|
2015-05-05 04:18:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
|
|
|
|
dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
|
|
|
|
)
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// CommonOptions are options common to both the client and the daemon.
|
|
|
|
type CommonOptions struct {
|
2016-04-21 21:51:28 +00:00
|
|
|
Debug bool
|
|
|
|
Hosts []string
|
|
|
|
LogLevel string
|
|
|
|
TLS bool
|
|
|
|
TLSVerify bool
|
|
|
|
TLSOptions *tlsconfig.Options
|
|
|
|
TrustKey string
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// NewCommonOptions returns a new CommonOptions
|
|
|
|
func NewCommonOptions() *CommonOptions {
|
2016-06-22 22:36:51 +00:00
|
|
|
return &CommonOptions{}
|
2016-06-21 20:42:47 +00:00
|
|
|
}
|
2016-02-19 22:42:51 +00:00
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// InstallFlags adds flags for the common options on the FlagSet
|
|
|
|
func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
|
2015-05-05 04:18:28 +00:00
|
|
|
if dockerCertPath == "" {
|
|
|
|
dockerCertPath = cliconfig.ConfigDir()
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
flags.BoolVarP(&commonOpts.Debug, "debug", "D", false, "Enable debug mode")
|
|
|
|
flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", "Set the logging level")
|
|
|
|
flags.BoolVar(&commonOpts.TLS, "tls", false, "Use TLS; implied by --tlsverify")
|
|
|
|
flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
|
2015-05-05 04:18:28 +00:00
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
|
2015-05-05 04:18:28 +00:00
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
commonOpts.TLSOptions = &tlsconfig.Options{}
|
2016-06-21 20:42:47 +00:00
|
|
|
tlsOptions := commonOpts.TLSOptions
|
|
|
|
flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
|
|
|
|
flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
|
|
|
|
flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
|
2015-05-05 04:18:28 +00:00
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
|
2016-06-22 22:36:51 +00:00
|
|
|
flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// SetDefaultOptions sets default values for options after flag parsing is
|
|
|
|
// complete
|
|
|
|
func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
|
2015-05-05 04:18:28 +00: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 22:42:51 +00: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
|
2016-06-21 20:42:47 +00:00
|
|
|
if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify {
|
|
|
|
commonOpts.TLS = true
|
2015-05-05 04:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
if !commonOpts.TLS {
|
|
|
|
commonOpts.TLSOptions = nil
|
2015-05-05 04:18:28 +00:00
|
|
|
} else {
|
2016-06-21 20:42:47 +00:00
|
|
|
tlsOptions := commonOpts.TLSOptions
|
|
|
|
tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify
|
2015-05-05 04:18:28 +00:00
|
|
|
|
|
|
|
// Reset CertFile and KeyFile to empty string if the user did not specify
|
|
|
|
// the respective flags and the respective default files were not found.
|
2016-06-21 20:42:47 +00:00
|
|
|
if !flags.Changed("tlscert") {
|
2015-05-05 04:18:28 +00:00
|
|
|
if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
|
|
|
|
tlsOptions.CertFile = ""
|
|
|
|
}
|
|
|
|
}
|
2016-06-21 20:42:47 +00:00
|
|
|
if !flags.Changed("tlskey") {
|
2015-05-05 04:18:28 +00:00
|
|
|
if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
|
|
|
|
tlsOptions.KeyFile = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2016-02-19 22:42:51 +00: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 19:16:07 +00: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)
|
|
|
|
}
|
|
|
|
}
|