common.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package flags
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/opts"
  8. "github.com/docker/go-connections/tlsconfig"
  9. "github.com/spf13/pflag"
  10. )
  11. const (
  12. // DefaultCaFile is the default filename for the CA pem file
  13. DefaultCaFile = "ca.pem"
  14. // DefaultKeyFile is the default filename for the key pem file
  15. DefaultKeyFile = "key.pem"
  16. // DefaultCertFile is the default filename for the cert pem file
  17. DefaultCertFile = "cert.pem"
  18. // FlagTLSVerify is the flag name for the TLS verification option
  19. FlagTLSVerify = "tlsverify"
  20. )
  21. var (
  22. dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
  23. dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
  24. )
  25. // CommonOptions are options common to both the client and the daemon.
  26. type CommonOptions struct {
  27. Debug bool
  28. Hosts []string
  29. LogLevel string
  30. TLS bool
  31. TLSVerify bool
  32. TLSOptions *tlsconfig.Options
  33. }
  34. // NewCommonOptions returns a new CommonOptions
  35. func NewCommonOptions() *CommonOptions {
  36. return &CommonOptions{}
  37. }
  38. // InstallFlags adds flags for the common options on the FlagSet
  39. func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
  40. if dockerCertPath == "" {
  41. dockerCertPath = ConfigurationDir()
  42. }
  43. flags.BoolVarP(&commonOpts.Debug, "debug", "D", false, "Enable debug mode")
  44. flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
  45. flags.BoolVar(&commonOpts.TLS, "tls", false, "Use TLS; implied by --tlsverify")
  46. flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
  47. // TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
  48. commonOpts.TLSOptions = &tlsconfig.Options{
  49. CAFile: filepath.Join(dockerCertPath, DefaultCaFile),
  50. CertFile: filepath.Join(dockerCertPath, DefaultCertFile),
  51. KeyFile: filepath.Join(dockerCertPath, DefaultKeyFile),
  52. }
  53. tlsOptions := commonOpts.TLSOptions
  54. flags.Var(opts.NewQuotedString(&tlsOptions.CAFile), "tlscacert", "Trust certs signed only by this CA")
  55. flags.Var(opts.NewQuotedString(&tlsOptions.CertFile), "tlscert", "Path to TLS certificate file")
  56. flags.Var(opts.NewQuotedString(&tlsOptions.KeyFile), "tlskey", "Path to TLS key file")
  57. hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
  58. flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
  59. }
  60. // SetDefaultOptions sets default values for options after flag parsing is
  61. // complete
  62. func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
  63. // Regardless of whether the user sets it to true or false, if they
  64. // specify --tlsverify at all then we need to turn on TLS
  65. // TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
  66. // to check that here as well
  67. if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify {
  68. commonOpts.TLS = true
  69. }
  70. if !commonOpts.TLS {
  71. commonOpts.TLSOptions = nil
  72. } else {
  73. tlsOptions := commonOpts.TLSOptions
  74. tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify
  75. // Reset CertFile and KeyFile to empty string if the user did not specify
  76. // the respective flags and the respective default files were not found.
  77. if !flags.Changed("tlscert") {
  78. if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
  79. tlsOptions.CertFile = ""
  80. }
  81. }
  82. if !flags.Changed("tlskey") {
  83. if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
  84. tlsOptions.KeyFile = ""
  85. }
  86. }
  87. }
  88. }
  89. // SetLogLevel sets the logrus logging level
  90. func SetLogLevel(logLevel string) {
  91. if logLevel != "" {
  92. lvl, err := logrus.ParseLevel(logLevel)
  93. if err != nil {
  94. fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
  95. os.Exit(1)
  96. }
  97. logrus.SetLevel(lvl)
  98. } else {
  99. logrus.SetLevel(logrus.InfoLevel)
  100. }
  101. }