github.com/taylorchu/nomad@v0.5.3-rc1.0.20170407200202-db11e7dd7b55/nomad/structs/config/consul.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  	"time"
     8  
     9  	consul "github.com/hashicorp/consul/api"
    10  	"github.com/hashicorp/nomad/helper"
    11  )
    12  
    13  // ConsulConfig contains the configuration information necessary to
    14  // communicate with a Consul Agent in order to:
    15  //
    16  // - Register services and their checks with Consul
    17  //
    18  // - Bootstrap this Nomad Client with the list of Nomad Servers registered
    19  //   with Consul
    20  //
    21  // Both the Agent and the executor need to be able to import ConsulConfig.
    22  type ConsulConfig struct {
    23  	// ServerServiceName is the name of the service that Nomad uses to register
    24  	// servers with Consul
    25  	ServerServiceName string `mapstructure:"server_service_name"`
    26  
    27  	// ClientServiceName is the name of the service that Nomad uses to register
    28  	// clients with Consul
    29  	ClientServiceName string `mapstructure:"client_service_name"`
    30  
    31  	// AutoAdvertise determines if this Nomad Agent will advertise its
    32  	// services via Consul.  When true, Nomad Agent will register
    33  	// services with Consul.
    34  	AutoAdvertise *bool `mapstructure:"auto_advertise"`
    35  
    36  	// ChecksUseAdvertise specifies that Consul checks should use advertise
    37  	// address instead of bind address
    38  	ChecksUseAdvertise *bool `mapstructure:"checks_use_advertise"`
    39  
    40  	// Addr is the address of the local Consul agent
    41  	Addr string `mapstructure:"address"`
    42  
    43  	// Timeout is used by Consul HTTP Client
    44  	Timeout time.Duration `mapstructure:"timeout"`
    45  
    46  	// Token is used to provide a per-request ACL token. This options overrides
    47  	// the agent's default token
    48  	Token string `mapstructure:"token"`
    49  
    50  	// Auth is the information to use for http access to Consul agent
    51  	Auth string `mapstructure:"auth"`
    52  
    53  	// EnableSSL sets the transport scheme to talk to the Consul agent as https
    54  	EnableSSL *bool `mapstructure:"ssl"`
    55  
    56  	// VerifySSL enables or disables SSL verification when the transport scheme
    57  	// for the consul api client is https
    58  	VerifySSL *bool `mapstructure:"verify_ssl"`
    59  
    60  	// CAFile is the path to the ca certificate used for Consul communication
    61  	CAFile string `mapstructure:"ca_file"`
    62  
    63  	// CertFile is the path to the certificate for Consul communication
    64  	CertFile string `mapstructure:"cert_file"`
    65  
    66  	// KeyFile is the path to the private key for Consul communication
    67  	KeyFile string `mapstructure:"key_file"`
    68  
    69  	// ServerAutoJoin enables Nomad servers to find peers by querying Consul and
    70  	// joining them
    71  	ServerAutoJoin *bool `mapstructure:"server_auto_join"`
    72  
    73  	// ClientAutoJoin enables Nomad servers to find addresses of Nomad servers
    74  	// and register with them
    75  	ClientAutoJoin *bool `mapstructure:"client_auto_join"`
    76  }
    77  
    78  // DefaultConsulConfig() returns the canonical defaults for the Nomad
    79  // `consul` configuration.
    80  func DefaultConsulConfig() *ConsulConfig {
    81  	return &ConsulConfig{
    82  		ServerServiceName:  "nomad",
    83  		ClientServiceName:  "nomad-client",
    84  		AutoAdvertise:      helper.BoolToPtr(true),
    85  		ChecksUseAdvertise: helper.BoolToPtr(false),
    86  		EnableSSL:          helper.BoolToPtr(false),
    87  		VerifySSL:          helper.BoolToPtr(false),
    88  		ServerAutoJoin:     helper.BoolToPtr(true),
    89  		ClientAutoJoin:     helper.BoolToPtr(true),
    90  		Timeout:            5 * time.Second,
    91  	}
    92  }
    93  
    94  // Merge merges two Consul Configurations together.
    95  func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
    96  	result := a.Copy()
    97  
    98  	if b.ServerServiceName != "" {
    99  		result.ServerServiceName = b.ServerServiceName
   100  	}
   101  	if b.ClientServiceName != "" {
   102  		result.ClientServiceName = b.ClientServiceName
   103  	}
   104  	if b.AutoAdvertise != nil {
   105  		result.AutoAdvertise = helper.BoolToPtr(*b.AutoAdvertise)
   106  	}
   107  	if b.Addr != "" {
   108  		result.Addr = b.Addr
   109  	}
   110  	if b.Timeout != 0 {
   111  		result.Timeout = b.Timeout
   112  	}
   113  	if b.Token != "" {
   114  		result.Token = b.Token
   115  	}
   116  	if b.Auth != "" {
   117  		result.Auth = b.Auth
   118  	}
   119  	if b.EnableSSL != nil {
   120  		result.EnableSSL = helper.BoolToPtr(*b.EnableSSL)
   121  	}
   122  	if b.VerifySSL != nil {
   123  		result.VerifySSL = helper.BoolToPtr(*b.VerifySSL)
   124  	}
   125  	if b.CAFile != "" {
   126  		result.CAFile = b.CAFile
   127  	}
   128  	if b.CertFile != "" {
   129  		result.CertFile = b.CertFile
   130  	}
   131  	if b.KeyFile != "" {
   132  		result.KeyFile = b.KeyFile
   133  	}
   134  	if b.ServerAutoJoin != nil {
   135  		result.ServerAutoJoin = helper.BoolToPtr(*b.ServerAutoJoin)
   136  	}
   137  	if b.ClientAutoJoin != nil {
   138  		result.ClientAutoJoin = helper.BoolToPtr(*b.ClientAutoJoin)
   139  	}
   140  	if b.ChecksUseAdvertise != nil {
   141  		result.ChecksUseAdvertise = helper.BoolToPtr(*b.ChecksUseAdvertise)
   142  	}
   143  	return result
   144  }
   145  
   146  // ApiConfig() returns a usable Consul config that can be passed directly to
   147  // hashicorp/consul/api.  NOTE: datacenter is not set
   148  func (c *ConsulConfig) ApiConfig() (*consul.Config, error) {
   149  	config := consul.DefaultConfig()
   150  	if c.Addr != "" {
   151  		config.Address = c.Addr
   152  	}
   153  	if c.Token != "" {
   154  		config.Token = c.Token
   155  	}
   156  	if c.Timeout != 0 {
   157  		config.HttpClient.Timeout = c.Timeout
   158  	}
   159  	if c.Auth != "" {
   160  		var username, password string
   161  		if strings.Contains(c.Auth, ":") {
   162  			split := strings.SplitN(c.Auth, ":", 2)
   163  			username = split[0]
   164  			password = split[1]
   165  		} else {
   166  			username = c.Auth
   167  		}
   168  
   169  		config.HttpAuth = &consul.HttpBasicAuth{
   170  			Username: username,
   171  			Password: password,
   172  		}
   173  	}
   174  	if c.EnableSSL != nil && *c.EnableSSL {
   175  		config.Scheme = "https"
   176  		tlsConfig := consul.TLSConfig{
   177  			Address:  config.Address,
   178  			CAFile:   c.CAFile,
   179  			CertFile: c.CertFile,
   180  			KeyFile:  c.KeyFile,
   181  		}
   182  		if c.VerifySSL != nil {
   183  			tlsConfig.InsecureSkipVerify = !*c.VerifySSL
   184  		}
   185  
   186  		tlsClientCfg, err := consul.SetupTLSConfig(&tlsConfig)
   187  		if err != nil {
   188  			return nil, fmt.Errorf("error creating tls client config for consul: %v", err)
   189  		}
   190  		config.HttpClient.Transport = &http.Transport{
   191  			TLSClientConfig: tlsClientCfg,
   192  		}
   193  	}
   194  
   195  	return config, nil
   196  }
   197  
   198  // Copy returns a copy of this Consul config.
   199  func (c *ConsulConfig) Copy() *ConsulConfig {
   200  	if c == nil {
   201  		return nil
   202  	}
   203  
   204  	nc := new(ConsulConfig)
   205  	*nc = *c
   206  
   207  	// Copy the bools
   208  	if nc.AutoAdvertise != nil {
   209  		nc.AutoAdvertise = helper.BoolToPtr(*nc.AutoAdvertise)
   210  	}
   211  	if nc.ChecksUseAdvertise != nil {
   212  		nc.ChecksUseAdvertise = helper.BoolToPtr(*nc.ChecksUseAdvertise)
   213  	}
   214  	if nc.EnableSSL != nil {
   215  		nc.EnableSSL = helper.BoolToPtr(*nc.EnableSSL)
   216  	}
   217  	if nc.VerifySSL != nil {
   218  		nc.VerifySSL = helper.BoolToPtr(*nc.VerifySSL)
   219  	}
   220  	if nc.ServerAutoJoin != nil {
   221  		nc.ServerAutoJoin = helper.BoolToPtr(*nc.ServerAutoJoin)
   222  	}
   223  	if nc.ClientAutoJoin != nil {
   224  		nc.ClientAutoJoin = helper.BoolToPtr(*nc.ClientAutoJoin)
   225  	}
   226  
   227  	return nc
   228  }