-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (92 loc) · 2.23 KB
/
main.go
File metadata and controls
103 lines (92 loc) · 2.23 KB
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
package main
import (
"cs-agent/backup"
"cs-agent/cnslclient"
"cs-agent/config"
"cs-agent/job"
"cs-agent/log"
"os"
"sync"
"time"
"github.com/getsentry/sentry-go"
"github.com/robfig/cron/v3"
"github.com/spf13/viper"
)
func main() {
var wg sync.WaitGroup
v := "1.7.0"
config.ConfigureApp()
configureSentry(v)
ensureConsulReady()
wgCount := 1 + viper.GetInt("queue.numworkers") // job.Watch() + Workers that will be created
wg.Add(wgCount)
if viper.GetBool("backups.enabled") {
c := cron.New()
consul, err := cnslclient.Client()
if err != nil {
panic(err)
}
backup.InitSchedule(consul.KV(), c)
}
go job.Watch(&wg)
log.New().Info("Starting CS-Agent", "version", v)
log.New().Info("Agent Configuration", "environment", config.ReleaseEnvironment())
log.New().Info("Agent Configuration", "backupWorkers", wgCount)
log.New().Info("Agent Configuration", "firewallWorkers", 1)
log.New().Info("Agent Configuration", "backingFS", currentBackupMethod())
//select {} // Hold open the process
wg.Wait()
}
func currentBackupMethod() string {
if viper.GetBool("backups.borg.ssh.enabled") {
return "ssh"
} else if viper.GetBool("backups.borg.nfs") {
return "nfs"
} else {
return "local"
}
}
func ensureConsulReady() {
count := 0
RETRY:
cli, err := cnslclient.Client()
if err != nil {
log.New().Error("Error loading consul config", "error", err.Error())
panic(err)
}
statusObj := cli.Status()
_, err = statusObj.Leader()
if err != nil {
if count > 10 {
log.New().Error("Fatal error, unable to connect to consul")
panic(err)
}
count = count + 1
log.New().Warn("Failed to connect to consul", "retry", count)
time.Sleep(5 * time.Second)
goto RETRY
}
}
/*
*
Configure Sentry
Resources:
- https://github.com/getsentry/sentry-go
- https://github.com/getsentry/sentry-go/blob/master/MIGRATION.md
- https://docs.sentry.io/clients/go/
*/
func configureSentry(v string) {
env := config.ReleaseEnvironment()
hostname, _ := os.Hostname()
err := sentry.Init(sentry.ClientOptions{
Dsn: viper.GetString("sentry.dsn"),
Environment: env,
Debug: env != "production",
ServerName: hostname,
AttachStacktrace: true,
Release: v,
})
if err != nil {
panic(err)
}
}