2015-05-05 00:18:28 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"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 (
|
|
|
|
defaultTrustKeyFile = "key.json"
|
|
|
|
defaultCaFile = "ca.pem"
|
|
|
|
defaultKeyFile = "key.pem"
|
|
|
|
defaultCertFile = "cert.pem"
|
2016-01-20 17:16:49 -05:00
|
|
|
tlsVerifyKey = "tlsverify"
|
2015-05-05 00:18:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
commonFlags = &cli.CommonFlags{FlagSet: new(flag.FlagSet)}
|
|
|
|
|
|
|
|
dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
|
|
|
|
dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if dockerCertPath == "" {
|
|
|
|
dockerCertPath = cliconfig.ConfigDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
commonFlags.PostParse = postParseCommon
|
|
|
|
|
|
|
|
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
|
|
|
|
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-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")
|
2015-05-05 00:18:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func postParseCommon() {
|
|
|
|
cmd := commonFlags.FlagSet
|
|
|
|
|
2016-01-19 14:16:07 -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
|
|
|
|
// 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-01-20 17:16:49 -05:00
|
|
|
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
|
|
|
|
|
|
|
func setDaemonLogLevel(logLevel string) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|