github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/client/config/config.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "strconv" 8 "strings" 9 "time" 10 11 "github.com/hashicorp/nomad/nomad/structs" 12 "github.com/hashicorp/nomad/nomad/structs/config" 13 ) 14 15 var ( 16 // DefaultEnvBlacklist is the default set of environment variables that are 17 // filtered when passing the environment variables of the host to a task. 18 DefaultEnvBlacklist = strings.Join([]string{ 19 "CONSUL_TOKEN", 20 "VAULT_TOKEN", 21 "ATLAS_TOKEN", 22 "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", 23 "GOOGLE_APPLICATION_CREDENTIALS", 24 }, ",") 25 26 // DefaulUserBlacklist is the default set of users that tasks are not 27 // allowed to run as when using a driver in "user.checked_drivers" 28 DefaultUserBlacklist = strings.Join([]string{ 29 "root", 30 "Administrator", 31 }, ",") 32 33 // DefaultUserCheckedDrivers is the set of drivers we apply the user 34 // blacklist onto. For virtualized drivers it often doesn't make sense to 35 // make this stipulation so by default they are ignored. 36 DefaultUserCheckedDrivers = strings.Join([]string{ 37 "exec", 38 "qemu", 39 "java", 40 }, ",") 41 ) 42 43 // RPCHandler can be provided to the Client if there is a local server 44 // to avoid going over the network. If not provided, the Client will 45 // maintain a connection pool to the servers 46 type RPCHandler interface { 47 RPC(method string, args interface{}, reply interface{}) error 48 } 49 50 // Config is used to parameterize and configure the behavior of the client 51 type Config struct { 52 // DevMode controls if we are in a development mode which 53 // avoids persistent storage. 54 DevMode bool 55 56 // StateDir is where we store our state 57 StateDir string 58 59 // AllocDir is where we store data for allocations 60 AllocDir string 61 62 // LogOutput is the destination for logs 63 LogOutput io.Writer 64 65 // Region is the clients region 66 Region string 67 68 // Network interface to be used in network fingerprinting 69 NetworkInterface string 70 71 // Network speed is the default speed of network interfaces if they can not 72 // be determined dynamically. 73 NetworkSpeed int 74 75 // MaxKillTimeout allows capping the user-specifiable KillTimeout. If the 76 // task's KillTimeout is greater than the MaxKillTimeout, MaxKillTimeout is 77 // used. 78 MaxKillTimeout time.Duration 79 80 // Servers is a list of known server addresses. These are as "host:port" 81 Servers []string 82 83 // RPCHandler can be provided to avoid network traffic if the 84 // server is running locally. 85 RPCHandler RPCHandler 86 87 // Node provides the base node 88 Node *structs.Node 89 90 // ClientMaxPort is the upper range of the ports that the client uses for 91 // communicating with plugin subsystems over loopback 92 ClientMaxPort uint 93 94 // ClientMinPort is the lower range of the ports that the client uses for 95 // communicating with plugin subsystems over loopback 96 ClientMinPort uint 97 98 // GloballyReservedPorts are ports that are reserved across all network 99 // devices and IPs. 100 GloballyReservedPorts []int 101 102 // A mapping of directories on the host OS to attempt to embed inside each 103 // task's chroot. 104 ChrootEnv map[string]string 105 106 // Options provides arbitrary key-value configuration for nomad internals, 107 // like fingerprinters and drivers. The format is: 108 // 109 // namespace.option = value 110 Options map[string]string 111 112 // Version is the version of the Nomad client 113 Version string 114 115 // Revision is the commit number of the Nomad client 116 Revision string 117 118 // ConsulConfig is this Agent's Consul configuration 119 ConsulConfig *config.ConsulConfig 120 121 // VaultConfig is this Agent's Vault configuration 122 VaultConfig *config.VaultConfig 123 124 // StatsCollectionInterval is the interval at which the Nomad client 125 // collects resource usage stats 126 StatsCollectionInterval time.Duration 127 128 // PublishNodeMetrics determines whether nomad is going to publish node 129 // level metrics to remote Telemetry sinks 130 PublishNodeMetrics bool 131 132 // PublishAllocationMetrics determines whether nomad is going to publish 133 // allocation metrics to remote Telemetry sinks 134 PublishAllocationMetrics bool 135 } 136 137 func (c *Config) Copy() *Config { 138 nc := new(Config) 139 *nc = *c 140 nc.Node = nc.Node.Copy() 141 nc.Servers = structs.CopySliceString(nc.Servers) 142 nc.Options = structs.CopyMapStringString(nc.Options) 143 nc.GloballyReservedPorts = structs.CopySliceInt(c.GloballyReservedPorts) 144 nc.ConsulConfig = c.ConsulConfig.Copy() 145 nc.VaultConfig = c.VaultConfig.Copy() 146 return nc 147 } 148 149 // DefaultConfig returns the default configuration 150 func DefaultConfig() *Config { 151 return &Config{ 152 VaultConfig: config.DefaultVaultConfig(), 153 ConsulConfig: config.DefaultConsulConfig(), 154 LogOutput: os.Stderr, 155 Region: "global", 156 StatsCollectionInterval: 1 * time.Second, 157 } 158 } 159 160 // Read returns the specified configuration value or "". 161 func (c *Config) Read(id string) string { 162 return c.Options[id] 163 } 164 165 // ReadDefault returns the specified configuration value, or the specified 166 // default value if none is set. 167 func (c *Config) ReadDefault(id string, defaultValue string) string { 168 val, ok := c.Options[id] 169 if !ok { 170 return defaultValue 171 } 172 return val 173 } 174 175 // ReadBool parses the specified option as a boolean. 176 func (c *Config) ReadBool(id string) (bool, error) { 177 val, ok := c.Options[id] 178 if !ok { 179 return false, fmt.Errorf("Specified config is missing from options") 180 } 181 bval, err := strconv.ParseBool(val) 182 if err != nil { 183 return false, fmt.Errorf("Failed to parse %s as bool: %s", val, err) 184 } 185 return bval, nil 186 } 187 188 // ReadBoolDefault tries to parse the specified option as a boolean. If there is 189 // an error in parsing, the default option is returned. 190 func (c *Config) ReadBoolDefault(id string, defaultValue bool) bool { 191 val, err := c.ReadBool(id) 192 if err != nil { 193 return defaultValue 194 } 195 return val 196 } 197 198 // ReadStringListToMap tries to parse the specified option as a comma separated list. 199 // If there is an error in parsing, an empty list is returned. 200 func (c *Config) ReadStringListToMap(key string) map[string]struct{} { 201 s := strings.TrimSpace(c.Read(key)) 202 list := make(map[string]struct{}) 203 if s != "" { 204 for _, e := range strings.Split(s, ",") { 205 trimmed := strings.TrimSpace(e) 206 list[trimmed] = struct{}{} 207 } 208 } 209 return list 210 } 211 212 // ReadStringListToMap tries to parse the specified option as a comma separated list. 213 // If there is an error in parsing, an empty list is returned. 214 func (c *Config) ReadStringListToMapDefault(key, defaultValue string) map[string]struct{} { 215 val, ok := c.Options[key] 216 if !ok { 217 val = defaultValue 218 } 219 220 list := make(map[string]struct{}) 221 if val != "" { 222 for _, e := range strings.Split(val, ",") { 223 trimmed := strings.TrimSpace(e) 224 list[trimmed] = struct{}{} 225 } 226 } 227 return list 228 }