github.com/quite/nomad@v0.8.6/command/agent/config.go (about)

     1  package agent
     2  
     3  import (
     4  	"encoding/base64"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"net"
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"sort"
    13  	"strconv"
    14  	"strings"
    15  	"time"
    16  
    17  	"github.com/hashicorp/go-sockaddr/template"
    18  
    19  	client "github.com/hashicorp/nomad/client/config"
    20  	"github.com/hashicorp/nomad/helper"
    21  	"github.com/hashicorp/nomad/nomad"
    22  	"github.com/hashicorp/nomad/nomad/structs/config"
    23  	"github.com/hashicorp/nomad/version"
    24  )
    25  
    26  // Config is the configuration for the Nomad agent.
    27  type Config struct {
    28  	// Region is the region this agent is in. Defaults to global.
    29  	Region string `mapstructure:"region"`
    30  
    31  	// Datacenter is the datacenter this agent is in. Defaults to dc1
    32  	Datacenter string `mapstructure:"datacenter"`
    33  
    34  	// NodeName is the name we register as. Defaults to hostname.
    35  	NodeName string `mapstructure:"name"`
    36  
    37  	// DataDir is the directory to store our state in
    38  	DataDir string `mapstructure:"data_dir"`
    39  
    40  	// LogLevel is the level of the logs to putout
    41  	LogLevel string `mapstructure:"log_level"`
    42  
    43  	// BindAddr is the address on which all of nomad's services will
    44  	// be bound. If not specified, this defaults to 127.0.0.1.
    45  	BindAddr string `mapstructure:"bind_addr"`
    46  
    47  	// EnableDebug is used to enable debugging HTTP endpoints
    48  	EnableDebug bool `mapstructure:"enable_debug"`
    49  
    50  	// Ports is used to control the network ports we bind to.
    51  	Ports *Ports `mapstructure:"ports"`
    52  
    53  	// Addresses is used to override the network addresses we bind to.
    54  	//
    55  	// Use normalizedAddrs if you need the host+port to bind to.
    56  	Addresses *Addresses `mapstructure:"addresses"`
    57  
    58  	// normalizedAddr is set to the Address+Port by normalizeAddrs()
    59  	normalizedAddrs *Addresses
    60  
    61  	// AdvertiseAddrs is used to control the addresses we advertise.
    62  	AdvertiseAddrs *AdvertiseAddrs `mapstructure:"advertise"`
    63  
    64  	// Client has our client related settings
    65  	Client *ClientConfig `mapstructure:"client"`
    66  
    67  	// Server has our server related settings
    68  	Server *ServerConfig `mapstructure:"server"`
    69  
    70  	// ACL has our acl related settings
    71  	ACL *ACLConfig `mapstructure:"acl"`
    72  
    73  	// Telemetry is used to configure sending telemetry
    74  	Telemetry *Telemetry `mapstructure:"telemetry"`
    75  
    76  	// LeaveOnInt is used to gracefully leave on the interrupt signal
    77  	LeaveOnInt bool `mapstructure:"leave_on_interrupt"`
    78  
    79  	// LeaveOnTerm is used to gracefully leave on the terminate signal
    80  	LeaveOnTerm bool `mapstructure:"leave_on_terminate"`
    81  
    82  	// EnableSyslog is used to enable sending logs to syslog
    83  	EnableSyslog bool `mapstructure:"enable_syslog"`
    84  
    85  	// SyslogFacility is used to control the syslog facility used.
    86  	SyslogFacility string `mapstructure:"syslog_facility"`
    87  
    88  	// DisableUpdateCheck is used to disable the periodic update
    89  	// and security bulletin checking.
    90  	DisableUpdateCheck *bool `mapstructure:"disable_update_check"`
    91  
    92  	// DisableAnonymousSignature is used to disable setting the
    93  	// anonymous signature when doing the update check and looking
    94  	// for security bulletins
    95  	DisableAnonymousSignature bool `mapstructure:"disable_anonymous_signature"`
    96  
    97  	// Consul contains the configuration for the Consul Agent and
    98  	// parameters necessary to register services, their checks, and
    99  	// discover the current Nomad servers.
   100  	Consul *config.ConsulConfig `mapstructure:"consul"`
   101  
   102  	// Vault contains the configuration for the Vault Agent and
   103  	// parameters necessary to derive tokens.
   104  	Vault *config.VaultConfig `mapstructure:"vault"`
   105  
   106  	// NomadConfig is used to override the default config.
   107  	// This is largely used for testing purposes.
   108  	NomadConfig *nomad.Config `mapstructure:"-" json:"-"`
   109  
   110  	// ClientConfig is used to override the default config.
   111  	// This is largely used for testing purposes.
   112  	ClientConfig *client.Config `mapstructure:"-" json:"-"`
   113  
   114  	// DevMode is set by the -dev CLI flag.
   115  	DevMode bool `mapstructure:"-"`
   116  
   117  	// Version information is set at compilation time
   118  	Version *version.VersionInfo
   119  
   120  	// List of config files that have been loaded (in order)
   121  	Files []string `mapstructure:"-"`
   122  
   123  	// TLSConfig provides TLS related configuration for the Nomad server and
   124  	// client
   125  	TLSConfig *config.TLSConfig `mapstructure:"tls"`
   126  
   127  	// HTTPAPIResponseHeaders allows users to configure the Nomad http agent to
   128  	// set arbitrary headers on API responses
   129  	HTTPAPIResponseHeaders map[string]string `mapstructure:"http_api_response_headers"`
   130  
   131  	// Sentinel holds sentinel related settings
   132  	Sentinel *config.SentinelConfig `mapstructure:"sentinel"`
   133  
   134  	// Autopilot contains the configuration for Autopilot behavior.
   135  	Autopilot *config.AutopilotConfig `mapstructure:"autopilot"`
   136  }
   137  
   138  // ClientConfig is configuration specific to the client mode
   139  type ClientConfig struct {
   140  	// Enabled controls if we are a client
   141  	Enabled bool `mapstructure:"enabled"`
   142  
   143  	// StateDir is the state directory
   144  	StateDir string `mapstructure:"state_dir"`
   145  
   146  	// AllocDir is the directory for storing allocation data
   147  	AllocDir string `mapstructure:"alloc_dir"`
   148  
   149  	// Servers is a list of known server addresses. These are as "host:port"
   150  	Servers []string `mapstructure:"servers"`
   151  
   152  	// NodeClass is used to group the node by class
   153  	NodeClass string `mapstructure:"node_class"`
   154  
   155  	// Options is used for configuration of nomad internals,
   156  	// like fingerprinters and drivers. The format is:
   157  	//
   158  	//  namespace.option = value
   159  	Options map[string]string `mapstructure:"options"`
   160  
   161  	// Metadata associated with the node
   162  	Meta map[string]string `mapstructure:"meta"`
   163  
   164  	// A mapping of directories on the host OS to attempt to embed inside each
   165  	// task's chroot.
   166  	ChrootEnv map[string]string `mapstructure:"chroot_env"`
   167  
   168  	// Interface to use for network fingerprinting
   169  	NetworkInterface string `mapstructure:"network_interface"`
   170  
   171  	// NetworkSpeed is used to override any detected or default network link
   172  	// speed.
   173  	NetworkSpeed int `mapstructure:"network_speed"`
   174  
   175  	// CpuCompute is used to override any detected or default total CPU compute.
   176  	CpuCompute int `mapstructure:"cpu_total_compute"`
   177  
   178  	// MemoryMB is used to override any detected or default total memory.
   179  	MemoryMB int `mapstructure:"memory_total_mb"`
   180  
   181  	// MaxKillTimeout allows capping the user-specifiable KillTimeout.
   182  	MaxKillTimeout string `mapstructure:"max_kill_timeout"`
   183  
   184  	// ClientMaxPort is the upper range of the ports that the client uses for
   185  	// communicating with plugin subsystems
   186  	ClientMaxPort int `mapstructure:"client_max_port"`
   187  
   188  	// ClientMinPort is the lower range of the ports that the client uses for
   189  	// communicating with plugin subsystems
   190  	ClientMinPort int `mapstructure:"client_min_port"`
   191  
   192  	// Reserved is used to reserve resources from being used by Nomad. This can
   193  	// be used to target a certain utilization or to prevent Nomad from using a
   194  	// particular set of ports.
   195  	Reserved *Resources `mapstructure:"reserved"`
   196  
   197  	// GCInterval is the time interval at which the client triggers garbage
   198  	// collection
   199  	GCInterval time.Duration `mapstructure:"gc_interval"`
   200  
   201  	// GCParallelDestroys is the number of parallel destroys the garbage
   202  	// collector will allow.
   203  	GCParallelDestroys int `mapstructure:"gc_parallel_destroys"`
   204  
   205  	// GCDiskUsageThreshold is the disk usage threshold given as a percent
   206  	// beyond which the Nomad client triggers GC of terminal allocations
   207  	GCDiskUsageThreshold float64 `mapstructure:"gc_disk_usage_threshold"`
   208  
   209  	// GCInodeUsageThreshold is the inode usage threshold beyond which the Nomad
   210  	// client triggers GC of the terminal allocations
   211  	GCInodeUsageThreshold float64 `mapstructure:"gc_inode_usage_threshold"`
   212  
   213  	// GCMaxAllocs is the maximum number of allocations a node can have
   214  	// before garbage collection is triggered.
   215  	GCMaxAllocs int `mapstructure:"gc_max_allocs"`
   216  
   217  	// NoHostUUID disables using the host's UUID and will force generation of a
   218  	// random UUID.
   219  	NoHostUUID *bool `mapstructure:"no_host_uuid"`
   220  
   221  	// ServerJoin contains information that is used to attempt to join servers
   222  	ServerJoin *ServerJoin `mapstructure:"server_join"`
   223  }
   224  
   225  // ACLConfig is configuration specific to the ACL system
   226  type ACLConfig struct {
   227  	// Enabled controls if we are enforce and manage ACLs
   228  	Enabled bool `mapstructure:"enabled"`
   229  
   230  	// TokenTTL controls how long we cache ACL tokens. This controls
   231  	// how stale they can be when we are enforcing policies. Defaults
   232  	// to "30s". Reducing this impacts performance by forcing more
   233  	// frequent resolution.
   234  	TokenTTL time.Duration `mapstructure:"token_ttl"`
   235  
   236  	// PolicyTTL controls how long we cache ACL policies. This controls
   237  	// how stale they can be when we are enforcing policies. Defaults
   238  	// to "30s". Reducing this impacts performance by forcing more
   239  	// frequent resolution.
   240  	PolicyTTL time.Duration `mapstructure:"policy_ttl"`
   241  
   242  	// ReplicationToken is used by servers to replicate tokens and policies
   243  	// from the authoritative region. This must be a valid management token
   244  	// within the authoritative region.
   245  	ReplicationToken string `mapstructure:"replication_token"`
   246  }
   247  
   248  // ServerConfig is configuration specific to the server mode
   249  type ServerConfig struct {
   250  	// Enabled controls if we are a server
   251  	Enabled bool `mapstructure:"enabled"`
   252  
   253  	// AuthoritativeRegion is used to control which region is treated as
   254  	// the source of truth for global tokens and ACL policies.
   255  	AuthoritativeRegion string `mapstructure:"authoritative_region"`
   256  
   257  	// BootstrapExpect tries to automatically bootstrap the Consul cluster,
   258  	// by withholding peers until enough servers join.
   259  	BootstrapExpect int `mapstructure:"bootstrap_expect"`
   260  
   261  	// DataDir is the directory to store our state in
   262  	DataDir string `mapstructure:"data_dir"`
   263  
   264  	// ProtocolVersion is the protocol version to speak. This must be between
   265  	// ProtocolVersionMin and ProtocolVersionMax.
   266  	ProtocolVersion int `mapstructure:"protocol_version"`
   267  
   268  	// RaftProtocol is the Raft protocol version to speak. This must be from [1-3].
   269  	RaftProtocol int `mapstructure:"raft_protocol"`
   270  
   271  	// NumSchedulers is the number of scheduler thread that are run.
   272  	// This can be as many as one per core, or zero to disable this server
   273  	// from doing any scheduling work.
   274  	NumSchedulers *int `mapstructure:"num_schedulers"`
   275  
   276  	// EnabledSchedulers controls the set of sub-schedulers that are
   277  	// enabled for this server to handle. This will restrict the evaluations
   278  	// that the workers dequeue for processing.
   279  	EnabledSchedulers []string `mapstructure:"enabled_schedulers"`
   280  
   281  	// NodeGCThreshold controls how "old" a node must be to be collected by GC.
   282  	// Age is not the only requirement for a node to be GCed but the threshold
   283  	// can be used to filter by age.
   284  	NodeGCThreshold string `mapstructure:"node_gc_threshold"`
   285  
   286  	// JobGCThreshold controls how "old" a job must be to be collected by GC.
   287  	// Age is not the only requirement for a Job to be GCed but the threshold
   288  	// can be used to filter by age.
   289  	JobGCThreshold string `mapstructure:"job_gc_threshold"`
   290  
   291  	// EvalGCThreshold controls how "old" an eval must be to be collected by GC.
   292  	// Age is not the only requirement for a eval to be GCed but the threshold
   293  	// can be used to filter by age.
   294  	EvalGCThreshold string `mapstructure:"eval_gc_threshold"`
   295  
   296  	// DeploymentGCThreshold controls how "old" a deployment must be to be
   297  	// collected by GC.  Age is not the only requirement for a deployment to be
   298  	// GCed but the threshold can be used to filter by age.
   299  	DeploymentGCThreshold string `mapstructure:"deployment_gc_threshold"`
   300  
   301  	// HeartbeatGrace is the grace period beyond the TTL to account for network,
   302  	// processing delays and clock skew before marking a node as "down".
   303  	HeartbeatGrace time.Duration `mapstructure:"heartbeat_grace"`
   304  
   305  	// MinHeartbeatTTL is the minimum time between heartbeats. This is used as
   306  	// a floor to prevent excessive updates.
   307  	MinHeartbeatTTL time.Duration `mapstructure:"min_heartbeat_ttl"`
   308  
   309  	// MaxHeartbeatsPerSecond is the maximum target rate of heartbeats
   310  	// being processed per second. This allows the TTL to be increased
   311  	// to meet the target rate.
   312  	MaxHeartbeatsPerSecond float64 `mapstructure:"max_heartbeats_per_second"`
   313  
   314  	// StartJoin is a list of addresses to attempt to join when the
   315  	// agent starts. If Serf is unable to communicate with any of these
   316  	// addresses, then the agent will error and exit.
   317  	// Deprecated in Nomad 0.10
   318  	StartJoin []string `mapstructure:"start_join"`
   319  
   320  	// RetryJoin is a list of addresses to join with retry enabled.
   321  	// Deprecated in Nomad 0.10
   322  	RetryJoin []string `mapstructure:"retry_join"`
   323  
   324  	// RetryMaxAttempts specifies the maximum number of times to retry joining a
   325  	// host on startup. This is useful for cases where we know the node will be
   326  	// online eventually.
   327  	// Deprecated in Nomad 0.10
   328  	RetryMaxAttempts int `mapstructure:"retry_max"`
   329  
   330  	// RetryInterval specifies the amount of time to wait in between join
   331  	// attempts on agent start. The minimum allowed value is 1 second and
   332  	// the default is 30s.
   333  	// Deprecated in Nomad 0.10
   334  	RetryInterval time.Duration `mapstructure:"retry_interval"`
   335  
   336  	// RejoinAfterLeave controls our interaction with the cluster after leave.
   337  	// When set to false (default), a leave causes Consul to not rejoin
   338  	// the cluster until an explicit join is received. If this is set to
   339  	// true, we ignore the leave, and rejoin the cluster on start.
   340  	RejoinAfterLeave bool `mapstructure:"rejoin_after_leave"`
   341  
   342  	// (Enterprise-only) NonVotingServer is whether this server will act as a
   343  	// non-voting member of the cluster to help provide read scalability.
   344  	NonVotingServer bool `mapstructure:"non_voting_server"`
   345  
   346  	// (Enterprise-only) RedundancyZone is the redundancy zone to use for this server.
   347  	RedundancyZone string `mapstructure:"redundancy_zone"`
   348  
   349  	// (Enterprise-only) UpgradeVersion is the custom upgrade version to use when
   350  	// performing upgrade migrations.
   351  	UpgradeVersion string `mapstructure:"upgrade_version"`
   352  
   353  	// Encryption key to use for the Serf communication
   354  	EncryptKey string `mapstructure:"encrypt" json:"-"`
   355  
   356  	// ServerJoin contains information that is used to attempt to join servers
   357  	ServerJoin *ServerJoin `mapstructure:"server_join"`
   358  }
   359  
   360  // ServerJoin is used in both clients and servers to bootstrap connections to
   361  // servers
   362  type ServerJoin struct {
   363  	// StartJoin is a list of addresses to attempt to join when the
   364  	// agent starts. If Serf is unable to communicate with any of these
   365  	// addresses, then the agent will error and exit.
   366  	StartJoin []string `mapstructure:"start_join"`
   367  
   368  	// RetryJoin is a list of addresses to join with retry enabled, or a single
   369  	// value to find multiple servers using go-discover syntax.
   370  	RetryJoin []string `mapstructure:"retry_join"`
   371  
   372  	// RetryMaxAttempts specifies the maximum number of times to retry joining a
   373  	// host on startup. This is useful for cases where we know the node will be
   374  	// online eventually.
   375  	RetryMaxAttempts int `mapstructure:"retry_max"`
   376  
   377  	// RetryInterval specifies the amount of time to wait in between join
   378  	// attempts on agent start. The minimum allowed value is 1 second and
   379  	// the default is 30s.
   380  	RetryInterval time.Duration `mapstructure:"retry_interval"`
   381  }
   382  
   383  func (s *ServerJoin) Merge(b *ServerJoin) *ServerJoin {
   384  	if s == nil {
   385  		return b
   386  	}
   387  
   388  	result := *s
   389  
   390  	if b == nil {
   391  		return &result
   392  	}
   393  
   394  	if len(b.StartJoin) != 0 {
   395  		result.StartJoin = b.StartJoin
   396  	}
   397  	if len(b.RetryJoin) != 0 {
   398  		result.RetryJoin = b.RetryJoin
   399  	}
   400  	if b.RetryMaxAttempts != 0 {
   401  		result.RetryMaxAttempts = b.RetryMaxAttempts
   402  	}
   403  	if b.RetryInterval != 0 {
   404  		result.RetryInterval = b.RetryInterval
   405  	}
   406  
   407  	return &result
   408  }
   409  
   410  // EncryptBytes returns the encryption key configured.
   411  func (s *ServerConfig) EncryptBytes() ([]byte, error) {
   412  	return base64.StdEncoding.DecodeString(s.EncryptKey)
   413  }
   414  
   415  // Telemetry is the telemetry configuration for the server
   416  type Telemetry struct {
   417  	StatsiteAddr             string        `mapstructure:"statsite_address"`
   418  	StatsdAddr               string        `mapstructure:"statsd_address"`
   419  	DataDogAddr              string        `mapstructure:"datadog_address"`
   420  	DataDogTags              []string      `mapstructure:"datadog_tags"`
   421  	PrometheusMetrics        bool          `mapstructure:"prometheus_metrics"`
   422  	DisableHostname          bool          `mapstructure:"disable_hostname"`
   423  	UseNodeName              bool          `mapstructure:"use_node_name"`
   424  	CollectionInterval       string        `mapstructure:"collection_interval"`
   425  	collectionInterval       time.Duration `mapstructure:"-"`
   426  	PublishAllocationMetrics bool          `mapstructure:"publish_allocation_metrics"`
   427  	PublishNodeMetrics       bool          `mapstructure:"publish_node_metrics"`
   428  
   429  	// DisableTaggedMetrics disables a new version of generating metrics which
   430  	// uses tags
   431  	DisableTaggedMetrics bool `mapstructure:"disable_tagged_metrics"`
   432  
   433  	// BackwardsCompatibleMetrics allows for generating metrics in a simple
   434  	// key/value structure as done in older versions of Nomad
   435  	BackwardsCompatibleMetrics bool `mapstructure:"backwards_compatible_metrics"`
   436  
   437  	// Circonus: see https://github.com/circonus-labs/circonus-gometrics
   438  	// for more details on the various configuration options.
   439  	// Valid configuration combinations:
   440  	//    - CirconusAPIToken
   441  	//      metric management enabled (search for existing check or create a new one)
   442  	//    - CirconusSubmissionUrl
   443  	//      metric management disabled (use check with specified submission_url,
   444  	//      broker must be using a public SSL certificate)
   445  	//    - CirconusAPIToken + CirconusCheckSubmissionURL
   446  	//      metric management enabled (use check with specified submission_url)
   447  	//    - CirconusAPIToken + CirconusCheckID
   448  	//      metric management enabled (use check with specified id)
   449  
   450  	// CirconusAPIToken is a valid API Token used to create/manage check. If provided,
   451  	// metric management is enabled.
   452  	// Default: none
   453  	CirconusAPIToken string `mapstructure:"circonus_api_token"`
   454  	// CirconusAPIApp is an app name associated with API token.
   455  	// Default: "nomad"
   456  	CirconusAPIApp string `mapstructure:"circonus_api_app"`
   457  	// CirconusAPIURL is the base URL to use for contacting the Circonus API.
   458  	// Default: "https://api.circonus.com/v2"
   459  	CirconusAPIURL string `mapstructure:"circonus_api_url"`
   460  	// CirconusSubmissionInterval is the interval at which metrics are submitted to Circonus.
   461  	// Default: 10s
   462  	CirconusSubmissionInterval string `mapstructure:"circonus_submission_interval"`
   463  	// CirconusCheckSubmissionURL is the check.config.submission_url field from a
   464  	// previously created HTTPTRAP check.
   465  	// Default: none
   466  	CirconusCheckSubmissionURL string `mapstructure:"circonus_submission_url"`
   467  	// CirconusCheckID is the check id (not check bundle id) from a previously created
   468  	// HTTPTRAP check. The numeric portion of the check._cid field.
   469  	// Default: none
   470  	CirconusCheckID string `mapstructure:"circonus_check_id"`
   471  	// CirconusCheckForceMetricActivation will force enabling metrics, as they are encountered,
   472  	// if the metric already exists and is NOT active. If check management is enabled, the default
   473  	// behavior is to add new metrics as they are encountered. If the metric already exists in the
   474  	// check, it will *NOT* be activated. This setting overrides that behavior.
   475  	// Default: "false"
   476  	CirconusCheckForceMetricActivation string `mapstructure:"circonus_check_force_metric_activation"`
   477  	// CirconusCheckInstanceID serves to uniquely identify the metrics coming from this "instance".
   478  	// It can be used to maintain metric continuity with transient or ephemeral instances as
   479  	// they move around within an infrastructure.
   480  	// Default: hostname:app
   481  	CirconusCheckInstanceID string `mapstructure:"circonus_check_instance_id"`
   482  	// CirconusCheckSearchTag is a special tag which, when coupled with the instance id, helps to
   483  	// narrow down the search results when neither a Submission URL or Check ID is provided.
   484  	// Default: service:app (e.g. service:nomad)
   485  	CirconusCheckSearchTag string `mapstructure:"circonus_check_search_tag"`
   486  	// CirconusCheckTags is a comma separated list of tags to apply to the check. Note that
   487  	// the value of CirconusCheckSearchTag will always be added to the check.
   488  	// Default: none
   489  	CirconusCheckTags string `mapstructure:"circonus_check_tags"`
   490  	// CirconusCheckDisplayName is the name for the check which will be displayed in the Circonus UI.
   491  	// Default: value of CirconusCheckInstanceID
   492  	CirconusCheckDisplayName string `mapstructure:"circonus_check_display_name"`
   493  	// CirconusBrokerID is an explicit broker to use when creating a new check. The numeric portion
   494  	// of broker._cid. If metric management is enabled and neither a Submission URL nor Check ID
   495  	// is provided, an attempt will be made to search for an existing check using Instance ID and
   496  	// Search Tag. If one is not found, a new HTTPTRAP check will be created.
   497  	// Default: use Select Tag if provided, otherwise, a random Enterprise Broker associated
   498  	// with the specified API token or the default Circonus Broker.
   499  	// Default: none
   500  	CirconusBrokerID string `mapstructure:"circonus_broker_id"`
   501  	// CirconusBrokerSelectTag is a special tag which will be used to select a broker when
   502  	// a Broker ID is not provided. The best use of this is to as a hint for which broker
   503  	// should be used based on *where* this particular instance is running.
   504  	// (e.g. a specific geo location or datacenter, dc:sfo)
   505  	// Default: none
   506  	CirconusBrokerSelectTag string `mapstructure:"circonus_broker_select_tag"`
   507  }
   508  
   509  // Ports encapsulates the various ports we bind to for network services. If any
   510  // are not specified then the defaults are used instead.
   511  type Ports struct {
   512  	HTTP int `mapstructure:"http"`
   513  	RPC  int `mapstructure:"rpc"`
   514  	Serf int `mapstructure:"serf"`
   515  }
   516  
   517  // Addresses encapsulates all of the addresses we bind to for various
   518  // network services. Everything is optional and defaults to BindAddr.
   519  type Addresses struct {
   520  	HTTP string `mapstructure:"http"`
   521  	RPC  string `mapstructure:"rpc"`
   522  	Serf string `mapstructure:"serf"`
   523  }
   524  
   525  // AdvertiseAddrs is used to control the addresses we advertise out for
   526  // different network services. All are optional and default to BindAddr and
   527  // their default Port.
   528  type AdvertiseAddrs struct {
   529  	HTTP string `mapstructure:"http"`
   530  	RPC  string `mapstructure:"rpc"`
   531  	Serf string `mapstructure:"serf"`
   532  }
   533  
   534  type Resources struct {
   535  	CPU                 int    `mapstructure:"cpu"`
   536  	MemoryMB            int    `mapstructure:"memory"`
   537  	DiskMB              int    `mapstructure:"disk"`
   538  	IOPS                int    `mapstructure:"iops"`
   539  	ReservedPorts       string `mapstructure:"reserved_ports"`
   540  	ParsedReservedPorts []int  `mapstructure:"-"`
   541  }
   542  
   543  // ParseReserved expands the ReservedPorts string into a slice of port numbers.
   544  // The supported syntax is comma separated integers or ranges separated by
   545  // hyphens. For example, "80,120-150,160"
   546  func (r *Resources) ParseReserved() error {
   547  	parts := strings.Split(r.ReservedPorts, ",")
   548  
   549  	// Hot path the empty case
   550  	if len(parts) == 1 && parts[0] == "" {
   551  		return nil
   552  	}
   553  
   554  	ports := make(map[int]struct{})
   555  	for _, part := range parts {
   556  		part = strings.TrimSpace(part)
   557  		rangeParts := strings.Split(part, "-")
   558  		l := len(rangeParts)
   559  		switch l {
   560  		case 1:
   561  			if val := rangeParts[0]; val == "" {
   562  				return fmt.Errorf("can't specify empty port")
   563  			} else {
   564  				port, err := strconv.Atoi(val)
   565  				if err != nil {
   566  					return err
   567  				}
   568  				ports[port] = struct{}{}
   569  			}
   570  		case 2:
   571  			// We are parsing a range
   572  			start, err := strconv.Atoi(rangeParts[0])
   573  			if err != nil {
   574  				return err
   575  			}
   576  
   577  			end, err := strconv.Atoi(rangeParts[1])
   578  			if err != nil {
   579  				return err
   580  			}
   581  
   582  			if end < start {
   583  				return fmt.Errorf("invalid range: starting value (%v) less than ending (%v) value", end, start)
   584  			}
   585  
   586  			for i := start; i <= end; i++ {
   587  				ports[i] = struct{}{}
   588  			}
   589  		default:
   590  			return fmt.Errorf("can only parse single port numbers or port ranges (ex. 80,100-120,150)")
   591  		}
   592  	}
   593  
   594  	for port := range ports {
   595  		r.ParsedReservedPorts = append(r.ParsedReservedPorts, port)
   596  	}
   597  
   598  	sort.Ints(r.ParsedReservedPorts)
   599  	return nil
   600  }
   601  
   602  // DevConfig is a Config that is used for dev mode of Nomad.
   603  func DevConfig() *Config {
   604  	conf := DefaultConfig()
   605  	conf.BindAddr = "127.0.0.1"
   606  	conf.LogLevel = "DEBUG"
   607  	conf.Client.Enabled = true
   608  	conf.Server.Enabled = true
   609  	conf.DevMode = true
   610  	conf.EnableDebug = true
   611  	conf.DisableAnonymousSignature = true
   612  	conf.Consul.AutoAdvertise = helper.BoolToPtr(true)
   613  	if runtime.GOOS == "darwin" {
   614  		conf.Client.NetworkInterface = "lo0"
   615  	} else if runtime.GOOS == "linux" {
   616  		conf.Client.NetworkInterface = "lo"
   617  	}
   618  	conf.Client.Options = map[string]string{
   619  		"driver.raw_exec.enable": "true",
   620  	}
   621  	conf.Client.Options = map[string]string{
   622  		"driver.docker.volumes": "true",
   623  	}
   624  	conf.Client.GCInterval = 10 * time.Minute
   625  	conf.Client.GCDiskUsageThreshold = 99
   626  	conf.Client.GCInodeUsageThreshold = 99
   627  	conf.Client.GCMaxAllocs = 50
   628  	conf.Telemetry.PrometheusMetrics = true
   629  	conf.Telemetry.PublishAllocationMetrics = true
   630  	conf.Telemetry.PublishNodeMetrics = true
   631  
   632  	return conf
   633  }
   634  
   635  // DefaultConfig is a the baseline configuration for Nomad
   636  func DefaultConfig() *Config {
   637  	return &Config{
   638  		LogLevel:   "INFO",
   639  		Region:     "global",
   640  		Datacenter: "dc1",
   641  		BindAddr:   "0.0.0.0",
   642  		Ports: &Ports{
   643  			HTTP: 4646,
   644  			RPC:  4647,
   645  			Serf: 4648,
   646  		},
   647  		Addresses:      &Addresses{},
   648  		AdvertiseAddrs: &AdvertiseAddrs{},
   649  		Consul:         config.DefaultConsulConfig(),
   650  		Vault:          config.DefaultVaultConfig(),
   651  		Client: &ClientConfig{
   652  			Enabled:               false,
   653  			MaxKillTimeout:        "30s",
   654  			ClientMinPort:         14000,
   655  			ClientMaxPort:         14512,
   656  			Reserved:              &Resources{},
   657  			GCInterval:            1 * time.Minute,
   658  			GCParallelDestroys:    2,
   659  			GCDiskUsageThreshold:  80,
   660  			GCInodeUsageThreshold: 70,
   661  			GCMaxAllocs:           50,
   662  			NoHostUUID:            helper.BoolToPtr(true),
   663  			ServerJoin: &ServerJoin{
   664  				RetryJoin:        []string{},
   665  				RetryInterval:    30 * time.Second,
   666  				RetryMaxAttempts: 0,
   667  			},
   668  		},
   669  		Server: &ServerConfig{
   670  			Enabled:   false,
   671  			StartJoin: []string{},
   672  			ServerJoin: &ServerJoin{
   673  				RetryJoin:        []string{},
   674  				RetryInterval:    30 * time.Second,
   675  				RetryMaxAttempts: 0,
   676  			},
   677  		},
   678  		ACL: &ACLConfig{
   679  			Enabled:   false,
   680  			TokenTTL:  30 * time.Second,
   681  			PolicyTTL: 30 * time.Second,
   682  		},
   683  		SyslogFacility: "LOCAL0",
   684  		Telemetry: &Telemetry{
   685  			CollectionInterval: "1s",
   686  			collectionInterval: 1 * time.Second,
   687  		},
   688  		TLSConfig:          &config.TLSConfig{},
   689  		Sentinel:           &config.SentinelConfig{},
   690  		Version:            version.GetVersion(),
   691  		Autopilot:          config.DefaultAutopilotConfig(),
   692  		DisableUpdateCheck: helper.BoolToPtr(false),
   693  	}
   694  }
   695  
   696  // Listener can be used to get a new listener using a custom bind address.
   697  // If the bind provided address is empty, the BindAddr is used instead.
   698  func (c *Config) Listener(proto, addr string, port int) (net.Listener, error) {
   699  	if addr == "" {
   700  		addr = c.BindAddr
   701  	}
   702  
   703  	// Do our own range check to avoid bugs in package net.
   704  	//
   705  	//   golang.org/issue/11715
   706  	//   golang.org/issue/13447
   707  	//
   708  	// Both of the above bugs were fixed by golang.org/cl/12447 which will be
   709  	// included in Go 1.6. The error returned below is the same as what Go 1.6
   710  	// will return.
   711  	if 0 > port || port > 65535 {
   712  		return nil, &net.OpError{
   713  			Op:  "listen",
   714  			Net: proto,
   715  			Err: &net.AddrError{Err: "invalid port", Addr: fmt.Sprint(port)},
   716  		}
   717  	}
   718  	return net.Listen(proto, net.JoinHostPort(addr, strconv.Itoa(port)))
   719  }
   720  
   721  // Merge merges two configurations.
   722  func (c *Config) Merge(b *Config) *Config {
   723  	result := *c
   724  
   725  	if b.Region != "" {
   726  		result.Region = b.Region
   727  	}
   728  	if b.Datacenter != "" {
   729  		result.Datacenter = b.Datacenter
   730  	}
   731  	if b.NodeName != "" {
   732  		result.NodeName = b.NodeName
   733  	}
   734  	if b.DataDir != "" {
   735  		result.DataDir = b.DataDir
   736  	}
   737  	if b.LogLevel != "" {
   738  		result.LogLevel = b.LogLevel
   739  	}
   740  	if b.BindAddr != "" {
   741  		result.BindAddr = b.BindAddr
   742  	}
   743  	if b.EnableDebug {
   744  		result.EnableDebug = true
   745  	}
   746  	if b.LeaveOnInt {
   747  		result.LeaveOnInt = true
   748  	}
   749  	if b.LeaveOnTerm {
   750  		result.LeaveOnTerm = true
   751  	}
   752  	if b.EnableSyslog {
   753  		result.EnableSyslog = true
   754  	}
   755  	if b.SyslogFacility != "" {
   756  		result.SyslogFacility = b.SyslogFacility
   757  	}
   758  	if b.DisableUpdateCheck != nil {
   759  		result.DisableUpdateCheck = helper.BoolToPtr(*b.DisableUpdateCheck)
   760  	}
   761  	if b.DisableAnonymousSignature {
   762  		result.DisableAnonymousSignature = true
   763  	}
   764  
   765  	// Apply the telemetry config
   766  	if result.Telemetry == nil && b.Telemetry != nil {
   767  		telemetry := *b.Telemetry
   768  		result.Telemetry = &telemetry
   769  	} else if b.Telemetry != nil {
   770  		result.Telemetry = result.Telemetry.Merge(b.Telemetry)
   771  	}
   772  
   773  	// Apply the TLS Config
   774  	if result.TLSConfig == nil && b.TLSConfig != nil {
   775  		result.TLSConfig = b.TLSConfig.Copy()
   776  	} else if b.TLSConfig != nil {
   777  		result.TLSConfig = result.TLSConfig.Merge(b.TLSConfig)
   778  	}
   779  
   780  	// Apply the client config
   781  	if result.Client == nil && b.Client != nil {
   782  		client := *b.Client
   783  		result.Client = &client
   784  	} else if b.Client != nil {
   785  		result.Client = result.Client.Merge(b.Client)
   786  	}
   787  
   788  	// Apply the server config
   789  	if result.Server == nil && b.Server != nil {
   790  		server := *b.Server
   791  		result.Server = &server
   792  	} else if b.Server != nil {
   793  		result.Server = result.Server.Merge(b.Server)
   794  	}
   795  
   796  	// Apply the acl config
   797  	if result.ACL == nil && b.ACL != nil {
   798  		server := *b.ACL
   799  		result.ACL = &server
   800  	} else if b.ACL != nil {
   801  		result.ACL = result.ACL.Merge(b.ACL)
   802  	}
   803  
   804  	// Apply the ports config
   805  	if result.Ports == nil && b.Ports != nil {
   806  		ports := *b.Ports
   807  		result.Ports = &ports
   808  	} else if b.Ports != nil {
   809  		result.Ports = result.Ports.Merge(b.Ports)
   810  	}
   811  
   812  	// Apply the address config
   813  	if result.Addresses == nil && b.Addresses != nil {
   814  		addrs := *b.Addresses
   815  		result.Addresses = &addrs
   816  	} else if b.Addresses != nil {
   817  		result.Addresses = result.Addresses.Merge(b.Addresses)
   818  	}
   819  
   820  	// Apply the advertise addrs config
   821  	if result.AdvertiseAddrs == nil && b.AdvertiseAddrs != nil {
   822  		advertise := *b.AdvertiseAddrs
   823  		result.AdvertiseAddrs = &advertise
   824  	} else if b.AdvertiseAddrs != nil {
   825  		result.AdvertiseAddrs = result.AdvertiseAddrs.Merge(b.AdvertiseAddrs)
   826  	}
   827  
   828  	// Apply the Consul Configuration
   829  	if result.Consul == nil && b.Consul != nil {
   830  		result.Consul = b.Consul.Copy()
   831  	} else if b.Consul != nil {
   832  		result.Consul = result.Consul.Merge(b.Consul)
   833  	}
   834  
   835  	// Apply the Vault Configuration
   836  	if result.Vault == nil && b.Vault != nil {
   837  		vaultConfig := *b.Vault
   838  		result.Vault = &vaultConfig
   839  	} else if b.Vault != nil {
   840  		result.Vault = result.Vault.Merge(b.Vault)
   841  	}
   842  
   843  	// Apply the sentinel config
   844  	if result.Sentinel == nil && b.Sentinel != nil {
   845  		server := *b.Sentinel
   846  		result.Sentinel = &server
   847  	} else if b.Sentinel != nil {
   848  		result.Sentinel = result.Sentinel.Merge(b.Sentinel)
   849  	}
   850  
   851  	if result.Autopilot == nil && b.Autopilot != nil {
   852  		autopilot := *b.Autopilot
   853  		result.Autopilot = &autopilot
   854  	} else if b.Autopilot != nil {
   855  		result.Autopilot = result.Autopilot.Merge(b.Autopilot)
   856  	}
   857  
   858  	// Merge config files lists
   859  	result.Files = append(result.Files, b.Files...)
   860  
   861  	// Add the http API response header map values
   862  	if result.HTTPAPIResponseHeaders == nil {
   863  		result.HTTPAPIResponseHeaders = make(map[string]string)
   864  	}
   865  	for k, v := range b.HTTPAPIResponseHeaders {
   866  		result.HTTPAPIResponseHeaders[k] = v
   867  	}
   868  
   869  	return &result
   870  }
   871  
   872  // normalizeAddrs normalizes Addresses and AdvertiseAddrs to always be
   873  // initialized and have sane defaults.
   874  func (c *Config) normalizeAddrs() error {
   875  	if c.BindAddr != "" {
   876  		ipStr, err := parseSingleIPTemplate(c.BindAddr)
   877  		if err != nil {
   878  			return fmt.Errorf("Bind address resolution failed: %v", err)
   879  		}
   880  		c.BindAddr = ipStr
   881  	}
   882  
   883  	addr, err := normalizeBind(c.Addresses.HTTP, c.BindAddr)
   884  	if err != nil {
   885  		return fmt.Errorf("Failed to parse HTTP address: %v", err)
   886  	}
   887  	c.Addresses.HTTP = addr
   888  
   889  	addr, err = normalizeBind(c.Addresses.RPC, c.BindAddr)
   890  	if err != nil {
   891  		return fmt.Errorf("Failed to parse RPC address: %v", err)
   892  	}
   893  	c.Addresses.RPC = addr
   894  
   895  	addr, err = normalizeBind(c.Addresses.Serf, c.BindAddr)
   896  	if err != nil {
   897  		return fmt.Errorf("Failed to parse Serf address: %v", err)
   898  	}
   899  	c.Addresses.Serf = addr
   900  
   901  	c.normalizedAddrs = &Addresses{
   902  		HTTP: net.JoinHostPort(c.Addresses.HTTP, strconv.Itoa(c.Ports.HTTP)),
   903  		RPC:  net.JoinHostPort(c.Addresses.RPC, strconv.Itoa(c.Ports.RPC)),
   904  		Serf: net.JoinHostPort(c.Addresses.Serf, strconv.Itoa(c.Ports.Serf)),
   905  	}
   906  
   907  	addr, err = normalizeAdvertise(c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode)
   908  	if err != nil {
   909  		return fmt.Errorf("Failed to parse HTTP advertise address (%v, %v, %v, %v): %v", c.AdvertiseAddrs.HTTP, c.Addresses.HTTP, c.Ports.HTTP, c.DevMode, err)
   910  	}
   911  	c.AdvertiseAddrs.HTTP = addr
   912  
   913  	addr, err = normalizeAdvertise(c.AdvertiseAddrs.RPC, c.Addresses.RPC, c.Ports.RPC, c.DevMode)
   914  	if err != nil {
   915  		return fmt.Errorf("Failed to parse RPC advertise address: %v", err)
   916  	}
   917  	c.AdvertiseAddrs.RPC = addr
   918  
   919  	// Skip serf if server is disabled
   920  	if c.Server != nil && c.Server.Enabled {
   921  		addr, err = normalizeAdvertise(c.AdvertiseAddrs.Serf, c.Addresses.Serf, c.Ports.Serf, c.DevMode)
   922  		if err != nil {
   923  			return fmt.Errorf("Failed to parse Serf advertise address: %v", err)
   924  		}
   925  		c.AdvertiseAddrs.Serf = addr
   926  	}
   927  
   928  	return nil
   929  }
   930  
   931  // parseSingleIPTemplate is used as a helper function to parse out a single IP
   932  // address from a config parameter.
   933  func parseSingleIPTemplate(ipTmpl string) (string, error) {
   934  	out, err := template.Parse(ipTmpl)
   935  	if err != nil {
   936  		return "", fmt.Errorf("Unable to parse address template %q: %v", ipTmpl, err)
   937  	}
   938  
   939  	ips := strings.Split(out, " ")
   940  	switch len(ips) {
   941  	case 0:
   942  		return "", errors.New("No addresses found, please configure one.")
   943  	case 1:
   944  		return ips[0], nil
   945  	default:
   946  		return "", fmt.Errorf("Multiple addresses found (%q), please configure one.", out)
   947  	}
   948  }
   949  
   950  // normalizeBind returns a normalized bind address.
   951  //
   952  // If addr is set it is used, if not the default bind address is used.
   953  func normalizeBind(addr, bind string) (string, error) {
   954  	if addr == "" {
   955  		return bind, nil
   956  	}
   957  	return parseSingleIPTemplate(addr)
   958  }
   959  
   960  // normalizeAdvertise returns a normalized advertise address.
   961  //
   962  // If addr is set, it is used and the default port is appended if no port is
   963  // set.
   964  //
   965  // If addr is not set and bind is a valid address, the returned string is the
   966  // bind+port.
   967  //
   968  // If addr is not set and bind is not a valid advertise address, the hostname
   969  // is resolved and returned with the port.
   970  //
   971  // Loopback is only considered a valid advertise address in dev mode.
   972  func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string, error) {
   973  	addr, err := parseSingleIPTemplate(addr)
   974  	if err != nil {
   975  		return "", fmt.Errorf("Error parsing advertise address template: %v", err)
   976  	}
   977  
   978  	if addr != "" {
   979  		// Default to using manually configured address
   980  		_, _, err = net.SplitHostPort(addr)
   981  		if err != nil {
   982  			if !isMissingPort(err) && !isTooManyColons(err) {
   983  				return "", fmt.Errorf("Error parsing advertise address %q: %v", addr, err)
   984  			}
   985  
   986  			// missing port, append the default
   987  			return net.JoinHostPort(addr, strconv.Itoa(defport)), nil
   988  		}
   989  
   990  		return addr, nil
   991  	}
   992  
   993  	// Fallback to bind address first, and then try resolving the local hostname
   994  	ips, err := net.LookupIP(bind)
   995  	if err != nil {
   996  		return "", fmt.Errorf("Error resolving bind address %q: %v", bind, err)
   997  	}
   998  
   999  	// Return the first non-localhost unicast address
  1000  	for _, ip := range ips {
  1001  		if ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() {
  1002  			return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
  1003  		}
  1004  		if ip.IsLoopback() {
  1005  			if dev {
  1006  				// loopback is fine for dev mode
  1007  				return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
  1008  			}
  1009  			return "", fmt.Errorf("Defaulting advertise to localhost is unsafe, please set advertise manually")
  1010  		}
  1011  	}
  1012  
  1013  	// Bind is not localhost but not a valid advertise IP, use first private IP
  1014  	addr, err = parseSingleIPTemplate("{{ GetPrivateIP }}")
  1015  	if err != nil {
  1016  		return "", fmt.Errorf("Unable to parse default advertise address: %v", err)
  1017  	}
  1018  	return net.JoinHostPort(addr, strconv.Itoa(defport)), nil
  1019  }
  1020  
  1021  // isMissingPort returns true if an error is a "missing port" error from
  1022  // net.SplitHostPort.
  1023  func isMissingPort(err error) bool {
  1024  	// matches error const in net/ipsock.go
  1025  	const missingPort = "missing port in address"
  1026  	return err != nil && strings.Contains(err.Error(), missingPort)
  1027  }
  1028  
  1029  // isTooManyColons returns true if an error is a "too many colons" error from
  1030  // net.SplitHostPort.
  1031  func isTooManyColons(err error) bool {
  1032  	// matches error const in net/ipsock.go
  1033  	const tooManyColons = "too many colons in address"
  1034  	return err != nil && strings.Contains(err.Error(), tooManyColons)
  1035  }
  1036  
  1037  // Merge is used to merge two ACL configs together. The settings from the input always take precedence.
  1038  func (a *ACLConfig) Merge(b *ACLConfig) *ACLConfig {
  1039  	result := *a
  1040  
  1041  	if b.Enabled {
  1042  		result.Enabled = true
  1043  	}
  1044  	if b.TokenTTL != 0 {
  1045  		result.TokenTTL = b.TokenTTL
  1046  	}
  1047  	if b.PolicyTTL != 0 {
  1048  		result.PolicyTTL = b.PolicyTTL
  1049  	}
  1050  	if b.ReplicationToken != "" {
  1051  		result.ReplicationToken = b.ReplicationToken
  1052  	}
  1053  	return &result
  1054  }
  1055  
  1056  // Merge is used to merge two server configs together
  1057  func (a *ServerConfig) Merge(b *ServerConfig) *ServerConfig {
  1058  	result := *a
  1059  
  1060  	if b.Enabled {
  1061  		result.Enabled = true
  1062  	}
  1063  	if b.AuthoritativeRegion != "" {
  1064  		result.AuthoritativeRegion = b.AuthoritativeRegion
  1065  	}
  1066  	if b.BootstrapExpect > 0 {
  1067  		result.BootstrapExpect = b.BootstrapExpect
  1068  	}
  1069  	if b.DataDir != "" {
  1070  		result.DataDir = b.DataDir
  1071  	}
  1072  	if b.ProtocolVersion != 0 {
  1073  		result.ProtocolVersion = b.ProtocolVersion
  1074  	}
  1075  	if b.RaftProtocol != 0 {
  1076  		result.RaftProtocol = b.RaftProtocol
  1077  	}
  1078  	if b.NumSchedulers != nil {
  1079  		result.NumSchedulers = helper.IntToPtr(*b.NumSchedulers)
  1080  	}
  1081  	if b.NodeGCThreshold != "" {
  1082  		result.NodeGCThreshold = b.NodeGCThreshold
  1083  	}
  1084  	if b.JobGCThreshold != "" {
  1085  		result.JobGCThreshold = b.JobGCThreshold
  1086  	}
  1087  	if b.EvalGCThreshold != "" {
  1088  		result.EvalGCThreshold = b.EvalGCThreshold
  1089  	}
  1090  	if b.DeploymentGCThreshold != "" {
  1091  		result.DeploymentGCThreshold = b.DeploymentGCThreshold
  1092  	}
  1093  	if b.HeartbeatGrace != 0 {
  1094  		result.HeartbeatGrace = b.HeartbeatGrace
  1095  	}
  1096  	if b.MinHeartbeatTTL != 0 {
  1097  		result.MinHeartbeatTTL = b.MinHeartbeatTTL
  1098  	}
  1099  	if b.MaxHeartbeatsPerSecond != 0.0 {
  1100  		result.MaxHeartbeatsPerSecond = b.MaxHeartbeatsPerSecond
  1101  	}
  1102  	if b.RetryMaxAttempts != 0 {
  1103  		result.RetryMaxAttempts = b.RetryMaxAttempts
  1104  	}
  1105  	if b.RetryInterval != 0 {
  1106  		result.RetryInterval = b.RetryInterval
  1107  	}
  1108  	if b.RejoinAfterLeave {
  1109  		result.RejoinAfterLeave = true
  1110  	}
  1111  	if b.NonVotingServer {
  1112  		result.NonVotingServer = true
  1113  	}
  1114  	if b.RedundancyZone != "" {
  1115  		result.RedundancyZone = b.RedundancyZone
  1116  	}
  1117  	if b.UpgradeVersion != "" {
  1118  		result.UpgradeVersion = b.UpgradeVersion
  1119  	}
  1120  	if b.EncryptKey != "" {
  1121  		result.EncryptKey = b.EncryptKey
  1122  	}
  1123  	if b.ServerJoin != nil {
  1124  		result.ServerJoin = result.ServerJoin.Merge(b.ServerJoin)
  1125  	}
  1126  
  1127  	// Add the schedulers
  1128  	result.EnabledSchedulers = append(result.EnabledSchedulers, b.EnabledSchedulers...)
  1129  
  1130  	// Copy the start join addresses
  1131  	result.StartJoin = make([]string, 0, len(a.StartJoin)+len(b.StartJoin))
  1132  	result.StartJoin = append(result.StartJoin, a.StartJoin...)
  1133  	result.StartJoin = append(result.StartJoin, b.StartJoin...)
  1134  
  1135  	// Copy the retry join addresses
  1136  	result.RetryJoin = make([]string, 0, len(a.RetryJoin)+len(b.RetryJoin))
  1137  	result.RetryJoin = append(result.RetryJoin, a.RetryJoin...)
  1138  	result.RetryJoin = append(result.RetryJoin, b.RetryJoin...)
  1139  
  1140  	return &result
  1141  }
  1142  
  1143  // Merge is used to merge two client configs together
  1144  func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
  1145  	result := *a
  1146  
  1147  	if b.Enabled {
  1148  		result.Enabled = true
  1149  	}
  1150  	if b.StateDir != "" {
  1151  		result.StateDir = b.StateDir
  1152  	}
  1153  	if b.AllocDir != "" {
  1154  		result.AllocDir = b.AllocDir
  1155  	}
  1156  	if b.NodeClass != "" {
  1157  		result.NodeClass = b.NodeClass
  1158  	}
  1159  	if b.NetworkInterface != "" {
  1160  		result.NetworkInterface = b.NetworkInterface
  1161  	}
  1162  	if b.NetworkSpeed != 0 {
  1163  		result.NetworkSpeed = b.NetworkSpeed
  1164  	}
  1165  	if b.CpuCompute != 0 {
  1166  		result.CpuCompute = b.CpuCompute
  1167  	}
  1168  	if b.MemoryMB != 0 {
  1169  		result.MemoryMB = b.MemoryMB
  1170  	}
  1171  	if b.MaxKillTimeout != "" {
  1172  		result.MaxKillTimeout = b.MaxKillTimeout
  1173  	}
  1174  	if b.ClientMaxPort != 0 {
  1175  		result.ClientMaxPort = b.ClientMaxPort
  1176  	}
  1177  	if b.ClientMinPort != 0 {
  1178  		result.ClientMinPort = b.ClientMinPort
  1179  	}
  1180  	if result.Reserved == nil && b.Reserved != nil {
  1181  		reserved := *b.Reserved
  1182  		result.Reserved = &reserved
  1183  	} else if b.Reserved != nil {
  1184  		result.Reserved = result.Reserved.Merge(b.Reserved)
  1185  	}
  1186  	if b.GCInterval != 0 {
  1187  		result.GCInterval = b.GCInterval
  1188  	}
  1189  	if b.GCParallelDestroys != 0 {
  1190  		result.GCParallelDestroys = b.GCParallelDestroys
  1191  	}
  1192  	if b.GCDiskUsageThreshold != 0 {
  1193  		result.GCDiskUsageThreshold = b.GCDiskUsageThreshold
  1194  	}
  1195  	if b.GCInodeUsageThreshold != 0 {
  1196  		result.GCInodeUsageThreshold = b.GCInodeUsageThreshold
  1197  	}
  1198  	if b.GCMaxAllocs != 0 {
  1199  		result.GCMaxAllocs = b.GCMaxAllocs
  1200  	}
  1201  	// NoHostUUID defaults to true, merge if false
  1202  	if b.NoHostUUID != nil {
  1203  		result.NoHostUUID = b.NoHostUUID
  1204  	}
  1205  
  1206  	// Add the servers
  1207  	result.Servers = append(result.Servers, b.Servers...)
  1208  
  1209  	// Add the options map values
  1210  	if result.Options == nil {
  1211  		result.Options = make(map[string]string)
  1212  	}
  1213  	for k, v := range b.Options {
  1214  		result.Options[k] = v
  1215  	}
  1216  
  1217  	// Add the meta map values
  1218  	if result.Meta == nil {
  1219  		result.Meta = make(map[string]string)
  1220  	}
  1221  	for k, v := range b.Meta {
  1222  		result.Meta[k] = v
  1223  	}
  1224  
  1225  	// Add the chroot_env map values
  1226  	if result.ChrootEnv == nil {
  1227  		result.ChrootEnv = make(map[string]string)
  1228  	}
  1229  	for k, v := range b.ChrootEnv {
  1230  		result.ChrootEnv[k] = v
  1231  	}
  1232  
  1233  	if b.ServerJoin != nil {
  1234  		result.ServerJoin = result.ServerJoin.Merge(b.ServerJoin)
  1235  	}
  1236  
  1237  	return &result
  1238  }
  1239  
  1240  // Merge is used to merge two telemetry configs together
  1241  func (a *Telemetry) Merge(b *Telemetry) *Telemetry {
  1242  	result := *a
  1243  
  1244  	if b.StatsiteAddr != "" {
  1245  		result.StatsiteAddr = b.StatsiteAddr
  1246  	}
  1247  	if b.StatsdAddr != "" {
  1248  		result.StatsdAddr = b.StatsdAddr
  1249  	}
  1250  	if b.DataDogAddr != "" {
  1251  		result.DataDogAddr = b.DataDogAddr
  1252  	}
  1253  	if b.DataDogTags != nil {
  1254  		result.DataDogTags = b.DataDogTags
  1255  	}
  1256  	if b.PrometheusMetrics {
  1257  		result.PrometheusMetrics = b.PrometheusMetrics
  1258  	}
  1259  	if b.DisableHostname {
  1260  		result.DisableHostname = true
  1261  	}
  1262  
  1263  	if b.UseNodeName {
  1264  		result.UseNodeName = true
  1265  	}
  1266  	if b.CollectionInterval != "" {
  1267  		result.CollectionInterval = b.CollectionInterval
  1268  	}
  1269  	if b.collectionInterval != 0 {
  1270  		result.collectionInterval = b.collectionInterval
  1271  	}
  1272  	if b.PublishNodeMetrics {
  1273  		result.PublishNodeMetrics = true
  1274  	}
  1275  	if b.PublishAllocationMetrics {
  1276  		result.PublishAllocationMetrics = true
  1277  	}
  1278  	if b.CirconusAPIToken != "" {
  1279  		result.CirconusAPIToken = b.CirconusAPIToken
  1280  	}
  1281  	if b.CirconusAPIApp != "" {
  1282  		result.CirconusAPIApp = b.CirconusAPIApp
  1283  	}
  1284  	if b.CirconusAPIURL != "" {
  1285  		result.CirconusAPIURL = b.CirconusAPIURL
  1286  	}
  1287  	if b.CirconusCheckSubmissionURL != "" {
  1288  		result.CirconusCheckSubmissionURL = b.CirconusCheckSubmissionURL
  1289  	}
  1290  	if b.CirconusSubmissionInterval != "" {
  1291  		result.CirconusSubmissionInterval = b.CirconusSubmissionInterval
  1292  	}
  1293  	if b.CirconusCheckID != "" {
  1294  		result.CirconusCheckID = b.CirconusCheckID
  1295  	}
  1296  	if b.CirconusCheckForceMetricActivation != "" {
  1297  		result.CirconusCheckForceMetricActivation = b.CirconusCheckForceMetricActivation
  1298  	}
  1299  	if b.CirconusCheckInstanceID != "" {
  1300  		result.CirconusCheckInstanceID = b.CirconusCheckInstanceID
  1301  	}
  1302  	if b.CirconusCheckSearchTag != "" {
  1303  		result.CirconusCheckSearchTag = b.CirconusCheckSearchTag
  1304  	}
  1305  	if b.CirconusCheckTags != "" {
  1306  		result.CirconusCheckTags = b.CirconusCheckTags
  1307  	}
  1308  	if b.CirconusCheckDisplayName != "" {
  1309  		result.CirconusCheckDisplayName = b.CirconusCheckDisplayName
  1310  	}
  1311  	if b.CirconusBrokerID != "" {
  1312  		result.CirconusBrokerID = b.CirconusBrokerID
  1313  	}
  1314  	if b.CirconusBrokerSelectTag != "" {
  1315  		result.CirconusBrokerSelectTag = b.CirconusBrokerSelectTag
  1316  	}
  1317  
  1318  	if b.DisableTaggedMetrics {
  1319  		result.DisableTaggedMetrics = b.DisableTaggedMetrics
  1320  	}
  1321  
  1322  	if b.BackwardsCompatibleMetrics {
  1323  		result.BackwardsCompatibleMetrics = b.BackwardsCompatibleMetrics
  1324  	}
  1325  
  1326  	return &result
  1327  }
  1328  
  1329  // Merge is used to merge two port configurations.
  1330  func (a *Ports) Merge(b *Ports) *Ports {
  1331  	result := *a
  1332  
  1333  	if b.HTTP != 0 {
  1334  		result.HTTP = b.HTTP
  1335  	}
  1336  	if b.RPC != 0 {
  1337  		result.RPC = b.RPC
  1338  	}
  1339  	if b.Serf != 0 {
  1340  		result.Serf = b.Serf
  1341  	}
  1342  	return &result
  1343  }
  1344  
  1345  // Merge is used to merge two address configs together.
  1346  func (a *Addresses) Merge(b *Addresses) *Addresses {
  1347  	result := *a
  1348  
  1349  	if b.HTTP != "" {
  1350  		result.HTTP = b.HTTP
  1351  	}
  1352  	if b.RPC != "" {
  1353  		result.RPC = b.RPC
  1354  	}
  1355  	if b.Serf != "" {
  1356  		result.Serf = b.Serf
  1357  	}
  1358  	return &result
  1359  }
  1360  
  1361  // Merge merges two advertise addrs configs together.
  1362  func (a *AdvertiseAddrs) Merge(b *AdvertiseAddrs) *AdvertiseAddrs {
  1363  	result := *a
  1364  
  1365  	if b.RPC != "" {
  1366  		result.RPC = b.RPC
  1367  	}
  1368  	if b.Serf != "" {
  1369  		result.Serf = b.Serf
  1370  	}
  1371  	if b.HTTP != "" {
  1372  		result.HTTP = b.HTTP
  1373  	}
  1374  	return &result
  1375  }
  1376  
  1377  func (r *Resources) Merge(b *Resources) *Resources {
  1378  	result := *r
  1379  	if b.CPU != 0 {
  1380  		result.CPU = b.CPU
  1381  	}
  1382  	if b.MemoryMB != 0 {
  1383  		result.MemoryMB = b.MemoryMB
  1384  	}
  1385  	if b.DiskMB != 0 {
  1386  		result.DiskMB = b.DiskMB
  1387  	}
  1388  	if b.IOPS != 0 {
  1389  		result.IOPS = b.IOPS
  1390  	}
  1391  	if b.ReservedPorts != "" {
  1392  		result.ReservedPorts = b.ReservedPorts
  1393  	}
  1394  	if len(b.ParsedReservedPorts) != 0 {
  1395  		result.ParsedReservedPorts = b.ParsedReservedPorts
  1396  	}
  1397  	return &result
  1398  }
  1399  
  1400  // LoadConfig loads the configuration at the given path, regardless if
  1401  // its a file or directory.
  1402  func LoadConfig(path string) (*Config, error) {
  1403  	fi, err := os.Stat(path)
  1404  	if err != nil {
  1405  		return nil, err
  1406  	}
  1407  
  1408  	if fi.IsDir() {
  1409  		return LoadConfigDir(path)
  1410  	}
  1411  
  1412  	cleaned := filepath.Clean(path)
  1413  	config, err := ParseConfigFile(cleaned)
  1414  	if err != nil {
  1415  		return nil, fmt.Errorf("Error loading %s: %s", cleaned, err)
  1416  	}
  1417  
  1418  	config.Files = append(config.Files, cleaned)
  1419  	return config, nil
  1420  }
  1421  
  1422  // LoadConfigDir loads all the configurations in the given directory
  1423  // in alphabetical order.
  1424  func LoadConfigDir(dir string) (*Config, error) {
  1425  	f, err := os.Open(dir)
  1426  	if err != nil {
  1427  		return nil, err
  1428  	}
  1429  	defer f.Close()
  1430  
  1431  	fi, err := f.Stat()
  1432  	if err != nil {
  1433  		return nil, err
  1434  	}
  1435  	if !fi.IsDir() {
  1436  		return nil, fmt.Errorf(
  1437  			"configuration path must be a directory: %s", dir)
  1438  	}
  1439  
  1440  	var files []string
  1441  	err = nil
  1442  	for err != io.EOF {
  1443  		var fis []os.FileInfo
  1444  		fis, err = f.Readdir(128)
  1445  		if err != nil && err != io.EOF {
  1446  			return nil, err
  1447  		}
  1448  
  1449  		for _, fi := range fis {
  1450  			// Ignore directories
  1451  			if fi.IsDir() {
  1452  				continue
  1453  			}
  1454  
  1455  			// Only care about files that are valid to load.
  1456  			name := fi.Name()
  1457  			skip := true
  1458  			if strings.HasSuffix(name, ".hcl") {
  1459  				skip = false
  1460  			} else if strings.HasSuffix(name, ".json") {
  1461  				skip = false
  1462  			}
  1463  			if skip || isTemporaryFile(name) {
  1464  				continue
  1465  			}
  1466  
  1467  			path := filepath.Join(dir, name)
  1468  			files = append(files, path)
  1469  		}
  1470  	}
  1471  
  1472  	// Fast-path if we have no files
  1473  	if len(files) == 0 {
  1474  		return &Config{}, nil
  1475  	}
  1476  
  1477  	sort.Strings(files)
  1478  
  1479  	var result *Config
  1480  	for _, f := range files {
  1481  		config, err := ParseConfigFile(f)
  1482  		if err != nil {
  1483  			return nil, fmt.Errorf("Error loading %s: %s", f, err)
  1484  		}
  1485  		config.Files = append(config.Files, f)
  1486  
  1487  		if result == nil {
  1488  			result = config
  1489  		} else {
  1490  			result = result.Merge(config)
  1491  		}
  1492  	}
  1493  
  1494  	return result, nil
  1495  }
  1496  
  1497  // isTemporaryFile returns true or false depending on whether the
  1498  // provided file name is a temporary file for the following editors:
  1499  // emacs or vim.
  1500  func isTemporaryFile(name string) bool {
  1501  	return strings.HasSuffix(name, "~") || // vim
  1502  		strings.HasPrefix(name, ".#") || // emacs
  1503  		(strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#")) // emacs
  1504  }