forked from Lucretius/vault_raft_snapshot_agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (71 loc) · 1.52 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
package main
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/wasilak/vault_raft_snapshot_agent/config"
"github.com/wasilak/vault_raft_snapshot_agent/snapshot_agent"
"golang.org/x/exp/slog"
)
func listenForInterruptSignals() chan bool {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, 1)
go func() {
<-sigs
done <- true
}()
return done
}
func main() {
slog.Info("Reading configuration...")
c, err := config.ReadConfig()
logLevel := "info"
if ll := os.Getenv("VRSA_LOGLEVEL"); ll != "" {
logLevel = ll
}
logFormat := "plain"
if lg := os.Getenv("VRSA_LOGFORMAT"); lg != "" {
logFormat = lg
}
config.InitLogging(logLevel, logFormat)
if err != nil {
slog.Error("Configuration could not be found")
os.Exit(1)
}
snapshotter, err := snapshot_agent.NewSnapshotter(c)
if err != nil {
slog.Error("Cannot instantiate snapshotter.", slog.AnyValue(err))
os.Exit(1)
}
if c.Daemon {
done := listenForInterruptSignals()
frequency, err := time.ParseDuration(c.Frequency)
if err != nil {
frequency = time.Hour
}
for {
result, err := snapshot_agent.RunBackup(snapshotter, c)
if err != nil {
slog.Error(err.Error())
} else {
slog.Info(result)
}
select {
case <-time.After(frequency):
continue
case <-done:
os.Exit(1)
}
}
} else {
result, err := snapshot_agent.RunBackup(snapshotter, c)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
} else {
slog.Info(result)
}
}
}