forked from Lucretius/vault_raft_snapshot_agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
68 lines (53 loc) · 1.65 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
package main
import (
"bytes"
"time"
log "github.com/sirupsen/logrus"
"vault_raft_snapshotter/config"
"vault_raft_snapshotter/snapshot"
)
func main() {
log.Infoln("Reading configuration...")
c, err := config.ReadConfig()
if err != nil {
log.Fatalln("Configuration could not be found")
}
snapshotter, err := snapshot.NewSnapshotter(c)
if err != nil {
log.Fatalln("Failed to create snapshotter")
}
if snapshotter.TokenExpiration.Before(time.Now()) && (c.Vault.RoleID != "" && c.Vault.SecretID != "") {
if err = snapshotter.SetClientTokenFromAppRole(c); err != nil {
log.Fatalf("Unable to login to vault")
}
}
var snapshot bytes.Buffer
err = snapshotter.API.Sys().RaftSnapshot(&snapshot)
if err != nil {
log.Fatalln("Unable to generate snapshot", err.Error())
}
now := time.Now().UnixNano()
if c.Local.Path != "" {
snapshotPath, err := snapshotter.CreateLocalSnapshot(&snapshot, c, now)
logSnapshotError("local", snapshotPath, err)
}
if c.AWS.Bucket != "" {
snapshotPath, err := snapshotter.CreateS3Snapshot(&snapshot, c, now)
logSnapshotError("aws", snapshotPath, err)
}
if c.GCP.Bucket != "" {
snapshotPath, err := snapshotter.CreateGCPSnapshot(&snapshot, c, now)
logSnapshotError("gcp", snapshotPath, err)
}
if c.Azure.ContainerName != "" {
snapshotPath, err := snapshotter.CreateAzureSnapshot(&snapshot, c, now)
logSnapshotError("azure", snapshotPath, err)
}
}
func logSnapshotError(dest, snapshotPath string, err error) {
if err != nil {
log.Errorf("Failed to generate %s snapshot to %s: %v\n", dest, snapshotPath, err)
} else {
log.Infof("Successfully created %s snapshot to %s\n", dest, snapshotPath)
}
}