mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 02:15:43 +03:00
@@ -14,7 +14,6 @@ var BuiltinFingerprints = map[string]Factory{
|
||||
"arch": NewArchFingerprint,
|
||||
"cpu": NewCPUFingerprint,
|
||||
"host": NewHostFingerprint,
|
||||
"os": NewOSFingerprint,
|
||||
"memory": NewMemoryFingerprint,
|
||||
"storage": NewStorageFingerprint,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user