forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfleet.go
200 lines (159 loc) · 5.31 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/coreos/fleet/third_party/github.com/golang/glog"
"github.com/coreos/fleet/third_party/github.com/rakyll/globalconf"
"github.com/coreos/fleet/agent"
"github.com/coreos/fleet/config"
"github.com/coreos/fleet/server"
"github.com/coreos/fleet/sign"
"github.com/coreos/fleet/version"
)
const (
DefaultConfigFile = "/etc/fleet/fleet.conf"
)
func main() {
// We use a FlagSets since glog adds a bunch of flags we do not want to publish
userset := flag.NewFlagSet("fleet", flag.ExitOnError)
printVersion := userset.Bool("version", false, "Print the version and exit")
cfgPath := userset.String("config", "", fmt.Sprintf("Path to config file. Fleet will look for a config at %s by default.", DefaultConfigFile))
// Initialize logging so we have it set up while parsing config information
config.UpdateLoggingFlagsFromConfig(flag.CommandLine, &config.Config{})
err := userset.Parse(os.Args[1:])
if err == flag.ErrHelp {
userset.Usage()
syscall.Exit(1)
}
if *printVersion {
fmt.Println("fleet version", version.Version)
os.Exit(0)
}
cfgset := flag.NewFlagSet("fleet", flag.ExitOnError)
cfgset.Int("verbosity", 0, "Logging level")
cfgset.Var(&stringSlice{}, "etcd_servers", "List of etcd endpoints")
cfgset.String("boot_id", "", "Override default BootID of fleet machine")
cfgset.String("public_ip", "", "IP address that fleet machine should publish")
cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine")
cfgset.String("unit_prefix", "", "Prefix that should be used for all systemd units")
cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd")
cfgset.Bool("verify_units", false, "Verify unit file signatures using local SSH identities")
cfgset.String("authorized_keys_file", sign.DefaultAuthorizedKeysFile, "File containing public SSH keys to be used for signature verification")
globalconf.Register("", cfgset)
cfg, err := getConfig(cfgset, *cfgPath)
if err != nil {
glog.Error(err.Error())
syscall.Exit(1)
}
srv := server.New(*cfg)
srv.Run()
reconfigure := func() {
glog.Infof("Reloading configuration from %s", *cfgPath)
cfg, err := getConfig(cfgset, *cfgPath)
if err != nil {
glog.Errorf(err.Error())
syscall.Exit(1)
}
srv.Stop()
srv = server.New(*cfg)
srv.Run()
}
shutdown := func() {
glog.Infof("Gracefully shutting down")
srv.Stop()
srv.Purge()
syscall.Exit(0)
}
writeState := func() {
glog.Infof("Dumping server state")
encoded, err := json.Marshal(srv)
if err != nil {
glog.Errorf("Failed to dump server state: %v", err)
return
}
if _, err := os.Stdout.Write(encoded); err != nil {
glog.Errorf("Failed to dump server state: %v", err)
return
}
os.Stdout.Write([]byte("\n"))
glog.V(1).Infof("Finished dumping server state")
}
signals := map[os.Signal]func(){
syscall.SIGHUP: reconfigure,
syscall.SIGTERM: shutdown,
syscall.SIGINT: shutdown,
syscall.SIGUSR1: writeState,
}
listenForSignals(signals)
}
func getConfig(flagset *flag.FlagSet, userCfgFile string) (*config.Config, error) {
opts := globalconf.Options{EnvPrefix: "FLEET_"}
if userCfgFile != "" {
// Fail hard if a user-provided config is not usable
if _, err := os.Stat(userCfgFile); err != nil {
glog.Errorf("Unable to use config file %s: %v", userCfgFile, err)
os.Exit(1)
}
glog.Infof("Using provided config file %s", userCfgFile)
opts.Filename = userCfgFile
} else if _, err := os.Stat(DefaultConfigFile); err == nil {
glog.Infof("Using default config file %s", DefaultConfigFile)
opts.Filename = DefaultConfigFile
} else {
glog.Infof("Continuing without config file")
}
gconf, err := globalconf.NewWithOptions(&opts)
if err != nil {
return nil, err
}
gconf.ParseSet("", flagset)
cfg := config.Config{
Verbosity: (*flagset.Lookup("verbosity")).Value.(flag.Getter).Get().(int),
EtcdServers: (*flagset.Lookup("etcd_servers")).Value.(flag.Getter).Get().(stringSlice),
BootId: (*flagset.Lookup("boot_id")).Value.(flag.Getter).Get().(string),
PublicIP: (*flagset.Lookup("public_ip")).Value.(flag.Getter).Get().(string),
RawMetadata: (*flagset.Lookup("metadata")).Value.(flag.Getter).Get().(string),
UnitPrefix: (*flagset.Lookup("unit_prefix")).Value.(flag.Getter).Get().(string),
AgentTTL: (*flagset.Lookup("agent_ttl")).Value.(flag.Getter).Get().(string),
VerifyUnits: (*flagset.Lookup("verify_units")).Value.(flag.Getter).Get().(bool),
AuthorizedKeysFile: (*flagset.Lookup("authorized_keys_file")).Value.(flag.Getter).Get().(string),
}
config.UpdateLoggingFlagsFromConfig(flag.CommandLine, &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 stringSlice []string
func (f *stringSlice) Set(value string) error {
for _, item := range strings.Split(value, ",") {
item = strings.TrimLeft(item, " [\"")
item = strings.TrimRight(item, " \"]")
*f = append(*f, item)
}
return nil
}
func (f *stringSlice) String() string {
return fmt.Sprintf("%v", *f)
}
func (f *stringSlice) Value() []string {
return *f
}
func (f *stringSlice) Get() interface{} {
return *f
}