diff --git a/internal/cli/root.go b/internal/cli/root.go index 5e6a6e0..98587f5 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -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 diff --git a/internal/cli/updates.go b/internal/cli/updates.go new file mode 100644 index 0000000..bebfddc --- /dev/null +++ b/internal/cli/updates.go @@ -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 []", + Short: "List newer images of the project's applications from published images", + Long: `For each application from a published image applied in (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 --update' moves to it; + - a newer tag of the same major (a minor or patch version): deploy it + with 'urbex deploy --app --version ', 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", "") + } + } +} diff --git a/internal/imageref/tags.go b/internal/imageref/tags.go new file mode 100644 index 0000000..a33db7e --- /dev/null +++ b/internal/imageref/tags.go @@ -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 +} diff --git a/internal/imageref/tags_test.go b/internal/imageref/tags_test.go new file mode 100644 index 0000000..3f13899 --- /dev/null +++ b/internal/imageref/tags_test.go @@ -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", `; 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) + } +} diff --git a/internal/provision/updates.go b/internal/provision/updates.go new file mode 100644 index 0000000..47b4b13 --- /dev/null +++ b/internal/provision/updates.go @@ -0,0 +1,99 @@ +package provision + +import ( + "context" + "fmt" + + "github.com/urbforge/urbex-cli/internal/envrepo" + "github.com/urbforge/urbex-cli/internal/imageref" + "github.com/urbforge/urbex-cli/internal/manifest" +) + +// TagLister lists a published image's tags - see internal/imageref. +type TagLister interface { + Tags(ctx context.Context, image string) ([]string, error) +} + +// ImageUpdate is what's newer for an application from a published image +// in an environment (urbforge/urbex-cli#113). +type ImageUpdate struct { + Env string `json:"env"` + App string `json:"app"` + Image string `json:"image"` + // Asked is the tag urbex.yaml asks for; Running what the environment + // runs (tag@digest). + Asked string `json:"asked"` + Running string `json:"running"` + // NewDigest: the asked tag points to another image than the one + // running - 'urbex deploy --update' moves to it. + NewDigest bool `json:"newDigest,omitempty"` + // Newer tags of the same shape as the asked one: Minor of the same + // major (deploy it with --app and --version), Major of a higher one - + // listed, never proposed: its data may need a migration. + imageref.Updates + // Error is why it couldn't be checked, if it couldn't. + Error string `json:"error,omitempty"` +} + +// Updates checks, for each application from a published image applied in +// env (or in every environment, env ""), whether a newer image is +// available: the asked tag resolved again, and the image's newer tags. +// It changes nothing. +func (o *Orchestrator) Updates(ctx context.Context, opts Options) ([]ImageUpdate, error) { + envs := []string{"staging", "prod"} + if opts.Env != "" { + if !ValidEnvs[opts.Env] { + return nil, fmt.Errorf("invalid environment %q: must be one of staging, prod", opts.Env) + } + envs = []string{opts.Env} + } + apps := manifest.FromImages(o.Manifest.Deployables()) + if len(apps) == 0 { + return nil, fmt.Errorf("%s has no application from a published image: nothing to check", o.Manifest.Project) + } + if o.Images == nil { + return nil, fmt.Errorf("no image resolver") + } + lister, _ := o.Images.(TagLister) + if err := o.refresh(ctx, opts); err != nil { + return nil, err + } + repo := envrepo.Repo{Path: opts.GitOpsRepoPath} + tags := map[string][]string{} + tagErr := map[string]error{} + digests := map[string]string{} + var out []ImageUpdate + for _, env := range envs { + for _, svc := range apps { + st, err := repo.Version(env, o.Manifest.Project, svc.Name) + if err != nil { + return nil, err + } + running, ok := imageref.ParsePinned(st.Version) + if !ok { + continue // not applied in env, or not pinned yet + } + u := ImageUpdate{Env: env, App: svc.Name, Image: svc.Image, Asked: svc.Version, Running: running.String()} + key := svc.Image + ":" + svc.Version + digest, seen := digests[key] + if !seen { + if digest, err = o.Images.Resolve(ctx, svc.Image, svc.Version); err != nil { + u.Error = err.Error() + } + digests[key] = digest + } + u.NewDigest = digest != "" && running.Tag == svc.Version && digest != running.Digest + if lister != nil { + if _, seen := tags[svc.Image]; !seen && tagErr[svc.Image] == nil { + tags[svc.Image], tagErr[svc.Image] = lister.Tags(ctx, svc.Image) + } + if err := tagErr[svc.Image]; err != nil && u.Error == "" { + u.Error = err.Error() + } + u.Updates = imageref.Newer(svc.Version, tags[svc.Image]) + } + out = append(out, u) + } + } + return out, nil +} diff --git a/internal/provision/updates_test.go b/internal/provision/updates_test.go new file mode 100644 index 0000000..57ede49 --- /dev/null +++ b/internal/provision/updates_test.go @@ -0,0 +1,84 @@ +package provision + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/urbforge/urbex-cli/internal/envrepo" + "github.com/urbforge/urbex-cli/internal/imageref" + "github.com/urbforge/urbex-cli/internal/manifest" +) + +// taggedImages also lists tags, as imageref.Resolver does. +type taggedImages struct { + fakeImages + tags map[string][]string +} + +func (f *taggedImages) Tags(ctx context.Context, image string) ([]string, error) { + return f.tags[image], nil +} + +// urbex updates: the asked tag resolved again, and the newer tags of the +// same shape - per environment applied, changing nothing +// (urbforge/urbex-cli#113). +func TestUpdates(t *testing.T) { + dir := t.TempDir() + m := &manifest.Manifest{APIVersion: "urbex/v1", Project: "my-app", Services: []manifest.Service{ + {Name: "api", Runtime: "go", Port: 8080}, + {Name: "cache", Image: "valkey/valkey", Version: "9", Port: 6379}, + {Name: "db", Image: "postgres", Version: "17.6", Port: 5432}, + }} + o, _, _, _, _ := newOrchestrator(m) + images := &taggedImages{ + fakeImages: fakeImages{digests: map[string]string{"valkey/valkey:9": "sha256:new9", "postgres:17.6": "sha256:pg176"}}, + tags: map[string][]string{ + "valkey/valkey": {"8", "9", "latest"}, + "postgres": {"17.6", "17.7", "18.1", "17", "18"}, + }, + } + o.Images = images + repo := envrepo.Repo{Path: dir} + for _, d := range []string{"staging/my-app/cache", "staging/my-app/db", "prod/my-app/db"} { + if err := os.MkdirAll(filepath.Join(dir, "environments", d), 0o755); err != nil { + t.Fatal(err) + } + } + for _, v := range []struct{ env, svc, version string }{ + {"staging", "cache", "9@sha256:old9"}, + {"staging", "db", "17.6@sha256:pg176"}, + {"prod", "db", "17.6@sha256:pg176"}, // cache isn't in prod yet + } { + if err := repo.SetVersion(v.env, "my-app", v.svc, envrepo.VersionFileState{Version: v.version}); err != nil { + t.Fatal(err) + } + } + + got, err := o.Updates(context.Background(), Options{GitOpsRepoPath: dir}) + if err != nil { + t.Fatal(err) + } + want := []ImageUpdate{ + {Env: "staging", App: "cache", Image: "valkey/valkey", Asked: "9", Running: "9@sha256:old9", NewDigest: true}, + {Env: "staging", App: "db", Image: "postgres", Asked: "17.6", Running: "17.6@sha256:pg176", Updates: imageref.Updates{Minor: "17.7", Major: "18.1"}}, + {Env: "prod", App: "db", Image: "postgres", Asked: "17.6", Running: "17.6@sha256:pg176", Updates: imageref.Updates{Minor: "17.7", Major: "18.1"}}, + } + if len(got) != len(want) { + t.Fatalf("got %+v", got) + } + for i := range want { + if got[i] != want[i] { + t.Errorf("[%d] = %+v, want %+v", i, got[i], want[i]) + } + } + if st, _ := repo.Version("staging", "my-app", "cache"); st.Version != "9@sha256:old9" { + t.Errorf("updates must change nothing, version is %q", st.Version) + } + + only, err := o.Updates(context.Background(), Options{Env: "prod", GitOpsRepoPath: dir}) + if err != nil || len(only) != 1 || only[0].App != "db" { + t.Errorf("prod only: %+v, %v", only, err) + } +}