forked from AVENTER-UG/mesos-compose
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
130 lines (111 loc) · 3.17 KB
/
app.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
127
128
129
130
package main
import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"net/http"
"time"
"github.com/AVENTER-UG/mesos-compose/api"
"github.com/AVENTER-UG/mesos-compose/redis"
"github.com/AVENTER-UG/mesos-compose/scheduler"
cfg "github.com/AVENTER-UG/mesos-compose/types"
util "github.com/AVENTER-UG/util/util"
"github.com/AVENTER-UG/util/vault"
"github.com/sirupsen/logrus"
)
// BuildVersion of m3s
var BuildVersion string
// GitVersion is the revision and commit number
var GitVersion string
// convert Base64 Encodes PEM Certificate to tls object
func decodeBase64Cert(pemCert string) []byte {
sslPem, err := base64.URLEncoding.DecodeString(pemCert)
if err != nil {
logrus.Fatal("Error decoding SSL PEM from Base64: ", err.Error())
}
return sslPem
}
func main() {
// Prints out current version
var version bool
flag.BoolVar(&version, "v", false, "Prints current version")
flag.Parse()
if version {
fmt.Print(GitVersion)
return
}
util.SetLogging(config.LogLevel, config.EnableSyslog, config.AppName)
logrus.Println(config.AppName + " build " + BuildVersion + " git " + GitVersion)
// Connect the vault if we got a token
v := vault.New(config.VaultToken, config.VaultURL, config.VaultTimeout)
if config.VaultToken != "" {
logrus.Info("Vault Connection: ")
logrus.Info(v.Connect())
}
r := redis.New(&config, &framework)
if !r.Connect() {
logrus.WithField("func", "main").Fatal("Could not connect to redis DB")
}
// get API
a := api.New(&config, &framework)
a.Redis = r
// load old framework config from database if they exist
var oldFramework cfg.FrameworkConfig
key := r.GetRedisKey(framework.FrameworkName + ":framework")
if key != "" {
json.Unmarshal([]byte(key), &oldFramework)
framework.FrameworkInfo.Id = oldFramework.FrameworkInfo.Id
framework.MesosStreamID = oldFramework.MesosStreamID
}
// The Hostname should ever be set after reading the state file.
framework.FrameworkInfo.Hostname = &framework.FrameworkHostname
r.SaveConfig(config)
r.SaveFrameworkRedis(&framework)
server := &http.Server{
Addr: config.Listen,
Handler: a.Commands(),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
TLSConfig: &tls.Config{
ClientAuth: tls.RequestClientCert,
MinVersion: tls.VersionTLS12,
},
}
if config.SSLCrt != "" && config.SSLKey != "" {
logrus.Debug("Enable TLS")
crt := decodeBase64Cert(config.SSLCrt)
key := decodeBase64Cert(config.SSLKey)
certs, err := tls.X509KeyPair(crt, key)
if err != nil {
logrus.Fatal("TLS Server Error: ", err.Error())
}
server.TLSConfig.Certificates = []tls.Certificate{certs}
}
go func() {
if config.SSLCrt != "" && config.SSLKey != "" {
server.ListenAndServeTLS("", "")
} else {
server.ListenAndServe()
}
}()
go loadPlugins(r)
// this loop is for resubscribtion purpose
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
//nolint:gosimple
for {
select {
case <-ticker.C:
e := scheduler.Subscribe(&config, &framework)
e.API = a
e.Vault = v
e.Redis = r
e.EventLoop()
time.Sleep(60 * time.Second)
}
}
}