From 04711ecde595dfd0dc362f0300004a5e01316dec Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Wed, 27 Nov 2013 12:17:20 -0800 Subject: [PATCH] fix(etcd): fixup the name and data dir guessing - Only generate the name from a hostname if the data dir is not specified and the name is not specified - Only guess the data dir from Name if the data dir wasn't already specified --- etcd.go | 30 +++++++++++++++--------------- server/config.go | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/etcd.go b/etcd.go index d30be757a..b72880d0f 100644 --- a/etcd.go +++ b/etcd.go @@ -52,24 +52,18 @@ func main() { profile(config.CPUProfileFile) } - // Load info object. - info, err := config.Info() - if err != nil { - log.Fatal("info:", err) - } - if info.Name == "" { - host, err := os.Hostname() - if err != nil || host == "" { - log.Fatal("Node name required and hostname not set. e.g. '-name=name'") - } - log.Warnf("Using hostname %s as the node name. You must ensure this name is unique among etcd nodes.", host) - info.Name = host + // Only guess the machine name if there is no data dir specified + // because the info file will should have our name + if config.Name == "" && config.DataDir == "" { + config.NameFromHostname() + } + + if config.DataDir == "" && config.Name != "" { + config.DataDirFromName() } - // Setup a default directory based on the node name if config.DataDir == "" { - config.DataDir = info.Name + ".etcd" - log.Warnf("Using the directory %s as the etcd configuration directory because a directory was not specified. ", config.DataDir) + log.Fatal("The data dir was not set and could not be guessed from machine name") } // Create data directory if it doesn't already exist. @@ -77,6 +71,12 @@ func main() { log.Fatalf("Unable to create path: %s", err) } + // Load info object. + info, err := config.Info() + if err != nil { + log.Fatal("info:", err) + } + // Retrieve TLS configuration. tlsConfig, err := info.EtcdTLS.Config() if err != nil { diff --git a/server/config.go b/server/config.go index 0740b45da..939a2580d 100644 --- a/server/config.go +++ b/server/config.go @@ -13,6 +13,7 @@ import ( "strconv" "strings" + "github.com/coreos/etcd/log" "github.com/BurntSushi/toml" ) @@ -300,6 +301,25 @@ func (c *Config) LoadPeersFile() error { return nil } +// DataDirFromName sets the data dir from a machine name and issue a warning +// that etcd is guessing. +func (c *Config) DataDirFromName() { + c.DataDir = c.Name + ".etcd" + log.Warnf("Using the directory %s as the etcd curation directory because a directory was not specified. ", c.DataDir) + + return +} + +// NameFromHostname sets the machine name from the hostname. This is to help +// people get started without thinking up a name. +func (c *Config) NameFromHostname() { + host, err := os.Hostname() + if err != nil && host == "" { + log.Fatal("Node name required and hostname not set. e.g. '-name=name'") + } + c.Name = host +} + // Reset removes all server configuration files. func (c *Config) Reset() error { if err := os.RemoveAll(filepath.Join(c.DataDir, "info")); err != nil {