forked from Dataman-Cloud/swan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (102 loc) · 2.58 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"flag"
"fmt"
"net/url"
"os"
"github.com/Dataman-Cloud/swan/api"
"github.com/Dataman-Cloud/swan/backend"
"github.com/Dataman-Cloud/swan/health"
"github.com/Dataman-Cloud/swan/mesosproto/mesos"
"github.com/Dataman-Cloud/swan/scheduler"
"github.com/Dataman-Cloud/swan/store/boltdb"
"github.com/Dataman-Cloud/swan/types"
"github.com/Sirupsen/logrus"
"github.com/andygrunwald/megos"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
)
var (
addr string
masters string
user string
consulAddr string
debug bool
)
func init() {
flag.StringVar(&addr, "addr", "127.0.0.1:9999", "API Server address <ip:port>")
flag.StringVar(&masters, "masters", "127.0.0.1:5050", "masters address <ip:port>,<ip:port>...")
flag.StringVar(&user, "user", "root", "mesos user")
flag.StringVar(&consulAddr, "consul", "127.0.0.1:8500", "Consul address <ip:port>")
flag.BoolVar(&debug, "debug", false, "log level")
flag.Parse()
}
func setupLogger() {
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
logrus.SetOutput(os.Stderr)
logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
}
func main() {
hostname, err := os.Hostname()
if err != nil {
hostname = "UNKNOWN"
}
fw := &mesos.FrameworkInfo{
User: proto.String(user),
Name: proto.String("swan"),
Hostname: proto.String(hostname),
FailoverTimeout: proto.Float64(60 * 60 * 24 * 7),
}
setupLogger()
db, err := bolt.Open("bolt.db", 0644, nil)
if err != nil {
logrus.Errorf("Init store engine failed:%s", err)
return
}
defer db.Close()
store := boltdb.NewBoltdbStore(db)
frameworkId, err := store.GetFrameworkID()
if err != nil {
logrus.Errorf("get framework id failed: %s", err)
}
if frameworkId != "" {
fw.Id = &mesos.FrameworkID{
Value: proto.String(frameworkId),
}
}
msgQueue := make(chan types.ReschedulerMsg, 1)
masters := []string{masters}
masterUrls := make([]*url.URL, 0)
for _, master := range masters {
masterUrl, _ := url.Parse(fmt.Sprintf("http://%s", master))
masterUrls = append(masterUrls, masterUrl)
}
mesos := megos.NewClient(masterUrls, nil)
state, err := mesos.GetStateFromCluster()
if err != nil {
panic(err)
}
cluster := state.Cluster
if cluster == "" {
cluster = "Unnamed"
}
sched := scheduler.NewScheduler(
state.Leader,
fw,
store,
cluster,
health.NewHealthCheckManager(store, msgQueue),
msgQueue,
)
backend := backend.NewBackend(sched, store)
srv := api.NewServer(backend)
go func() {
srv.ListenAndServe(addr)
}()
<-sched.Start()
}