Skip to content

Commit

Permalink
Changed log output to only log updated images
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Aug 22, 2023
1 parent 245b4fa commit 6be93e1
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions docker-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, "|")
Expand Down

0 comments on commit 6be93e1

Please sign in to comment.