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
|
package initialize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -38,8 +39,12 @@ func (ec EtcdEnvironment) String() (out string) {
|
|||||||
// 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 _, ok := env["name"]; !ok {
|
||||||
if name := system.MachineID(root); name != "" {
|
if machineID := system.MachineID(root); machineID != "" {
|
||||||
env["name"] = name
|
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{}
|
ec := EtcdEnvironment{}
|
||||||
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,16 +104,7 @@ func TestEtcdEnvironmentWrittenToDiskDefaults(t *testing.T) {
|
|||||||
t.Fatalf("Processing of EtcdEnvironment failed: %v", err)
|
t.Fatalf("Processing of EtcdEnvironment failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fullPath := path.Join(dir, "etc", "systemd", "system", "etcd.service.d", "20-cloudinit.conf")
|
fullPath := path.Join(dir, "run", "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)
|
contents, err := ioutil.ReadFile(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -13,6 +13,10 @@ import (
|
|||||||
"github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus"
|
"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 {
|
type Unit struct {
|
||||||
Name string
|
Name string
|
||||||
Runtime bool
|
Runtime bool
|
||||||
@ -163,7 +167,20 @@ func SetHostname(hostname string) error {
|
|||||||
return exec.Command("hostnamectl", "set-hostname", hostname).Run()
|
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 {
|
func MachineID(root string) string {
|
||||||
contents, _ := ioutil.ReadFile(path.Join(root, "etc", "machine-id"))
|
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…
Reference in New Issue
Block a user