feat(etcd): Fall back to hostname if no machine-id
This commit is contained in:
parent
f5765e4dde
commit
9818565c7d
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user