From f5765e4dde5fb2bb2e20bacd88d1784928fc37d4 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 18 Mar 2014 09:36:31 -0700 Subject: [PATCH 1/3] feat(etcd): Default etcd name to /etc/machine-id --- initialize/etcd.go | 7 +++++++ initialize/etcd_test.go | 44 +++++++++++++++++++++++++++++++++++++++++ system/systemd.go | 6 ++++++ system/systemd_test.go | 14 +++++++++++++ test.yaml | 1 - 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/initialize/etcd.go b/initialize/etcd.go index a55be07..ad93cb6 100644 --- a/initialize/etcd.go +++ b/initialize/etcd.go @@ -31,11 +31,18 @@ func (ec EtcdEnvironment) String() (out string) { out += fmt.Sprintf("Environment=\"ETCD_%s=%s\"\n", key, val) } + return } // 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 + } + } + file := system.File{ Path: path.Join(root, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf"), RawFilePermissions: "0644", diff --git a/initialize/etcd_test.go b/initialize/etcd_test.go index f1c3e94..56cd88c 100644 --- a/initialize/etcd_test.go +++ b/initialize/etcd_test.go @@ -46,6 +46,7 @@ Environment="ETCD_PEER_BIND_ADDR=192.0.2.13:7001" func TestEtcdEnvironmentWrittenToDisk(t *testing.T) { ec := EtcdEnvironment{ + "name": "node001", "discovery_url": "http://disco.example.com/foobar", "peer-bind-addr": "127.0.0.1:7002", } @@ -76,6 +77,7 @@ func TestEtcdEnvironmentWrittenToDisk(t *testing.T) { } expect := `[Service] +Environment="ETCD_NAME=node001" Environment="ETCD_DISCOVERY_URL=http://disco.example.com/foobar" Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002" ` @@ -84,6 +86,48 @@ Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002" } } +func TestEtcdEnvironmentWrittenToDiskDefaults(t *testing.T) { + ec := EtcdEnvironment{} + dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-") + if err != nil { + t.Fatalf("Unable to create tempdir: %v", err) + } + defer syscall.Rmdir(dir) + + os.Mkdir(path.Join(dir, "etc"), os.FileMode(0755)) + err = ioutil.WriteFile(path.Join(dir, "etc", "machine-id"), []byte("node007"), os.FileMode(0444)) + if err != nil { + t.Fatalf("Failed writing out /etc/machine-id: %v", err) + } + + if err := WriteEtcdEnvironment(ec, dir); err != nil { + 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()) + } + + contents, err := ioutil.ReadFile(fullPath) + if err != nil { + t.Fatalf("Unable to read expected file: %v", err) + } + + expect := `[Service] +Environment="ETCD_NAME=node007" +` + if string(contents) != expect { + t.Fatalf("File has incorrect contents") + } +} + func rmdir(path string) error { cmd := exec.Command("rm", "-rf", path) return cmd.Run() diff --git a/system/systemd.go b/system/systemd.go index 5861238..5b77f9f 100644 --- a/system/systemd.go +++ b/system/systemd.go @@ -2,6 +2,7 @@ package system import ( "fmt" + "io/ioutil" "log" "os" "os/exec" @@ -161,3 +162,8 @@ func ExecuteScript(scriptPath string) (string, error) { func SetHostname(hostname string) error { return exec.Command("hostnamectl", "set-hostname", hostname).Run() } + +func MachineID(root string) string { + contents, _ := ioutil.ReadFile(path.Join(root, "etc", "machine-id")) + return strings.TrimSpace(string(contents)) +} diff --git a/system/systemd_test.go b/system/systemd_test.go index 02924b0..6635d0e 100644 --- a/system/systemd_test.go +++ b/system/systemd_test.go @@ -100,3 +100,17 @@ Where=/media/state } } +func TestMachineID(t *testing.T) { + dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-") + if err != nil { + t.Fatalf("Unable to create tempdir: %v", err) + } + defer syscall.Rmdir(dir) + + os.Mkdir(path.Join(dir, "etc"), os.FileMode(0755)) + ioutil.WriteFile(path.Join(dir, "etc", "machine-id"), []byte("node007\n"), os.FileMode(0444)) + + if MachineID(dir) != "node007" { + t.Fatalf("File has incorrect contents") + } +} diff --git a/test.yaml b/test.yaml index 8941ecd..cbf0170 100644 --- a/test.yaml +++ b/test.yaml @@ -4,4 +4,3 @@ coreos: etcd: discovery_url: https://discovery.etcd.io/0022cb5027f8f5167a874794c3a13e0d bind-addr: $public_ipv4:4001 - name: polvi From 9818565c7de0dd19df1317cf132e43df2ad808ca Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 18 Mar 2014 10:57:10 -0700 Subject: [PATCH 2/3] feat(etcd): Fall back to hostname if no machine-id --- initialize/etcd.go | 9 +++++++-- initialize/etcd_test.go | 13 ++----------- system/systemd.go | 19 ++++++++++++++++++- test.yaml | 6 ------ 4 files changed, 27 insertions(+), 20 deletions(-) delete mode 100644 test.yaml 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 From dcd82e6c508f44ed58f54e0fb139e75c0f60233e Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 18 Mar 2014 11:04:33 -0700 Subject: [PATCH 3/3] fix(system): Use os.Hostname --- system/systemd.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/system/systemd.go b/system/systemd.go index 152002a..a2605ee 100644 --- a/system/systemd.go +++ b/system/systemd.go @@ -168,10 +168,7 @@ func SetHostname(hostname string) error { } func Hostname() (string, error) { - cmd := exec.Command("hostname") - output, err := cmd.CombinedOutput() - hostname := strings.TrimSpace(string(output)) - return hostname, err + return os.Hostname() } func MachineID(root string) string {