forked from getarcaneapp/arcane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.go
More file actions
431 lines (364 loc) · 14 KB
/
bootstrap.go
File metadata and controls
431 lines (364 loc) · 14 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package bootstrap
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/joho/godotenv"
"github.com/moby/moby/client"
"github.com/getarcaneapp/arcane/backend/internal/config"
"github.com/getarcaneapp/arcane/backend/internal/services"
libcrypto "github.com/getarcaneapp/arcane/backend/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/pkg/libarcane/edge"
tunnelpb "github.com/getarcaneapp/arcane/backend/pkg/libarcane/edge/proto/tunnel/v1"
"github.com/getarcaneapp/arcane/backend/pkg/libarcane/startup"
"github.com/getarcaneapp/arcane/backend/pkg/scheduler"
httputils "github.com/getarcaneapp/arcane/backend/pkg/utils/httpx"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
)
func Bootstrap(ctx context.Context) error {
_ = godotenv.Load()
cfg := config.Load()
SetupGinLogger(cfg)
ConfigureGormLogger(cfg)
slog.InfoContext(ctx, "Arcane is starting", "version", config.Version)
appCtx, cancelApp := context.WithCancel(ctx)
defer cancelApp()
db, err := initializeDBAndMigrate(appCtx, cfg)
if err != nil {
return fmt.Errorf("failed to initialize database: %w", err)
}
defer func(ctx context.Context) {
// Use background context for shutdown as appCtx is already canceled
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) //nolint:contextcheck
defer shutdownCancel()
if err := db.Close(); err != nil {
slog.ErrorContext(shutdownCtx, "Error closing database", "error", err) //nolint:contextcheck
}
}(appCtx)
httpClient := newConfiguredHTTPClient(cfg)
appServices, dockerClientService, err := initializeServices(appCtx, db, cfg, httpClient)
if err != nil {
return fmt.Errorf("failed to initialize services: %w", err)
}
defer func(ctx context.Context) {
baseCtx := context.WithoutCancel(ctx)
shutdownCtx, shutdownCancel := context.WithTimeout(baseCtx, 10*time.Second)
defer shutdownCancel()
if appServices.Volume != nil {
appServices.Volume.CleanupHelperContainers(shutdownCtx)
}
}(appCtx)
initializeStartupState(appCtx, cfg, appServices, dockerClientService, httpClient)
cronLocation := cfg.GetLocation()
scheduler := scheduler.NewJobScheduler(appCtx, cronLocation)
appServices.JobSchedule.SetScheduler(scheduler)
registerJobs(appCtx, scheduler, appServices, cfg)
router, tunnelServer := setupRouter(appCtx, cfg, appServices)
startEdgeTunnelClientIfConfigured(appCtx, cfg, router)
err = runServices(appCtx, cfg, router, tunnelServer, scheduler)
if err != nil {
return fmt.Errorf("failed to run services: %w", err)
}
slog.InfoContext(appCtx, "Arcane shutdown complete")
return nil
}
func newConfiguredHTTPClient(cfg *config.Config) *http.Client {
if cfg.HTTPClientTimeout > 0 {
return httputils.NewHTTPClientWithTimeout(time.Duration(cfg.HTTPClientTimeout) * time.Second)
}
return httputils.NewHTTPClient()
}
func initializeStartupState(appCtx context.Context, cfg *config.Config, appServices *Services, dockerClientService *services.DockerClientService, httpClient *http.Client) {
if appServices.Volume != nil {
if err := appServices.Volume.CleanupOrphanedVolumeHelpers(appCtx); err != nil {
slog.WarnContext(appCtx, "Failed to cleanup orphaned volume helpers on startup", "error", err)
}
}
runtimeCfg := &startup.RuntimeConfig{
AgentMode: cfg.AgentMode,
AgentToken: cfg.AgentToken,
Environment: string(cfg.Environment),
EncryptionKey: cfg.EncryptionKey,
AutoLoginUsername: cfg.AutoLoginUsername,
}
startup.LoadAgentToken(appCtx, runtimeCfg, appServices.Settings.GetStringSetting)
startup.EnsureEncryptionKey(appCtx, runtimeCfg, appServices.Settings.EnsureEncryptionKey)
cfg.AgentToken = runtimeCfg.AgentToken
cfg.EncryptionKey = runtimeCfg.EncryptionKey
libcrypto.InitEncryption(&libcrypto.Config{
EncryptionKey: cfg.EncryptionKey,
Environment: string(cfg.Environment),
AgentMode: cfg.AgentMode,
})
startup.InitializeDefaultSettings(appCtx, runtimeCfg, appServices.Settings)
startup.MigrateSchedulerCronValues(
appCtx,
appServices.Settings.GetStringSetting,
appServices.Settings.UpdateSetting,
appServices.Settings.LoadDatabaseSettings,
)
if appServices.GitOpsSync != nil {
startup.MigrateGitOpsSyncIntervals(
appCtx,
appServices.GitOpsSync.ListSyncIntervalsRaw,
appServices.GitOpsSync.UpdateSyncIntervalMinutes,
)
}
if err := appServices.Settings.NormalizeProjectsDirectory(appCtx, cfg.ProjectsDirectory); err != nil {
slog.WarnContext(appCtx, "Failed to normalize projects directory", "error", err)
}
if err := appServices.Settings.NormalizeBuildsDirectory(appCtx); err != nil {
slog.WarnContext(appCtx, "Failed to normalize builds directory", "error", err)
}
if err := appServices.Environment.EnsureLocalEnvironment(appCtx, cfg.AppUrl); err != nil {
slog.WarnContext(appCtx, "Failed to ensure local environment", "error", err)
}
if !cfg.AgentMode {
if err := appServices.Environment.ReconcileEdgeStatusesOnStartup(appCtx); err != nil {
slog.WarnContext(appCtx, "Failed to reconcile edge environment statuses on startup", "error", err)
}
}
startup.TestDockerConnection(appCtx, func(ctx context.Context) error {
dockerClient, err := dockerClientService.GetClient(ctx)
if err != nil {
return err
}
version, err := dockerClient.ServerVersion(ctx, client.ServerVersionOptions{})
if err != nil {
return err
}
effectiveAPIVersion := strings.TrimSpace(dockerClient.ClientVersion())
if effectiveAPIVersion == "" {
effectiveAPIVersion = strings.TrimSpace(version.APIVersion)
}
slog.InfoContext(ctx, "Docker API versions detected", "client_api_version", dockerClient.ClientVersion(), "server_api_version", version.APIVersion, "effective_api_version", effectiveAPIVersion)
return nil
})
startup.InitializeNonAgentFeatures(appCtx, runtimeCfg,
appServices.User.CreateDefaultAdmin,
func(ctx context.Context) error {
startup.InitializeAutoLogin(ctx, runtimeCfg)
return nil
},
appServices.Settings.MigrateOidcConfigToFields,
appServices.Notification.MigrateDiscordWebhookUrlToFields,
)
startup.CleanupUnknownSettings(appCtx, appServices.Settings)
// Handle agent auto-pairing with API key.
if cfg.AgentMode && cfg.AgentToken != "" && cfg.ManagerApiUrl != "" {
if err := handleAgentBootstrapPairing(appCtx, cfg, httpClient); err != nil {
slog.WarnContext(appCtx, "Failed to auto-pair agent with manager", "error", err)
}
}
}
func startEdgeTunnelClientIfConfigured(appCtx context.Context, cfg *config.Config, router http.Handler) {
managerEndpointConfigured := cfg.ManagerApiUrl != ""
if !cfg.EdgeAgent || !managerEndpointConfigured || cfg.AgentToken == "" {
return
}
edgeCfg := &edge.Config{
EdgeAgent: cfg.EdgeAgent,
EdgeTransport: cfg.EdgeTransport,
EdgeReconnectInterval: cfg.EdgeReconnectInterval,
ManagerApiUrl: cfg.ManagerApiUrl,
AgentToken: cfg.AgentToken,
Port: cfg.Port,
Listen: cfg.Listen,
}
slog.InfoContext(appCtx, "Starting edge tunnel client",
"transport_mode", edge.NormalizeEdgeTransport(edgeCfg.EdgeTransport),
"live_tunnel_attempt_grpc", edge.UseGRPCEdgeTransport(edgeCfg) || (edge.UsePollEdgeTransport(edgeCfg) && strings.TrimSpace(edgeCfg.GetManagerGRPCAddr()) != ""),
"live_tunnel_attempt_websocket", edge.UseWebSocketEdgeTransport(edgeCfg) || (edge.UsePollEdgeTransport(edgeCfg) && strings.TrimSpace(edgeCfg.GetManagerBaseURL()) != ""),
"manager_url", cfg.ManagerApiUrl,
)
errCh, err := edge.StartTunnelClientWithErrors(appCtx, edgeCfg, router)
if err != nil {
slog.ErrorContext(appCtx, "Failed to start edge tunnel client", "error", err)
return
}
slog.InfoContext(appCtx, "Edge tunnel client started", "manager_url", cfg.ManagerApiUrl)
go func() {
for err := range errCh {
slog.ErrorContext(appCtx, "Edge tunnel client error", "error", err)
}
}()
}
func handleAgentBootstrapPairing(ctx context.Context, cfg *config.Config, httpClient *http.Client) error {
slog.InfoContext(ctx, "Agent mode detected with token, attempting auto-pairing", "managerUrl", cfg.ManagerApiUrl)
pairURL := strings.TrimRight(cfg.GetManagerBaseURL(), "/") + "/api/environments/pair"
reqCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, pairURL, nil)
if err != nil {
return fmt.Errorf("failed to create pairing request: %w", err)
}
req.Header.Set("X-API-Key", cfg.AgentToken)
resp, err := httpClient.Do(req) //nolint:gosec // intentional request to configured manager pairing endpoint
if err != nil {
return fmt.Errorf("pairing request failed: %w", err)
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
switch resp.StatusCode {
case http.StatusOK:
slog.InfoContext(ctx, "Successfully paired agent with manager", "managerUrl", cfg.ManagerApiUrl)
return nil
case http.StatusBadRequest:
// Environment is not in pending status - already paired, this is fine
if strings.Contains(string(body), "not in pending status") {
slog.InfoContext(ctx, "Agent already paired with manager", "managerUrl", cfg.ManagerApiUrl)
return nil
}
return fmt.Errorf("pairing failed with status %d: %s", resp.StatusCode, string(body))
case http.StatusUnauthorized:
// Invalid API key - could be already paired with a different key, or key was deleted
// This is not fatal; the agent can still function if it has a valid token configured
slog.DebugContext(ctx, "Pairing skipped - API key not recognized (agent may already be paired)", "managerUrl", cfg.ManagerApiUrl)
return nil
default:
return fmt.Errorf("pairing failed with status %d: %s", resp.StatusCode, string(body))
}
}
func runServices(appCtx context.Context, cfg *config.Config, router http.Handler, tunnelServer *edge.TunnelServer, schedulers ...interface{ Run(context.Context) error }) error {
for _, s := range schedulers {
scheduler := s
go func() {
slog.InfoContext(appCtx, "Starting scheduler")
if err := scheduler.Run(appCtx); err != nil {
if !errors.Is(err, context.Canceled) {
slog.ErrorContext(appCtx, "Job scheduler exited with error", "error", err)
}
}
slog.InfoContext(appCtx, "Scheduler stopped")
}()
}
listenAddr := cfg.ListenAddr()
httpHandler := router
useTLS := cfg.TLSEnabled
tlsCertFile := strings.TrimSpace(cfg.TLSCertFile)
tlsKeyFile := strings.TrimSpace(cfg.TLSKeyFile)
if useTLS && (tlsCertFile == "" || tlsKeyFile == "") {
return fmt.Errorf("TLS_ENABLED requires both TLS_CERT_FILE and TLS_KEY_FILE")
}
var grpcServer *grpc.Server
if !cfg.AgentMode && tunnelServer != nil {
grpcServer = grpc.NewServer(tunnelServer.GRPCServerOptions(appCtx)...)
tunnelpb.RegisterTunnelServiceServer(grpcServer, tunnelServer)
httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isTunnelGRPCRequestInternal(r) {
grpcReq := normalizeTunnelGRPCRequestPathInternal(r)
grpcServer.ServeHTTP(w, grpcReq)
return
}
router.ServeHTTP(w, r)
})
slog.InfoContext(appCtx, "Using shared HTTP/gRPC listener for edge tunnel", "addr", listenAddr)
}
var protocols http.Protocols
protocols.SetHTTP1(true)
if useTLS {
protocols.SetHTTP2(true)
} else {
protocols.SetUnencryptedHTTP2(true)
httpHandler = h2c.NewHandler(httpHandler, &http2.Server{})
}
srv := &http.Server{
Addr: listenAddr,
Handler: httpHandler,
Protocols: &protocols,
ReadHeaderTimeout: 5 * time.Second,
}
go func() {
slog.InfoContext(appCtx, "Starting HTTP server", "addr", listenAddr, "listen", cfg.Listen, "port", cfg.Port, "tls_enabled", useTLS)
var err error
if useTLS {
err = srv.ListenAndServeTLS(tlsCertFile, tlsKeyFile)
} else {
err = srv.ListenAndServe()
}
if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.ErrorContext(appCtx, "Failed to start server", "error", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
select {
case <-quit:
slog.InfoContext(appCtx, "Received shutdown signal")
case <-appCtx.Done():
slog.InfoContext(appCtx, "Context canceled")
}
// Use background context for shutdown as appCtx is already canceled
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) //nolint:contextcheck
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil { //nolint:contextcheck
slog.ErrorContext(shutdownCtx, "Server forced to shutdown", "error", err) //nolint:contextcheck
return err
}
if grpcServer != nil {
grpcServer.GracefulStop()
}
// Wait for tunnel cleanup loop to finish
if tunnelServer != nil {
tunnelServer.WaitForCleanupDone()
}
slog.InfoContext(shutdownCtx, "Server stopped gracefully") //nolint:contextcheck
return nil
}
func normalizeTunnelGRPCRequestPathInternal(r *http.Request) *http.Request {
if r == nil {
return nil
}
if r.URL == nil {
return r
}
connectMethodPath := tunnelpb.TunnelService_Connect_FullMethodName
legacyAPIPath := "/api/tunnel/connect"
if strings.HasSuffix(r.URL.Path, legacyAPIPath) {
clone := r.Clone(r.Context())
cloneURL := *clone.URL
cloneURL.Path = connectMethodPath
clone.URL = &cloneURL
clone.RequestURI = connectMethodPath
return clone
}
idx := strings.Index(r.URL.Path, connectMethodPath)
if idx <= 0 {
return r
}
normalizedPath := r.URL.Path[idx:]
if normalizedPath == r.URL.Path {
return r
}
clone := r.Clone(r.Context())
cloneURL := *clone.URL
cloneURL.Path = normalizedPath
clone.URL = &cloneURL
clone.RequestURI = normalizedPath
return clone
}
func isTunnelGRPCRequestInternal(r *http.Request) bool {
if r == nil || r.URL == nil {
return false
}
if r.Method != http.MethodPost {
return false
}
path := r.URL.Path
fullMethodPath := tunnelpb.TunnelService_Connect_FullMethodName
if path == fullMethodPath || strings.HasSuffix(path, fullMethodPath) || strings.HasSuffix(path, "/api/tunnel/connect") {
return true
}
contentType := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type")))
return strings.HasPrefix(contentType, "application/grpc")
}