Use persisted peer name if it matches old-style ID
This commit is contained in:
1 parent
e03c7b1b7c
commit
ebcb7f6108
3 files changed
+57
-14
No files matched your search
@@ -71,7 +71,7 @@ testrunner: $(RUNNER_EXE) $(TEST_TLS_EXE)
|
|||||||
$(WEAVER_EXE) $(WEAVEPROXY_EXE) $(WEAVEUTIL_EXE): common/*.go common/*/*.go net/*.go net/*/*.go
|
$(WEAVER_EXE) $(WEAVEPROXY_EXE) $(WEAVEUTIL_EXE): common/*.go common/*/*.go net/*.go net/*/*.go
|
||||||
$(WEAVER_EXE): router/*.go ipam/*.go ipam/*/*.go db/*.go nameserver/*.go prog/weaver/*.go
|
$(WEAVER_EXE): router/*.go ipam/*.go ipam/*/*.go db/*.go nameserver/*.go prog/weaver/*.go
|
||||||
$(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/*.go
|
$(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/*.go
|
||||||
$(WEAVEUTIL_EXE): prog/weaveutil/*.go net/*.go plugin/net/*.go plugin/ipam/*.go
|
$(WEAVEUTIL_EXE): prog/weaveutil/*.go net/*.go plugin/net/*.go plugin/ipam/*.go db/*.go
|
||||||
$(SIGPROXY_EXE): prog/sigproxy/*.go
|
$(SIGPROXY_EXE): prog/sigproxy/*.go
|
||||||
$(KUBEPEERS_EXE): prog/kube-peers/*.go
|
$(KUBEPEERS_EXE): prog/kube-peers/*.go
|
||||||
$(WEAVENPC_EXE): prog/weave-npc/*.go npc/*.go npc/*/*.go
|
$(WEAVENPC_EXE): prog/weave-npc/*.go npc/*.go npc/*/*.go
|
||||||
|
|||||||
+51
-9
@@ -1,37 +1,79 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/weaveworks/mesh"
|
||||||
|
"github.com/weaveworks/weave/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getOldStyleSystemUUID() ([]byte, error) {
|
||||||
|
uuid, err := ioutil.ReadFile("/sys/class/dmi/id/product_uuid")
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
uuid, err = ioutil.ReadFile("/sys/hypervisor/uuid")
|
||||||
|
}
|
||||||
|
return uuid, err
|
||||||
|
}
|
||||||
|
|
||||||
func getSystemUUID(hostRoot string) ([]byte, error) {
|
func getSystemUUID(hostRoot string) ([]byte, error) {
|
||||||
|
uuid, err := getOldStyleSystemUUID()
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
machineid, err := ioutil.ReadFile(hostRoot + "/etc/machine-id")
|
machineid, err := ioutil.ReadFile(hostRoot + "/etc/machine-id")
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
machineid, _ = ioutil.ReadFile(hostRoot + "/var/lib/dbus/machine-id")
|
machineid, err = ioutil.ReadFile(hostRoot + "/var/lib/dbus/machine-id")
|
||||||
}
|
|
||||||
uuid, err := ioutil.ReadFile(hostRoot + "/sys/class/dmi/id/product_uuid")
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
uuid, _ = ioutil.ReadFile(hostRoot + "/sys/hypervisor/uuid")
|
|
||||||
}
|
}
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(machineid)+len(uuid) == 0 {
|
if len(uuid) == 0 {
|
||||||
return nil, fmt.Errorf("Empty system uuid")
|
return nil, errors.New("All system IDs are blank")
|
||||||
}
|
}
|
||||||
return append(machineid, uuid...), nil
|
return append(machineid, uuid...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPersistedPeerName(pathName string) (mesh.PeerName, error) {
|
||||||
|
d, err := db.NewBoltDBReadOnly(pathName)
|
||||||
|
if err != nil {
|
||||||
|
return mesh.UnknownPeerName, err
|
||||||
|
}
|
||||||
|
defer d.Close()
|
||||||
|
var peerName mesh.PeerName
|
||||||
|
nameFound, err := d.Load(db.NameIdent, &peerName)
|
||||||
|
if err != nil || !nameFound {
|
||||||
|
return mesh.UnknownPeerName, err
|
||||||
|
}
|
||||||
|
return peerName, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetSystemPeerName returns an ID derived from concatenated machine-id
|
// GetSystemPeerName returns an ID derived from concatenated machine-id
|
||||||
// (either systemd or dbus), the system (aka bios) UUID and the
|
// (either systemd or dbus), the system (aka bios) UUID and the
|
||||||
// hypervisor UUID. It is tweaked and formatted to be usable as a mac address
|
// hypervisor UUID. It is tweaked and formatted to be usable as a mac address
|
||||||
func GetSystemPeerName(hostRoot string) (string, error) {
|
func GetSystemPeerName(dbPrefix, hostRoot string) (string, error) {
|
||||||
|
// Check if we have a persisted name that matches the old-style ID for this host
|
||||||
|
if oldUUID, err := getOldStyleSystemUUID(); err == nil {
|
||||||
|
pathName := dbPrefix + db.FileName
|
||||||
|
if _, err := os.Stat(pathName); err == nil {
|
||||||
|
persistedPeerName, err := getPersistedPeerName(pathName)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if persistedPeerName == mesh.PeerNameFromBin(MACfromUUID(oldUUID)) {
|
||||||
|
return persistedPeerName.String(), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
var mac net.HardwareAddr
|
var mac net.HardwareAddr
|
||||||
if uuid, err := getSystemUUID(hostRoot); err == nil {
|
if uuid, err := getSystemUUID(hostRoot); err == nil {
|
||||||
mac = MACfromUUID(uuid)
|
mac = MACfromUUID(uuid)
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return "", err
|
||||||
} else {
|
} else {
|
||||||
mac, err = RandomMAC()
|
mac, err = RandomMAC()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func uniqueID(args []string) error {
|
func uniqueID(args []string) error {
|
||||||
if len(args) != 1 {
|
if len(args) != 2 {
|
||||||
cmdUsage("unique-id", "<host-root>")
|
cmdUsage("unique-id", "<db-prefix> <host-root>")
|
||||||
}
|
}
|
||||||
hostRoot := args[0]
|
dbPrefix := args[0]
|
||||||
uid, err := weavenet.GetSystemPeerName(hostRoot)
|
hostRoot := args[1]
|
||||||
|
uid, err := weavenet.GetSystemPeerName(dbPrefix, hostRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user