github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/meta.go (about)

     1  package command
     2  
     3  import (
     4  	"bufio"
     5  	"flag"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/hashicorp/nomad/api"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/mitchellh/colorstring"
    13  )
    14  
    15  const (
    16  	// Names of environment variables used to supply various
    17  	// config options to the Nomad CLI.
    18  	EnvNomadAddress = "NOMAD_ADDR"
    19  	EnvNomadRegion  = "NOMAD_REGION"
    20  
    21  	// Constants for CLI identifier length
    22  	shortId = 8
    23  	fullId  = 36
    24  )
    25  
    26  // FlagSetFlags is an enum to define what flags are present in the
    27  // default FlagSet returned by Meta.FlagSet.
    28  type FlagSetFlags uint
    29  
    30  const (
    31  	FlagSetNone    FlagSetFlags = 0
    32  	FlagSetClient  FlagSetFlags = 1 << iota
    33  	FlagSetDefault              = FlagSetClient
    34  )
    35  
    36  // Meta contains the meta-options and functionality that nearly every
    37  // Nomad command inherits.
    38  type Meta struct {
    39  	Ui cli.Ui
    40  
    41  	// These are set by the command line flags.
    42  	flagAddress string
    43  
    44  	// Whether to not-colorize output
    45  	noColor bool
    46  
    47  	// The region to send API requests
    48  	region string
    49  
    50  	caCert     string
    51  	caPath     string
    52  	clientCert string
    53  	clientKey  string
    54  	insecure   bool
    55  }
    56  
    57  // FlagSet returns a FlagSet with the common flags that every
    58  // command implements. The exact behavior of FlagSet can be configured
    59  // using the flags as the second parameter, for example to disable
    60  // server settings on the commands that don't talk to a server.
    61  func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
    62  	f := flag.NewFlagSet(n, flag.ContinueOnError)
    63  
    64  	// FlagSetClient is used to enable the settings for specifying
    65  	// client connectivity options.
    66  	if fs&FlagSetClient != 0 {
    67  		f.StringVar(&m.flagAddress, "address", "", "")
    68  		f.StringVar(&m.region, "region", "", "")
    69  		f.BoolVar(&m.noColor, "no-color", false, "")
    70  		f.StringVar(&m.caCert, "ca-cert", "", "")
    71  		f.StringVar(&m.caPath, "ca-path", "", "")
    72  		f.StringVar(&m.clientCert, "client-cert", "", "")
    73  		f.StringVar(&m.clientKey, "client-key", "", "")
    74  		f.BoolVar(&m.insecure, "insecure", false, "")
    75  		f.BoolVar(&m.insecure, "tls-skip-verify", false, "")
    76  
    77  	}
    78  
    79  	// Create an io.Writer that writes to our UI properly for errors.
    80  	// This is kind of a hack, but it does the job. Basically: create
    81  	// a pipe, use a scanner to break it into lines, and output each line
    82  	// to the UI. Do this forever.
    83  	errR, errW := io.Pipe()
    84  	errScanner := bufio.NewScanner(errR)
    85  	go func() {
    86  		for errScanner.Scan() {
    87  			m.Ui.Error(errScanner.Text())
    88  		}
    89  	}()
    90  	f.SetOutput(errW)
    91  
    92  	return f
    93  }
    94  
    95  // Client is used to initialize and return a new API client using
    96  // the default command line arguments and env vars.
    97  func (m *Meta) Client() (*api.Client, error) {
    98  	config := api.DefaultConfig()
    99  	if v := os.Getenv(EnvNomadAddress); v != "" {
   100  		config.Address = v
   101  	}
   102  	if m.flagAddress != "" {
   103  		config.Address = m.flagAddress
   104  	}
   105  	if v := os.Getenv(EnvNomadRegion); v != "" {
   106  		config.Region = v
   107  	}
   108  	if m.region != "" {
   109  		config.Region = m.region
   110  	}
   111  	// If we need custom TLS configuration, then set it
   112  	if m.caCert != "" || m.caPath != "" || m.clientCert != "" || m.clientKey != "" || m.insecure {
   113  		t := &api.TLSConfig{
   114  			CACert:     m.caCert,
   115  			CAPath:     m.caPath,
   116  			ClientCert: m.clientCert,
   117  			ClientKey:  m.clientKey,
   118  			Insecure:   m.insecure,
   119  		}
   120  		config.TLSConfig = t
   121  	}
   122  
   123  	return api.NewClient(config)
   124  }
   125  
   126  func (m *Meta) Colorize() *colorstring.Colorize {
   127  	return &colorstring.Colorize{
   128  		Colors:  colorstring.DefaultColors,
   129  		Disable: m.noColor,
   130  		Reset:   true,
   131  	}
   132  }
   133  
   134  // generalOptionsUsage returns the help string for the global options.
   135  func generalOptionsUsage() string {
   136  	helpText := `
   137    -address=<addr>
   138      The address of the Nomad server.
   139      Overrides the NOMAD_ADDR environment variable if set.
   140      Default = http://127.0.0.1:4646
   141  
   142    -region=<region>
   143      The region of the Nomad servers to forward commands to.
   144      Overrides the NOMAD_REGION environment variable if set.
   145      Defaults to the Agent's local region.
   146    
   147    -no-color
   148      Disables colored command output.
   149  
   150    -ca-cert=<path>           
   151      Path to a PEM encoded CA cert file to use to verify the 
   152      Nomad server SSL certificate.  Overrides the NOMAD_CACERT 
   153      environment variable if set.
   154  
   155    -ca-path=<path>           
   156      Path to a directory of PEM encoded CA cert files to verify 
   157      the Nomad server SSL certificate. If both -ca-cert and 
   158      -ca-path are specified, -ca-cert is used. Overrides the 
   159      NOMAD_CAPATH environment variable if set.
   160  
   161    -client-cert=<path>       
   162      Path to a PEM encoded client certificate for TLS authentication 
   163      to the Nomad server. Must also specify -client-key. Overrides 
   164      the NOMAD_CLIENT_CERT environment variable if set.
   165  
   166    -client-key=<path>        
   167      Path to an unencrypted PEM encoded private key matching the 
   168      client certificate from -client-cert. Overrides the 
   169      NOMAD_CLIENT_KEY environment variable if set.
   170  
   171    -tls-skip-verify        
   172      Do not verify TLS certificate. This is highly not recommended. Verification
   173      will also be skipped if NOMAD_SKIP_VERIFY is set.
   174  `
   175  	return strings.TrimSpace(helpText)
   176  }