feat(etcd): Fall back to hostname if no machine-id

This commit is contained in:
Brian Waldon
2014-03-18 10:57:10 -07:00
parent f5765e4dde
commit 9818565c7d
4 changed files with 27 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
package initialize
import (
"errors"
"fmt"
"os"
"path"
@@ -38,8 +39,12 @@ func (ec EtcdEnvironment) String() (out string) {
// Write an EtcdEnvironment to the appropriate path on disk for etcd.service
func WriteEtcdEnvironment(env EtcdEnvironment, root string) error {
if _, ok := env["name"]; !ok {
if name := system.MachineID(root); name != "" {
env["name"] = name
if machineID := system.MachineID(root); machineID != "" {
env["name"] = machineID
} else if hostname, err := system.Hostname(); err == nil {
env["name"] = hostname
} else {
return errors.New("Unable to determine default etcd name")
}
}

View File

@@ -86,7 +86,7 @@ Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
}
}
func TestEtcdEnvironmentWrittenToDiskDefaults(t *testing.T) {
func TestEtcdEnvironmentWrittenToDiskDefaultToMachineID(t *testing.T) {
ec := EtcdEnvironment{}
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
if err != nil {
@@ -104,16 +104,7 @@ func TestEtcdEnvironmentWrittenToDiskDefaults(t *testing.T) {
t.Fatalf("Processing of EtcdEnvironment failed: %v", err)
}
fullPath := path.Join(dir, "etc", "systemd", "system", "etcd.service.d", "20-cloudinit.conf")
fi, err := os.Stat(fullPath)
if err != nil {
t.Fatalf("Unable to stat file: %v", err)
}
if fi.Mode() != os.FileMode(0644) {
t.Errorf("File has incorrect mode: %v", fi.Mode())
}
fullPath := path.Join(dir, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf")
contents, err := ioutil.ReadFile(fullPath)
if err != nil {