diff --git a/client/fingerprint/fingerprint.go b/client/fingerprint/fingerprint.go index ee9198bc2..3a4587105 100644 --- a/client/fingerprint/fingerprint.go +++ b/client/fingerprint/fingerprint.go @@ -14,7 +14,6 @@ var BuiltinFingerprints = map[string]Factory{ "arch": NewArchFingerprint, "cpu": NewCPUFingerprint, "host": NewHostFingerprint, - "os": NewOSFingerprint, "memory": NewMemoryFingerprint, "storage": NewStorageFingerprint, } diff --git a/client/fingerprint/host.go b/client/fingerprint/host.go index 9acad1d54..5cbfee755 100644 --- a/client/fingerprint/host.go +++ b/client/fingerprint/host.go @@ -1,7 +1,10 @@ package fingerprint import ( + "fmt" "log" + "os/exec" + "runtime" "github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/nomad/structs" @@ -28,8 +31,19 @@ func (f *HostFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (b node.Attributes["os.name"] = hostInfo.Platform node.Attributes["os.version"] = hostInfo.PlatformVersion + + node.Attributes["kernel.name"] = runtime.GOOS + node.Attributes["kernel.version"] = "" + + if runtime.GOOS != "windows" { + out, err := exec.Command("uname", "-r").Output() + if err != nil { + return false, fmt.Errorf("Failed to run uname: %s", err) + } + node.Attributes["kernel.version"] = string(out) + } + node.Attributes["hostname"] = hostInfo.Hostname - node.Attributes["kernel.name"] = hostInfo.OS return true, nil } diff --git a/client/fingerprint/os.go b/client/fingerprint/os.go deleted file mode 100644 index e6d884576..000000000 --- a/client/fingerprint/os.go +++ /dev/null @@ -1,27 +0,0 @@ -package fingerprint - -import ( - "log" - "runtime" - - "github.com/hashicorp/nomad/client/config" - "github.com/hashicorp/nomad/nomad/structs" -) - -// OSFingerprint is used to fingerprint the operating system -type OSFingerprint struct { - logger *log.Logger -} - -// NewOSFingerprint is used to create an OS fingerprint -func NewOSFingerprint(logger *log.Logger) Fingerprint { - f := &OSFingerprint{logger: logger} - return f -} - -func (f *OSFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) { - node.Attributes["os"] = runtime.GOOS - f.logger.Printf("[DEBUG] fingerprint.os: detected '%s'", runtime.GOOS) - - return true, nil -} diff --git a/client/fingerprint/os_test.go b/client/fingerprint/os_test.go deleted file mode 100644 index 8f4add494..000000000 --- a/client/fingerprint/os_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package fingerprint - -import ( - "testing" - - "github.com/hashicorp/nomad/client/config" - "github.com/hashicorp/nomad/nomad/structs" -) - -func TestOSFingerprint(t *testing.T) { - f := NewOSFingerprint(testLogger()) - node := &structs.Node{ - Attributes: make(map[string]string), - } - ok, err := f.Fingerprint(&config.Config{}, node) - if err != nil { - t.Fatalf("err: %v", err) - } - if !ok { - t.Fatalf("should apply") - } - - // OS info - if node.Attributes["os"] == "" { - t.Fatalf("missing OS") - } - -} diff --git a/client/fingerprint/storage.go b/client/fingerprint/storage.go index 45539c48c..f1e4a855c 100644 --- a/client/fingerprint/storage.go +++ b/client/fingerprint/storage.go @@ -3,6 +3,7 @@ package fingerprint import ( "fmt" "log" + "os" "os/exec" "path/filepath" "regexp" @@ -40,10 +41,20 @@ func (f *StorageFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) node.Resources = &structs.Resources{} } - if runtime.GOOS == "windows" { - path, err := filepath.Abs(cfg.AllocDir) + // Guard against unset AllocDir + storageDir := cfg.AllocDir + if storageDir == "" { + var err error + storageDir, err = os.Getwd() if err != nil { - return false, fmt.Errorf("Failed to detect volume for storage directory %s: %s", cfg.AllocDir, err) + return false, fmt.Errorf("Unable to get CWD from filesystem: %s", err) + } + } + + if runtime.GOOS == "windows" { + path, err := filepath.Abs(storageDir) + if err != nil { + return false, fmt.Errorf("Failed to detect volume for storage directory %s: %s", storageDir, err) } volume := filepath.VolumeName(path) node.Attributes["storage.volume"] = volume @@ -78,9 +89,9 @@ func (f *StorageFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) return false, fmt.Errorf("Failed to parse output from fsutil") } } else { - path, err := filepath.Abs(cfg.AllocDir) + path, err := filepath.Abs(storageDir) if err != nil { - return false, fmt.Errorf("Failed to determine absolute path for %s", cfg.AllocDir) + return false, fmt.Errorf("Failed to determine absolute path for %s", storageDir) } // Use -k to standardize the output values between darwin and linux diff --git a/client/fingerprint/storage_test.go b/client/fingerprint/storage_test.go index 054b95be6..469018a16 100644 --- a/client/fingerprint/storage_test.go +++ b/client/fingerprint/storage_test.go @@ -1,11 +1,9 @@ package fingerprint import ( - "os" "strconv" "testing" - "github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/nomad/structs" ) @@ -15,21 +13,7 @@ func TestStorageFingerprint(t *testing.T) { Attributes: make(map[string]string), } - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("Failed to get test working directory: %s", err) - } - cfg := &config.Config{ - AllocDir: cwd, - } - - ok, err := fp.Fingerprint(cfg, node) - if err != nil { - t.Fatalf("Failed to fingerprint: `%s`", err) - } - if !ok { - t.Fatal("Failed to apply node attributes") - } + assertFingerprintOK(t, fp, node) assertNodeAttributeContains(t, node, "storage.volume") assertNodeAttributeContains(t, node, "storage.bytestotal")