feat(etcd): Default etcd name to /etc/machine-id
This commit is contained in:
parent
61ffbd41c9
commit
f5765e4dde
@ -31,11 +31,18 @@ func (ec EtcdEnvironment) String() (out string) {
|
|||||||
|
|
||||||
out += fmt.Sprintf("Environment=\"ETCD_%s=%s\"\n", key, val)
|
out += fmt.Sprintf("Environment=\"ETCD_%s=%s\"\n", key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write an EtcdEnvironment to the appropriate path on disk for etcd.service
|
// Write an EtcdEnvironment to the appropriate path on disk for etcd.service
|
||||||
func WriteEtcdEnvironment(env EtcdEnvironment, root string) error {
|
func WriteEtcdEnvironment(env EtcdEnvironment, root string) error {
|
||||||
|
if _, ok := env["name"]; !ok {
|
||||||
|
if name := system.MachineID(root); name != "" {
|
||||||
|
env["name"] = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
file := system.File{
|
file := system.File{
|
||||||
Path: path.Join(root, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf"),
|
Path: path.Join(root, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf"),
|
||||||
RawFilePermissions: "0644",
|
RawFilePermissions: "0644",
|
||||||
|
@ -46,6 +46,7 @@ Environment="ETCD_PEER_BIND_ADDR=192.0.2.13:7001"
|
|||||||
|
|
||||||
func TestEtcdEnvironmentWrittenToDisk(t *testing.T) {
|
func TestEtcdEnvironmentWrittenToDisk(t *testing.T) {
|
||||||
ec := EtcdEnvironment{
|
ec := EtcdEnvironment{
|
||||||
|
"name": "node001",
|
||||||
"discovery_url": "http://disco.example.com/foobar",
|
"discovery_url": "http://disco.example.com/foobar",
|
||||||
"peer-bind-addr": "127.0.0.1:7002",
|
"peer-bind-addr": "127.0.0.1:7002",
|
||||||
}
|
}
|
||||||
@ -76,6 +77,7 @@ func TestEtcdEnvironmentWrittenToDisk(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expect := `[Service]
|
expect := `[Service]
|
||||||
|
Environment="ETCD_NAME=node001"
|
||||||
Environment="ETCD_DISCOVERY_URL=http://disco.example.com/foobar"
|
Environment="ETCD_DISCOVERY_URL=http://disco.example.com/foobar"
|
||||||
Environment="ETCD_PEER_BIND_ADDR=127.0.0.1:7002"
|
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 {
|
func rmdir(path string) error {
|
||||||
cmd := exec.Command("rm", "-rf", path)
|
cmd := exec.Command("rm", "-rf", path)
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
|
@ -2,6 +2,7 @@ package system
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -161,3 +162,8 @@ func ExecuteScript(scriptPath string) (string, error) {
|
|||||||
func SetHostname(hostname string) error {
|
func SetHostname(hostname string) error {
|
||||||
return exec.Command("hostnamectl", "set-hostname", hostname).Run()
|
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))
|
||||||
|
}
|
||||||
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user