From a5bfb19b10f495801efe34bcf69e989d043c0893 Mon Sep 17 00:00:00 2001 From: Karl Hepworth Date: Tue, 31 Oct 2023 22:39:44 +1100 Subject: [PATCH] #478: Add update loop for uselagoon/* (#479) * #478 - add update loop for uselagoon/* Signed-off-by: Karl Hepworth * Better filters and targetting Signed-off-by: Karl Hepworth * loop over all imagetags - useless but comprehensive Signed-off-by: Karl Hepworth * Pull images with version identifiers in reference, handle access denied errors gracefully Signed-off-by: Karl Hepworth --------- Signed-off-by: Karl Hepworth --- service/interface/docker/docker.go | 57 +++++++++++++++++------------- service/library/update.go | 11 ++++++ 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/service/interface/docker/docker.go b/service/interface/docker/docker.go index f9a52820..b4648e0e 100644 --- a/service/interface/docker/docker.go +++ b/service/interface/docker/docker.go @@ -68,7 +68,9 @@ func DockerImageList() ([]types.ImageSummary, error) { fmt.Println(err) } - images, err := cli.ImageList(ctx, types.ImageListOptions{}) + images, err := cli.ImageList(ctx, types.ImageListOptions{ + All: true, + }) if err != nil { return []types.ImageSummary{}, err } @@ -89,37 +91,37 @@ func DockerPull(image string) (string, error) { // To support image references from external sources to docker.io we need to check // and validate the image reference for all known cases of validity. - if m, _ := regexp.MatchString("^(([a-zA-Z0-9._-]+)[/]([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_-]+)[:]([a-zA-Z0-9_-]+))$", image); m { + if m, _ := regexp.MatchString("^(([a-zA-Z0-9._-]+)[/]([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_.-]+)[:]([a-zA-Z0-9_-]+))$", image); m { // URL was provided (in full), but the tag was provided. // For this, we do not alter the value provided. // Examples: // - quay.io/pygmystack/pygmy:latest image = fmt.Sprintf("%v", image) - } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9._-]+)[/]([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_-]+))$", image); m { + } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9._-]+)[/]([a-zA-Z0-9_.-]+)[/]([a-zA-Z0-9_-]+))$", image); m { // URL was provided (in full), but the tag was not provided. // For this, we do not alter the value provided. // Examples: // - quay.io/pygmystack/pygmy image = fmt.Sprintf("%v:latest", image) - } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_-]+)[:]([a-zA-Z0-9_-]+))$", image); m { + } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_.-]+)[:]([a-zA-Z0-9_-]+))$", image); m { // URL was not provided (in full), but the tag was provided. // For this, we prepend 'docker.io/' to the reference. // Examples: // - pygmystack/pygmy:latest image = fmt.Sprintf("docker.io/%v", image) - } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_-]+))$", image); m { + } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9_-]+)[/]([a-zA-Z0-9_.-]+))$", image); m { // URL was not provided (in full), but the tag was not provided. // For this, we prepend 'docker.io/' to the reference. // Examples: // - pygmystack/pygmy image = fmt.Sprintf("docker.io/%v:latest", image) - } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9_-]+)[:]([a-zA-Z0-9_-]+))$", image); m { + } else if m, _ := regexp.MatchString("^(([a-zA-Z0-9_-]+)[:]([a-zA-Z0-9_.-]+))$", image); m { // Library image was provided with tag identifier. // For this, we prepend 'docker.io/' to the reference. // Examples: // - pygmy:latest image = fmt.Sprintf("docker.io/%v", image) - } else if m, _ := regexp.MatchString("^([a-zA-Z0-9_-]+)$", image); m { + } else if m, _ := regexp.MatchString("^([a-zA-Z0-9_.-]+)$", image); m { // Library image was provided without tag identifier. // For this, we prepend 'docker.io/' to the reference. // Examples: @@ -141,10 +143,6 @@ func DockerPull(image string) (string, error) { } data, err := cli.ImagePull(ctx, image, types.ImagePullOptions{}) - if err != nil { - fmt.Println(err) - } - d := json.NewDecoder(data) type Event struct { @@ -158,26 +156,35 @@ func DockerPull(image string) (string, error) { } var event *Event - for { - if err := d.Decode(&event); err != nil { - if err == io.EOF { - break - } + if err == nil { + for { + if err := d.Decode(&event); err != nil { + if err == io.EOF { + break + } - panic(err) + panic(err) + } } - } - if event != nil { - if strings.Contains(event.Status, "Downloaded newer image") { - return fmt.Sprintf("Successfully pulled %v", image), nil - } + if event != nil { + if strings.Contains(event.Status, "Downloaded newer image") { + return fmt.Sprintf("Successfully pulled %v", image), nil + } - if strings.Contains(event.Status, "Image is up to date") { - return fmt.Sprintf("Image %v is up to date", image), nil + if strings.Contains(event.Status, "Image is up to date") { + return fmt.Sprintf("Image %v is up to date", image), nil + } } + + return event.Status, nil + } + + if strings.Contains(err.Error(), "pull access denied") { + return fmt.Sprintf("Error trying to update image %v: pull access denied", image), nil } - return event.Status, nil + + return "", nil } // DockerStop will stop the container. diff --git a/service/library/update.go b/service/library/update.go index 6bea61ca..7e292dd4 100644 --- a/service/library/update.go +++ b/service/library/update.go @@ -44,4 +44,15 @@ func Update(c Config) { } } + images, _ := docker.DockerImageList() + for _, image := range images { + for _, tag := range image.RepoTags { + if strings.Contains(tag, "uselagoon") { + result, err := docker.DockerPull(tag) + if err == nil { + fmt.Println(result) + } + } + } + } }