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