github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/cli/flags/common.go (about) 1 package flags 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/Sirupsen/logrus" 9 "github.com/docker/docker/cliconfig" 10 "github.com/docker/docker/opts" 11 flag "github.com/docker/docker/pkg/mflag" 12 "github.com/docker/go-connections/tlsconfig" 13 ) 14 15 const ( 16 // DefaultTrustKeyFile is the default filename for the trust key 17 DefaultTrustKeyFile = "key.json" 18 // DefaultCaFile is the default filename for the CA pem file 19 DefaultCaFile = "ca.pem" 20 // DefaultKeyFile is the default filename for the key pem file 21 DefaultKeyFile = "key.pem" 22 // DefaultCertFile is the default filename for the cert pem file 23 DefaultCertFile = "cert.pem" 24 // TLSVerifyKey is the default flag name for the tls verification option 25 TLSVerifyKey = "tlsverify" 26 ) 27 28 var ( 29 dockerCertPath = os.Getenv("DOCKER_CERT_PATH") 30 dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != "" 31 ) 32 33 // CommonFlags are flags common to both the client and the daemon. 34 type CommonFlags struct { 35 FlagSet *flag.FlagSet 36 PostParse func() 37 38 Debug bool 39 Hosts []string 40 LogLevel string 41 TLS bool 42 TLSVerify bool 43 TLSOptions *tlsconfig.Options 44 TrustKey string 45 } 46 47 // InitCommonFlags initializes flags common to both client and daemon 48 func InitCommonFlags() *CommonFlags { 49 var commonFlags = &CommonFlags{FlagSet: new(flag.FlagSet)} 50 51 if dockerCertPath == "" { 52 dockerCertPath = cliconfig.ConfigDir() 53 } 54 55 commonFlags.PostParse = func() { postParseCommon(commonFlags) } 56 57 cmd := commonFlags.FlagSet 58 59 cmd.BoolVar(&commonFlags.Debug, []string{"D", "-debug"}, false, "Enable debug mode") 60 cmd.StringVar(&commonFlags.LogLevel, []string{"l", "-log-level"}, "info", "Set the logging level") 61 cmd.BoolVar(&commonFlags.TLS, []string{"-tls"}, false, "Use TLS; implied by --tlsverify") 62 cmd.BoolVar(&commonFlags.TLSVerify, []string{"-tlsverify"}, dockerTLSVerify, "Use TLS and verify the remote") 63 64 // TODO use flag flag.String([]string{"i", "-identity"}, "", "Path to libtrust key file") 65 66 var tlsOptions tlsconfig.Options 67 commonFlags.TLSOptions = &tlsOptions 68 cmd.StringVar(&tlsOptions.CAFile, []string{"-tlscacert"}, filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA") 69 cmd.StringVar(&tlsOptions.CertFile, []string{"-tlscert"}, filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file") 70 cmd.StringVar(&tlsOptions.KeyFile, []string{"-tlskey"}, filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file") 71 72 cmd.Var(opts.NewNamedListOptsRef("hosts", &commonFlags.Hosts, opts.ValidateHost), []string{"H", "-host"}, "Daemon socket(s) to connect to") 73 return commonFlags 74 } 75 76 func postParseCommon(commonFlags *CommonFlags) { 77 cmd := commonFlags.FlagSet 78 79 SetDaemonLogLevel(commonFlags.LogLevel) 80 81 // Regardless of whether the user sets it to true or false, if they 82 // specify --tlsverify at all then we need to turn on tls 83 // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need 84 // to check that here as well 85 if cmd.IsSet("-"+TLSVerifyKey) || commonFlags.TLSVerify { 86 commonFlags.TLS = true 87 } 88 89 if !commonFlags.TLS { 90 commonFlags.TLSOptions = nil 91 } else { 92 tlsOptions := commonFlags.TLSOptions 93 tlsOptions.InsecureSkipVerify = !commonFlags.TLSVerify 94 95 // Reset CertFile and KeyFile to empty string if the user did not specify 96 // the respective flags and the respective default files were not found. 97 if !cmd.IsSet("-tlscert") { 98 if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) { 99 tlsOptions.CertFile = "" 100 } 101 } 102 if !cmd.IsSet("-tlskey") { 103 if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) { 104 tlsOptions.KeyFile = "" 105 } 106 } 107 } 108 } 109 110 // SetDaemonLogLevel sets the logrus logging level 111 // TODO: this is a bad name, it applies to the client as well. 112 func SetDaemonLogLevel(logLevel string) { 113 if logLevel != "" { 114 lvl, err := logrus.ParseLevel(logLevel) 115 if err != nil { 116 fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel) 117 os.Exit(1) 118 } 119 logrus.SetLevel(lvl) 120 } else { 121 logrus.SetLevel(logrus.InfoLevel) 122 } 123 }