/
hoardix.go
386 lines (322 loc) · 9.4 KB
/
hoardix.go
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
package hoardix
import (
"bufio"
"bytes"
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"time"
"github.com/allegro/bigcache/v3"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"github.com/simonswine/hoardix/pkg/cache"
"github.com/simonswine/hoardix/pkg/httputil"
"github.com/simonswine/hoardix/pkg/storage"
"github.com/simonswine/hoardix/pkg/token"
"github.com/simonswine/hoardix/pkg/validate"
)
var (
appName string = "hoardix"
version string = "unknown"
commitHash string = "unknown"
commitTimestamp string = "unknown"
)
type Config struct {
BaseURL URL `yaml:"base_url,omitempty"`
ListenPort *int `yaml:"listen_port,omitempty"`
MetricsListenPort *int `yaml:"metrics_listen_port,omitempty"`
Tokens []token.Config `yaml:"tokens,omitempty"`
Storage storage.Config `yaml:"storage,omitempty"`
Caches map[string]cache.Config `yaml:"caches,omitempty"`
}
type URL struct {
*url.URL
}
func (u URL) MarshalYAML() (interface{}, error) {
return u.URL.String(), nil
}
func (u *URL) UnmarshalYAML(value *yaml.Node) error {
var strValue string
if err := value.Decode(&strValue); err != nil {
return err
}
url, err := url.Parse(strValue)
if err != nil {
return err
}
u.URL = url
return nil
}
func (c *Config) String() string {
var buf = new(bytes.Buffer)
enc := yaml.NewEncoder(buf)
enc.SetIndent(2)
if err := enc.Encode(c); err != nil {
panic(err)
}
return buf.String()
}
func readConfigFromFile(path string) (*Config, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
return nil, fmt.Errorf("error opening YAML file: %w", err)
}
dec := yaml.NewDecoder(f)
dec.KnownFields(true)
var config Config
if err := dec.Decode(&config); err != nil {
return nil, fmt.Errorf("error parsing YAML file: %w", err)
}
return &config, nil
}
type App struct {
cfg *Config
cli *cli.App
storage storage.Storage
logger zerolog.Logger
caches map[string]*cache.Cache
narinfoCache *bigcache.BigCache
}
func New() *App {
logger :=
log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339Nano,
})
app := &App{
cli: &cli.App{
Name: appName,
Authors: []*cli.Author{
{Name: "Christian Simon", Email: fmt.Sprintf("simon+%s@swine.de", appName)},
},
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config-file",
Value: "config.yaml",
Usage: "path to configuration file",
},
},
Usage: "Hoardix self-hosted Nix derivation binary cache",
},
logger: logger,
caches: make(map[string]*cache.Cache),
}
app.cli.Action = app.run
return app
}
func (a *App) handleCacheInfo(w http.ResponseWriter, r *http.Request) {
cache := a.cacheOrNotFound(w, r)
if cache == nil {
return
}
cache.HandleCacheInfo(w, r)
}
func (a *App) handleUploadNar(w http.ResponseWriter, r *http.Request) {
cache := a.cacheOrNotFound(w, r)
if cache == nil {
return
}
cache.HandleUploadNar(w, r)
}
func (a *App) handleUploadNarinfo(w http.ResponseWriter, r *http.Request) {
cache := a.cacheOrNotFound(w, r)
if cache == nil {
return
}
cache.HandleUploadNarinfo(w, r)
}
// relative path in bucket
func (a *App) cacheOrNotFound(w http.ResponseWriter, r *http.Request) *cache.Cache {
vars := mux.Vars(r)
cacheName := vars["cache_name"]
c, ok := a.caches[cacheName]
if ok {
return c
}
httputil.WriteError(w, r, &httputil.Error{
StatusCode: http.StatusNotFound,
Msg: fmt.Sprintf("cache '%s' not found", cacheName),
//err: "cache not found",
})
return nil
}
func (a *App) handleCacheNarinfo(w http.ResponseWriter, r *http.Request) {
cache := a.cacheOrNotFound(w, r)
if cache == nil {
return
}
cache.HandleCacheNarinfo(w, r)
}
func (a *App) handleCacheRead(w http.ResponseWriter, r *http.Request) {
cache := a.cacheOrNotFound(w, r)
if cache == nil {
return
}
cache.HandleCacheRead(w, r)
}
func (a *App) initRouter() http.Handler {
router := mux.NewRouter().StrictSlash(true)
router.Use(token.New(a.cfg.Tokens).Middleware)
// check hostname for cache name
hostMatcher := fmt.Sprintf("{cache_name:[a-z0-9-_]+}.%s", a.cfg.BaseURL.Hostname())
router.Path(`/nix-cache-info`).Host(hostMatcher).Methods("GET").HandlerFunc(a.handleCacheRead)
router.Path(`/{path_narinfo:[a-z0-9-_]+}.narinfo`).Host(hostMatcher).Methods("GET").HandlerFunc(a.handleCacheRead)
router.Path(`/nar/{path_nar:[a-z0-9-_]+}.nar.xz`).Host(hostMatcher).Methods("GET").HandlerFunc(a.handleCacheRead)
apiRouter := router.PathPrefix("/api/v1").Subrouter()
apiRouter.HandleFunc("/cache", a.handleCacheInfo).Methods("GET")
apiRouter.HandleFunc("/cache/{cache_name:[a-z0-9-_]+}", a.handleCacheInfo).Methods("GET")
apiRouter.HandleFunc("/cache/{cache_name:[a-z0-9-_]+}/narinfo", a.handleCacheNarinfo).Methods("POST")
apiRouter.HandleFunc("/cache/{cache_name:[a-z0-9-_]+}/nar", a.handleUploadNar).Methods("POST")
apiRouter.HandleFunc("/cache/{cache_name:[a-z0-9-_]+}/{path_narinfo:[a-z0-9-_]+}.narinfo", a.handleUploadNarinfo).Methods("POST")
return router
}
const metricNamespace = "hoardix"
func (a *App) Run(args []string) error {
return a.cli.Run(args)
}
func (a *App) run(ctx *cli.Context) error {
// parse timestamp to a readable format
if ts, err := strconv.ParseInt(commitTimestamp, 10, 64); err == nil {
promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: metricNamespace,
Name: "commit_date_timestamp",
Help: "hoardix's commit date in unix timestamp",
},
).Set(float64(ts))
commitTimestamp = time.Unix(ts, 0).Format(time.RFC3339)
}
promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: metricNamespace,
Name: "info",
Help: "hoardix's build version and commit hash",
ConstLabels: prometheus.Labels{"version": version, "commit_hash": commitHash},
},
).Set(1)
a.logger.Info().Str("version", version).Str("commit", commitHash).Str("commit_time", commitTimestamp).Str("go_version", runtime.Version()).Msg("starting hoardix")
var err error
a.cfg, err = readConfigFromFile(ctx.String("config-file"))
if err != nil {
return err
}
a.logger.Debug().Msg("parsed config")
scanner := bufio.NewScanner(strings.NewReader(a.cfg.String()))
for scanner.Scan() {
a.logger.Debug().Msg(scanner.Text())
}
// init storage
a.storage, err = storage.New(&a.cfg.Storage)
if err != nil {
return err
}
// create narinfo cache
bigcacheCfg := bigcache.DefaultConfig(time.Hour * 24)
bigcacheCfg.HardMaxCacheSize = 512
a.narinfoCache, err = bigcache.NewBigCache(bigcacheCfg)
if err != nil {
return err
}
// initialize caches
for name, cfg := range a.cfg.Caches {
if errs := validate.IsValidName(name); len(errs) != 0 {
return fmt.Errorf("invalid cache name %s: %s", name, strings.Join(errs, ","))
}
a.caches[name], err = cache.New(
name,
&cfg,
a,
)
if err != nil {
return err
}
}
logHandler := hlog.NewHandler(a.logger)
accessHandler := hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Debug().
Str("method", r.Method).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
})
// signal handler
ctxSignal, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
listenPort := 5000
if a.cfg.ListenPort != nil {
listenPort = *a.cfg.ListenPort
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", listenPort),
Handler: logHandler(accessHandler(a.initRouter())),
}
go func() {
if err = srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
a.logger.Fatal().Err(err).Msgf("error listening on '%s' for HTTP traffic", srv.Addr)
}
}()
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
// Opt into OpenMetrics to support exemplars.
EnableOpenMetrics: true,
},
))
metricsListenPort := 9500
if a.cfg.MetricsListenPort != nil {
metricsListenPort = *a.cfg.MetricsListenPort
}
metricsSrv := &http.Server{
Addr: fmt.Sprintf(":%d", metricsListenPort),
Handler: metricsMux,
}
go func() {
if err = metricsSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
a.logger.Fatal().Err(err).Msgf("error listening on '%s' for metrics", srv.Addr)
}
}()
// wait till signal to shutdown
<-ctxSignal.Done()
ctxStopTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer func() {
cancel()
}()
if err = srv.Shutdown(ctxStopTimeout); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("error shutting down http server: %w", err)
}
if err = metricsSrv.Shutdown(ctxStopTimeout); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("error shutting down metrics server: %w", err)
}
a.logger.Info().Msg("http servers exited properly")
return nil
}
func (a *App) Logger() zerolog.Logger {
return a.logger
}
func (a *App) NarinfoCache() cache.NarinfoCache {
return a.narinfoCache
}
func (a *App) Storage() storage.Storage {
return a.storage
}
func (a *App) BaseURL() *url.URL {
var u = *a.cfg.BaseURL.URL
return &u
}