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