github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/client/fingerprint/fingerprint.go (about) 1 package fingerprint 2 3 import ( 4 "fmt" 5 "log" 6 "sort" 7 "time" 8 9 "github.com/hashicorp/nomad/client/config" 10 "github.com/hashicorp/nomad/nomad/structs" 11 ) 12 13 // EmptyDuration is to be used by fingerprinters that are not periodic. 14 const ( 15 EmptyDuration = time.Duration(0) 16 ) 17 18 func init() { 19 20 // Initialize the list of available fingerprinters per platform. Each 21 // platform defines its own list of available fingerprinters. 22 initPlatformFingerprints(hostFingerprinters) 23 } 24 25 var ( 26 // hostFingerprinters contains the host fingerprints which are available for a 27 // given platform. 28 hostFingerprinters = map[string]Factory{ 29 "arch": NewArchFingerprint, 30 "consul": NewConsulFingerprint, 31 "cpu": NewCPUFingerprint, 32 "host": NewHostFingerprint, 33 "memory": NewMemoryFingerprint, 34 "network": NewNetworkFingerprint, 35 "nomad": NewNomadFingerprint, 36 "signal": NewSignalFingerprint, 37 "storage": NewStorageFingerprint, 38 "vault": NewVaultFingerprint, 39 } 40 41 // envFingerprinters contains the fingerprints that are environment specific. 42 // This should run after the host fingerprinters as they may override specific 43 // node resources with more detailed information. 44 envFingerprinters = map[string]Factory{ 45 "env_aws": NewEnvAWSFingerprint, 46 "env_gce": NewEnvGCEFingerprint, 47 } 48 ) 49 50 // BuiltinFingerprints is a slice containing the key names of all registered 51 // fingerprints available. The order of this slice should be preserved when 52 // fingerprinting. 53 func BuiltinFingerprints() []string { 54 fingerprints := make([]string, 0, len(hostFingerprinters)) 55 for k := range hostFingerprinters { 56 fingerprints = append(fingerprints, k) 57 } 58 sort.Strings(fingerprints) 59 for k := range envFingerprinters { 60 fingerprints = append(fingerprints, k) 61 } 62 return fingerprints 63 } 64 65 // NewFingerprint is used to instantiate and return a new fingerprint 66 // given the name and a logger 67 func NewFingerprint(name string, logger *log.Logger) (Fingerprint, error) { 68 // Lookup the factory function 69 factory, ok := hostFingerprinters[name] 70 if !ok { 71 factory, ok = envFingerprinters[name] 72 if !ok { 73 return nil, fmt.Errorf("unknown fingerprint '%s'", name) 74 } 75 } 76 77 // Instantiate the fingerprint 78 f := factory(logger) 79 return f, nil 80 } 81 82 // Factory is used to instantiate a new Fingerprint 83 type Factory func(*log.Logger) Fingerprint 84 85 // Fingerprint is used for doing "fingerprinting" of the 86 // host to automatically determine attributes, resources, 87 // and metadata about it. Each of these is a heuristic, and 88 // many of them can be applied on a particular host. 89 type Fingerprint interface { 90 // Fingerprint is used to update properties of the Node, 91 // and returns if the fingerprint was applicable and a potential error. 92 Fingerprint(*config.Config, *structs.Node) (bool, error) 93 94 // Periodic is a mechanism for the fingerprinter to indicate that it should 95 // be run periodically. The return value is a boolean indicating if it 96 // should be periodic, and if true, a duration. 97 Periodic() (bool, time.Duration) 98 } 99 100 // StaticFingerprinter can be embedded in a struct that has a Fingerprint method 101 // to make it non-periodic. 102 type StaticFingerprinter struct{} 103 104 func (s *StaticFingerprinter) Periodic() (bool, time.Duration) { 105 return false, EmptyDuration 106 }