Skip to content

Commit

Permalink
#478: Add update loop for uselagoon/* (#479)
Browse files Browse the repository at this point in the history
* #478 - add update loop for uselagoon/*

Signed-off-by: Karl Hepworth <karl.hepworth@gmail.com>

* Better filters and targetting

Signed-off-by: Karl Hepworth <karl.hepworth@gmail.com>

* loop over all imagetags - useless but comprehensive

Signed-off-by: Karl Hepworth <karl.hepworth@gmail.com>

* Pull images with version identifiers in reference, handle access denied errors gracefully

Signed-off-by: Karl Hepworth <karl.hepworth@gmail.com>

---------

Signed-off-by: Karl Hepworth <karl.hepworth@gmail.com>
  • Loading branch information
fubarhouse committed Oct 31, 2023
1 parent f37c044 commit a5bfb19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
57 changes: 32 additions & 25 deletions service/interface/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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:
Expand All @@ -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 {
Expand All @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions service/library/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
}

0 comments on commit a5bfb19

Please sign in to comment.