From 6be93e1cdb86b2825db46bcb484acee4150e669d Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 22 Aug 2023 15:59:53 +1000 Subject: [PATCH] Changed log output to only log updated images --- docker-host/main.go | 67 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 337fb7d9..301f47a0 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -125,21 +125,27 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println("Starting update images") ctx := context.Background() filters := addFilters(repositoriesToUpdate) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + preUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) return } + var preUpdateIDs []string + for _, img := range preUpdateImages { + preUpdateIDs = append(preUpdateIDs, img.ID) + } + var imgRepoTags []string - for _, img := range images { - imgRepoTags = append(imgRepoTags, img.RepoTags...) + for _, img := range preUpdateImages { + if img.RepoTags != nil { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } } - // # Iterates through all images that have the name of the repository we are interested in in it + // # Iterates through all images that have the name of the repository we are interested in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) - log.Println("Checking update for", image) if err != nil { log.Println(err) @@ -151,10 +157,59 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println(err) } } - log.Println("Update images complete") + + postUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + log.Println(err) + } + + var postUpdateIDs []string + for _, img := range postUpdateImages { + postUpdateIDs = append(postUpdateIDs, img.ID) + } + + updatedImages := imgComparison(preUpdateIDs, postUpdateIDs) + for _, img := range postUpdateImages { + for _, updatedImg := range updatedImages { + if img.ID == updatedImg { + log.Println(fmt.Sprintf("Updated image %s", img.RepoTags)) + } + } + } + + imgPluralize := "" + if len(updatedImages) == 1 { + imgPluralize = "image" + } else { + imgPluralize = "images" + } + log.Println(fmt.Sprintf("Update images complete | %d %s updated", len(updatedImages), imgPluralize)) }) } +func imgComparison(preUpdate, postUpdate []string) []string { + var updatedImgs []string + + for i := 0; i < 2; i++ { + for _, preUpdateImg := range preUpdate { + found := false + for _, postUpdateImg := range postUpdate { + if preUpdateImg == postUpdateImg { + found = true + break + } + } + if !found { + updatedImgs = append(updatedImgs, preUpdateImg) + } + } + if i == 0 { + preUpdate, postUpdate = postUpdate, preUpdate + } + } + return updatedImgs +} + func addFilters(repo string) filters.Args { filters := filters.NewArgs() splitRepos := strings.Split(repo, "|")