1package app
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "time"
8
9 "github.com/bnema/zerowrap"
10
11 "github.com/bnema/gordon/internal/boundaries/in"
12 configusecase "github.com/bnema/gordon/internal/usecase/config"
13)
14
15// Kernel provides in-process service access for local CLI execution.
16//
17// It intentionally does not start HTTP servers or register signal handlers.
18type Kernel struct {
19 authEnabled bool
20 configSvc in.ConfigService
21 containerSvc in.ContainerService
22 backupSvc in.BackupService
23 volumeBackupSvc in.VolumeBackupService
24 registrySvc in.RegistryService
25 healthSvc in.HealthService
26 logSvc in.LogService
27 volumeSvc in.VolumeService
28 publicTLSSvc in.PublicTLSService
29 appSvc in.AppService
30 // appAdmin is the daemon-owned app lifecycle (when the full wiring is
31 // available). Close cancels and joins its in-flight background deploy
32 // executions before any other kernel resource is torn down.
33 appAdmin appAdministration
34 log zerowrap.Logger
35 cleanup func()
36}
37
38// NewKernel initializes local services without starting server listeners.
39func NewKernel(configPath string) (*Kernel, error) {
40 return newKernel(configPath, initLogger)
41}
42
43// NewKernelQuiet initializes local services without emitting console logs.
44func NewKernelQuiet(configPath string) (*Kernel, error) {
45 return newKernel(configPath, quietInitLogger)
46}
47
48type kernelLoggerInit func(cfg Config) (zerowrap.Logger, func(), error)
49
50func newKernel(configPath string, initLog kernelLoggerInit) (*Kernel, error) {
51 ctx := context.Background()
52 v, cfg, err := initConfig(configPath)
53 if err != nil {
54 return nil, err
55 }
56
57 log, cleanup, err := initLog(cfg)
58 if err != nil {
59 return nil, err
60 }
61 if cleanup == nil {
62 cleanup = func() {}
63 }
64
65 ctx = zerowrap.WithCtx(ctx, log)
66
67 // Prefer full service wiring so local CLI can execute the same operations
68 // as remote mode without going through HTTP admin endpoints.
69 // ACME Reconcile and renewal loop are only started from runServers,
70 // so there are no side effects for read-only CLI commands.
71 if svc, fullErr := createServicesWithOptions(ctx, v, cfg, log); fullErr == nil {
72 // Wrap cleanup to stop public TLS service (with its renewal loop)
73 // before the logger is cleaned up.
74 wrappedCleanup := func() {
75 if svc.publicTLSSvc != nil {
76 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
77 defer cancel()
78 if err := svc.publicTLSSvc.Stop(ctx); err != nil {
79 log.Warn().Err(err).Msg("failed to stop public TLS service")
80 }
81 }
82 cleanup()
83 }
84
85 kernel := &Kernel{
86 authEnabled: cfg.Auth.Enabled,
87 configSvc: svc.configSvc,
88 containerSvc: svc.containerSvc,
89 backupSvc: svc.backupSvc,
90 volumeBackupSvc: svc.volumeBackupSvc,
91 registrySvc: svc.registrySvc,
92 healthSvc: svc.healthSvc,
93 logSvc: svc.logSvc,
94 volumeSvc: svc.volumeSvc,
95 publicTLSSvc: svc.publicTLSSvc,
96 appSvc: svc.appSvc,
97 log: log,
98 cleanup: wrappedCleanup,
99 }
100 // A nil *AppServiceImpl must not be stored in the interface: the
101 // interface would be non-nil and Close would call it.
102 if svc.appSvcImpl != nil {
103 kernel.appAdmin = svc.appSvcImpl
104 }
105 return kernel, nil
106 } else {
107 log.Warn().Err(fullErr).Msg("local kernel running in minimal mode")
108 }
109
110 configSvc := configusecase.NewService(v, nil)
111 if err := configSvc.Load(ctx); err != nil {
112 cleanup()
113 return nil, fmt.Errorf("failed to load configuration: %w", err)
114 }
115
116 return &Kernel{
117 authEnabled: cfg.Auth.Enabled,
118 configSvc: configSvc,
119 log: log,
120 cleanup: cleanup,
121 }, nil
122}
123
124func quietInitLogger(Config) (zerowrap.Logger, func(), error) {
125 return zerowrap.New(zerowrap.Config{Level: "disabled", Output: io.Discard}), func() {}, nil
126}
127
128// Close tears the kernel down. It first cancels and joins daemon-owned app
129// administration on a bounded context, so a background deploy execution is
130// never torn down mid-flight alongside the state and runtime it uses. When
131// that quiescence times out the remaining cleanup is skipped and the error is
132// returned: an unfinished execution may still be writing to state.
133func (k *Kernel) Close() error {
134 if k == nil {
135 return nil
136 }
137 if k.appAdmin != nil {
138 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
139 err := quiesceAppAdministration(ctx, k.appAdmin, k.log)
140 cancel()
141 if err != nil {
142 return err
143 }
144 }
145 if k.cleanup != nil {
146 k.cleanup()
147 }
148 return nil
149}
150
151func (k *Kernel) Config() in.ConfigService { return k.configSvc }
152
153func (k *Kernel) Container() in.ContainerService { return k.containerSvc }
154
155func (k *Kernel) Backup() in.BackupService { return k.backupSvc }
156
157func (k *Kernel) VolumeBackup() in.VolumeBackupService { return k.volumeBackupSvc }
158
159func (k *Kernel) Registry() in.RegistryService { return k.registrySvc }
160
161func (k *Kernel) Health() in.HealthService { return k.healthSvc }
162
163func (k *Kernel) Logs() in.LogService { return k.logSvc }
164
165func (k *Kernel) Volumes() in.VolumeService { return k.volumeSvc }
166
167func (k *Kernel) PublicTLS() in.PublicTLSService { return k.publicTLSSvc }
168
169func (k *Kernel) Apps() in.AppService { return k.appSvc }
170
171func (k *Kernel) AuthEnabled() bool { return k != nil && k.authEnabled }