Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Check current app version vs bundle app version if any and print a wa…
Browse files Browse the repository at this point in the history
…rning if it differs on the following commands:

- inspect
- pull
- rm
- run
- update
- image inspect
- image render

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
  • Loading branch information
silvin-lubecki committed Nov 28, 2019
1 parent eb0a375 commit 7735c7d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 9 deletions.
5 changes: 5 additions & 0 deletions internal/commands/image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
"os"

"github.com/docker/app/internal/packager"

"github.com/deislabs/cnab-go/action"
"github.com/docker/app/internal"
"github.com/docker/app/internal/cliopts"
Expand Down Expand Up @@ -66,6 +68,9 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions, inst
if err != nil {
return err
}
if err := packager.CheckAppVersion(dockerCli.Err(), bndl.Bundle); err != nil {
return err
}

format := "json"
if opts.pretty {
Expand Down
9 changes: 7 additions & 2 deletions internal/commands/image/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"os"

"github.com/docker/app/internal/packager"

"github.com/deislabs/cnab-go/driver"

"github.com/deislabs/cnab-go/action"
Expand Down Expand Up @@ -91,11 +93,14 @@ func prepareCustomAction(actionName string,
if err != nil {
return nil, nil, nil, err
}
bundle, ref, err := cnab.GetBundle(dockerCli, bundleStore, appname)
bndl, ref, err := cnab.GetBundle(dockerCli, bundleStore, appname)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "could not render %q: no such App image", appname)
}
installation, err := appstore.NewInstallation("custom-action", ref.String(), bundle)
if err := packager.CheckAppVersion(dockerCli.Err(), bndl.Bundle); err != nil {
return nil, nil, nil, err
}
installation, err := appstore.NewInstallation("custom-action", ref.String(), bndl)
if err != nil {
return nil, nil, nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion internal/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/inspect"
"github.com/docker/app/internal/packager"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,7 +52,9 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
if err != nil {
return err
}

if err := packager.CheckAppVersion(dockerCli.Err(), installation.Bundle); err != nil {
return err
}
creds, err := prepareCredentialSet(installation.Bundle, inspectOptions.CredentialSetOpts(dockerCli, credentialStore)...)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion internal/commands/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/packager"
"github.com/docker/app/internal/store"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -46,7 +47,9 @@ func runPull(dockerCli command.Cli, name string) error {
if err != nil {
return errors.Wrap(err, name)
}

if err := packager.CheckAppVersion(dockerCli.Err(), bndl.Bundle); err != nil {
return err
}
fmt.Fprintf(os.Stdout, "Successfully pulled %q (%s) from %s\n", bndl.Name, bndl.Version, ref.String())

return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/packager"

"github.com/deislabs/cnab-go/driver"

Expand Down Expand Up @@ -52,6 +53,10 @@ func runRemove(dockerCli command.Cli, installationName string, opts removeOption
if err != nil {
return err
}
if err := packager.CheckAppVersion(dockerCli.Err(), installation.Bundle); err != nil {
return err
}

if opts.force {
defer func() {
if mainErr == nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/docker/app/internal/packager"

"github.com/docker/app/internal/relocated"

"github.com/deislabs/cnab-go/driver"
Expand Down Expand Up @@ -98,6 +100,9 @@ func runDockerApp(dockerCli command.Cli, appname string, opts runOptions, instal
}

func runBundle(dockerCli command.Cli, bndl *relocated.Bundle, opts runOptions, installerContext *cliopts.InstallerContextOptions, ref string) (err error) {
if err := packager.CheckAppVersion(dockerCli.Err(), bndl.Bundle); err != nil {
return err
}
_, installationStore, credentialStore, err := prepareStores(dockerCli.CurrentContext())
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions internal/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/docker/app/internal/bundle"
"github.com/docker/app/internal/cliopts"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/packager"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -63,6 +64,10 @@ func runUpdate(dockerCli command.Cli, installationName string, opts updateOption
}
installation.Bundle = b.Bundle
}
if err := packager.CheckAppVersion(dockerCli.Err(), installation.Bundle); err != nil {
return err
}

if err := bundle.MergeBundleParameters(installation,
bundle.WithFileParameters(opts.ParametersFiles),
bundle.WithCommandLineParameters(opts.Overrides),
Expand Down
2 changes: 1 addition & 1 deletion internal/packager/cnab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestToCNAB(t *testing.T) {
assert.NilError(t, err)
s := golden.Get(t, "bundle-json.golden")
expectedLiteral := regexp.QuoteMeta(string(s))
expected := fmt.Sprintf(expectedLiteral, DockerAppPayloadVersionCurrent, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z`, internal.Version)
expected := fmt.Sprintf(expectedLiteral, DockerAppPayloadVersionCurrent, internal.Version, `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z`)
matches, err := regexp.Match(expected, actualJSON)
assert.NilError(t, err)
assert.Assert(t, matches)
Expand Down
24 changes: 23 additions & 1 deletion internal/packager/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package packager

import (
"encoding/json"
"fmt"
"io"
"time"

"github.com/deislabs/cnab-go/bundle"
Expand Down Expand Up @@ -37,8 +39,8 @@ type CustomPayloadAppVersion interface {
}

type payloadV1_0 struct {
Created time.Time `json:"created"`
Version string `json:"app-version"`
Created time.Time `json:"created"`
}

func (p payloadV1_0) CreatedTime() time.Time {
Expand All @@ -58,6 +60,26 @@ func newCustomPayload() (json.RawMessage, error) {
return j, nil
}

// CheckAppVersion
func CheckAppVersion(stderr io.Writer, bndl *bundle.Bundle) error {
payload, err := CustomPayload(bndl)
if err != nil {
return err
}
if payload == nil {
return nil
}

var version string
if versionPayload, ok := payload.(CustomPayloadAppVersion); ok {
version = versionPayload.AppVersion()
}
if version != internal.Version {
fmt.Fprintf(stderr, "WARNING: App Image has been built with a different version of docker app: %q\n", version)
}
return nil
}

// CustomPayload parses and returns the bundle's custom payload
func CustomPayload(b *bundle.Bundle) (interface{}, error) {
custom, err := parseCustomPayload(b)
Expand Down
2 changes: 1 addition & 1 deletion internal/packager/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestCustomPayloadNil(t *testing.T) {

func TestCustomPayloadV1_0_0(t *testing.T) {
now := time.Now().UTC()
b := createBundle(t, DockerAppPayloadVersion1_0_0, payloadV1_0{now, "version"})
b := createBundle(t, DockerAppPayloadVersion1_0_0, payloadV1_0{"version", now})
payload, err := CustomPayload(&b)
assert.NilError(t, err)
v1, ok := payload.(payloadV1_0)
Expand Down
4 changes: 2 additions & 2 deletions internal/packager/testdata/bundle-json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@
"com.docker.app": {
"version": "%s",
"payload": {
"created": "%s",
"app-version": "%s"
"app-version": "%s",
"created": "%s"
}
}
}
Expand Down

0 comments on commit 7735c7d

Please sign in to comment.