-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
290 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,9 @@ | ||
package cnabprovider | ||
|
||
import ( | ||
"github.com/cnabio/cnab-go/action" | ||
"github.com/cnabio/cnab-go/claim" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func (r *Runtime) Install(args ActionArguments) error { | ||
b, err := r.ProcessBundle(args.BundlePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
params, err := r.loadParameters(b, args.Params, args.ParameterSets, claim.ActionInstall) | ||
if err != nil { | ||
return errors.Wrap(err, "invalid parameters") | ||
} | ||
|
||
c, err := claim.New(args.Instance, claim.ActionInstall, b, params) | ||
if err != nil { | ||
return errors.Wrap(err, "invalid bundle instance name") | ||
} | ||
|
||
creds, err := r.loadCredentials(b, args.CredentialIdentifiers) | ||
if err != nil { | ||
return errors.Wrap(err, "could not load credentials") | ||
} | ||
|
||
driver, err := r.newDriver(args.Driver, c.Installation, args) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to instantiate driver") | ||
} | ||
|
||
a := action.New(driver, r.claims) | ||
a.SaveAllOutputs = true | ||
|
||
err = a.SaveInitialClaim(c, claim.StatusRunning) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.printDebugInfo(creds, params) | ||
|
||
opResult, result, err := a.Run(c, creds, r.ApplyConfig(args)...) | ||
if err != nil { | ||
err = r.appendFailedResult(err, c) | ||
return errors.Wrap(err, "failed to install the bundle") | ||
} | ||
|
||
return a.SaveOperationResult(opResult, c, result) | ||
return r.ExecuteAction(claim.ActionInstall, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cnabprovider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cnabio/cnab-go/claim" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRuntime_Install(t *testing.T) { | ||
r := NewTestRuntime(t) | ||
r.TestConfig.TestContext.AddTestFile("testdata/bundle.json", "bundle.json") | ||
|
||
args := ActionArguments{ | ||
Instance: "mybuns", | ||
BundlePath: "bundle.json", | ||
} | ||
err := r.Install(args) | ||
require.NoError(t, err, "Install failed") | ||
|
||
c, err := r.claims.ReadLastClaim(args.Instance) | ||
require.NoError(t, err, "ReadLastClaim failed") | ||
|
||
assert.Equal(t, claim.ActionInstall, c.Action, "wrong action recorded") | ||
assert.Equal(t, args.Instance, c.Installation, "wrong installation name recorded") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,5 @@ | ||
package cnabprovider | ||
|
||
import ( | ||
cnabaction "github.com/cnabio/cnab-go/action" | ||
"github.com/cnabio/cnab-go/bundle" | ||
"github.com/cnabio/cnab-go/claim" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// getClaimForInvoke reads an instance from the runtime's claim storage. If one is not found, the bundle | ||
// is examined to see if the action is stateless. If the action is stateless, we create a new, temporary, claim | ||
// Returns a pointer to the claim, a flag to indicate if the claim is temporary, and an error if present. | ||
func (r *Runtime) getClaimForInvoke(bun bundle.Bundle, actionName, instanceName string) (claim.Claim, bool, error) { | ||
c, err := r.claims.ReadLastClaim(instanceName) | ||
if err != nil { | ||
if action, ok := bun.Actions[actionName]; ok { | ||
if action.Stateless { | ||
c = claim.Claim{ | ||
Action: actionName, | ||
Installation: instanceName, | ||
Bundle: bun, | ||
} | ||
return c, true, nil | ||
} | ||
} | ||
return claim.Claim{}, false, errors.Wrapf(err, "could not load bundle instance %s", instanceName) | ||
} | ||
return c, false, nil | ||
} | ||
|
||
func (r *Runtime) Invoke(action string, args ActionArguments) error { | ||
var b bundle.Bundle | ||
var err error | ||
|
||
if args.BundlePath != "" { | ||
b, err = r.ProcessBundle(args.BundlePath) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
existingClaim, isTempClaim, err := r.getClaimForInvoke(b, action, args.Instance) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// If the user didn't override the bundle definition, use the one | ||
// from the existing claim | ||
if args.BundlePath == "" { | ||
b = existingClaim.Bundle | ||
} | ||
|
||
params, err := r.loadParameters(b, args.Params, args.ParameterSets, claim.ActionUpgrade) | ||
if err != nil { | ||
return errors.Wrap(err, "invalid parameters") | ||
} | ||
|
||
// TODO: (carolynvs) this should be consolidated with get claim for invoke, or teh logic in that function simplified | ||
c, err := existingClaim.NewClaim(action, b, params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
creds, err := r.loadCredentials(c.Bundle, args.CredentialIdentifiers) | ||
if err != nil { | ||
return errors.Wrap(err, "could not load credentials") | ||
} | ||
|
||
driver, err := r.newDriver(args.Driver, args.Instance, args) | ||
if err != nil { | ||
return errors.Wrap(err, "unable to instantiate driver") | ||
} | ||
|
||
a := cnabaction.New(driver, r.claims) | ||
a.SaveAllOutputs = true | ||
|
||
modifies, err := c.IsModifyingAction() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Only record runs that modify the bundle, e.g. don't save "logs" or "dry-run" | ||
// In theory a custom action shouldn't ever have modifies AND stateless | ||
// (which creates a temp claim) but just in case, if it does modify, we must | ||
// persist. | ||
shouldPersistClaim := modifies || !isTempClaim | ||
|
||
if shouldPersistClaim { | ||
err = a.SaveInitialClaim(c, claim.StatusRunning) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
r.printDebugInfo(creds, params) | ||
|
||
opResult, result, err := a.Run(c, creds, r.ApplyConfig(args)...) | ||
|
||
if shouldPersistClaim { | ||
if err != nil { | ||
err = r.appendFailedResult(err, c) | ||
return errors.Wrap(err, "failed to invoke the bundle") | ||
} | ||
return a.SaveOperationResult(opResult, c, result) | ||
} else { | ||
return errors.Wrap(err, "failed to invoke the bundle") | ||
} | ||
return r.ExecuteAction(action, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.