-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servant.go
64 lines (49 loc) · 1.83 KB
/
servant.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"time"
cli "github.com/jawher/mow.cli"
dehub "github.com/ysmood/dehub/lib"
)
type servantConf struct {
id string
hubAddr string
websocket bool
retryInterval RetryInterval
prvKey string
pubKeys []string
jsonOutput bool
}
func setupServantCLI(app *cli.Cli) {
app.Command("s servant",
"A client that connects to the hub server to follow the master client.",
func(c *cli.Cmd) {
var conf servantConf
c.Spec = "-p [OPTIONS] PUBLIC_KEYS..."
c.StringOptPtr(&conf.hubAddr, "a addr", "dehub.ysmood.org:8813", "The address of the hub server.")
c.StringOptPtr(&conf.id, "i id", id(), "The id of the servant. It should be unique.")
c.BoolOptPtr(&conf.websocket, "w ws", false,
"Use websocket to connect to hub. If set, the addr should be a websocket address.")
c.VarOpt("r retry-interval", &conf.retryInterval, "The retry interval in seconds.")
c.StringOptPtr(&conf.prvKey, "p private-key", "", "The private key file path.")
c.StringsArgPtr(&conf.pubKeys, "PUBLIC_KEYS", nil,
"The list of github user id, public key content, or path that are allowed to connect to the servant. "+
"The github user id must be prefix with @ .")
c.BoolOptPtr(&conf.jsonOutput, "j json", true, "json output to stdout")
c.Action = func() { runServant(conf) }
})
}
func runServant(conf servantConf) {
logger := output(conf.jsonOutput)
servant := dehub.NewServant(dehub.ServantID(conf.id), privateKey(conf.prvKey), publicKeys(logger, conf.pubKeys))
servant.Logger = logger
for {
conn, err := dial(conf.websocket, conf.hubAddr)
if err != nil {
logger.Error("failed to connect to the hub", "err", err)
} else {
servant.Serve(conn)()
}
logger.Info("servant retries to connect to the hub", "wait", conf.retryInterval.String())
time.Sleep(conf.retryInterval.Get())
}
}