github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/client/config/config.go (about)

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