-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathstart.go
More file actions
183 lines (156 loc) · 5 KB
/
start.go
File metadata and controls
183 lines (156 loc) · 5 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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package daemon
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
reconcilerconfig "github.com/agntcy/dir/reconciler/config"
reconciler "github.com/agntcy/dir/reconciler/service"
"github.com/agntcy/dir/reconciler/tasks/indexer"
"github.com/agntcy/dir/reconciler/tasks/name"
"github.com/agntcy/dir/reconciler/tasks/regsync"
"github.com/agntcy/dir/reconciler/tasks/signature"
"github.com/agntcy/dir/server"
serverconfig "github.com/agntcy/dir/server/config"
dbconfig "github.com/agntcy/dir/server/database/config"
namingconfig "github.com/agntcy/dir/server/naming/config"
publication "github.com/agntcy/dir/server/publication/config"
routingconfig "github.com/agntcy/dir/server/routing/config"
storeconfig "github.com/agntcy/dir/server/store/config"
ociconfig "github.com/agntcy/dir/server/store/oci/config"
"github.com/agntcy/dir/utils/logging"
"github.com/spf13/cobra"
ocistore "oras.land/oras-go/v2/content/oci"
)
var logger = logging.Logger("daemon")
var startCmd = &cobra.Command{
Use: "start",
Short: "Start the local directory daemon",
Long: `Start the gRPC apiserver and reconciler in a single process with local-mode
defaults (embedded SQLite, filesystem OCI store, all reconciler tasks in-process).
The daemon blocks until SIGINT or SIGTERM is received.`,
RunE: runStart,
}
func runStart(cmd *cobra.Command, _ []string) error {
running, pid, err := readPID()
if err != nil {
return err
}
if running {
return fmt.Errorf("daemon already running (pid %d)", pid)
}
if err := os.MkdirAll(opts.DataDir, 0o700); err != nil { //nolint:mnd
return fmt.Errorf("failed to create data directory %s: %w", opts.DataDir, err)
}
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
serverCfg := buildServerConfig()
reconcilerCfg := buildReconcilerConfig()
// Start the gRPC server.
srv, err := server.New(ctx, serverCfg, nil)
if err != nil {
return fmt.Errorf("failed to create server: %w", err)
}
if err := srv.Start(ctx); err != nil {
return fmt.Errorf("failed to start server: %w", err)
}
defer srv.Close(ctx)
logger.Info("Server started", "address", serverCfg.ListenAddress)
localRepo, err := ocistore.New(opts.StoreDir())
if err != nil {
return fmt.Errorf("failed to open local OCI store for indexer: %w", err)
}
svc, err := reconciler.New(reconcilerCfg, srv.Database(), srv.Store(), localRepo)
if err != nil {
return fmt.Errorf("failed to create reconciler: %w", err)
}
if err := svc.Start(ctx); err != nil {
return fmt.Errorf("failed to start reconciler: %w", err)
}
defer func() {
if err := svc.Stop(); err != nil {
logger.Error("Failed to stop reconciler", "error", err)
}
}()
logger.Info("Reconciler started")
if err := writePIDFile(); err != nil {
return fmt.Errorf("failed to write PID file: %w", err)
}
defer removePIDFile()
logger.Info("Daemon ready", "data_dir", opts.DataDir, "pid", os.Getpid())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigCh:
logger.Info("Received signal, shutting down", "signal", sig)
case <-ctx.Done():
logger.Info("Context cancelled, shutting down")
}
return nil
}
func buildServerConfig() *serverconfig.Config {
return &serverconfig.Config{
ListenAddress: "localhost:8888",
Connection: serverconfig.DefaultConnectionConfig(),
OASFAPIValidation: serverconfig.OASFAPIValidationConfig{
SchemaURL: "https://schema.oasf.outshift.com",
},
Store: storeconfig.Config{
Provider: storeconfig.DefaultProvider,
OCI: ociconfig.Config{
LocalDir: opts.StoreDir(),
},
Verification: storeconfig.VerificationConfig{
Enabled: storeconfig.DefaultVerificationEnabled,
},
},
Routing: routingconfig.Config{
ListenAddress: "/ip4/0.0.0.0/tcp/0",
BootstrapPeers: []string{},
DatastoreDir: opts.RoutingDir(),
GossipSub: routingconfig.GossipSubConfig{
Enabled: routingconfig.DefaultGossipSubEnabled,
},
},
Database: dbconfig.Config{
Type: "sqlite",
SQLite: dbconfig.SQLiteConfig{
Path: opts.DBFile(),
},
},
Publication: publication.Config{
SchedulerInterval: publication.DefaultPublicationSchedulerInterval,
WorkerCount: publication.DefaultPublicationWorkerCount,
WorkerTimeout: publication.DefaultPublicationWorkerTimeout,
},
Naming: namingconfig.Config{
TTL: namingconfig.DefaultTTL,
},
}
}
func buildReconcilerConfig() *reconcilerconfig.Config {
return &reconcilerconfig.Config{
Regsync: regsync.Config{
Enabled: false,
},
Indexer: indexer.Config{
Enabled: true,
Interval: indexer.DefaultInterval,
},
Signature: signature.Config{
Enabled: true,
Interval: signature.DefaultInterval,
TTL: signature.DefaultTTL,
RecordTimeout: signature.DefaultRecordTimeout,
},
Name: name.Config{
Enabled: true,
Interval: name.DefaultInterval,
TTL: namingconfig.DefaultTTL,
RecordTimeout: name.DefaultRecordTimeout,
},
}
}