-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (79 loc) · 1.83 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/caarlos0/env"
log "github.com/sirupsen/logrus"
"github.com/ski2per/gru/minion"
)
var m = minion.Minion{}
func init() {
// Init configuration from evnironmental variables
// cfg := config{}
if err := env.Parse(&m); err != nil {
fmt.Printf("%+v\n", err)
}
if len(m.MinionID) <= 0 {
hostname, err := os.Hostname()
if err != nil {
log.Error("Error while getting hostname")
log.Errorln(err)
}
m.MinionID = hostname
}
// Init Logrus, default to INFO
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.00000",
})
logLvl, err := log.ParseLevel(m.LogLevel)
if err != nil {
logLvl = log.InfoLevel
}
log.SetLevel(logLvl)
// Skip TLS secure verify
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
func main() {
fmt.Printf("\nMinion: %s\n\n", minion.Version)
log.Debugf("%+v\n", m)
randomPort := -1
// Use os.Singal channel to some cleanups
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
log.Info("Got OS signal, Deregister...")
if randomPort > 0 {
m.Deregister(randomPort)
}
os.Exit(1)
}()
for {
log.Info("Trying to get random reversed port from Gru")
randomPort = m.GetRandomPort()
log.Infof("Got random reversed port: %d\n", randomPort)
internalIP := minion.GetLocalAddr()
meta := minion.Meta{
Name: m.MinionID,
Port: randomPort,
InternalIP: internalIP.String(),
}
err := m.Register(meta)
if err != nil {
time.Sleep(2 * time.Second)
continue
}
err = minion.ConnectToGru(&m, randomPort)
if err != nil {
log.Error("Lost connection to Gru, try to reconnect...")
time.Sleep(2 * time.Second)
continue
}
}
}