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