Skip to content

Commit

Permalink
Write bundle.json to cnab/ (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs-msft authored and jeremyrickard committed May 31, 2019
1 parent 75bb688 commit 2e3950a
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 51 deletions.
6 changes: 3 additions & 3 deletions cmd/porter/bundle.go
Expand Up @@ -129,7 +129,7 @@ For instance, the 'debug' driver may be specified, which simply logs the info gi
porter bundle install --driver debug
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
return opts.Validate(args, p.Context)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.InstallBundle(opts)
Expand Down Expand Up @@ -181,7 +181,7 @@ For instance, the 'debug' driver may be specified, which simply logs the info gi
porter bundle upgrade --driver debug
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
return opts.Validate(args, p.Context)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.UpgradeBundle(opts)
Expand Down Expand Up @@ -233,7 +233,7 @@ For instance, the 'debug' driver may be specified, which simply logs the info gi
porter bundle uninstall --driver debug
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
return opts.Validate(args, p.Context)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.UninstallBundle(opts)
Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/credentials.go
Expand Up @@ -84,7 +84,7 @@ will then provide it to the bundle in the correct location. `,
porter bundle credential generate kubecred --file myapp/bundle.json --dry-run
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.Validate(args)
return opts.Validate(args, p.Context)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.GenerateCredentials(opts)
Expand Down
6 changes: 3 additions & 3 deletions pkg/porter/build.go
Expand Up @@ -302,13 +302,13 @@ func (p *Porter) buildBundle(invocationImage string, digest string) error {
}

func (p Porter) writeBundle(b bundle.Bundle) error {
f, err := p.Config.FileSystem.OpenFile("bundle.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
f, err := p.Config.FileSystem.OpenFile("cnab/bundle.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer f.Close()
if err != nil {
return errors.Wrapf(err, "error creating bundle.json")
return errors.Wrapf(err, "error creating cnab/bundle.json")
}
_, err = b.WriteTo(f)
return errors.Wrap(err, "error writing to bundle.json")
return errors.Wrap(err, "error writing to cnab/bundle.json")
}

func (p *Porter) generateBundleParameters() map[string]bundle.ParameterDefinition {
Expand Down
12 changes: 6 additions & 6 deletions pkg/porter/build_test.go
Expand Up @@ -173,16 +173,16 @@ func TestPorter_buildBundle(t *testing.T) {
err = p.buildBundle("foo", "digest")
require.NoError(t, err)

bundleJSONExists, err := p.FileSystem.Exists("bundle.json")
bundleJSONExists, err := p.FileSystem.Exists("cnab/bundle.json")
require.NoError(t, err)
require.True(t, bundleJSONExists, "bundle.json wasn't written")
require.True(t, bundleJSONExists, "cnab/bundle.json wasn't written")

f, _ := p.FileSystem.Stat("bundle.json")
f, _ := p.FileSystem.Stat("cnab/bundle.json")
if f.Size() == 0 {
t.Fatalf("bundle.json is empty")
t.Fatalf("cnab/bundle.json is empty")
}

bundleBytes, err := p.FileSystem.ReadFile("bundle.json")
bundleBytes, err := p.FileSystem.ReadFile("cnab/bundle.json")
require.NoError(t, err)

var bundle bundle.Bundle
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestPorter_paramRequired(t *testing.T) {
err = p.buildBundle("foo", "digest")
require.NoError(t, err)

bundleBytes, err := p.FileSystem.ReadFile("bundle.json")
bundleBytes, err := p.FileSystem.ReadFile("cnab/bundle.json")
require.NoError(t, err)

var bundle bundle.Bundle
Expand Down
43 changes: 14 additions & 29 deletions pkg/porter/cnab.go
Expand Up @@ -2,17 +2,15 @@ package porter

import (
"bufio"
"io/ioutil"
"os"
"path/filepath"

"github.com/deislabs/duffle/pkg/bundle"

cnabprovider "github.com/deislabs/porter/pkg/cnab/provider"
"github.com/deislabs/porter/pkg/config"
"github.com/deislabs/porter/pkg/context"
"github.com/deislabs/porter/pkg/parameters"
"github.com/pkg/errors"

cnabprovider "github.com/deislabs/porter/pkg/cnab/provider"
)

// CNABProvider
Expand Down Expand Up @@ -61,13 +59,13 @@ type sharedOptions struct {
// Validate prepares for an action and validates the options.
// For example, relative paths are converted to full paths and then checked that
// they exist and are accessible.
func (o *sharedOptions) Validate(args []string) error {
func (o *sharedOptions) Validate(args []string, cxt *context.Context) error {
err := o.validateClaimName(args)
if err != nil {
return err
}

err = o.validateBundlePath()
err = o.validateBundlePath(cxt)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,8 +95,8 @@ func (o *sharedOptions) validateClaimName(args []string) error {
}

// validateBundlePath gets the absolute path to the bundle file.
func (o *sharedOptions) validateBundlePath() error {
err := o.defaultBundleFile()
func (o *sharedOptions) validateBundlePath(cxt *context.Context) error {
err := o.defaultBundleFile(cxt)
if err != nil {
return err
}
Expand All @@ -122,42 +120,29 @@ func (o *sharedOptions) validateBundlePath() error {

// defaultBundleFile defaults the bundle file to the bundle in the current directory
// when none is set.
func (o *sharedOptions) defaultBundleFile() error {
func (o *sharedOptions) defaultBundleFile(cxt *context.Context) error {
if o.File != "" {
return nil
}

// We are looking both for a bundle.json OR a porter manifest
// If we can't find a bundle.json, but we found manifest, tell them to run porter build first
pwd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "could not get current working directory")
}

files, err := ioutil.ReadDir(pwd)
if err != nil {
return errors.Wrapf(err, "could not list current directory %s", pwd)
}

// We are looking both for a bundle.json OR a porter manifest
// If we can't find a bundle.json, but we found manifest, tell them to run porter build first
foundManifest := false
for _, f := range files {
// TODO: handle defaulting to a signed bundle
if !f.IsDir() && f.Name() == "bundle.json" {
o.File = "bundle.json"
break
}

if !f.IsDir() && f.Name() == config.Name {
foundManifest = true
}
foundCNAB, _ := cxt.FileSystem.Exists(filepath.Join(pwd, "cnab/bundle.json"))
if foundCNAB {
o.File = "cnab/bundle.json"
}
foundManifest, _ := cxt.FileSystem.Exists(filepath.Join(pwd, config.Name))

if !o.bundleRequired && o.File == "" {
return nil
}

if o.File == "" && foundManifest {
return errors.New("first run 'porter build' to generate a bundle.json, then run 'porter install'")
return errors.New("first run 'porter build' and then run 'porter install'")
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/porter/credentials.go
Expand Up @@ -3,10 +3,10 @@ package porter
import (
"fmt"

"github.com/deislabs/porter/pkg/context"
"github.com/deislabs/porter/pkg/credentials"
"github.com/deislabs/porter/pkg/printer"
"github.com/pkg/errors"

yaml "gopkg.in/yaml.v2"
)

Expand All @@ -23,13 +23,13 @@ type CredentialOptions struct {
// Validate prepares for an action and validates the options.
// For example, relative paths are converted to full paths and then checked that
// they exist and are accessible.
func (g *CredentialOptions) Validate(args []string) error {
func (g *CredentialOptions) Validate(args []string, cxt *context.Context) error {
err := g.validateCredName(args)
if err != nil {
return err
}

err = g.validateBundlePath()
err = g.validateBundlePath(cxt)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/porter/install.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

cnabprovider "github.com/deislabs/porter/pkg/cnab/provider"
"github.com/deislabs/porter/pkg/context"
)

// InstallOptions that may be specified when installing a bundle.
Expand All @@ -12,9 +13,9 @@ type InstallOptions struct {
sharedOptions
}

func (o *InstallOptions) Validate(args []string) error {
func (o *InstallOptions) Validate(args []string, cxt *context.Context) error {
o.bundleRequired = true
return o.sharedOptions.Validate(args)
return o.sharedOptions.Validate(args, cxt)
}

// ToDuffleArgs converts this instance of user-provided installation options
Expand Down
5 changes: 3 additions & 2 deletions pkg/porter/uninstall.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

cnabprovider "github.com/deislabs/porter/pkg/cnab/provider"
"github.com/deislabs/porter/pkg/context"
)

// UninstallOptions that may be specified when uninstalling a bundle.
Expand All @@ -12,9 +13,9 @@ type UninstallOptions struct {
sharedOptions
}

func (o *UninstallOptions) Validate(args []string) error {
func (o *UninstallOptions) Validate(args []string, cxt *context.Context) error {
o.bundleRequired = false
return o.sharedOptions.Validate(args)
return o.sharedOptions.Validate(args, cxt)
}

// UninstallBundle accepts a set of pre-validated UninstallOptions and uses
Expand Down
5 changes: 3 additions & 2 deletions pkg/porter/upgrade.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

cnabprovider "github.com/deislabs/porter/pkg/cnab/provider"
"github.com/deislabs/porter/pkg/context"
)

// UpgradeOptions that may be specified when uninstalling a bundle.
Expand All @@ -12,9 +13,9 @@ type UpgradeOptions struct {
sharedOptions
}

func (o *UpgradeOptions) Validate(args []string) error {
func (o *UpgradeOptions) Validate(args []string, cxt *context.Context) error {
o.bundleRequired = false
return o.sharedOptions.Validate(args)
return o.sharedOptions.Validate(args, cxt)
}

// UpgradeBundle accepts a set of pre-validated UpgradeOptions and uses
Expand Down

0 comments on commit 2e3950a

Please sign in to comment.