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

     1  package fingerprint
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/nomad/client/config"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  // EmptyDuration is to be used by fingerprinters that are not periodic.
    13  const EmptyDuration = time.Duration(0)
    14  
    15  // BuiltinFingerprints is a slice containing the key names of all regestered
    16  // fingerprints available, to provided an ordered iteration
    17  var BuiltinFingerprints = []string{
    18  	"arch",
    19  	"consul",
    20  	"cpu",
    21  	"env_aws",
    22  	"env_gce",
    23  	"host",
    24  	"memory",
    25  	"network",
    26  	"storage",
    27  }
    28  
    29  // builtinFingerprintMap contains the built in registered fingerprints
    30  // which are available, corresponding to a key found in BuiltinFingerprints
    31  var builtinFingerprintMap = map[string]Factory{
    32  	"arch":    NewArchFingerprint,
    33  	"consul":  NewConsulFingerprint,
    34  	"cpu":     NewCPUFingerprint,
    35  	"env_aws": NewEnvAWSFingerprint,
    36  	"env_gce": NewEnvGCEFingerprint,
    37  	"host":    NewHostFingerprint,
    38  	"memory":  NewMemoryFingerprint,
    39  	"network": NewNetworkFingerprinter,
    40  	"storage": NewStorageFingerprint,
    41  }
    42  
    43  // NewFingerprint is used to instantiate and return a new fingerprint
    44  // given the name and a logger
    45  func NewFingerprint(name string, logger *log.Logger) (Fingerprint, error) {
    46  	// Lookup the factory function
    47  	factory, ok := builtinFingerprintMap[name]
    48  	if !ok {
    49  		return nil, fmt.Errorf("unknown fingerprint '%s'", name)
    50  	}
    51  
    52  	// Instantiate the fingerprint
    53  	f := factory(logger)
    54  	return f, nil
    55  }
    56  
    57  // Factory is used to instantiate a new Fingerprint
    58  type Factory func(*log.Logger) Fingerprint
    59  
    60  // Fingerprint is used for doing "fingerprinting" of the
    61  // host to automatically determine attributes, resources,
    62  // and metadata about it. Each of these is a heuristic, and
    63  // many of them can be applied on a particular host.
    64  type Fingerprint interface {
    65  	// Fingerprint is used to update properties of the Node,
    66  	// and returns if the fingerprint was applicable and a potential error.
    67  	Fingerprint(*config.Config, *structs.Node) (bool, error)
    68  
    69  	// Periodic is a mechanism for the fingerprinter to indicate that it should
    70  	// be run periodically. The return value is a boolean indicating if it
    71  	// should be periodic, and if true, a duration.
    72  	Periodic() (bool, time.Duration)
    73  }
    74  
    75  // StaticFingerprinter can be embeded in a struct that has a Fingerprint method
    76  // to make it non-periodic.
    77  type StaticFingerprinter struct{}
    78  
    79  func (s *StaticFingerprinter) Periodic() (bool, time.Duration) {
    80  	return false, EmptyDuration
    81  }