Use UUID instead of machine ID

pull/69/head
Doflatango 2018-03-08 14:35:49 +08:00
parent 5ac4ed6fda
commit 43152d2597
5 changed files with 46 additions and 9 deletions

View File

@ -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 ./...

View File

@ -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
}

View File

@ -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,

View File

@ -14,8 +14,8 @@
<input type="text" ref="ids" v-model:value="ids" :placeholder="$L('multiple IDs can separated by commas')"/>
</div>
<div class="field">
<label>{{$L('select group')}}</label>
<Dropdown :title="$L('select group')" v-bind:items="prefetchs.groups" v-on:change="changeGroup" :selected="groups" :multiple="true"/>
<label>{{$L('select groups')}}</label>
<Dropdown :title="$L('select groups')" v-bind:items="prefetchs.groups" v-on:change="changeGroup" :selected="groups" :multiple="true"/>
</div>
<div class="field">
<label>{{$L('select nodes')}}</label>
@ -137,4 +137,4 @@ export default {
Dropdown
}
}
</script>
</script>

View File

@ -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) {