Only ever run one scan at the same time

Also use a logging library
This commit is contained in:
2020-11-10 20:14:43 +01:00
parent e8afa72dfb
commit 28c9f61569
9 changed files with 155 additions and 72 deletions

72
main.go
View File

@ -3,78 +3,32 @@ package main
import (
"fmt"
"net/http"
"os"
"github.com/docker/docker/client"
"github.com/sirupsen/logrus"
)
func checkForUpdates() {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
hub, err := anonymousClient()
if err != nil {
panic(err)
}
labeledContainers := getLabeledContainers(cli)
fmt.Println()
for _, lc := range labeledContainers {
owner, repository, tag := lc.SplitImageParts()
fmt.Printf("container image: %s\n", combineImageParts(owner, repository, nil))
fmt.Printf(" versioning: %+v\n", lc.Mode.Label())
fmt.Printf(" id: %s\n", lc.Container.ImageID)
if tag != nil {
fmt.Printf(" current tag: %s\n", *tag)
} else {
fmt.Printf(" no current tag, skipping\n")
continue
}
repoTags, err := getDockerRepoTags(hub, owner, repository)
if err != nil {
panic(err)
}
fmt.Println(" tags in registry:")
for _, tag := range repoTags {
fmt.Printf(" - \"%s\" %s\n", tag.Name, tag.Digest)
svt := parseTagAsSemVer(tag.Name)
if svt != nil {
fmt.Printf(" semver: true\n")
}
}
shouldUpdateTo := lc.Mode.ShouldUpdate(*tag, repoTags)
if shouldUpdateTo != nil {
fmt.Printf(" updating to: %s\n", shouldUpdateTo.Name)
err = lc.UpdateTo(cli, *shouldUpdateTo)
if err != nil {
panic(err)
}
} else {
fmt.Println(" no update available")
}
}
fmt.Println("All done")
}
var (
Logger logrus.Logger = *logrus.New()
)
func main() {
addr := "0.0.0.0:8000"
addr, isPresent := os.LookupEnv(ENV_ADDR)
if !isPresent {
addr = "0.0.0.0:8000"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
go checkForUpdates()
TriggerScan()
fmt.Fprintf(w, "OK")
})
fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Printf("Listening on %s\n", addr)
Logger.Infof(`listening on %s`, addr)
go Worker()
err := http.ListenAndServe(addr, nil)
if err != nil {