Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ See https://github.com/urbforge/urbex for architecture and design decisions.`,
root.AddCommand(newUsersCmd())
root.AddCommand(newDoctorCmd())
root.AddCommand(newDiscoverCmd())
root.AddCommand(newUpdatesCmd())
root.AddCommand(newVersionCmd())

return root
Expand Down
101 changes: 101 additions & 0 deletions internal/cli/updates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cli

import (
"fmt"
"io"

"github.com/spf13/cobra"

"github.com/urbforge/urbex-cli/internal/provision"
)

func newUpdatesCmd() *cobra.Command {
var manifestPath, gitopsRepoPath string
var output outputFlag

cmd := &cobra.Command{
Use: "updates [<env>]",
Short: "List newer images of the project's applications from published images",
Long: `For each application from a published image applied in <env> (or in
staging and prod), says whether a newer image is available:

- the tag urbex.yaml asks for now points to another image than the one
running: 'urbex deploy <env> --update' moves to it;
- a newer tag of the same major (a minor or patch version): deploy it
with 'urbex deploy <env> --app <app> --version <tag>', and set it in
urbex.yaml;
- a newer major: listed only - its data may need a migration, and its
release notes say how. urbex never moves an application to it by
itself.

Only tags shaped like the one urbex.yaml asks for are compared: "1.29"
with "1.30" and "2.0", not with "1.30.1" or "latest". It changes nothing.
--output json prints the list as one JSON document. See
docs/reference/cli.md in urbforge/urbex.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
repoPath, err := resolveGitOpsRepoPath(gitopsRepoPath)
if err != nil {
return err
}
p, err := newProvisionOrchestrator(cmd, manifestPath, repoPath, "", false)
if err != nil {
return err
}
opts := provision.Options{GitOpsRepoPath: repoPath}
if len(args) == 1 {
opts.Env = args[0]
}
updates, err := p.Updates(cmd.Context(), opts)
if err != nil {
return err
}
if output.JSON() {
if updates == nil {
updates = []provision.ImageUpdate{}
}
return writeJSON(cmd.OutOrStdout(), map[string]any{"project": p.Manifest.Project, "updates": updates})
}
printUpdates(cmd.OutOrStdout(), p.Manifest.Project, updates)
return nil
},
}
cmd.Flags().StringVar(&manifestPath, "file", "urbex.yaml", "path to the app manifest")
cmd.Flags().StringVar(&gitopsRepoPath, "gitops-repo", "", "local working directory of the GitOps repo (or set URBEX_GITOPS_REPO)")
output.Register(cmd)
return cmd
}

func printUpdates(out io.Writer, project string, updates []provision.ImageUpdate) {
if len(updates) == 0 {
fmt.Fprintf(out, "%s: no application from a published image is applied yet.\n", project)
return
}
env := ""
for _, u := range updates {
if u.Env != env {
env = u.Env
fmt.Fprintf(out, "%s (%s):\n", project, env)
}
fmt.Fprintf(out, " %-16s %s:%s, running %s\n", u.App, u.Image, u.Asked, u.Running)
if u.Error != "" {
fmt.Fprintf(out, " %-16s couldn't check: %s\n", "", u.Error)
}
none := true
if u.NewDigest {
none = false
fmt.Fprintf(out, " %-16s %s points to a newer image: urbex deploy %s --update\n", "", u.Asked, u.Env)
}
if u.Minor != "" {
none = false
fmt.Fprintf(out, " %-16s newer: %s - urbex deploy %s --app %s --version %s\n", "", u.Minor, u.Env, u.App, u.Minor)
}
if u.Major != "" {
none = false
fmt.Fprintf(out, " %-16s new major: %s - read its release notes first; not proposed\n", "", u.Major)
}
if none && u.Error == "" {
fmt.Fprintf(out, " %-16s up to date\n", "")
}
}
}
139 changes: 139 additions & 0 deletions internal/imageref/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package imageref

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)

// Tags lists image's tags, as the registry's tags/list gives them,
// following its pages (urbforge/urbex-cli#113).
func (r *Resolver) Tags(ctx context.Context, image string) ([]string, error) {
ref, err := Parse(image)
if err != nil {
return nil, err
}
scheme := r.Scheme
if scheme == "" {
scheme = "https"
}
base := fmt.Sprintf("%s://%s", scheme, ref.Registry)
next := fmt.Sprintf("%s/v2/%s/tags/list?n=1000", base, ref.Repository)
token := ""
var tags []string
for page := 0; next != "" && page < 50; page++ {
res, err := r.request(ctx, http.MethodGet, next, ref.Repository, &token)
if err != nil {
return nil, fmt.Errorf("%s: listing tags: %w", image, err)
}
var body struct {
Tags []string `json:"tags"`
}
data, err := io.ReadAll(io.LimitReader(res.Body, 8<<20))
res.Body.Close()
if err != nil {
return nil, fmt.Errorf("%s: listing tags: %w", image, err)
}
if err := statusError(res, image, "tags"); err != nil {
return nil, err
}
if err := json.Unmarshal(data, &body); err != nil {
return nil, fmt.Errorf("%s: listing tags: %w", image, err)
}
tags = append(tags, body.Tags...)
next = nextPage(res.Header.Get("Link"), base)
}
return tags, nil
}

// nextPage is the URL of a Link header's rel="next", made absolute.
func nextPage(link, base string) string {
for _, part := range strings.Split(link, ",") {
target, params, ok := strings.Cut(strings.TrimSpace(part), ";")
if !ok || !strings.Contains(params, `rel="next"`) {
continue
}
target = strings.Trim(strings.TrimSpace(target), "<>")
if u, err := url.Parse(target); err == nil && !u.IsAbs() {
return base + target
}
return target
}
return ""
}

// Updates are the newer versions of a tag among an image's tags: Minor
// is the newest of the same major (minor and patch versions), Major the
// newest of a higher major; empty when there is none.
type Updates struct {
Minor string `json:"minor,omitempty"`
Major string `json:"major,omitempty"`
}

// tagShape splits a version tag: an optional "v", its numbers, and what
// follows them ("17.6-alpine": "", [17 6], "-alpine").
var tagShape = regexp.MustCompile(`^(v?)(\d+(?:\.\d+)*)(.*)$`)

type version struct {
prefix, suffix string
parts []int
}

func parseVersion(tag string) (version, bool) {
m := tagShape.FindStringSubmatch(tag)
if m == nil {
return version{}, false
}
var parts []int
for _, p := range strings.Split(m[2], ".") {
n, err := strconv.Atoi(p)
if err != nil {
return version{}, false
}
parts = append(parts, n)
}
return version{prefix: m[1], suffix: m[3], parts: parts}, true
}

func (a version) less(b version) bool {
for i := range a.parts {
if a.parts[i] != b.parts[i] {
return a.parts[i] < b.parts[i]
}
}
return false
}

// Newer finds the updates of current among tags. Only tags of the same
// shape count: as many numbers, the same "v" and the same suffix, so that
// "1.29" is compared with "1.30" and "2.0", not with "1.30.1", "latest" or
// "1.30-alpine" - the tag stays as precise as the one urbex.yaml asks for.
// A tag without a version ("latest") has no updates.
func Newer(current string, tags []string) Updates {
cur, ok := parseVersion(current)
if !ok {
return Updates{}
}
var u Updates
var minor, major version
for _, t := range tags {
v, ok := parseVersion(t)
if !ok || v.prefix != cur.prefix || v.suffix != cur.suffix || len(v.parts) != len(cur.parts) || !cur.less(v) {
continue
}
if v.parts[0] == cur.parts[0] {
if u.Minor == "" || minor.less(v) {
u.Minor, minor = t, v
}
} else if u.Major == "" || major.less(v) {
u.Major, major = t, v
}
}
return u
}
65 changes: 65 additions & 0 deletions internal/imageref/tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package imageref

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestNewer(t *testing.T) {
nginx := []string{"latest", "1.28", "1.29", "1.30", "1.30.1", "1.31-alpine", "1.31", "2.0", "2.1", "v1.40"}
postgres := []string{"latest", "17", "18", "17.6", "17.7", "18.1", "17-alpine", "18-alpine"}
for _, tc := range []struct {
current string
tags []string
want Updates
}{
{"1.29", nginx, Updates{Minor: "1.31", Major: "2.1"}},
{"1.31", nginx, Updates{Major: "2.1"}},
{"2.1", nginx, Updates{}},
{"1.31-alpine", nginx, Updates{}},
{"latest", nginx, Updates{}},
{"v1.39", nginx, Updates{Minor: "v1.40"}},
{"17", postgres, Updates{Major: "18"}},
{"17.6", postgres, Updates{Minor: "17.7", Major: "18.1"}},
{"17-alpine", postgres, Updates{Major: "18-alpine"}},
} {
if got := Newer(tc.current, tc.tags); got != tc.want {
t.Errorf("Newer(%q) = %+v, want %+v", tc.current, got, tc.want)
}
}
}

// Tags follows the registry's pages, with an anonymous token.
func TestTags_FollowsPages(t *testing.T) {
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/token":
fmt.Fprint(w, `{"token":"anon"}`)
case r.Header.Get("Authorization") != "Bearer anon":
w.Header().Set("WWW-Authenticate", `Bearer realm="`+srv.URL+`/token",service="test"`)
w.WriteHeader(http.StatusUnauthorized)
case r.URL.Path == "/v2/library/nginx/tags/list" && r.URL.Query().Get("last") == "":
w.Header().Set("Link", `</v2/library/nginx/tags/list?n=1000&last=1.28>; rel="next"`)
fmt.Fprint(w, `{"name":"library/nginx","tags":["1.27","1.28"]}`)
case r.URL.Path == "/v2/library/nginx/tags/list":
fmt.Fprint(w, `{"name":"library/nginx","tags":["1.29"]}`)
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
r := &Resolver{Scheme: "http"}
host := strings.TrimPrefix(srv.URL, "http://")
tags, err := r.Tags(context.Background(), host+"/library/nginx")
if err != nil {
t.Fatal(err)
}
if strings.Join(tags, " ") != "1.27 1.28 1.29" {
t.Errorf("tags = %v", tags)
}
}
Loading
Loading