diff --git a/client/client.go b/client/client.go index 6971896f9..36ea768be 100644 --- a/client/client.go +++ b/client/client.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/hashicorp/nomad/client/driver" "github.com/hashicorp/nomad/client/fingerprint" "github.com/hashicorp/nomad/nomad" "github.com/hashicorp/nomad/nomad/structs" @@ -99,6 +100,11 @@ func NewClient(config *Config) (*Client, error) { if err := c.fingerprint(); err != nil { return nil, fmt.Errorf("fingerprinting failed: %v", err) } + + // Scan for drivers + if err := c.setupDrivers(); err != nil { + return nil, fmt.Errorf("driver setup failed: %v", err) + } return c, nil } @@ -239,3 +245,23 @@ func (c *Client) fingerprint() error { c.logger.Printf("[DEBUG] client: applied fingerprints %v", applied) return nil } + +// setupDrivers is used to find the available drivers +func (c *Client) setupDrivers() error { + var avail []string + for name := range driver.BuiltinDrivers { + d, err := driver.NewDriver(name, c.logger) + if err != nil { + return err + } + applies, err := d.Fingerprint(c.config.Node) + if err != nil { + return err + } + if applies { + avail = append(avail, name) + } + } + c.logger.Printf("[DEBUG] client: available drivers %v", avail) + return nil +} diff --git a/client/client_test.go b/client/client_test.go index 20c4c8ede..0b605c513 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -126,3 +126,14 @@ func TestClient_Fingerprint(t *testing.T) { t.Fatalf("missing arch") } } + +func TestClient_Drivers(t *testing.T) { + c := testClient(t, nil) + defer c.Shutdown() + + // Ensure os and arch are always present + node := c.Node() + if node.Attributes["driver.exec"] == "" { + t.Fatalf("missing exec driver") + } +} diff --git a/client/driver/exec.go b/client/driver/exec.go index 90f05c4ad..79cf9b00c 100644 --- a/client/driver/exec.go +++ b/client/driver/exec.go @@ -21,7 +21,8 @@ func NewExecDriver(logger *log.Logger) Driver { return d } -func (d *ExecDriver) Fingerprint(*structs.Node) (bool, error) { +func (d *ExecDriver) Fingerprint(node *structs.Node) (bool, error) { // We can always do a fork/exec + node.Attributes["driver.exec"] = "1" return true, nil } diff --git a/client/driver/exec_test.go b/client/driver/exec_test.go index 1195b7396..d6b7adb5f 100644 --- a/client/driver/exec_test.go +++ b/client/driver/exec_test.go @@ -4,6 +4,8 @@ import ( "log" "os" "testing" + + "github.com/hashicorp/nomad/nomad/structs" ) func testLogger() *log.Logger { @@ -12,11 +14,17 @@ func testLogger() *log.Logger { func TestExecDriver_Fingerprint(t *testing.T) { d := NewExecDriver(testLogger()) - apply, err := d.Fingerprint(nil) + node := &structs.Node{ + Attributes: make(map[string]string), + } + apply, err := d.Fingerprint(node) if err != nil { t.Fatalf("err: %v", err) } if !apply { t.Fatalf("should apply") } + if node.Attributes["driver.exec"] == "" { + t.Fatalf("missing driver") + } }