tag-checker/main.go

243 lines
6.1 KiB
Go
Raw Normal View History

2020-12-02 01:07:06 +00:00
package main
import (
"context"
"encoding/json"
2020-12-02 01:25:40 +00:00
"flag"
2020-12-02 01:07:06 +00:00
"fmt"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"git.iamthefij.com/iamthefij/slog"
2020-12-02 01:07:06 +00:00
dockerTypes "github.com/docker/docker/api/types"
dockerClient "github.com/docker/docker/client"
)
2020-12-02 01:25:40 +00:00
var (
2020-12-02 04:27:27 +00:00
// defaultRegistryBaseURL is the base URL of the docker registry
defaultRegistryBaseURL = "https://hub.docker.com"
2020-12-02 01:25:40 +00:00
// maxPages is the max number of pages to fetch from docker registry results
maxPages = 10
2020-12-02 01:07:06 +00:00
2020-12-02 01:25:40 +00:00
// Regexp used to extract tag information
tagRegexp = regexp.MustCompile(`(.*):[vV]{0,1}([0-9.]+)(-(.*)){0,1}`)
// version of tag checker
version = "dev"
)
2020-12-02 01:07:06 +00:00
// ImageTag is wraps an image and tag values for a container
type ImageTag struct {
ImageTag string
2020-12-02 04:27:27 +00:00
Registry string
2020-12-02 01:07:06 +00:00
Image string
TagDesc string
Version string
VersionParts []int
}
// IsComparable will return true if to images share the same base, description, and version resolution
func (thisTag ImageTag) IsComparable(otherTag ImageTag) bool {
return thisTag.Image == otherTag.Image && thisTag.TagDesc == otherTag.TagDesc && len(thisTag.VersionParts) == len(otherTag.VersionParts)
}
// IsNewerThan will return true if two tags are comparable and this tag is newer than the one passed in
func (thisTag ImageTag) IsNewerThan(otherTag ImageTag) bool {
return thisTag.CompareTo(otherTag) == 1
}
// CompareTo compares two ImageTags. It will return 0 if they are not-comparable or equal 1 if newer and -1 if less
func (thisTag ImageTag) CompareTo(otherTag ImageTag) int {
if !thisTag.IsComparable(otherTag) {
return 0
}
for i := range thisTag.VersionParts {
if thisTag.VersionParts[i] > otherTag.VersionParts[i] {
return 1
} else if thisTag.VersionParts[i] < otherTag.VersionParts[i] {
return -1
}
}
// Everything must be equal
return 0
}
// ParseImageTag parses an image and tag name to a struct
func ParseImageTag(imageTag string) (ImageTag, error) {
results := tagRegexp.FindStringSubmatch(imageTag)
if results == nil || results[0] == "" {
return ImageTag{}, fmt.Errorf("could not recognize versions in %s", imageTag)
}
// Extract image name with repo
image := results[1]
2020-12-02 04:27:27 +00:00
registry := ""
switch strings.Count(image, "/") {
case 0:
2020-12-02 01:07:06 +00:00
image = "library/" + image
2020-12-02 04:27:27 +00:00
case 2:
parts := strings.Split(image, "/")
2020-12-02 04:37:57 +00:00
if parts[0] != "docker.io" {
registry = parts[0]
}
2020-12-02 04:27:27 +00:00
image = strings.Join(parts[1:], "/")
2020-12-02 01:07:06 +00:00
}
// Extract version number
version := results[2]
versionParts := []int{}
var verPart int
var err error
for _, v := range strings.Split(version, ".") {
verPart, err = strconv.Atoi(v)
if err != nil {
return ImageTag{}, err
}
versionParts = append(versionParts, verPart)
}
return ImageTag{
ImageTag: imageTag,
Image: image,
2020-12-02 04:27:27 +00:00
Registry: registry,
2020-12-02 01:07:06 +00:00
Version: version,
VersionParts: versionParts,
TagDesc: results[4],
}, nil
}
func getJSON(url string, response interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
}
// defer func() { _ = resp.Body.Close() }()
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(response)
if err != nil {
return err
}
return nil
}
func listTags(current ImageTag) ([]ImageTag, error) {
var err error
results := []ImageTag{}
type tagsResponse struct {
Count int
Next string
Previous string
Results []struct {
ID int
LastUpdated string `json:"last_updated"`
Name string
}
}
2020-12-02 04:27:27 +00:00
registryBaseURL := defaultRegistryBaseURL
if current.Registry != "" {
registryBaseURL = fmt.Sprintf("https://%s", current.Registry)
}
2020-12-02 01:25:40 +00:00
url := fmt.Sprintf("%s/v2/repositories/%s/tags", registryBaseURL, current.Image)
2020-12-02 01:07:06 +00:00
pageCount := 0
var response tagsResponse
var newTag ImageTag
for url != "" && pageCount <= maxPages {
err = getJSON(url, &response)
if err != nil {
return results, err
}
for _, tag := range response.Results {
newTag, err = ParseImageTag(fmt.Sprintf("%s:%s", current.Image, tag.Name))
if err == nil {
results = append(results, newTag)
}
}
url = response.Next
pageCount++
}
return results, nil
}
func getNewerTags(current ImageTag) ([]ImageTag, error) {
newerTags := []ImageTag{}
tags, err := listTags(current)
if err != nil {
return newerTags, err
}
for _, tag := range tags {
if tag.IsNewerThan(current) {
newerTags = append(newerTags, tag)
}
}
// Sort tags with newest first
2020-12-02 01:11:30 +00:00
sort.Slice(newerTags, func(i, j int) bool { return newerTags[i].CompareTo(newerTags[j]) == 1 })
2020-12-02 01:07:06 +00:00
return newerTags, nil
}
func main() {
flag.StringVar(&defaultRegistryBaseURL, "registry-url", defaultRegistryBaseURL, "base url of the registry you want to check against")
2020-12-02 01:25:40 +00:00
flag.IntVar(&maxPages, "max-pages", maxPages, "max number of pages to retrieve from registry")
var showVersion = flag.Bool("version", false, "display the version and exit")
flag.BoolVar(&slog.DebugLevel, "debug", false, "show debug logs")
2020-12-02 01:25:40 +00:00
flag.Parse()
// Print version if asked
if *showVersion {
fmt.Println("version:", version)
os.Exit(0)
}
2020-12-02 01:07:06 +00:00
dockerClient, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv)
if err != nil {
2020-12-02 01:11:30 +00:00
fmt.Println("Could not initialize docker client")
2020-12-02 01:07:06 +00:00
panic(err)
}
containers, err := dockerClient.ContainerList(context.Background(), dockerTypes.ContainerListOptions{})
if err != nil {
2020-12-02 01:11:30 +00:00
fmt.Println("Could list container from docker client")
2020-12-02 01:07:06 +00:00
panic(err)
}
hasUpdate := false
images := map[string]bool{}
for _, container := range containers {
images[container.Image] = true
}
for image := range images {
slog.Debug("[%s] Checking for updates...", image)
2020-12-02 01:07:06 +00:00
it, err := ParseImageTag(image)
if err != nil {
slog.Debug("[%s] Can't parse tag: %v", image, err)
2020-12-02 01:07:06 +00:00
continue
}
newerTags, err := getNewerTags(it)
slog.PanicOnErr(err, "[%s] failed getting new tags", image)
2020-12-02 01:07:06 +00:00
if len(newerTags) == 0 {
slog.Debug("[%s] No newer versions found", image)
2020-12-02 01:07:06 +00:00
continue
}
hasUpdate = true
if slog.DebugLevel {
slog.Info("[%s] Newer version found! Recommended update to %s", image, newerTags[0].ImageTag)
} else {
fmt.Printf("[%s] Newer version found! Recommended update to %s\n", image, newerTags[0].ImageTag)
}
2020-12-02 01:07:06 +00:00
}
if hasUpdate {
os.Exit(10)
}
}