2015-08-16 01:54:41 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-11 00:48:02 +00:00
|
|
|
"net"
|
2015-08-16 01:54:41 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-10-29 12:30:26 +00:00
|
|
|
"runtime"
|
2015-12-15 07:05:25 +00:00
|
|
|
"sort"
|
2016-03-12 02:24:58 +00:00
|
|
|
"strconv"
|
2015-08-16 01:54:41 +00:00
|
|
|
"strings"
|
2015-12-02 19:55:29 +00:00
|
|
|
"time"
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-09-06 01:41:00 +00:00
|
|
|
client "github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2016-05-22 03:15:58 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
2015-08-16 01:54:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration for the Nomad agent.
|
|
|
|
type Config struct {
|
2015-09-14 01:18:40 +00:00
|
|
|
// Region is the region this agent is in. Defaults to global.
|
2016-03-12 02:24:58 +00:00
|
|
|
Region string `mapstructure:"region"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-08-31 01:10:23 +00:00
|
|
|
// Datacenter is the datacenter this agent is in. Defaults to dc1
|
2016-03-12 02:24:58 +00:00
|
|
|
Datacenter string `mapstructure:"datacenter"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
|
|
|
// NodeName is the name we register as. Defaults to hostname.
|
2016-03-12 02:24:58 +00:00
|
|
|
NodeName string `mapstructure:"name"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
// DataDir is the directory to store our state in
|
2016-03-12 02:24:58 +00:00
|
|
|
DataDir string `mapstructure:"data_dir"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
|
|
|
// LogLevel is the level of the logs to putout
|
2016-03-12 02:24:58 +00:00
|
|
|
LogLevel string `mapstructure:"log_level"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-09-11 00:48:02 +00:00
|
|
|
// BindAddr is the address on which all of nomad's services will
|
|
|
|
// be bound. If not specified, this defaults to 127.0.0.1.
|
2016-03-12 02:24:58 +00:00
|
|
|
BindAddr string `mapstructure:"bind_addr"`
|
2015-09-11 00:48:02 +00:00
|
|
|
|
2015-08-24 00:50:33 +00:00
|
|
|
// EnableDebug is used to enable debugging HTTP endpoints
|
2016-03-12 02:24:58 +00:00
|
|
|
EnableDebug bool `mapstructure:"enable_debug"`
|
2015-08-24 00:50:33 +00:00
|
|
|
|
2015-09-11 01:37:42 +00:00
|
|
|
// Ports is used to control the network ports we bind to.
|
2016-03-12 02:24:58 +00:00
|
|
|
Ports *Ports `mapstructure:"ports"`
|
2015-09-11 01:37:42 +00:00
|
|
|
|
|
|
|
// Addresses is used to override the network addresses we bind to.
|
2016-03-12 02:24:58 +00:00
|
|
|
Addresses *Addresses `mapstructure:"addresses"`
|
2015-09-11 01:37:42 +00:00
|
|
|
|
|
|
|
// AdvertiseAddrs is used to control the addresses we advertise.
|
2016-03-12 02:24:58 +00:00
|
|
|
AdvertiseAddrs *AdvertiseAddrs `mapstructure:"advertise"`
|
2015-09-11 01:37:42 +00:00
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
// Client has our client related settings
|
2016-03-12 02:24:58 +00:00
|
|
|
Client *ClientConfig `mapstructure:"client"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
|
|
|
// Server has our server related settings
|
2016-03-12 02:24:58 +00:00
|
|
|
Server *ServerConfig `mapstructure:"server"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// Telemetry is used to configure sending telemetry
|
2016-03-12 02:24:58 +00:00
|
|
|
Telemetry *Telemetry `mapstructure:"telemetry"`
|
2015-08-16 20:54:49 +00:00
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// LeaveOnInt is used to gracefully leave on the interrupt signal
|
2016-03-12 02:24:58 +00:00
|
|
|
LeaveOnInt bool `mapstructure:"leave_on_interrupt"`
|
2015-08-16 21:34:38 +00:00
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// LeaveOnTerm is used to gracefully leave on the terminate signal
|
2016-03-12 02:24:58 +00:00
|
|
|
LeaveOnTerm bool `mapstructure:"leave_on_terminate"`
|
2015-08-16 21:34:38 +00:00
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// EnableSyslog is used to enable sending logs to syslog
|
2016-03-12 02:24:58 +00:00
|
|
|
EnableSyslog bool `mapstructure:"enable_syslog"`
|
2015-08-24 00:40:27 +00:00
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// SyslogFacility is used to control the syslog facility used.
|
2016-03-12 02:24:58 +00:00
|
|
|
SyslogFacility string `mapstructure:"syslog_facility"`
|
2015-09-22 17:41:12 +00:00
|
|
|
|
|
|
|
// DisableUpdateCheck is used to disable the periodic update
|
|
|
|
// and security bulletin checking.
|
2016-03-12 02:24:58 +00:00
|
|
|
DisableUpdateCheck bool `mapstructure:"disable_update_check"`
|
2015-09-22 17:41:12 +00:00
|
|
|
|
|
|
|
// DisableAnonymousSignature is used to disable setting the
|
|
|
|
// anonymous signature when doing the update check and looking
|
|
|
|
// for security bulletins
|
2016-03-12 02:24:58 +00:00
|
|
|
DisableAnonymousSignature bool `mapstructure:"disable_anonymous_signature"`
|
2015-09-06 01:41:00 +00:00
|
|
|
|
2015-09-14 22:33:08 +00:00
|
|
|
// AtlasConfig is used to configure Atlas
|
2016-03-12 02:24:58 +00:00
|
|
|
Atlas *AtlasConfig `mapstructure:"atlas"`
|
2015-09-14 22:33:08 +00:00
|
|
|
|
2016-05-22 00:39:45 +00:00
|
|
|
// Consul contains the configuration for the Consul Agent and
|
|
|
|
// parameters necessary to register services, their checks, and
|
|
|
|
// discover the current Nomad servers.
|
2016-05-22 03:15:58 +00:00
|
|
|
Consul *config.ConsulConfig `mapstructure:"consul"`
|
2016-03-31 06:20:24 +00:00
|
|
|
|
2015-09-06 01:41:00 +00:00
|
|
|
// NomadConfig is used to override the default config.
|
|
|
|
// This is largly used for testing purposes.
|
2016-03-12 02:24:58 +00:00
|
|
|
NomadConfig *nomad.Config `mapstructure:"-" json:"-"`
|
2015-09-06 01:41:00 +00:00
|
|
|
|
|
|
|
// ClientConfig is used to override the default config.
|
|
|
|
// This is largly used for testing purposes.
|
2016-03-12 02:24:58 +00:00
|
|
|
ClientConfig *client.Config `mapstructure:"-" json:"-"`
|
2015-09-22 17:41:12 +00:00
|
|
|
|
|
|
|
// DevMode is set by the -dev CLI flag.
|
2016-03-12 02:24:58 +00:00
|
|
|
DevMode bool `mapstructure:"-"`
|
2015-09-22 17:41:12 +00:00
|
|
|
|
|
|
|
// Version information is set at compilation time
|
|
|
|
Revision string
|
|
|
|
Version string
|
|
|
|
VersionPrerelease string
|
2015-12-05 00:52:17 +00:00
|
|
|
|
|
|
|
// List of config files that have been loaded (in order)
|
2016-03-12 02:24:58 +00:00
|
|
|
Files []string `mapstructure:"-"`
|
2016-01-22 18:52:20 +00:00
|
|
|
|
|
|
|
// HTTPAPIResponseHeaders allows users to configure the Nomad http agent to
|
|
|
|
// set arbritrary headers on API responses
|
2016-03-12 02:24:58 +00:00
|
|
|
HTTPAPIResponseHeaders map[string]string `mapstructure:"http_api_response_headers"`
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 22:33:08 +00:00
|
|
|
// AtlasConfig is used to enable an parameterize the Atlas integration
|
|
|
|
type AtlasConfig struct {
|
2015-09-22 17:41:12 +00:00
|
|
|
// Infrastructure is the name of the infrastructure
|
|
|
|
// we belong to. e.g. hashicorp/stage
|
2016-03-12 02:24:58 +00:00
|
|
|
Infrastructure string `mapstructure:"infrastructure"`
|
2015-09-14 22:33:08 +00:00
|
|
|
|
|
|
|
// Token is our authentication token from Atlas
|
2016-03-12 02:24:58 +00:00
|
|
|
Token string `mapstructure:"token" json:"-"`
|
2015-09-14 22:33:08 +00:00
|
|
|
|
|
|
|
// Join controls if Atlas will attempt to auto-join the node
|
|
|
|
// to it's cluster. Requires Atlas integration.
|
2016-03-12 02:24:58 +00:00
|
|
|
Join bool `mapstructure:"join"`
|
2015-09-14 22:33:08 +00:00
|
|
|
|
|
|
|
// Endpoint is the SCADA endpoint used for Atlas integration. If
|
|
|
|
// empty, the defaults from the provider are used.
|
2016-03-12 02:24:58 +00:00
|
|
|
Endpoint string `mapstructure:"endpoint"`
|
2015-09-14 22:33:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 05:30:10 +00:00
|
|
|
// StatsConfig determines behavior of resource usage stats collections
|
|
|
|
type StatsConfig struct {
|
|
|
|
|
|
|
|
// DataPoints is the number of data points Nomad client stores in-memory
|
|
|
|
DataPoints int `mapstructure:"data_points"`
|
|
|
|
|
|
|
|
// CollectionInterval is the interval of resource usage stats collection
|
|
|
|
CollectionInterval string `mapstructure:"collection_interval"`
|
|
|
|
collectionInterval time.Duration `mapstructure:"_"`
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// ClientConfig is configuration specific to the client mode
|
2015-08-16 01:54:41 +00:00
|
|
|
type ClientConfig struct {
|
|
|
|
// Enabled controls if we are a client
|
2016-03-12 02:24:58 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-08-31 01:10:23 +00:00
|
|
|
// StateDir is the state directory
|
2016-03-12 02:24:58 +00:00
|
|
|
StateDir string `mapstructure:"state_dir"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-08-31 01:10:23 +00:00
|
|
|
// AllocDir is the directory for storing allocation data
|
2016-03-12 02:24:58 +00:00
|
|
|
AllocDir string `mapstructure:"alloc_dir"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
|
|
|
// Servers is a list of known server addresses. These are as "host:port"
|
2016-03-12 02:24:58 +00:00
|
|
|
Servers []string `mapstructure:"servers"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-08-31 01:10:23 +00:00
|
|
|
// NodeClass is used to group the node by class
|
2016-03-12 02:24:58 +00:00
|
|
|
NodeClass string `mapstructure:"node_class"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
2015-09-28 23:45:32 +00:00
|
|
|
// Options is used for configuration of nomad internals,
|
|
|
|
// like fingerprinters and drivers. The format is:
|
|
|
|
//
|
|
|
|
// namespace.option = value
|
2016-03-12 02:24:58 +00:00
|
|
|
Options map[string]string `mapstructure:"options"`
|
2015-09-28 23:45:32 +00:00
|
|
|
|
2015-08-31 01:10:23 +00:00
|
|
|
// Metadata associated with the node
|
2016-03-12 02:24:58 +00:00
|
|
|
Meta map[string]string `mapstructure:"meta"`
|
2015-10-01 15:31:47 +00:00
|
|
|
|
|
|
|
// Interface to use for network fingerprinting
|
2016-03-12 02:24:58 +00:00
|
|
|
NetworkInterface string `mapstructure:"network_interface"`
|
2015-10-03 00:32:11 +00:00
|
|
|
|
|
|
|
// The network link speed to use if it can not be determined dynamically.
|
2016-03-12 02:24:58 +00:00
|
|
|
NetworkSpeed int `mapstructure:"network_speed"`
|
2015-12-23 00:10:30 +00:00
|
|
|
|
|
|
|
// MaxKillTimeout allows capping the user-specifiable KillTimeout.
|
2016-03-12 02:24:58 +00:00
|
|
|
MaxKillTimeout string `mapstructure:"max_kill_timeout"`
|
2016-02-08 21:29:53 +00:00
|
|
|
|
|
|
|
// ClientMaxPort is the upper range of the ports that the client uses for
|
|
|
|
// communicating with plugin subsystems
|
2016-03-12 02:24:58 +00:00
|
|
|
ClientMaxPort int `mapstructure:"client_max_port"`
|
2016-02-08 21:29:53 +00:00
|
|
|
|
|
|
|
// ClientMinPort is the lower range of the ports that the client uses for
|
|
|
|
// communicating with plugin subsystems
|
2016-03-12 02:24:58 +00:00
|
|
|
ClientMinPort int `mapstructure:"client_min_port"`
|
|
|
|
|
|
|
|
// Reserved is used to reserve resources from being used by Nomad. This can
|
|
|
|
// be used to target a certain utilization or to prevent Nomad from using a
|
|
|
|
// particular set of ports.
|
|
|
|
Reserved *Resources `mapstructure:"reserved"`
|
2016-05-25 05:30:10 +00:00
|
|
|
|
|
|
|
// StatsConfig determines behavior of resource usage stats collection in
|
|
|
|
// Nomad client
|
|
|
|
StatsConfig *StatsConfig `mapstructure:"stats"`
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:41:12 +00:00
|
|
|
// ServerConfig is configuration specific to the server mode
|
2015-08-16 01:54:41 +00:00
|
|
|
type ServerConfig struct {
|
|
|
|
// Enabled controls if we are a server
|
2016-03-12 02:24:58 +00:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
|
|
|
// BootstrapExpect tries to automatically bootstrap the Consul cluster,
|
2016-05-15 16:41:34 +00:00
|
|
|
// by withholding peers until enough servers join.
|
2016-03-12 02:24:58 +00:00
|
|
|
BootstrapExpect int `mapstructure:"bootstrap_expect"`
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-08-31 01:10:23 +00:00
|
|
|
// DataDir is the directory to store our state in
|
2016-03-12 02:24:58 +00:00
|
|
|
DataDir string `mapstructure:"data_dir"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
|
|
|
// ProtocolVersion is the protocol version to speak. This must be between
|
|
|
|
// ProtocolVersionMin and ProtocolVersionMax.
|
2016-03-12 02:24:58 +00:00
|
|
|
ProtocolVersion int `mapstructure:"protocol_version"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
|
|
|
// NumSchedulers is the number of scheduler thread that are run.
|
|
|
|
// This can be as many as one per core, or zero to disable this server
|
|
|
|
// from doing any scheduling work.
|
2016-03-12 02:24:58 +00:00
|
|
|
NumSchedulers int `mapstructure:"num_schedulers"`
|
2015-08-31 01:10:23 +00:00
|
|
|
|
|
|
|
// EnabledSchedulers controls the set of sub-schedulers that are
|
|
|
|
// enabled for this server to handle. This will restrict the evaluations
|
|
|
|
// that the workers dequeue for processing.
|
2016-03-12 02:24:58 +00:00
|
|
|
EnabledSchedulers []string `mapstructure:"enabled_schedulers"`
|
2015-10-29 13:47:06 +00:00
|
|
|
|
2016-03-04 23:44:12 +00:00
|
|
|
// NodeGCThreshold controls how "old" a node must be to be collected by GC.
|
2016-03-12 02:24:58 +00:00
|
|
|
NodeGCThreshold string `mapstructure:"node_gc_threshold"`
|
2015-12-02 19:55:29 +00:00
|
|
|
|
2016-03-04 23:44:12 +00:00
|
|
|
// HeartbeatGrace is the grace period beyond the TTL to account for network,
|
|
|
|
// processing delays and clock skew before marking a node as "down".
|
2016-03-12 02:24:58 +00:00
|
|
|
HeartbeatGrace string `mapstructure:"heartbeat_grace"`
|
2016-03-04 23:44:12 +00:00
|
|
|
|
2015-12-02 19:55:29 +00:00
|
|
|
// StartJoin is a list of addresses to attempt to join when the
|
|
|
|
// agent starts. If Serf is unable to communicate with any of these
|
|
|
|
// addresses, then the agent will error and exit.
|
2016-03-12 02:24:58 +00:00
|
|
|
StartJoin []string `mapstructure:"start_join"`
|
2015-12-02 19:55:29 +00:00
|
|
|
|
|
|
|
// RetryJoin is a list of addresses to join with retry enabled.
|
2016-03-12 02:24:58 +00:00
|
|
|
RetryJoin []string `mapstructure:"retry_join"`
|
2015-12-02 19:55:29 +00:00
|
|
|
|
|
|
|
// RetryMaxAttempts specifies the maximum number of times to retry joining a
|
|
|
|
// host on startup. This is useful for cases where we know the node will be
|
|
|
|
// online eventually.
|
2016-03-12 02:24:58 +00:00
|
|
|
RetryMaxAttempts int `mapstructure:"retry_max"`
|
2015-12-02 19:55:29 +00:00
|
|
|
|
|
|
|
// RetryInterval specifies the amount of time to wait in between join
|
|
|
|
// attempts on agent start. The minimum allowed value is 1 second and
|
|
|
|
// the default is 30s.
|
2016-03-12 02:24:58 +00:00
|
|
|
RetryInterval string `mapstructure:"retry_interval"`
|
|
|
|
retryInterval time.Duration `mapstructure:"-"`
|
2015-12-02 19:55:29 +00:00
|
|
|
|
|
|
|
// RejoinAfterLeave controls our interaction with the cluster after leave.
|
|
|
|
// When set to false (default), a leave causes Consul to not rejoin
|
|
|
|
// the cluster until an explicit join is received. If this is set to
|
|
|
|
// true, we ignore the leave, and rejoin the cluster on start.
|
2016-03-12 02:24:58 +00:00
|
|
|
RejoinAfterLeave bool `mapstructure:"rejoin_after_leave"`
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Telemetry is the telemetry configuration for the server
|
|
|
|
type Telemetry struct {
|
2016-03-12 02:24:58 +00:00
|
|
|
StatsiteAddr string `mapstructure:"statsite_address"`
|
|
|
|
StatsdAddr string `mapstructure:"statsd_address"`
|
|
|
|
DisableHostname bool `mapstructure:"disable_hostname"`
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 01:37:42 +00:00
|
|
|
// Ports is used to encapsulate the various ports we bind to for network
|
|
|
|
// services. If any are not specified then the defaults are used instead.
|
|
|
|
type Ports struct {
|
2016-03-12 02:24:58 +00:00
|
|
|
HTTP int `mapstructure:"http"`
|
|
|
|
RPC int `mapstructure:"rpc"`
|
|
|
|
Serf int `mapstructure:"serf"`
|
2015-09-11 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addresses encapsulates all of the addresses we bind to for various
|
|
|
|
// network services. Everything is optional and defaults to BindAddr.
|
|
|
|
type Addresses struct {
|
2016-03-12 02:24:58 +00:00
|
|
|
HTTP string `mapstructure:"http"`
|
|
|
|
RPC string `mapstructure:"rpc"`
|
|
|
|
Serf string `mapstructure:"serf"`
|
2015-09-11 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AdvertiseAddrs is used to control the addresses we advertise out for
|
|
|
|
// different network services. Not all network services support an
|
|
|
|
// advertise address. All are optional and default to BindAddr.
|
|
|
|
type AdvertiseAddrs struct {
|
2016-03-12 02:24:58 +00:00
|
|
|
HTTP string `mapstructure:"http"`
|
|
|
|
RPC string `mapstructure:"rpc"`
|
|
|
|
Serf string `mapstructure:"serf"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resources struct {
|
2016-03-12 03:02:44 +00:00
|
|
|
CPU int `mapstructure:"cpu"`
|
|
|
|
MemoryMB int `mapstructure:"memory"`
|
|
|
|
DiskMB int `mapstructure:"disk"`
|
|
|
|
IOPS int `mapstructure:"iops"`
|
2016-03-12 02:24:58 +00:00
|
|
|
ReservedPorts string `mapstructure:"reserved_ports"`
|
|
|
|
ParsedReservedPorts []int `mapstructure:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseReserved expands the ReservedPorts string into a slice of port numbers.
|
|
|
|
// The supported syntax is comma seperated integers or ranges seperated by
|
|
|
|
// hyphens. For example, "80,120-150,160"
|
2016-03-12 03:02:44 +00:00
|
|
|
func (r *Resources) ParseReserved() error {
|
|
|
|
parts := strings.Split(r.ReservedPorts, ",")
|
2016-03-12 02:24:58 +00:00
|
|
|
|
|
|
|
// Hot path the empty case
|
|
|
|
if len(parts) == 1 && parts[0] == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ports := make(map[int]struct{})
|
|
|
|
for _, part := range parts {
|
|
|
|
part = strings.TrimSpace(part)
|
|
|
|
rangeParts := strings.Split(part, "-")
|
|
|
|
l := len(rangeParts)
|
|
|
|
switch l {
|
|
|
|
case 1:
|
|
|
|
if val := rangeParts[0]; val == "" {
|
|
|
|
return fmt.Errorf("can't specify empty port")
|
|
|
|
} else {
|
|
|
|
port, err := strconv.Atoi(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ports[port] = struct{}{}
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
// We are parsing a range
|
|
|
|
start, err := strconv.Atoi(rangeParts[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
end, err := strconv.Atoi(rangeParts[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if end < start {
|
|
|
|
return fmt.Errorf("invalid range: starting value (%v) less than ending (%v) value", end, start)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
ports[i] = struct{}{}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("can only parse single port numbers or port ranges (ex. 80,100-120,150)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for port := range ports {
|
2016-03-12 03:02:44 +00:00
|
|
|
r.ParsedReservedPorts = append(r.ParsedReservedPorts, port)
|
2016-03-12 02:24:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 03:02:44 +00:00
|
|
|
sort.Ints(r.ParsedReservedPorts)
|
2016-03-12 02:24:58 +00:00
|
|
|
return nil
|
2015-09-11 01:37:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
// DevConfig is a Config that is used for dev mode of Nomad.
|
|
|
|
func DevConfig() *Config {
|
2015-09-06 00:06:05 +00:00
|
|
|
conf := DefaultConfig()
|
|
|
|
conf.LogLevel = "DEBUG"
|
|
|
|
conf.Client.Enabled = true
|
|
|
|
conf.Server.Enabled = true
|
|
|
|
conf.DevMode = true
|
|
|
|
conf.EnableDebug = true
|
|
|
|
conf.DisableAnonymousSignature = true
|
2016-05-24 09:17:20 +00:00
|
|
|
conf.Consul.AutoRegister = true
|
2015-10-29 12:30:26 +00:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
conf.Client.NetworkInterface = "lo0"
|
2015-10-30 00:03:54 +00:00
|
|
|
} else if runtime.GOOS == "linux" {
|
2015-10-29 12:30:26 +00:00
|
|
|
conf.Client.NetworkInterface = "lo"
|
|
|
|
}
|
2015-12-10 23:24:21 +00:00
|
|
|
conf.Client.Options = map[string]string{
|
|
|
|
"driver.raw_exec.enable": "true",
|
|
|
|
}
|
2015-10-29 12:30:26 +00:00
|
|
|
|
2015-09-06 00:06:05 +00:00
|
|
|
return conf
|
2015-08-16 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig is a the baseline configuration for Nomad
|
|
|
|
func DefaultConfig() *Config {
|
|
|
|
return &Config{
|
2015-09-11 19:19:05 +00:00
|
|
|
LogLevel: "INFO",
|
2015-09-14 01:18:40 +00:00
|
|
|
Region: "global",
|
2015-09-11 19:19:05 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
BindAddr: "127.0.0.1",
|
|
|
|
Ports: &Ports{
|
|
|
|
HTTP: 4646,
|
|
|
|
RPC: 4647,
|
|
|
|
Serf: 4648,
|
|
|
|
},
|
2015-09-11 01:37:42 +00:00
|
|
|
Addresses: &Addresses{},
|
|
|
|
AdvertiseAddrs: &AdvertiseAddrs{},
|
2015-09-22 04:37:55 +00:00
|
|
|
Atlas: &AtlasConfig{},
|
2016-05-22 03:15:58 +00:00
|
|
|
Consul: &config.ConsulConfig{
|
2016-05-27 22:57:04 +00:00
|
|
|
ServerServiceName: "nomad",
|
2016-05-13 17:19:49 +00:00
|
|
|
ClientServiceName: "nomad-client",
|
2016-05-22 03:15:58 +00:00
|
|
|
AutoRegister: true,
|
2016-05-26 20:31:32 +00:00
|
|
|
Timeout: 500 * time.Millisecond,
|
2016-05-13 17:19:49 +00:00
|
|
|
},
|
2015-09-06 00:06:05 +00:00
|
|
|
Client: &ClientConfig{
|
2015-12-23 00:10:30 +00:00
|
|
|
Enabled: false,
|
|
|
|
NetworkSpeed: 100,
|
|
|
|
MaxKillTimeout: "30s",
|
2016-02-08 21:29:53 +00:00
|
|
|
ClientMinPort: 14000,
|
2016-02-20 19:43:02 +00:00
|
|
|
ClientMaxPort: 14512,
|
2016-03-14 02:05:41 +00:00
|
|
|
Reserved: &Resources{},
|
2016-05-25 05:30:10 +00:00
|
|
|
StatsConfig: &StatsConfig{
|
|
|
|
DataPoints: 60,
|
|
|
|
CollectionInterval: "1s",
|
2016-05-26 07:53:41 +00:00
|
|
|
collectionInterval: 1 * time.Second,
|
2016-05-25 05:30:10 +00:00
|
|
|
},
|
2015-09-06 00:06:05 +00:00
|
|
|
},
|
|
|
|
Server: &ServerConfig{
|
2015-12-02 21:50:57 +00:00
|
|
|
Enabled: false,
|
|
|
|
StartJoin: []string{},
|
|
|
|
RetryJoin: []string{},
|
2015-12-02 22:05:05 +00:00
|
|
|
RetryInterval: "30s",
|
2015-12-02 21:50:57 +00:00
|
|
|
RetryMaxAttempts: 0,
|
2015-09-06 00:06:05 +00:00
|
|
|
},
|
2015-12-08 23:57:06 +00:00
|
|
|
SyslogFacility: "LOCAL0",
|
2015-08-16 20:54:49 +00:00
|
|
|
}
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 00:05:15 +00:00
|
|
|
// Listener can be used to get a new listener using a custom bind address.
|
2015-09-11 00:48:02 +00:00
|
|
|
// If the bind provided address is empty, the BindAddr is used instead.
|
|
|
|
func (c *Config) Listener(proto, addr string, port int) (net.Listener, error) {
|
|
|
|
if addr == "" {
|
|
|
|
addr = c.BindAddr
|
|
|
|
}
|
2015-12-02 18:57:40 +00:00
|
|
|
|
|
|
|
// Do our own range check to avoid bugs in package net.
|
|
|
|
//
|
|
|
|
// golang.org/issue/11715
|
|
|
|
// golang.org/issue/13447
|
|
|
|
//
|
|
|
|
// Both of the above bugs were fixed by golang.org/cl/12447 which will be
|
|
|
|
// included in Go 1.6. The error returned below is the same as what Go 1.6
|
|
|
|
// will return.
|
|
|
|
if 0 > port || port > 65535 {
|
|
|
|
return nil, &net.OpError{
|
|
|
|
Op: "listen",
|
|
|
|
Net: proto,
|
|
|
|
Err: &net.AddrError{Err: "invalid port", Addr: fmt.Sprint(port)},
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 00:48:02 +00:00
|
|
|
return net.Listen(proto, fmt.Sprintf("%s:%d", addr, port))
|
|
|
|
}
|
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
// Merge merges two configurations.
|
2015-12-09 00:05:15 +00:00
|
|
|
func (c *Config) Merge(b *Config) *Config {
|
|
|
|
result := *c
|
2015-08-16 01:54:41 +00:00
|
|
|
|
2015-09-10 04:42:50 +00:00
|
|
|
if b.Region != "" {
|
|
|
|
result.Region = b.Region
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
2015-09-10 04:42:50 +00:00
|
|
|
if b.Datacenter != "" {
|
|
|
|
result.Datacenter = b.Datacenter
|
|
|
|
}
|
|
|
|
if b.NodeName != "" {
|
|
|
|
result.NodeName = b.NodeName
|
|
|
|
}
|
|
|
|
if b.DataDir != "" {
|
|
|
|
result.DataDir = b.DataDir
|
|
|
|
}
|
|
|
|
if b.LogLevel != "" {
|
|
|
|
result.LogLevel = b.LogLevel
|
|
|
|
}
|
2015-09-11 00:48:02 +00:00
|
|
|
if b.BindAddr != "" {
|
|
|
|
result.BindAddr = b.BindAddr
|
|
|
|
}
|
2015-09-10 04:42:50 +00:00
|
|
|
if b.EnableDebug {
|
|
|
|
result.EnableDebug = true
|
|
|
|
}
|
|
|
|
if b.LeaveOnInt {
|
|
|
|
result.LeaveOnInt = true
|
|
|
|
}
|
|
|
|
if b.LeaveOnTerm {
|
|
|
|
result.LeaveOnTerm = true
|
|
|
|
}
|
|
|
|
if b.EnableSyslog {
|
|
|
|
result.EnableSyslog = true
|
|
|
|
}
|
|
|
|
if b.SyslogFacility != "" {
|
|
|
|
result.SyslogFacility = b.SyslogFacility
|
|
|
|
}
|
|
|
|
if b.DisableUpdateCheck {
|
|
|
|
result.DisableUpdateCheck = true
|
|
|
|
}
|
|
|
|
if b.DisableAnonymousSignature {
|
|
|
|
result.DisableAnonymousSignature = true
|
|
|
|
}
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
// Apply the telemetry config
|
|
|
|
if result.Telemetry == nil && b.Telemetry != nil {
|
|
|
|
telemetry := *b.Telemetry
|
|
|
|
result.Telemetry = &telemetry
|
|
|
|
} else if b.Telemetry != nil {
|
|
|
|
result.Telemetry = result.Telemetry.Merge(b.Telemetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the client config
|
|
|
|
if result.Client == nil && b.Client != nil {
|
|
|
|
client := *b.Client
|
|
|
|
result.Client = &client
|
|
|
|
} else if b.Client != nil {
|
|
|
|
result.Client = result.Client.Merge(b.Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the server config
|
|
|
|
if result.Server == nil && b.Server != nil {
|
|
|
|
server := *b.Server
|
|
|
|
result.Server = &server
|
|
|
|
} else if b.Server != nil {
|
|
|
|
result.Server = result.Server.Merge(b.Server)
|
|
|
|
}
|
|
|
|
|
2015-09-11 01:37:42 +00:00
|
|
|
// Apply the ports config
|
|
|
|
if result.Ports == nil && b.Ports != nil {
|
|
|
|
ports := *b.Ports
|
|
|
|
result.Ports = &ports
|
|
|
|
} else if b.Ports != nil {
|
|
|
|
result.Ports = result.Ports.Merge(b.Ports)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the address config
|
|
|
|
if result.Addresses == nil && b.Addresses != nil {
|
|
|
|
addrs := *b.Addresses
|
|
|
|
result.Addresses = &addrs
|
|
|
|
} else if b.Addresses != nil {
|
|
|
|
result.Addresses = result.Addresses.Merge(b.Addresses)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the advertise addrs config
|
|
|
|
if result.AdvertiseAddrs == nil && b.AdvertiseAddrs != nil {
|
|
|
|
advertise := *b.AdvertiseAddrs
|
|
|
|
result.AdvertiseAddrs = &advertise
|
|
|
|
} else if b.AdvertiseAddrs != nil {
|
|
|
|
result.AdvertiseAddrs = result.AdvertiseAddrs.Merge(b.AdvertiseAddrs)
|
|
|
|
}
|
|
|
|
|
2015-11-30 23:21:50 +00:00
|
|
|
// Apply the Atlas configuration
|
|
|
|
if result.Atlas == nil && b.Atlas != nil {
|
|
|
|
atlasConfig := *b.Atlas
|
|
|
|
result.Atlas = &atlasConfig
|
|
|
|
} else if b.Atlas != nil {
|
|
|
|
result.Atlas = result.Atlas.Merge(b.Atlas)
|
|
|
|
}
|
|
|
|
|
2016-05-11 22:24:37 +00:00
|
|
|
// Apply the Consul Configuration
|
2016-05-22 00:39:45 +00:00
|
|
|
if result.Consul == nil && b.Consul != nil {
|
|
|
|
consulConfig := *b.Consul
|
|
|
|
result.Consul = &consulConfig
|
|
|
|
} else if b.Consul != nil {
|
|
|
|
result.Consul = result.Consul.Merge(b.Consul)
|
2016-05-11 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 00:52:17 +00:00
|
|
|
// Merge config files lists
|
2015-12-05 00:59:04 +00:00
|
|
|
result.Files = append(result.Files, b.Files...)
|
2015-12-05 00:52:17 +00:00
|
|
|
|
2016-05-17 03:57:54 +00:00
|
|
|
// Add the http API response header map values
|
|
|
|
if result.HTTPAPIResponseHeaders == nil {
|
|
|
|
result.HTTPAPIResponseHeaders = make(map[string]string)
|
|
|
|
}
|
|
|
|
for k, v := range b.HTTPAPIResponseHeaders {
|
|
|
|
result.HTTPAPIResponseHeaders[k] = v
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:24:59 +00:00
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge is used to merge two server configs together
|
|
|
|
func (a *ServerConfig) Merge(b *ServerConfig) *ServerConfig {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
if b.Enabled {
|
|
|
|
result.Enabled = true
|
|
|
|
}
|
|
|
|
if b.BootstrapExpect > 0 {
|
|
|
|
result.BootstrapExpect = b.BootstrapExpect
|
|
|
|
}
|
|
|
|
if b.DataDir != "" {
|
|
|
|
result.DataDir = b.DataDir
|
|
|
|
}
|
|
|
|
if b.ProtocolVersion != 0 {
|
|
|
|
result.ProtocolVersion = b.ProtocolVersion
|
|
|
|
}
|
|
|
|
if b.NumSchedulers != 0 {
|
|
|
|
result.NumSchedulers = b.NumSchedulers
|
|
|
|
}
|
2015-10-29 13:47:06 +00:00
|
|
|
if b.NodeGCThreshold != "" {
|
|
|
|
result.NodeGCThreshold = b.NodeGCThreshold
|
|
|
|
}
|
2016-03-04 23:44:12 +00:00
|
|
|
if b.HeartbeatGrace != "" {
|
|
|
|
result.HeartbeatGrace = b.HeartbeatGrace
|
|
|
|
}
|
2015-12-02 19:55:29 +00:00
|
|
|
if b.RetryMaxAttempts != 0 {
|
|
|
|
result.RetryMaxAttempts = b.RetryMaxAttempts
|
|
|
|
}
|
|
|
|
if b.RetryInterval != "" {
|
|
|
|
result.RetryInterval = b.RetryInterval
|
|
|
|
result.retryInterval = b.retryInterval
|
|
|
|
}
|
|
|
|
if b.RejoinAfterLeave {
|
|
|
|
result.RejoinAfterLeave = true
|
|
|
|
}
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
// Add the schedulers
|
|
|
|
result.EnabledSchedulers = append(result.EnabledSchedulers, b.EnabledSchedulers...)
|
|
|
|
|
2015-12-02 19:55:29 +00:00
|
|
|
// Copy the start join addresses
|
|
|
|
result.StartJoin = make([]string, 0, len(a.StartJoin)+len(b.StartJoin))
|
|
|
|
result.StartJoin = append(result.StartJoin, a.StartJoin...)
|
|
|
|
result.StartJoin = append(result.StartJoin, b.StartJoin...)
|
|
|
|
|
|
|
|
// Copy the retry join addresses
|
|
|
|
result.RetryJoin = make([]string, 0, len(a.RetryJoin)+len(b.RetryJoin))
|
|
|
|
result.RetryJoin = append(result.RetryJoin, a.RetryJoin...)
|
|
|
|
result.RetryJoin = append(result.RetryJoin, b.RetryJoin...)
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge is used to merge two client configs together
|
|
|
|
func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
if b.Enabled {
|
|
|
|
result.Enabled = true
|
|
|
|
}
|
|
|
|
if b.StateDir != "" {
|
|
|
|
result.StateDir = b.StateDir
|
|
|
|
}
|
|
|
|
if b.AllocDir != "" {
|
|
|
|
result.AllocDir = b.AllocDir
|
|
|
|
}
|
|
|
|
if b.NodeClass != "" {
|
|
|
|
result.NodeClass = b.NodeClass
|
|
|
|
}
|
2015-10-02 07:29:18 +00:00
|
|
|
if b.NetworkInterface != "" {
|
|
|
|
result.NetworkInterface = b.NetworkInterface
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
2015-10-03 00:32:11 +00:00
|
|
|
if b.NetworkSpeed != 0 {
|
|
|
|
result.NetworkSpeed = b.NetworkSpeed
|
|
|
|
}
|
2015-12-23 00:10:30 +00:00
|
|
|
if b.MaxKillTimeout != "" {
|
|
|
|
result.MaxKillTimeout = b.MaxKillTimeout
|
|
|
|
}
|
2016-02-08 21:29:53 +00:00
|
|
|
if b.ClientMaxPort != 0 {
|
|
|
|
result.ClientMaxPort = b.ClientMaxPort
|
|
|
|
}
|
|
|
|
if b.ClientMinPort != 0 {
|
|
|
|
result.ClientMinPort = b.ClientMinPort
|
|
|
|
}
|
2016-03-12 02:24:58 +00:00
|
|
|
if b.Reserved != nil {
|
|
|
|
result.Reserved = result.Reserved.Merge(b.Reserved)
|
|
|
|
}
|
2016-05-25 05:30:10 +00:00
|
|
|
if b.StatsConfig != nil {
|
|
|
|
result.StatsConfig = result.StatsConfig.Merge(b.StatsConfig)
|
|
|
|
}
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
// Add the servers
|
|
|
|
result.Servers = append(result.Servers, b.Servers...)
|
|
|
|
|
2015-09-28 23:45:32 +00:00
|
|
|
// Add the options map values
|
|
|
|
if result.Options == nil {
|
|
|
|
result.Options = make(map[string]string)
|
|
|
|
}
|
|
|
|
for k, v := range b.Options {
|
|
|
|
result.Options[k] = v
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:24:59 +00:00
|
|
|
// Add the meta map values
|
|
|
|
if result.Meta == nil {
|
|
|
|
result.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
for k, v := range b.Meta {
|
|
|
|
result.Meta[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge is used to merge two telemetry configs together
|
|
|
|
func (a *Telemetry) Merge(b *Telemetry) *Telemetry {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-09-10 18:24:59 +00:00
|
|
|
|
|
|
|
if b.StatsiteAddr != "" {
|
|
|
|
result.StatsiteAddr = b.StatsiteAddr
|
|
|
|
}
|
|
|
|
if b.StatsdAddr != "" {
|
|
|
|
result.StatsdAddr = b.StatsdAddr
|
|
|
|
}
|
|
|
|
if b.DisableHostname {
|
|
|
|
result.DisableHostname = true
|
|
|
|
}
|
2015-09-10 04:42:50 +00:00
|
|
|
return &result
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 01:37:42 +00:00
|
|
|
// Merge is used to merge two port configurations.
|
|
|
|
func (a *Ports) Merge(b *Ports) *Ports {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-09-11 01:37:42 +00:00
|
|
|
|
|
|
|
if b.HTTP != 0 {
|
|
|
|
result.HTTP = b.HTTP
|
|
|
|
}
|
|
|
|
if b.RPC != 0 {
|
|
|
|
result.RPC = b.RPC
|
|
|
|
}
|
|
|
|
if b.Serf != 0 {
|
|
|
|
result.Serf = b.Serf
|
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge is used to merge two address configs together.
|
|
|
|
func (a *Addresses) Merge(b *Addresses) *Addresses {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-09-11 01:37:42 +00:00
|
|
|
|
|
|
|
if b.HTTP != "" {
|
|
|
|
result.HTTP = b.HTTP
|
|
|
|
}
|
|
|
|
if b.RPC != "" {
|
|
|
|
result.RPC = b.RPC
|
|
|
|
}
|
|
|
|
if b.Serf != "" {
|
|
|
|
result.Serf = b.Serf
|
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges two advertise addrs configs together.
|
|
|
|
func (a *AdvertiseAddrs) Merge(b *AdvertiseAddrs) *AdvertiseAddrs {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-09-11 01:37:42 +00:00
|
|
|
|
|
|
|
if b.RPC != "" {
|
|
|
|
result.RPC = b.RPC
|
|
|
|
}
|
|
|
|
if b.Serf != "" {
|
|
|
|
result.Serf = b.Serf
|
|
|
|
}
|
2016-02-12 17:40:28 +00:00
|
|
|
if b.HTTP != "" {
|
|
|
|
result.HTTP = b.HTTP
|
|
|
|
}
|
2015-09-11 01:37:42 +00:00
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2015-11-30 23:21:50 +00:00
|
|
|
// Merge merges two Atlas configurations together.
|
|
|
|
func (a *AtlasConfig) Merge(b *AtlasConfig) *AtlasConfig {
|
2015-12-09 00:05:15 +00:00
|
|
|
result := *a
|
2015-11-30 23:21:50 +00:00
|
|
|
|
|
|
|
if b.Infrastructure != "" {
|
|
|
|
result.Infrastructure = b.Infrastructure
|
|
|
|
}
|
|
|
|
if b.Token != "" {
|
|
|
|
result.Token = b.Token
|
|
|
|
}
|
|
|
|
if b.Join {
|
|
|
|
result.Join = true
|
|
|
|
}
|
|
|
|
if b.Endpoint != "" {
|
|
|
|
result.Endpoint = b.Endpoint
|
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2016-03-12 02:24:58 +00:00
|
|
|
func (r *Resources) Merge(b *Resources) *Resources {
|
|
|
|
result := *r
|
|
|
|
if b.CPU != 0 {
|
|
|
|
result.CPU = b.CPU
|
|
|
|
}
|
|
|
|
if b.MemoryMB != 0 {
|
|
|
|
result.MemoryMB = b.MemoryMB
|
|
|
|
}
|
|
|
|
if b.DiskMB != 0 {
|
|
|
|
result.DiskMB = b.DiskMB
|
|
|
|
}
|
|
|
|
if b.IOPS != 0 {
|
|
|
|
result.IOPS = b.IOPS
|
|
|
|
}
|
2016-03-12 03:02:44 +00:00
|
|
|
if b.ReservedPorts != "" {
|
|
|
|
result.ReservedPorts = b.ReservedPorts
|
2016-03-12 02:24:58 +00:00
|
|
|
}
|
2016-03-12 03:02:44 +00:00
|
|
|
if len(b.ParsedReservedPorts) != 0 {
|
|
|
|
result.ParsedReservedPorts = b.ParsedReservedPorts
|
2016-03-12 02:24:58 +00:00
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2016-05-25 05:30:10 +00:00
|
|
|
func (s *StatsConfig) Merge(b *StatsConfig) *StatsConfig {
|
|
|
|
result := *s
|
|
|
|
if b.DataPoints != 0 {
|
|
|
|
result.DataPoints = b.DataPoints
|
|
|
|
}
|
|
|
|
if b.CollectionInterval != "" {
|
|
|
|
result.CollectionInterval = b.CollectionInterval
|
|
|
|
result.collectionInterval = b.collectionInterval
|
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
// LoadConfig loads the configuration at the given path, regardless if
|
|
|
|
// its a file or directory.
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
return LoadConfigDir(path)
|
|
|
|
}
|
|
|
|
|
2016-03-12 02:24:58 +00:00
|
|
|
cleaned := filepath.Clean(path)
|
|
|
|
config, err := ParseConfigFile(cleaned)
|
2015-08-16 01:54:41 +00:00
|
|
|
if err != nil {
|
2016-03-12 02:24:58 +00:00
|
|
|
return nil, fmt.Errorf("Error loading %s: %s", cleaned, err)
|
2015-12-05 00:52:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 02:24:58 +00:00
|
|
|
config.Files = append(config.Files, cleaned)
|
|
|
|
return config, nil
|
2015-09-22 18:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
// LoadConfigDir loads all the configurations in the given directory
|
|
|
|
// in alphabetical order.
|
|
|
|
func LoadConfigDir(dir string) (*Config, error) {
|
|
|
|
f, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, fmt.Errorf(
|
2016-03-12 02:24:58 +00:00
|
|
|
"configuration path must be a directory: %s", dir)
|
2015-08-16 01:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
err = nil
|
|
|
|
for err != io.EOF {
|
|
|
|
var fis []os.FileInfo
|
|
|
|
fis, err = f.Readdir(128)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
// Ignore directories
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only care about files that are valid to load.
|
|
|
|
name := fi.Name()
|
|
|
|
skip := true
|
|
|
|
if strings.HasSuffix(name, ".hcl") {
|
|
|
|
skip = false
|
|
|
|
} else if strings.HasSuffix(name, ".json") {
|
|
|
|
skip = false
|
|
|
|
}
|
|
|
|
if skip || isTemporaryFile(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(dir, name)
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 22:07:41 +00:00
|
|
|
// Fast-path if we have no files
|
|
|
|
if len(files) == 0 {
|
|
|
|
return &Config{}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-15 07:05:25 +00:00
|
|
|
sort.Strings(files)
|
|
|
|
|
2015-08-16 01:54:41 +00:00
|
|
|
var result *Config
|
|
|
|
for _, f := range files {
|
2016-03-12 02:24:58 +00:00
|
|
|
config, err := ParseConfigFile(f)
|
2015-08-16 01:54:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error loading %s: %s", f, err)
|
|
|
|
}
|
2016-03-12 02:24:58 +00:00
|
|
|
config.Files = append(config.Files, f)
|
2015-08-16 01:54:41 +00:00
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
result = config
|
|
|
|
} else {
|
|
|
|
result = result.Merge(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isTemporaryFile returns true or false depending on whether the
|
|
|
|
// provided file name is a temporary file for the following editors:
|
|
|
|
// emacs or vim.
|
|
|
|
func isTemporaryFile(name string) bool {
|
|
|
|
return strings.HasSuffix(name, "~") || // vim
|
|
|
|
strings.HasPrefix(name, ".#") || // emacs
|
|
|
|
(strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#")) // emacs
|
|
|
|
}
|