feat(etcd): Default etcd name to /etc/machine-id

This commit is contained in:
Brian Waldon
2014-03-18 09:36:31 -07:00
parent 61ffbd41c9
commit f5765e4dde
5 changed files with 71 additions and 1 deletions

View File

@@ -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))
}

View File

@@ -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")
}
}