-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
327 lines (315 loc) · 6.81 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"errors"
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/cenkalti/log"
"github.com/getsentry/raven-go"
// Register MySQL database driver.
_ "github.com/go-sql-driver/mysql"
"github.com/urfave/cli"
)
var Version string
func init() {
log.DefaultHandler.SetLevel(log.DEBUG)
if Version == "" {
Version = "v0.0.0"
}
}
func main() {
rand.Seed(time.Now().UnixNano())
cfg := NewConfig()
chunkSize := ChunkSize(1 * M)
app := cli.NewApp()
app.Version = Version
app.Usage = "Simple yet powerful distributed file system"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "/etc/efes.toml",
Usage: "configuration file path",
EnvVar: "EFES_CONFIG",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "enable debug log",
},
cli.BoolFlag{
Name: "no-debug, D",
Usage: "disable debug log",
},
}
app.Before = func(c *cli.Context) error {
err := cfg.ReadFile(c.GlobalString("config"))
if err != nil {
log.Fatalln("Cannot read config:", err)
}
if c.IsSet("debug") {
cfg.Debug = true
}
if c.IsSet("no-debug") {
cfg.Debug = false
}
err = raven.SetDSN(cfg.SentryDSN)
if err != nil {
log.Warningln("Cannot set Sentry DSN:", err)
}
raven.SetRelease(Version)
return nil
}
app.Commands = []cli.Command{
{
Name: "tracker",
Usage: "runs Tracker process",
Action: func(c *cli.Context) error {
t, err := NewTracker(cfg)
if err != nil {
return err
}
runUntilInterrupt(t)
return nil
},
},
{
Name: "server",
Usage: "runs Server process",
Action: func(c *cli.Context) error {
s, err := NewServer(cfg)
if err != nil {
return err
}
runUntilInterrupt(s)
return nil
},
},
{
Name: "write",
Usage: "write file to efes",
ArgsUsage: "key path",
Flags: []cli.Flag{
cli.GenericFlag{
Name: "chunk, c",
Usage: "chunk size",
Value: &chunkSize,
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
cli.ShowAppHelpAndExit(c, 1)
}
key := c.Args().Get(0)
path := c.Args().Get(1)
if c.IsSet("chunk") {
cfg.Client.ChunkSize = chunkSize
}
client, err := NewClient(cfg)
if err != nil {
return err
}
if path == "-" {
return client.WriteReader(key, os.Stdin)
}
return client.Write(key, path)
},
},
{
Name: "read",
Usage: "read file from efes",
ArgsUsage: "key path",
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
cli.ShowAppHelpAndExit(c, 1)
}
key := c.Args().Get(0)
path := c.Args().Get(1)
client, err := NewClient(cfg)
if err != nil {
return err
}
return client.Read(key, path)
},
},
{
Name: "delete",
Usage: "delete file from efes",
ArgsUsage: "key",
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
cli.ShowAppHelpAndExit(c, 1)
}
key := c.Args().Get(0)
client, err := NewClient(cfg)
if err != nil {
return err
}
return client.Delete(key)
},
},
{
Name: "exists",
Usage: "check if a key exists in efes",
ArgsUsage: "key",
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
cli.ShowAppHelpAndExit(c, 1)
}
key := c.Args().Get(0)
client, err := NewClient(cfg)
if err != nil {
return err
}
exists, err := client.Exists(key)
if err != nil {
return err
}
if !exists {
return errors.New("key does not exist")
}
return nil
},
},
{
Name: "status",
Usage: "show system status",
Flags: []cli.Flag{
cli.StringFlag{
Name: "sort, s",
Usage: "sort devices by column",
Value: "zone",
},
},
Action: func(c *cli.Context) error {
client, err := NewClient(cfg)
if err != nil {
return err
}
s, err := client.Status(c.String("sort"))
if err != nil {
return err
}
s.Print()
return nil
},
},
{
Name: "drain",
Usage: "drain device by moving files to another device",
Flags: []cli.Flag{
cli.StringFlag{
Name: "dest, d",
Usage: "move files to given devices",
},
},
Action: func(c *cli.Context) error {
d, err := NewDrainer(cfg)
if err != nil {
return err
}
destFlag := c.String("dest")
if destFlag != "" {
for _, devidString := range strings.Split(destFlag, ",") {
devid, err := strconv.ParseInt(strings.TrimSpace(devidString), 10, 64)
if err != nil {
return err
}
d.Dest = append(d.Dest, devid)
}
}
runUntilInterrupt(d)
return nil
},
},
{
Name: "ready",
Hidden: true,
Flags: []cli.Flag{
cli.DurationFlag{
Name: "timeout",
Value: 10 * time.Second,
},
},
Subcommands: []cli.Command{
{
Name: "mysql",
Flags: []cli.Flag{
cli.StringFlag{
Name: "exec",
},
},
Action: func(c *cli.Context) error {
return readyMysql(cfg.Database, c.Parent().Duration("timeout"), c.String("exec"))
},
},
{
Name: "rabbitmq",
Action: func(c *cli.Context) error {
return readyRabbitmq(cfg.AMQP, c.Parent().Duration("timeout"))
},
},
},
},
{
Name: "mount",
Action: func(c *cli.Context) error {
mountPoint := c.Args().Get(0)
return mount(cfg, mountPoint)
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
type process interface {
// Run does the main work for the process.
// Run must return nil when Shutdown is called.
Run() error
// Shutdown stops the running Run method.
// After shutdown is called Run must return.
Shutdown() error
}
// runUntilInterrupt runs s until it exits cleanly.
// If SIGINT or SIGTERM is received, s is asked to shutdown gracefully.
// if SIGINT or SIGTERM is received again while waiting for shutdown, process exits with exit code 1.
func runUntilInterrupt(p process) {
shutdown := make(chan struct{})
go handleSignals(p, shutdown)
err := p.Run()
if err == nil {
log.Notice("Process end successfully.")
return
}
log.Fatal(err)
// Run returned with no error. Wait process.Shutdown to return before exit.
<-shutdown
log.Notice("Process shut down successfully.")
}
func handleSignals(p process, shutdown chan struct{}) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
first := true
for sig := range signals {
log.Notice("Got signal: ", sig)
if first {
log.Notice("Shutting down process. Process will exit when active requests are finished.")
log.Notice("Press Ctrl-C again to kill the process.")
go shutdownProcess(p, shutdown)
first = false
} else {
log.Error("Exiting with code 1.")
os.Exit(1)
}
}
}
func shutdownProcess(p process, shutdown chan struct{}) {
if err := p.Shutdown(); err != nil {
log.Fatal(err)
}
close(shutdown)
}