github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/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  	"github.com/docker/go-connections/tlsconfig"
    12  	"github.com/spf13/pflag"
    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  	// FlagTLSVerify is the flag name for the tls verification option
    25  	FlagTLSVerify = "tlsverify"
    26  )
    27  
    28  var (
    29  	dockerCertPath  = os.Getenv("DOCKER_CERT_PATH")
    30  	dockerTLSVerify = os.Getenv("DOCKER_TLS_VERIFY") != ""
    31  )
    32  
    33  // CommonOptions are options common to both the client and the daemon.
    34  type CommonOptions struct {
    35  	Debug      bool
    36  	Hosts      []string
    37  	LogLevel   string
    38  	TLS        bool
    39  	TLSVerify  bool
    40  	TLSOptions *tlsconfig.Options
    41  	TrustKey   string
    42  }
    43  
    44  // NewCommonOptions returns a new CommonOptions
    45  func NewCommonOptions() *CommonOptions {
    46  	return &CommonOptions{}
    47  }
    48  
    49  // InstallFlags adds flags for the common options on the FlagSet
    50  func (commonOpts *CommonOptions) InstallFlags(flags *pflag.FlagSet) {
    51  	if dockerCertPath == "" {
    52  		dockerCertPath = cliconfig.ConfigDir()
    53  	}
    54  
    55  	flags.BoolVarP(&commonOpts.Debug, "debug", "D", false, "Enable debug mode")
    56  	flags.StringVarP(&commonOpts.LogLevel, "log-level", "l", "info", "Set the logging level")
    57  	flags.BoolVar(&commonOpts.TLS, "tls", false, "Use TLS; implied by --tlsverify")
    58  	flags.BoolVar(&commonOpts.TLSVerify, FlagTLSVerify, dockerTLSVerify, "Use TLS and verify the remote")
    59  
    60  	// TODO use flag flags.String("identity"}, "i", "", "Path to libtrust key file")
    61  
    62  	commonOpts.TLSOptions = &tlsconfig.Options{}
    63  	tlsOptions := commonOpts.TLSOptions
    64  	flags.StringVar(&tlsOptions.CAFile, "tlscacert", filepath.Join(dockerCertPath, DefaultCaFile), "Trust certs signed only by this CA")
    65  	flags.StringVar(&tlsOptions.CertFile, "tlscert", filepath.Join(dockerCertPath, DefaultCertFile), "Path to TLS certificate file")
    66  	flags.StringVar(&tlsOptions.KeyFile, "tlskey", filepath.Join(dockerCertPath, DefaultKeyFile), "Path to TLS key file")
    67  
    68  	hostOpt := opts.NewNamedListOptsRef("hosts", &commonOpts.Hosts, opts.ValidateHost)
    69  	flags.VarP(hostOpt, "host", "H", "Daemon socket(s) to connect to")
    70  }
    71  
    72  // SetDefaultOptions sets default values for options after flag parsing is
    73  // complete
    74  func (commonOpts *CommonOptions) SetDefaultOptions(flags *pflag.FlagSet) {
    75  	// Regardless of whether the user sets it to true or false, if they
    76  	// specify --tlsverify at all then we need to turn on tls
    77  	// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need
    78  	// to check that here as well
    79  	if flags.Changed(FlagTLSVerify) || commonOpts.TLSVerify {
    80  		commonOpts.TLS = true
    81  	}
    82  
    83  	if !commonOpts.TLS {
    84  		commonOpts.TLSOptions = nil
    85  	} else {
    86  		tlsOptions := commonOpts.TLSOptions
    87  		tlsOptions.InsecureSkipVerify = !commonOpts.TLSVerify
    88  
    89  		// Reset CertFile and KeyFile to empty string if the user did not specify
    90  		// the respective flags and the respective default files were not found.
    91  		if !flags.Changed("tlscert") {
    92  			if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
    93  				tlsOptions.CertFile = ""
    94  			}
    95  		}
    96  		if !flags.Changed("tlskey") {
    97  			if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
    98  				tlsOptions.KeyFile = ""
    99  			}
   100  		}
   101  	}
   102  }
   103  
   104  // SetDaemonLogLevel sets the logrus logging level
   105  // TODO: this is a bad name, it applies to the client as well.
   106  func SetDaemonLogLevel(logLevel string) {
   107  	if logLevel != "" {
   108  		lvl, err := logrus.ParseLevel(logLevel)
   109  		if err != nil {
   110  			fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
   111  			os.Exit(1)
   112  		}
   113  		logrus.SetLevel(lvl)
   114  	} else {
   115  		logrus.SetLevel(logrus.InfoLevel)
   116  	}
   117  }