This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
125 lines (108 loc) · 3.3 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
package main
import (
"os"
"github.com/op/go-logging"
"github.com/microscaling/microbadger/api"
"github.com/microscaling/microbadger/database"
"github.com/microscaling/microbadger/encryption"
"github.com/microscaling/microbadger/hub"
"github.com/microscaling/microbadger/inspector"
"github.com/microscaling/microbadger/queue"
"github.com/microscaling/microbadger/registry"
"github.com/microscaling/microbadger/utils"
)
var (
log = logging.MustGetLogger("microbadger")
)
const constPollQueueTimeout = 250 // milliseconds - how often to check the queue for images.
func init() {
utils.InitLogging()
}
// For this prototype either inspects the image and saves the metadata or shows
// the stored metadata.
func main() {
var err error
var image string
var db database.PgDB
var qs queue.Service
if os.Getenv("MB_QUEUE_TYPE") == "nats" {
qs = queue.NewNatsService()
} else {
qs = queue.NewSqsService()
}
cmd := utils.GetArgOrLogError("cmd", 1)
if cmd != "api" && cmd != "inspector" && cmd != "size" {
image = utils.GetArgOrLogError("image", 2)
}
db, err = database.GetDB()
if err != nil {
log.Errorf("Failed to get DB: %v", err)
return
}
switch cmd {
case "api":
log.Info("starting microbadger api")
rs := registry.NewService()
hs := hub.NewService()
es := encryption.NewService()
api.StartServer(db, qs, rs, hs, es)
case "inspector":
log.Info("starting inspector")
hs := hub.NewService()
rs := registry.NewService()
es := encryption.NewService()
startInspector(db, qs, hs, rs, es)
case "size":
log.Info("starting size inspector")
rs := registry.NewService()
es := encryption.NewService()
startSizeInspector(db, qs, rs, es)
case "feature":
log.Infof("Feature image %s", image)
err := db.FeatureImage(image, true)
if err != nil {
log.Error("Failed to feature %s: %v", image, err)
}
case "unfeature":
log.Infof("Unfeature image %s", image)
err := db.FeatureImage(image, false)
if err != nil {
log.Error("Failed to unfeature %s: %v", image, err)
}
default:
log.Errorf("Unrecognised command: %v", cmd)
}
}
func startInspector(db database.PgDB, qs queue.Service, hs hub.InfoService, rs registry.Service, es encryption.Service) {
for {
img := qs.ReceiveImage()
if img != nil && img.ImageName != "" {
log.Infof("Received Image: %v", img.ImageName)
err := inspector.Inspect(img.ImageName, &db, &rs, &hs, qs, es)
// If we failed to inspect this item, it might well be because Docker Hub has behaved badly.
// By not deleting it, it will get resent again at some point in the future.
if err == nil {
qs.DeleteImage(img)
// Getting the size information can take a while so we do this asynchronously.
// We have different environment variables for deciding which queue to send & receive on.
qs.SendImage(img.ImageName, "Sent for size inspection")
} else {
log.Errorf("Failed to inspect %s", img.ImageName)
}
}
}
}
func startSizeInspector(db database.PgDB, qs queue.Service, rs registry.Service, es encryption.Service) {
for {
img := qs.ReceiveImage()
if img != nil {
log.Debugf("Received Image for size processing: %v", img.ImageName)
err := inspector.InspectSize(img.ImageName, &db, &rs, es)
if err == nil {
qs.DeleteImage(img)
} else {
log.Errorf("size inspection of %s: %v", img.ImageName, err)
}
}
}
}