Skip to content

Commit

Permalink
Merge pull request #525 from carolynvs/dep-outputs
Browse files Browse the repository at this point in the history
Support dependency outputs
  • Loading branch information
carolynvs-msft authored Aug 21, 2019
2 parents 2987203 + fee79b0 commit a105848
Show file tree
Hide file tree
Showing 33 changed files with 560 additions and 275 deletions.
11 changes: 6 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[[constraint]]
name = "github.com/deislabs/cnab-go"
version = "v0.3.2-beta1"
source = "github.com/carolynvs/cnab-go"
branch = "op-config-func"

# override currently needed or else cnab-to-oci dep causes issues
[[override]]
name = "github.com/deislabs/cnab-go"
version = "v0.3.2-beta1"
source = "github.com/carolynvs/cnab-go"
branch = "op-config-func"

# Using master until there is a release of cnab-to-oci
[[constraint]]
Expand Down
8 changes: 8 additions & 0 deletions build/testdata/bundles/mysql/porter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ uninstall:
purge: true
releases:
- "{{ bundle.parameters.mysql-name }}"

outputs:
- name: mysql-password
description: "The mysql database password"
type: string
applyTo:
- install
sensitive: true
2 changes: 1 addition & 1 deletion build/testdata/bundles/wordpress/porter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ install:
#externalDatabase.database: "{{ bundle.dependencies.mysql.parameters.database-name }}"
#externalDatabase.host: "{{ bundle.dependencies.mysql.outputs.services }}"
#externalDatabase.user: "{{ bundle.dependencies.mysql.parameters.mysql-user }}"
#externalDatabase.password: "{{ bundle.dependencies.mysql.outputs.mysql-password }}"
externalDatabase.password: "{{ bundle.dependencies.mysql.outputs.mysql-password }}"
externalDatabase.port: 3306
mariadb.enabled: false
outputs:
Expand Down
8 changes: 4 additions & 4 deletions pkg/cnab/extensions/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

type DependencyLock struct {
Name string
Tag string
Alias string
Tag string
}

type DependencySolver struct {
Expand All @@ -36,8 +36,8 @@ func (s *DependencySolver) ResolveDependencies(bun *bundle.Bundle) ([]Dependency
return nil, err
}
lock := DependencyLock{
Name: alias,
Tag: fmt.Sprintf("%s:%s", bundle, version),
Alias: alias,
Tag: fmt.Sprintf("%s:%s", bundle, version),
}
q = append(q, lock)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cnab/extensions/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDependencySolver_ResolveDependencies(t *testing.T) {
require.Len(t, locks, 1)

lock := locks[0]
assert.Equal(t, "mysql", lock.Name)
assert.Equal(t, "mysql", lock.Alias)
assert.Equal(t, "deislabs/mysql:5.7", lock.Tag)
}

Expand Down
19 changes: 16 additions & 3 deletions pkg/cnab/provider/action.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package cnabprovider

import (
"github.com/deislabs/cnab-go/driver"
)

// Shared arguments for all CNAB actions supported by duffle
type ActionArguments struct {
// Name of the claim.
Claim string

// Either a filepath to the bundle or the name of the bundle.
BundleIdentifier string
BundlePath string

// BundleIdentifier is a filepath.
BundleIsFile bool
// Additional files to copy into the bundle
// Target Path => File Contents
Files map[string]string

// Insecure bundle action allowed.
Insecure bool
Expand All @@ -23,3 +28,11 @@ type ActionArguments struct {
// Driver is the CNAB-compliant driver used to run bundle actions.
Driver string
}

func (args ActionArguments) ApplyFiles() func(op *driver.Operation) {
return func(op *driver.Operation) {
for k, v := range args.Files {
op.Files[k] = v
}
}
}
2 changes: 1 addition & 1 deletion pkg/cnab/provider/dockerdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewDriver_Docker(t *testing.T) {
c := config.NewTestConfig(t)
d := NewDuffle(c.Config)

driver, err := d.newDriver("docker", "myclaim")
driver, err := d.newDriver("docker", "myclaim", ActionArguments{})
require.NoError(t, err)

if _, ok := driver.(*docker.Driver); ok {
Expand Down
26 changes: 13 additions & 13 deletions pkg/cnab/provider/duffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"os"
"path/filepath"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/pkg/errors"

"github.com/deislabs/cnab-go/claim"
"github.com/deislabs/cnab-go/driver"
dockerdriver "github.com/deislabs/cnab-go/driver/docker"
"github.com/deislabs/cnab-go/driver/docker"
"github.com/deislabs/cnab-go/driver/lookup"

"github.com/deislabs/porter/pkg/config"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/pkg/errors"
)

type Duffle struct {
Expand All @@ -26,19 +24,21 @@ func NewDuffle(c *config.Config) *Duffle {
}
}

func (d *Duffle) newDriver(driverName, claimName string) (driver.Driver, error) {
func (d *Duffle) newDriver(driverName string, claimName string, args ActionArguments) (driver.Driver, error) {
driverImpl, err := lookup.Lookup(driverName)
if err != nil {
return driverImpl, err
}

// Load any driver-specific config out of the environment.
// TODO: This should be exposed in duffle, taken from cmd/duffle/main.go prepareDriver
if configurable, ok := driverImpl.(driver.Configurable); ok {
driverCfg := map[string]string{}
driverCfg := make(map[string]string)
// Load any driver-specific config out of the environment
for env := range configurable.Config() {
driverCfg[env] = os.Getenv(env)
if val, ok := os.LookupEnv(env); ok {
driverCfg[env] = val
}
}

configurable.SetConfig(driverCfg)
}

Expand All @@ -53,7 +53,7 @@ func (d *Duffle) newDriver(driverName, claimName string) (driver.Driver, error)

func (d *Duffle) setupOutputsMount(driverImpl driver.Driver, claimName string) error {
// If docker driver, setup host bind mount for outputs
if dockerish, ok := driverImpl.(*dockerdriver.Driver); ok {
if dockerish, ok := driverImpl.(*docker.Driver); ok {
outputsDir, err := d.Config.GetOutputsDir()
if err != nil {
return errors.Wrap(err, "unable to get outputs directory")
Expand All @@ -66,7 +66,7 @@ func (d *Duffle) setupOutputsMount(driverImpl driver.Driver, claimName string) e
return errors.Wrapf(err, "could not create outputs directory %s for docker driver bind mount", bundleOutputsDir)
}

var cfgOpt dockerdriver.ConfigurationOption = func(containerCfg *container.Config, hostCfg *container.HostConfig) error {
var cfgOpt docker.ConfigurationOption = func(containerCfg *container.Config, hostCfg *container.HostConfig) error {
outputsMount := mount.Mount{
Type: mount.TypeBind,
Source: bundleOutputsDir,
Expand Down
13 changes: 6 additions & 7 deletions pkg/cnab/provider/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (

"github.com/deislabs/cnab-go/action"
"github.com/deislabs/cnab-go/claim"
"github.com/pkg/errors"

"github.com/deislabs/porter/pkg/config"
"github.com/pkg/errors"
)

func (d *Duffle) Install(args ActionArguments) error {
Expand All @@ -19,8 +18,7 @@ func (d *Duffle) Install(args ActionArguments) error {
return errors.Wrap(err, "invalid claim name")
}

// TODO: handle resolving based on bundle name
b, err := d.LoadBundle(args.BundleIdentifier, args.Insecure)
b, err := d.LoadBundle(args.BundlePath, args.Insecure)
if err != nil {
return err
}
Expand All @@ -37,12 +35,13 @@ func (d *Duffle) Install(args ActionArguments) error {
}
c.Parameters = params

driver, err := d.newDriver(args.Driver, c.Name)
dvr, err := d.newDriver(args.Driver, c.Name, args)
if err != nil {
return errors.Wrap(err, "unable to instantiate driver")
}
i := action.Install{
Driver: driver,
Driver: dvr,
OperationConfig: args.ApplyFiles(),
}

creds, err := d.loadCredentials(b, args.CredentialIdentifiers)
Expand All @@ -61,7 +60,7 @@ func (d *Duffle) Install(args ActionArguments) error {
for k := range params {
paramKeys = append(paramKeys, k)
}
fmt.Fprintf(d.Err, "installing bundle %s (%s) as %s\n\tparams: %v\n\tcreds: %v\n", c.Bundle.Name, args.BundleIdentifier, c.Name, paramKeys, credKeys)
fmt.Fprintf(d.Err, "installing bundle %s (%s) as %s\n\tparams: %v\n\tcreds: %v\n", c.Bundle.Name, args.BundlePath, c.Name, paramKeys, credKeys)
}

// Install and capture error
Expand Down
30 changes: 15 additions & 15 deletions pkg/cnab/provider/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ func (d *Duffle) Invoke(action string, args ActionArguments) error {
if err != nil {
return errors.Wrapf(err, "could not access claim store")
}
claim, err := claims.Read(args.Claim)
c, err := claims.Read(args.Claim)
if err != nil {
return errors.Wrapf(err, "could not load claim %s", args.Claim)
}

if args.BundleIdentifier != "" {
// TODO: handle resolving based on bundle name
claim.Bundle, err = d.LoadBundle(args.BundleIdentifier, args.Insecure)
if args.BundlePath != "" {
c.Bundle, err = d.LoadBundle(args.BundlePath, args.Insecure)
if err != nil {
return err
}
}

if len(args.Params) > 0 {
claim.Parameters, err = d.loadParameters(&claim, args.Params, action)
c.Parameters, err = d.loadParameters(&c, args.Params, action)
if err != nil {
return errors.Wrap(err, "invalid parameters")
}
}

driver, err := d.newDriver(args.Driver, claim.Name)
driver, err := d.newDriver(args.Driver, c.Name, args)
if err != nil {
return errors.Wrap(err, "unable to instantiate driver")
}

i := cnabaction.RunCustom{
Action: action,
Driver: driver,
Action: action,
Driver: driver,
OperationConfig: args.ApplyFiles(),
}

creds, err := d.loadCredentials(claim.Bundle, args.CredentialIdentifiers)
creds, err := d.loadCredentials(c.Bundle, args.CredentialIdentifiers)
if err != nil {
return errors.Wrap(err, "could not load credentials")
}
Expand All @@ -54,21 +54,21 @@ func (d *Duffle) Invoke(action string, args ActionArguments) error {
credKeys = append(credKeys, k)
}
// param values may also be sensitive, so just print names
paramKeys := make([]string, 0, len(claim.Parameters))
for k := range claim.Parameters {
paramKeys := make([]string, 0, len(c.Parameters))
for k := range c.Parameters {
paramKeys = append(paramKeys, k)
}
fmt.Fprintf(d.Err, "invoking bundle %s (%s) with action %s as %s\n\tparams: %v\n\tcreds: %v\n", claim.Bundle.Name, args.BundleIdentifier, action, claim.Name, paramKeys, credKeys)
fmt.Fprintf(d.Err, "invoking bundle %s (%s) with action %s as %s\n\tparams: %v\n\tcreds: %v\n", c.Bundle.Name, args.BundlePath, action, c.Name, paramKeys, credKeys)
}

// Run the action and ALWAYS write out a claim, even if the action fails
runErr := i.Run(&claim, creds, d.Out)
runErr := i.Run(&c, creds, d.Out)

// Add/update the outputs section of a claim and capture error
err = d.WriteClaimOutputs(&claim, action)
err = d.WriteClaimOutputs(&c, action)

// ALWAYS write out a claim, even if the action fails
saveErr := claims.Store(claim)
saveErr := claims.Store(c)
if runErr != nil {
return errors.Wrap(runErr, "failed to invoke the bundle")
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/cnab/provider/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ func (d *Duffle) Uninstall(args ActionArguments) error {
return errors.Wrapf(err, "could not load claim %s", args.Claim)
}

if args.BundleIdentifier != "" {
// TODO: handle resolving based on bundle name
// TODO: if they installed an insecure bundle, do they really need to do --insecure again to unisntall it?
c.Bundle, err = d.LoadBundle(args.BundleIdentifier, args.Insecure)
if args.BundlePath != "" {
// TODO: if they installed an insecure bundle, do they really need to do --insecure again to uninstall it?
c.Bundle, err = d.LoadBundle(args.BundlePath, args.Insecure)
if err != nil {
return err
}
Expand All @@ -42,12 +41,13 @@ func (d *Duffle) Uninstall(args ActionArguments) error {
}
}

driver, err := d.newDriver(args.Driver, c.Name)
driver, err := d.newDriver(args.Driver, c.Name, args)
if err != nil {
return errors.Wrap(err, "unable to instantiate driver")
}
i := action.Uninstall{
Driver: driver,
Driver: driver,
OperationConfig: args.ApplyFiles(),
}

creds, err := d.loadCredentials(c.Bundle, args.CredentialIdentifiers)
Expand All @@ -66,7 +66,7 @@ func (d *Duffle) Uninstall(args ActionArguments) error {
for k := range c.Parameters {
paramKeys = append(paramKeys, k)
}
fmt.Fprintf(d.Err, "uninstalling bundle %s (%s) as %s\n\tparams: %v\n\tcreds: %v\n", c.Bundle.Name, args.BundleIdentifier, c.Name, paramKeys, credKeys)
fmt.Fprintf(d.Err, "uninstalling bundle %s (%s) as %s\n\tparams: %v\n\tcreds: %v\n", c.Bundle.Name, args.BundlePath, c.Name, paramKeys, credKeys)
}

err = i.Run(&c, creds, d.Out)
Expand Down
Loading

0 comments on commit a105848

Please sign in to comment.