forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleet.go
134 lines (108 loc) · 2.75 KB
/
fleet.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
131
132
133
134
package main
import (
"errors"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/coreos/fleet/third_party/github.com/coreos/go-etcd/etcd"
"github.com/coreos/fleet/third_party/github.com/golang/glog"
"github.com/coreos/fleet/config"
"github.com/coreos/fleet/server"
"github.com/coreos/fleet/version"
)
func main() {
// We use a custom FlagSet since golang/glog adds a bunch of flags we
// do not want to publish
flagset := flag.NewFlagSet("fleet", flag.ExitOnError)
printVersion := flagset.Bool("version", false, "Prints the version.")
cfgPath := flagset.String("config", "", "Path to config file.")
err := flagset.Parse(os.Args[1:])
// We do this manually since we're using a custom FlagSet
if err == flag.ErrHelp {
flag.Usage()
syscall.Exit(1)
}
if *printVersion {
fmt.Println("fleet version", version.Version)
os.Exit(0)
}
// Print out to stderr by default (stderr instead of stdout due to glog's choices)
flag.Lookup("logtostderr").Value.Set("true")
cfg, err := loadConfigFromPath(*cfgPath)
if err != nil {
glog.Errorf(err.Error())
syscall.Exit(1)
}
etcd.SetLogger(etcdLogger{})
srv := server.New(*cfg)
srv.Run()
reconfigure := func() {
glog.Infof("Reloading config file from %s", *cfgPath)
cfg, err := loadConfigFromPath(*cfgPath)
if err != nil {
glog.Errorf(err.Error())
syscall.Exit(1)
} else {
srv.Stop()
srv = server.New(*cfg)
srv.Run()
}
}
shutdown := func() {
glog.Infof("Gracefully shutting down")
srv.Stop()
srv.Purge()
syscall.Exit(0)
}
signals := map[os.Signal]func(){
syscall.SIGHUP: reconfigure,
syscall.SIGTERM: shutdown,
syscall.SIGINT: shutdown,
}
listenForSignals(signals)
}
func loadConfigFromPath(cp string) (*config.Config, error) {
cfg := config.NewConfig()
if cp != "" {
cfgFile, err := os.Open(cp)
if err != nil {
msg := fmt.Sprintf("Unable to open config file at %s: %s", cp, err)
return nil, errors.New(msg)
}
err = config.UpdateConfigFromFile(cfg, cfgFile)
if err != nil {
msg := fmt.Sprintf("Failed to parse config file at %s: %s", cp, err)
return nil, errors.New(msg)
}
}
config.UpdateFlagsFromConfig(cfg)
return cfg, nil
}
func listenForSignals(sigmap map[os.Signal]func()) {
sigchan := make(chan os.Signal, 1)
for k, _ := range sigmap {
signal.Notify(sigchan, k)
}
for true {
sig := <-sigchan
handler, ok := sigmap[sig]
if ok {
handler()
}
}
}
type etcdLogger struct{}
func (el etcdLogger) Debug(args ...interface{}) {
glog.V(3).Info(args...)
}
func (el etcdLogger) Debugf(fmt string, args ...interface{}) {
glog.V(3).Infof(fmt, args...)
}
func (el etcdLogger) Warning(args ...interface{}) {
glog.Warning(args...)
}
func (el etcdLogger) Warningf(fmt string, args ...interface{}) {
glog.Warningf(fmt, args...)
}