-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
207 lines (186 loc) · 6.68 KB
/
Copy pathmain.go
File metadata and controls
207 lines (186 loc) · 6.68 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
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
package main
import (
"context"
"cs-agent/backup"
"cs-agent/config"
"cs-agent/firewall"
"cs-agent/httpapi"
"cs-agent/job"
"cs-agent/log"
"cs-agent/s3upload"
"cs-agent/store"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/getsentry/sentry-go"
"github.com/spf13/viper"
)
// Build-info, injected at release time via GoReleaser ldflags
// (-X main.version / -X main.commit / -X main.date). Defaults apply to
// `go build` / `go run` so behavior is unchanged when unset.
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
showVersion := flag.Bool("version", false, "print version information and exit")
flag.Parse()
if *showVersion {
fmt.Printf("cs-agent %s (commit %s, built %s)\n", version, commit, date)
return
}
config.ConfigureApp()
configureSentry(version)
log.New().Info("Starting CS-Agent", "version", version, "commit", commit, "date", date)
validateExportConfig()
// Open the embedded data plane. control.db is the sole source of truth for
// coordination state now (no Consul); opening it also runs migrations.
st, err := store.Open(viper.GetString("store.data_dir"), store.Options{})
if err != nil {
log.New().Error("Failed to open control store", "error", err.Error())
sentry.CaptureException(err)
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
// In-process coordination components (all read/write control.db).
dispatcher := job.NewDispatcher(st)
fwReconciler := firewall.NewReconciler(st)
// control.db retention runs unconditionally (independent of backups.enabled).
housekeeper := backup.NewHousekeeper(st)
var scheduler *backup.Scheduler
if viper.GetBool("backups.enabled") {
scheduler = backup.NewScheduler(st, dispatcher.Signal)
}
// Customer-metadata + admin HTTP front door. Reconcile hooks wake the
// in-process consumers after a controller DOWN write (all non-blocking).
srv := httpapi.New(httpapi.Config{
ListenAddr: viper.GetString("metadata.listen_addr"),
AdminTokenHash: viper.GetString("metadata.admin_token_hash"),
MaxBodyBytes: viper.GetInt64("metadata.max_body_bytes"),
OnTaskCreated: dispatcher.Signal,
OnFirewallChanged: fwReconciler.Signal,
OnVolumesChanged: func() {
if scheduler != nil {
scheduler.ReconcileSignal()
}
},
}, st, log.New())
// Start order: components (dispatcher runs its boot crash-reconcile before
// accepting work) → then the HTTP front door LAST, so the DOWN surface only
// opens once the consumers that react to it are running.
dispatcher.Start(ctx, &wg) // registers its own worker/loop wg counts
wg.Add(1)
go func() { defer wg.Done(); fwReconciler.Run(ctx) }()
wg.Add(1)
go func() { defer wg.Done(); housekeeper.Run(ctx) }()
if scheduler != nil {
wg.Add(1)
go func() { defer wg.Done(); scheduler.Run(ctx) }()
}
go func() {
if err := srv.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.New().Error("metadata HTTP server stopped", "error", err.Error())
sentry.CaptureException(err)
}
}()
log.New().Info("Agent Configuration", "environment", config.ReleaseEnvironment())
log.New().Info("Agent Configuration", "backupWorkers", viper.GetInt("queue.numworkers")+1)
log.New().Info("Agent Configuration", "backingFS", currentBackupMethod())
// Ordered shutdown. On SIGTERM/SIGINT:
// 1. Shut the HTTP DOWN surface first (drain in-flight customer requests;
// they use the store) so a late DOWN write can't signal a reconciler we're
// about to stop. In-process signals are non-blocking, so this order is safe.
// 2. Cancel ctx → dispatcher/workers/scheduler/firewall observe it and drain.
// 3. Bounded wait for the workers/loops to finish an in-flight job.
// 4. Close the store (nothing uses it after the workers stop).
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
<-sig
log.New().Info("Shutdown signal received, draining")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.New().Warn("metadata server shutdown", "error", err)
}
cancel()
waitWorkers(&wg, 25*time.Second)
if err := st.Close(); err != nil {
log.New().Warn("control store close", "error", err)
}
log.New().Info("Shutdown complete")
}
// waitWorkers blocks until wg drains or timeout elapses, whichever is first. The
// dispatcher/scheduler/firewall loops all observe ctx cancel, so wg normally
// drains promptly; the bound is a safety net so a worker stuck in a long-running
// borg job can't hold shutdown open forever.
func waitWorkers(wg *sync.WaitGroup, timeout time.Duration) {
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
log.New().Info("Workers stopped")
case <-time.After(timeout):
log.New().Warn("Workers did not stop within timeout; proceeding with shutdown", "timeout", timeout.String())
}
}
func currentBackupMethod() string {
if viper.GetBool("backups.borg.ssh.enabled") {
return "ssh"
} else if viper.GetBool("backups.borg.nfs") {
return "nfs"
} else {
return "local"
}
}
// validateExportConfig loudly surfaces export misconfiguration at startup. It
// does not abort the agent (export jobs fail individually if misconfigured), but
// the no-compactor combination is dangerous enough to flag prominently: with
// export enabled and the host compact cron retired, an empty compact_freq means
// nothing ever compacts repos and they grow unbounded.
func validateExportConfig() {
if viper.GetString("backups.export.s3.bucket") == "" {
return // export disabled (no bucket)
}
if viper.GetString("backups.compact_freq") == "" {
log.New().Error("backups.export is enabled but backups.compact_freq is empty: with the host compact cron retired, nothing compacts repositories and they will grow unbounded. Set backups.compact_freq.")
sentry.CaptureMessage("backup export enabled with compaction disabled (no compactor)")
}
if err := s3upload.ConfigFromViper().Validate(0); err != nil {
log.New().Error("Invalid backups.export.s3 configuration", "error", err.Error())
sentry.CaptureException(err)
}
}
/*
*
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)
}
}