From 43152d25970668dfd893a86eef171e9ded31edd3 Mon Sep 17 00:00:00 2001 From: Doflatango Date: Thu, 8 Mar 2018 14:35:49 +0800 Subject: [PATCH] Use UUID instead of machine ID --- .travis.yml | 2 +- conf/conf.go | 38 ++++++++++++++++++++++++++ node/node.go | 7 ++--- web/ui/src/components/JobExecuting.vue | 6 ++-- web/ui/src/components/Node.vue | 2 +- 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78dcaee..c5d48fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ install: - go get github.com/gorilla/mux - go get github.com/smartystreets/goconvey/convey - go get github.com/spf13/cobra -- go get github.com/denisbrodbeck/machineid +- go get github.com/satori/go.uuid before_script: - go vet -x ./... diff --git a/conf/conf.go b/conf/conf.go index 9fb124c..332a3e0 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -1,12 +1,15 @@ package conf import ( + "io/ioutil" + "os" "path" "time" client "github.com/coreos/etcd/clientv3" "github.com/fsnotify/fsnotify" "github.com/go-gomail/gomail" + "github.com/satori/go.uuid" "github.com/shunfei/cronsun/db" "github.com/shunfei/cronsun/event" @@ -42,6 +45,7 @@ func Init(confFile string, watchConfiFile bool) error { } type Conf struct { + dir string Node string // node 进程路径 Proc string // 当前执行任务路径 Cmd string // cmd 路径 @@ -136,12 +140,46 @@ func cleanKeyPrefix(p string) string { return p } +const UUID_FILE = "CRONSUN_UUID" + +func (c *Conf) UUID() (string, error) { + b, err := ioutil.ReadFile(path.Join(c.dir, UUID_FILE)) + if err == nil { + if len(b) == 0 { + return c.genUUID() + } + return string(b), nil + } + + if !os.IsNotExist(err) { + return "", err + } + + return c.genUUID() +} + +func (c *Conf) genUUID() (string, error) { + u, err := uuid.NewV4() + if err != nil { + return "", err + } + + err = ioutil.WriteFile(path.Join(c.dir, UUID_FILE), []byte(u.String()), 0600) + if err != nil { + return "", err + } + + return u.String(), nil +} + func (c *Conf) parse(confFile string) error { err := utils.LoadExtendConf(confFile, c) if err != nil { return err } + c.dir = path.Dir(confFile) + if c.Etcd.DialTimeout > 0 { c.Etcd.conf.DialTimeout = time.Duration(c.Etcd.DialTimeout) * time.Second } diff --git a/node/node.go b/node/node.go index 6dd405e..9fbecd0 100644 --- a/node/node.go +++ b/node/node.go @@ -7,7 +7,6 @@ import ( "time" client "github.com/coreos/etcd/clientv3" - "github.com/denisbrodbeck/machineid" "github.com/shunfei/cronsun" "github.com/shunfei/cronsun/conf" @@ -36,7 +35,7 @@ type Node struct { } func NewNode(cfg *conf.Conf) (n *Node, err error) { - mid, err := machineid.ProtectedID("cronsun") + uuid, err := cfg.UUID() if err != nil { return } @@ -48,14 +47,14 @@ func NewNode(cfg *conf.Conf) (n *Node, err error) { hostname, err := os.Hostname() if err != nil { - hostname = mid + hostname = uuid err = nil } n = &Node{ Client: cronsun.DefalutClient, Node: &cronsun.Node{ - ID: mid, + ID: uuid, PID: strconv.Itoa(os.Getpid()), IP: ip.String(), Hostname: hostname, diff --git a/web/ui/src/components/JobExecuting.vue b/web/ui/src/components/JobExecuting.vue index 25395aa..dd04bdd 100644 --- a/web/ui/src/components/JobExecuting.vue +++ b/web/ui/src/components/JobExecuting.vue @@ -14,8 +14,8 @@
- - + +
@@ -137,4 +137,4 @@ export default { Dropdown } } - \ No newline at end of file + diff --git a/web/ui/src/components/Node.vue b/web/ui/src/components/Node.vue index 7703593..514ccae 100644 --- a/web/ui/src/components/Node.vue +++ b/web/ui/src/components/Node.vue @@ -73,7 +73,7 @@ export default { var nodes = this.$store.getters.nodes; for (var id in nodes) { var n = nodes[id]; - n.title = n.ip + "\n" + n.id.substr(0, 16) + "\n" + n.version + "\nstarted at: " + n.up + n.title = n.ip + "\n" + n.id + "\n" + n.version + "\nstarted at: " + n.up if (n.alived && n.connected) { vm.groups[2].nodes.push(n); } else if (n.alived && !n.connected) {