github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/client/config/config.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io" 6 "strconv" 7 "strings" 8 "time" 9 10 "github.com/hashicorp/nomad/nomad/structs" 11 ) 12 13 // RPCHandler can be provided to the Client if there is a local server 14 // to avoid going over the network. If not provided, the Client will 15 // maintain a connection pool to the servers 16 type RPCHandler interface { 17 RPC(method string, args interface{}, reply interface{}) error 18 } 19 20 // Config is used to parameterize and configure the behavior of the client 21 type Config struct { 22 // DevMode controls if we are in a development mode which 23 // avoids persistent storage. 24 DevMode bool 25 26 // StateDir is where we store our state 27 StateDir string 28 29 // AllocDir is where we store data for allocations 30 AllocDir string 31 32 // LogOutput is the destination for logs 33 LogOutput io.Writer 34 35 // Region is the clients region 36 Region string 37 38 // Network interface to be used in network fingerprinting 39 NetworkInterface string 40 41 // Network speed is the default speed of network interfaces if they can not 42 // be determined dynamically. 43 NetworkSpeed int 44 45 // MaxKillTimeout allows capping the user-specifiable KillTimeout. If the 46 // task's KillTimeout is greater than the MaxKillTimeout, MaxKillTimeout is 47 // used. 48 MaxKillTimeout time.Duration 49 50 // Servers is a list of known server addresses. These are as "host:port" 51 Servers []string 52 53 // RPCHandler can be provided to avoid network traffic if the 54 // server is running locally. 55 RPCHandler RPCHandler 56 57 // Node provides the base node 58 Node *structs.Node 59 60 // Options provides arbitrary key-value configuration for nomad internals, 61 // like fingerprinters and drivers. The format is: 62 // 63 // namespace.option = value 64 Options map[string]string 65 } 66 67 // Read returns the specified configuration value or "". 68 func (c *Config) Read(id string) string { 69 val, ok := c.Options[id] 70 if !ok { 71 return "" 72 } 73 return val 74 } 75 76 // ReadDefault returns the specified configuration value, or the specified 77 // default value if none is set. 78 func (c *Config) ReadDefault(id string, defaultValue string) string { 79 val := c.Read(id) 80 if val != "" { 81 return val 82 } 83 return defaultValue 84 } 85 86 // ReadBool parses the specified option as a boolean. 87 func (c *Config) ReadBool(id string) (bool, error) { 88 val, ok := c.Options[id] 89 if !ok { 90 return false, fmt.Errorf("Specified config is missing from options") 91 } 92 bval, err := strconv.ParseBool(val) 93 if err != nil { 94 return false, fmt.Errorf("Failed to parse %s as bool: %s", val, err) 95 } 96 return bval, nil 97 } 98 99 // ReadBoolDefault tries to parse the specified option as a boolean. If there is 100 // an error in parsing, the default option is returned. 101 func (c *Config) ReadBoolDefault(id string, defaultValue bool) bool { 102 val, err := c.ReadBool(id) 103 if err != nil { 104 return defaultValue 105 } 106 return val 107 } 108 109 // ReadStringListToMap tries to parse the specified option as a comma seperated list. 110 // If there is an error in parsing, an empty list is returned. 111 func (c *Config) ReadStringListToMap(key string) map[string]struct{} { 112 s := strings.TrimSpace(c.Read(key)) 113 list := make(map[string]struct{}) 114 if s != "" { 115 for _, e := range strings.Split(s, ",") { 116 trimmed := strings.TrimSpace(e) 117 list[trimmed] = struct{}{} 118 } 119 } 120 return list 121 }