From a57ede51d1196e56acfe50d3fb2ba8a48a8927b5 Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Wed, 27 Nov 2019 13:49:13 +0100 Subject: [PATCH] Check current app version vs bundle app version if any and print a warning if it differs on the following commands: - inspect - pull - rm - run - update - image inspect - image render Signed-off-by: Silvin Lubecki --- internal/commands/image/inspect.go | 5 +++++ internal/commands/image/render.go | 9 +++++++-- internal/commands/inspect.go | 5 ++++- internal/commands/pull.go | 5 ++++- internal/commands/remove.go | 5 +++++ internal/commands/run.go | 5 +++++ internal/commands/update.go | 5 +++++ internal/packager/custom.go | 24 +++++++++++++++++++++++- internal/packager/custom_test.go | 2 +- 9 files changed, 59 insertions(+), 6 deletions(-) diff --git a/internal/commands/image/inspect.go b/internal/commands/image/inspect.go index 54bf30983..8a9627a93 100644 --- a/internal/commands/image/inspect.go +++ b/internal/commands/image/inspect.go @@ -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" @@ -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 { diff --git a/internal/commands/image/render.go b/internal/commands/image/render.go index c33b1a83d..fc0c55bed 100644 --- a/internal/commands/image/render.go +++ b/internal/commands/image/render.go @@ -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" @@ -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 } diff --git a/internal/commands/inspect.go b/internal/commands/inspect.go index cefd8b293..3f5758685 100644 --- a/internal/commands/inspect.go +++ b/internal/commands/inspect.go @@ -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" @@ -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 diff --git a/internal/commands/pull.go b/internal/commands/pull.go index 5c8ae0529..6452d4c16 100644 --- a/internal/commands/pull.go +++ b/internal/commands/pull.go @@ -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" @@ -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 diff --git a/internal/commands/remove.go b/internal/commands/remove.go index bcbadb0f2..024ab4741 100644 --- a/internal/commands/remove.go +++ b/internal/commands/remove.go @@ -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" @@ -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 { diff --git a/internal/commands/run.go b/internal/commands/run.go index ffacd6416..0c8e8597b 100644 --- a/internal/commands/run.go +++ b/internal/commands/run.go @@ -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" @@ -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 diff --git a/internal/commands/update.go b/internal/commands/update.go index 1c298051e..c1cd2d1ae 100644 --- a/internal/commands/update.go +++ b/internal/commands/update.go @@ -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" ) @@ -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), diff --git a/internal/packager/custom.go b/internal/packager/custom.go index 199dbae24..ec6b0a8e9 100644 --- a/internal/packager/custom.go +++ b/internal/packager/custom.go @@ -2,6 +2,8 @@ package packager import ( "encoding/json" + "fmt" + "io" "time" "github.com/deislabs/cnab-go/bundle" @@ -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 { @@ -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 prior 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) diff --git a/internal/packager/custom_test.go b/internal/packager/custom_test.go index 235886a8c..c389b5866 100644 --- a/internal/packager/custom_test.go +++ b/internal/packager/custom_test.go @@ -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)