diff --git a/command/agent/agent.go b/command/agent/agent.go index 8b3c96856..c8fccfd1c 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -533,26 +533,10 @@ func (a *Agent) setupServer() error { // setupNodeID will pull the persisted node ID, if any, or create a random one // and persist it. func (a *Agent) setupNodeID(config *nomad.Config) error { - // If they've configured a node ID manually then just use that, as - // long as it's valid. - if config.NodeID != "" { - config.NodeID = strings.ToLower(string(config.NodeID)) - if _, err := uuidparse.ParseUUID(string(config.NodeID)); err != nil { - return err - } - - return nil - } - - // For dev mode we have no filesystem access so just make one. - if a.config.DevMode { - config.NodeID = uuid.Generate() - return nil - } - // Load saved state, if any. Since a user could edit this, we also - // validate it. + // validate it. Saved state overwrites any configured node id fileID := filepath.Join(config.DataDir, "node-id") + savedNodeID := false if _, err := os.Stat(fileID); err == nil { rawID, err := ioutil.ReadFile(fileID) if err != nil { @@ -564,8 +548,31 @@ func (a *Agent) setupNodeID(config *nomad.Config) error { if _, err := uuidparse.ParseUUID(nodeID); err != nil { return err } - config.NodeID = nodeID + savedNodeID = true + } + + // If they've configured a node ID manually then just use that, as + // long as it's valid. + if !savedNodeID && config.NodeID != "" { + config.NodeID = strings.ToLower(string(config.NodeID)) + if _, err := uuidparse.ParseUUID(string(config.NodeID)); err != nil { + return err + } + // Persist this configured nodeID to our data directory + if err := lib.EnsurePath(fileID, false); err != nil { + return err + } + if err := ioutil.WriteFile(fileID, []byte(config.NodeID), 0600); err != nil { + return err + } + return nil + } + + // For dev mode we have no filesystem access so just make one. + if a.config.DevMode { + config.NodeID = uuid.Generate() + return nil } // If we still don't have a valid node ID, make one. diff --git a/nomad/config.go b/nomad/config.go index 17dcbed42..12cb9825c 100644 --- a/nomad/config.go +++ b/nomad/config.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/memberlist" "github.com/hashicorp/nomad/helper/tlsutil" + "github.com/hashicorp/nomad/helper/uuid" "github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs/config" "github.com/hashicorp/nomad/scheduler" @@ -311,13 +312,12 @@ func DefaultConfig() *Config { panic(err) } - // TODO Add back random node ID and always persist in the agent startup - // TODO Remove places I added uuid.Generate() c := &Config{ Region: DefaultRegion, AuthoritativeRegion: DefaultRegion, Datacenter: DefaultDC, NodeName: hostname, + NodeID: uuid.Generate(), ProtocolVersion: ProtocolVersionMax, RaftConfig: raft.DefaultConfig(), RaftTimeout: 10 * time.Second, diff --git a/nomad/server_test.go b/nomad/server_test.go index dd9f2b81b..3bdb58e7e 100644 --- a/nomad/server_test.go +++ b/nomad/server_test.go @@ -532,7 +532,6 @@ func TestServer_InvalidSchedulers(t *testing.T) { require := require.New(t) config := DefaultConfig() - config.NodeID = uuid.Generate() config.DevMode = true config.LogOutput = testlog.NewWriter(t) config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1" diff --git a/nomad/testing.go b/nomad/testing.go index 9e09e60c7..2859dfb63 100644 --- a/nomad/testing.go +++ b/nomad/testing.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/consul/lib/freeport" "github.com/hashicorp/nomad/command/agent/consul" "github.com/hashicorp/nomad/helper/testlog" - "github.com/hashicorp/nomad/helper/uuid" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" "github.com/mitchellh/go-testing-interface" @@ -39,7 +38,6 @@ func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken) { func TestServer(t testing.T, cb func(*Config)) *Server { // Setup the default settings config := DefaultConfig() - config.NodeID = uuid.Generate() config.Build = "0.8.0+unittest" config.DevMode = true nodeNum := atomic.AddUint32(&nodeNumber, 1)