2015-08-20 23:26:32 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-11-05 21:46:02 +00:00
|
|
|
"time"
|
2015-08-20 23:26:32 +00:00
|
|
|
|
2015-08-25 23:21:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2015-08-20 23:26:32 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-11-05 21:46:02 +00:00
|
|
|
// EmptyDuration is to be used by fingerprinters that are not periodic.
|
|
|
|
const EmptyDuration = time.Duration(0)
|
|
|
|
|
2016-01-29 13:34:29 +00:00
|
|
|
// BuiltinFingerprints is a slice containing the key names of all registered
|
2015-09-22 19:23:48 +00:00
|
|
|
// fingerprints available, to provided an ordered iteration
|
|
|
|
var BuiltinFingerprints = []string{
|
|
|
|
"arch",
|
2016-01-27 06:22:25 +00:00
|
|
|
"cgroup",
|
2015-11-05 18:39:52 +00:00
|
|
|
"consul",
|
2015-09-22 19:23:48 +00:00
|
|
|
"cpu",
|
2015-11-05 18:39:52 +00:00
|
|
|
"env_aws",
|
|
|
|
"env_gce",
|
2015-09-22 19:23:48 +00:00
|
|
|
"host",
|
|
|
|
"memory",
|
2015-09-23 15:12:56 +00:00
|
|
|
"network",
|
2016-03-23 00:12:30 +00:00
|
|
|
"nomad",
|
2015-11-05 18:39:52 +00:00
|
|
|
"storage",
|
2015-09-22 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// builtinFingerprintMap contains the built in registered fingerprints
|
|
|
|
// which are available, corresponding to a key found in BuiltinFingerprints
|
|
|
|
var builtinFingerprintMap = map[string]Factory{
|
2015-09-23 15:12:56 +00:00
|
|
|
"arch": NewArchFingerprint,
|
2016-01-27 06:22:25 +00:00
|
|
|
"cgroup": NewCGroupFingerprint,
|
2015-11-05 18:39:52 +00:00
|
|
|
"consul": NewConsulFingerprint,
|
2015-09-23 15:12:56 +00:00
|
|
|
"cpu": NewCPUFingerprint,
|
2015-11-05 18:39:52 +00:00
|
|
|
"env_aws": NewEnvAWSFingerprint,
|
|
|
|
"env_gce": NewEnvGCEFingerprint,
|
2015-09-23 15:12:56 +00:00
|
|
|
"host": NewHostFingerprint,
|
|
|
|
"memory": NewMemoryFingerprint,
|
2016-03-23 00:12:30 +00:00
|
|
|
"network": NewNetworkFingerprint,
|
|
|
|
"nomad": NewNomadFingerprint,
|
2015-11-05 18:39:52 +00:00
|
|
|
"storage": NewStorageFingerprint,
|
2015-08-20 23:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFingerprint is used to instantiate and return a new fingerprint
|
|
|
|
// given the name and a logger
|
|
|
|
func NewFingerprint(name string, logger *log.Logger) (Fingerprint, error) {
|
|
|
|
// Lookup the factory function
|
2015-09-22 19:23:48 +00:00
|
|
|
factory, ok := builtinFingerprintMap[name]
|
2015-08-20 23:26:32 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown fingerprint '%s'", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate the fingerprint
|
|
|
|
f := factory(logger)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factory is used to instantiate a new Fingerprint
|
|
|
|
type Factory func(*log.Logger) Fingerprint
|
|
|
|
|
|
|
|
// Fingerprint is used for doing "fingerprinting" of the
|
|
|
|
// host to automatically determine attributes, resources,
|
|
|
|
// and metadata about it. Each of these is a heuristic, and
|
|
|
|
// many of them can be applied on a particular host.
|
|
|
|
type Fingerprint interface {
|
|
|
|
// Fingerprint is used to update properties of the Node,
|
|
|
|
// and returns if the fingerprint was applicable and a potential error.
|
2015-08-25 23:21:29 +00:00
|
|
|
Fingerprint(*config.Config, *structs.Node) (bool, error)
|
2015-11-05 21:46:02 +00:00
|
|
|
|
|
|
|
// Periodic is a mechanism for the fingerprinter to indicate that it should
|
|
|
|
// be run periodically. The return value is a boolean indicating if it
|
|
|
|
// should be periodic, and if true, a duration.
|
|
|
|
Periodic() (bool, time.Duration)
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// StaticFingerprinter can be embedded in a struct that has a Fingerprint method
|
2015-11-05 21:46:02 +00:00
|
|
|
// to make it non-periodic.
|
|
|
|
type StaticFingerprinter struct{}
|
|
|
|
|
|
|
|
func (s *StaticFingerprinter) Periodic() (bool, time.Duration) {
|
|
|
|
return false, EmptyDuration
|
2015-08-20 23:26:32 +00:00
|
|
|
}
|