github.com/jdolitsky/cnab-go@v0.7.1-beta1/action/run_custom.go (about)

     1  package action
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/deislabs/cnab-go/claim"
     7  	"github.com/deislabs/cnab-go/credentials"
     8  	"github.com/deislabs/cnab-go/driver"
     9  )
    10  
    11  var (
    12  	// ErrBlockedAction indicates that the requested action was not allowed.
    13  	ErrBlockedAction = errors.New("action not allowed")
    14  	// ErrUndefinedAction indicates that a bundle does not define this action.
    15  	ErrUndefinedAction = errors.New("action not defined for bundle")
    16  )
    17  
    18  // RunCustom allows the execution of an arbitrary target in a CNAB bundle.
    19  type RunCustom struct {
    20  	Driver driver.Driver
    21  	Action string
    22  }
    23  
    24  // blockedActions is a list of actions that cannot be run as custom.
    25  //
    26  // This prevents accidental circumvention of standard behavior.
    27  var blockedActions = map[string]struct{}{"install": {}, "uninstall": {}, "upgrade": {}}
    28  
    29  // Run executes a status action in an image
    30  func (i *RunCustom) Run(c *claim.Claim, creds credentials.Set, opCfgs ...OperationConfigFunc) error {
    31  	if _, ok := blockedActions[i.Action]; ok {
    32  		return ErrBlockedAction
    33  	}
    34  
    35  	actionDef, ok := c.Bundle.Actions[i.Action]
    36  	if !ok {
    37  		return ErrUndefinedAction
    38  	}
    39  
    40  	invocImage, err := selectInvocationImage(i.Driver, c)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	op, err := opFromClaim(i.Action, actionDef.Stateless, c, invocImage, creds)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	err = OperationConfigs(opCfgs).ApplyConfig(op)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	opResult, err := i.Driver.Run(op)
    56  
    57  	// If this action says it does not modify the release, then we don't track
    58  	// it in the claim. Otherwise, we do.
    59  	if !actionDef.Modifies {
    60  		return err
    61  	}
    62  
    63  	// update outputs in claim even if there were errors so users can see the output files.
    64  	outputErrors := setOutputsOnClaim(c, opResult.Outputs)
    65  
    66  	if err != nil {
    67  		c.Update(i.Action, claim.StatusFailure)
    68  		c.Result.Message = err.Error()
    69  		return err
    70  	}
    71  	c.Update(i.Action, claim.StatusSuccess)
    72  
    73  	return outputErrors
    74  }