-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
317 lines (275 loc) · 8.15 KB
/
main.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
package main
import (
"bufio"
"context"
"crypto/sha256"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"github.com/LINBIT/bestdrbdmodule/pkg/repos"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
var GitCommit string
const pkgUrl = "https://packages.linbit.com/yum/index.kmp.json"
var (
flagAddr = flag.String("addr", ":8080", "Server address")
flagRepo = flag.String("repo", "drbd-9", "Repository to use")
flagDists = flag.String("dist", "rhel7,rhel8,rhel9", "Distributions")
flagFetch = flag.Duration("fetch", 20*time.Minute, "Package DB fetch interval")
flagMaxBytesBody = flag.Int("maxbytesbody", 250*1024, "Maximum nunber of bytes in the body")
flagCertFile = flag.String("certfile", "", "Path to a TLS cert file")
flagKeyFile = flag.String("keyfile", "", "Path to a TLS key file")
flagDebug = flag.Bool("debug", false, "Enable debug logging (otherwise production level log)")
flagVersion = flag.Bool("version", false, "Print version and exit")
)
type server struct {
router *mux.Router
versions map[string]versionMap
kmodCache map[string]moduleCache
maxBytesBody int64
logger *zap.Logger
m sync.Mutex
}
type moduleCache map[[32]byte]string
func main() {
flag.Parse()
if *flagVersion {
fmt.Printf("Git-commit: '%s'\n", GitCommit)
os.Exit(0)
}
if *flagMaxBytesBody < 0 {
log.Fatal("maxbytesbody has to be a positive value")
}
s := &server{
router: mux.NewRouter(),
kmodCache: make(map[string]moduleCache),
maxBytesBody: int64(*flagMaxBytesBody),
versions: make(map[string]versionMap),
}
// additional setup
var err error
if *flagDebug {
s.logger, err = zap.NewDevelopment()
} else {
s.logger, err = zap.NewProduction()
}
if err != nil {
log.Fatal(err)
}
s.routes()
// try to initially fill kmods before we serve the first request
s.updateKmps(*flagRepo, *flagDists)
go s.repoWatcher(*flagRepo, *flagDists, *flagFetch)
server := http.Server{
Addr: *flagAddr,
Handler: s,
MaxHeaderBytes: 4 * 1024,
}
if *flagCertFile != "" && *flagKeyFile != "" {
log.Fatal(server.ListenAndServeTLS(*flagCertFile, *flagKeyFile))
} else {
log.Fatal(server.ListenAndServe())
}
}
// handler interface, wrapped for MaxBytesReader
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, s.maxBytesBody)
s.router.ServeHTTP(w, r)
}
func (s *server) hello() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/text")
if _, err := fmt.Fprintf(w, "Successfully connected to bestkernelmatch ('%s')\n", GitCommit); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}
func (s *server) bestKmod() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/text")
kernelRelease, ok := mux.Vars(r)["kernelrelease"]
if !ok || kernelRelease == "" || len(kernelRelease) > 42 {
s.errorf(http.StatusBadRequest, r.RemoteAddr, w, "Could not get valid kernelrelease parameter")
return
}
// this piece of fine work called centos does not properly set version information in os-release
// and they don't give a damn, so we can not use lbdisttool without /etc/centos_release
// BUT for kmod in this context we don't care about the centos dot release, so we actually can use
// their broken os-release and then use disttool with a forced version
h := sha256.New()
if _, err := h.Write([]byte(kernelRelease)); err != nil {
s.errorf(http.StatusInternalServerError, r.RemoteAddr, w, "Could not hash input line")
return
}
dist, major := "", -1
scanner := bufio.NewScanner(r.Body)
for scanner.Scan() {
text := scanner.Text()
// hash early/everything
// do not break out of this loop
if _, err := h.Write([]byte(text)); err != nil {
s.errorf(http.StatusInternalServerError, r.RemoteAddr, w, "Could not hash input line")
return
}
text = strings.TrimSpace(text)
if strings.HasPrefix(text, "#") {
continue
}
kv := strings.SplitN(text, "=", 2)
if len(kv) < 2 {
continue
}
k := strings.TrimSpace(kv[0])
v := strings.TrimSpace(kv[1])
if k == "ID" {
dist = osreleaseValue(v)
} else if k == "VERSION_ID" {
v = osreleaseValue(v)
majorminor := strings.SplitN(v, ".", 2)
if len(majorminor) == 0 {
continue
}
var err error
major, err = strconv.Atoi(majorminor[0])
if err != nil {
major = -1
continue
}
}
}
if err := scanner.Err(); err != nil {
s.errorf(http.StatusBadRequest, r.RemoteAddr, w, "Scanner error while processing your os-release: %v", err)
return
}
hSum := sha256.Sum256(h.Sum(nil))
// mappings
if dist == "centos" || dist == "almalinux" || dist == "rocky" {
dist = "rhel"
}
if dist == "" {
s.errorf(http.StatusBadRequest, r.RemoteAddr, w, "Could not determine distribution from os-release")
return
}
if major == -1 {
s.errorf(http.StatusBadRequest, r.RemoteAddr, w, "Could not determine major release from os-release")
return
}
dist = fmt.Sprintf("%s%d", dist, major)
if hit, ok := s.getKmodCache(dist, hSum); ok {
s.logger.Info("Cache hit for: " + hit)
if _, err := fmt.Fprint(w, hit); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
s.m.Lock()
defer s.m.Unlock()
versions, ok := s.versions[dist]
if !ok {
s.errorf(http.StatusBadRequest, r.RemoteAddr, w, "Distribution '%v' not supported", dist)
return
} else if len(versions) == 0 {
s.errorf(http.StatusInternalServerError, r.RemoteAddr, w, "No versions for distribution '%v'", dist)
return
}
lbdisttool, err := exec.LookPath("lbdisttool.py")
if err != nil {
s.errorf(http.StatusInternalServerError, r.RemoteAddr, w, "lbdistool: %v", err)
return
}
args := []string{
lbdisttool,
"--force-name", dist,
"--force-kernel-release", kernelRelease,
"-k",
}
for _, v := range versions {
args = append(args, v.pkg)
}
s.logger.Info("python3 " + strings.Join(args, " "))
out, err := exec.Command("python3", args...).Output()
if err != nil {
s.errorf(http.StatusBadRequest, r.RemoteAddr, w, "lbdistool: %v", err)
return
}
// if there is no hit, lbdisttool errors out.
hit := strings.TrimSpace(string(out))
s._setKmodCache(dist, hSum, hit)
if _, err := fmt.Fprint(w, hit); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}
func (s *server) updateKmps(repo, dists string) {
repoInfo, err := repos.Get(context.Background(), pkgUrl)
if err != nil { // not fatal, we just can not offer anything
s.logger.Info("Could not fetch repo info: " + err.Error())
}
for _, d := range strings.Split(dists, ",") {
kmps := repoInfo.GetKmps(repo, d, "amd64")
if len(kmps) == 0 {
continue
}
vmap, err := filterKmps(kmps)
if err != nil {
s.logger.Info("filterKmps: " + err.Error())
continue
}
s.m.Lock()
s.versions[d] = vmap
// invalidate cache
s.kmodCache[d] = make(moduleCache)
s.m.Unlock()
}
}
func (s *server) repoWatcher(repo, dists string, interval time.Duration) {
for {
s.updateKmps(repo, dists)
time.Sleep(interval)
}
}
func (s *server) getKmodCache(dist string, hSum [32]byte) (string, bool) {
s.m.Lock()
defer s.m.Unlock()
c, ok := s.kmodCache[dist]
if !ok {
return "", false
}
v, ok := c[hSum]
return v, ok
}
// needs to hold the lock
func (s *server) _setKmodCache(dist string, hSum [32]byte, value string) {
_, ok := s.kmodCache[dist]
if !ok {
s.kmodCache[dist] = make(moduleCache)
}
s.kmodCache[dist][hSum] = value
}
func (s *server) errorf(code int, remoteAddr string, w http.ResponseWriter, format string, a ...interface{}) {
w.WriteHeader(code)
_, _ = fmt.Fprintf(w, format, a...)
s.logger.Error(fmt.Sprintf(format, a...),
zap.String("type", "error"),
zap.String("remoteAddr", remoteAddr),
zap.Int("code", code))
}
// does not handle things like asym quotes but good enough...
func osreleaseValue(s string) string {
if len(s) > 0 && (s[0] == '"' || s[0] == '\'') {
s = s[1:]
}
if len(s) > 0 && (s[len(s)-1] == '"' || s[len(s)-1] == '\'') {
s = s[:len(s)-1]
}
return s
}