diff --git a/initialize/etcd.go b/initialize/etcd.go index ad93cb6..4e5e9dc 100644 --- a/initialize/etcd.go +++ b/initialize/etcd.go @@ -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") } } diff --git a/initialize/etcd_test.go b/initialize/etcd_test.go index 56cd88c..8ee86fe 100644 --- a/initialize/etcd_test.go +++ b/initialize/etcd_test.go @@ -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 { diff --git a/system/systemd.go b/system/systemd.go index 5b77f9f..152002a 100644 --- a/system/systemd.go +++ b/system/systemd.go @@ -13,6 +13,10 @@ import ( "github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus" ) +// fakeMachineID is placed on non-usr CoreOS images and should +// never be used as a true MachineID +const fakeMachineID = "42000000000000000000000000000042" + type Unit struct { Name string Runtime bool @@ -163,7 +167,20 @@ func SetHostname(hostname string) error { return exec.Command("hostnamectl", "set-hostname", hostname).Run() } +func Hostname() (string, error) { + cmd := exec.Command("hostname") + output, err := cmd.CombinedOutput() + hostname := strings.TrimSpace(string(output)) + return hostname, err +} + func MachineID(root string) string { contents, _ := ioutil.ReadFile(path.Join(root, "etc", "machine-id")) - return strings.TrimSpace(string(contents)) + id := strings.TrimSpace(string(contents)) + + if id == fakeMachineID { + id = "" + } + + return id } diff --git a/test.yaml b/test.yaml deleted file mode 100644 index cbf0170..0000000 --- a/test.yaml +++ /dev/null @@ -1,6 +0,0 @@ -#cloud-config - -coreos: - etcd: - discovery_url: https://discovery.etcd.io/0022cb5027f8f5167a874794c3a13e0d - bind-addr: $public_ipv4:4001