forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
46 lines (37 loc) · 994 Bytes
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"regexp"
"time"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/docker/swarm/discovery"
)
func checkAddrFormat(addr string) bool {
m, _ := regexp.MatchString("^[0-9a-zA-Z._-]+:[0-9]{1,5}$", addr)
return m
}
func join(c *cli.Context) {
dflag := getDiscovery(c)
if dflag == "" {
log.Fatalf("discovery required to join a cluster. See '%s join --help'.", c.App.Name)
}
d, err := discovery.New(dflag, c.Int("heartbeat"))
if err != nil {
log.Fatal(err)
}
addr := c.String("addr")
if !checkAddrFormat(addr) {
log.Fatal("--addr should be of the form ip:port or hostname:port")
}
if err := d.Register(addr); err != nil {
log.Fatal(err)
}
hb := time.Duration(c.Int("heartbeat"))
for {
log.WithFields(log.Fields{"addr": addr, "discovery": dflag}).Infof("Registering on the discovery service every %d seconds...", hb)
time.Sleep(hb * time.Second)
if err := d.Register(addr); err != nil {
log.Error(err)
}
}
}