github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/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  	// ClientMaxPort is the upper range of the ports that the client uses for
    61  	// communicating with plugin subsystems
    62  	ClientMaxPort uint
    63  
    64  	// ClientMinPort is the lower range of the ports that the client uses for
    65  	// communicating with plugin subsystems
    66  	ClientMinPort uint
    67  
    68  	// Options provides arbitrary key-value configuration for nomad internals,
    69  	// like fingerprinters and drivers. The format is:
    70  	//
    71  	//	namespace.option = value
    72  	Options map[string]string
    73  
    74  	// Version is the version of the Nomad client
    75  	Version string
    76  }
    77  
    78  func (c *Config) Copy() *Config {
    79  	nc := new(Config)
    80  	*nc = *c
    81  	nc.Node = nc.Node.Copy()
    82  	nc.Servers = structs.CopySliceString(nc.Servers)
    83  	nc.Options = structs.CopyMapStringString(nc.Options)
    84  	return nc
    85  }
    86  
    87  // Read returns the specified configuration value or "".
    88  func (c *Config) Read(id string) string {
    89  	val, ok := c.Options[id]
    90  	if !ok {
    91  		return ""
    92  	}
    93  	return val
    94  }
    95  
    96  // ReadDefault returns the specified configuration value, or the specified
    97  // default value if none is set.
    98  func (c *Config) ReadDefault(id string, defaultValue string) string {
    99  	val := c.Read(id)
   100  	if val != "" {
   101  		return val
   102  	}
   103  	return defaultValue
   104  }
   105  
   106  // ReadBool parses the specified option as a boolean.
   107  func (c *Config) ReadBool(id string) (bool, error) {
   108  	val, ok := c.Options[id]
   109  	if !ok {
   110  		return false, fmt.Errorf("Specified config is missing from options")
   111  	}
   112  	bval, err := strconv.ParseBool(val)
   113  	if err != nil {
   114  		return false, fmt.Errorf("Failed to parse %s as bool: %s", val, err)
   115  	}
   116  	return bval, nil
   117  }
   118  
   119  // ReadBoolDefault tries to parse the specified option as a boolean. If there is
   120  // an error in parsing, the default option is returned.
   121  func (c *Config) ReadBoolDefault(id string, defaultValue bool) bool {
   122  	val, err := c.ReadBool(id)
   123  	if err != nil {
   124  		return defaultValue
   125  	}
   126  	return val
   127  }
   128  
   129  // ReadStringListToMap tries to parse the specified option as a comma seperated list.
   130  // If there is an error in parsing, an empty list is returned.
   131  func (c *Config) ReadStringListToMap(key string) map[string]struct{} {
   132  	s := strings.TrimSpace(c.Read(key))
   133  	list := make(map[string]struct{})
   134  	if s != "" {
   135  		for _, e := range strings.Split(s, ",") {
   136  			trimmed := strings.TrimSpace(e)
   137  			list[trimmed] = struct{}{}
   138  		}
   139  	}
   140  	return list
   141  }