This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathvolt.go
102 lines (80 loc) · 2.15 KB
/
volt.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
94
95
96
97
98
99
100
101
102
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"time"
"github.com/Sirupsen/logrus"
"github.com/VoltFramework/volt/api"
"github.com/VoltFramework/volt/mesoslib"
"github.com/VoltFramework/volt/mesosproto"
)
var (
port int
master string
user string
ip string
debug bool
log = logrus.New()
frameworkName = "volt"
registerTimeout = 5 * time.Second
)
func init() {
flag.IntVar(&port, "port", 8080, "Port to listen on for the API")
flag.StringVar(&master, "master", "localhost:5050", "Master to connect to")
flag.BoolVar(&debug, "debug", false, "")
flag.StringVar(&user, "user", "root", "User to execute tasks as")
flag.StringVar(&ip, "ip", "", "IP address to listen on [default: autodetect]")
flag.Parse()
}
func waitForSignals(m *mesoslib.MesosLib) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
for sig := range signals {
log.Debugf("received signal %s unregistering framework\n", sig)
if err := m.UnRegisterFramework(); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
}
func setupLogger() error {
if debug {
log.Level = logrus.DebugLevel
}
if dsn := os.Getenv("SENTRY_DSN"); dsn != "" {
hook, err := newSentryHook(dsn, map[string]string{
"master": master,
"ip": ip,
"user": user,
})
if err != nil {
return err
}
log.Hooks.Add(hook)
}
return nil
}
func main() {
frameworkInfo := &mesosproto.FrameworkInfo{Name: &frameworkName, User: &user}
if err := setupLogger(); err != nil {
log.Fatal(err)
}
// initialize MesosLib
m := mesoslib.NewMesosLib(master, log, frameworkInfo, ip, port)
// start the API
api.ListenAndServe(m, port)
// try to register against the master
if err := m.RegisterFramework(); err != nil {
log.Fatal(err)
}
// wait for the registered event
select {
case event := <-m.GetEvent(mesosproto.Event_REGISTERED):
log.WithFields(logrus.Fields{"FrameworkId": *event.Registered.FrameworkId.Value}).Info("Registration successful.")
case <-time.After(registerTimeout):
log.WithField("--ip", ip).Fatal("Registration timed out. --ip must route to this host from the mesos-master.")
}
waitForSignals(m)
}