github.com/Datadog/cnab-go@v0.3.3-beta1.0.20191007143216-bba4b7e723d0/driver/driver.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/deislabs/cnab-go/bundle"
     8  	"github.com/docker/go/canonical/json"
     9  )
    10  
    11  // ImageType constants provide some of the image types supported
    12  // TODO: I think we can remove all but Docker, since the rest are supported externally
    13  const (
    14  	ImageTypeDocker = "docker"
    15  	ImageTypeOCI    = "oci"
    16  	ImageTypeQCOW   = "qcow"
    17  )
    18  
    19  // Operation describes the data passed into the driver to run an operation
    20  type Operation struct {
    21  	// Installation is the name of this installation
    22  	Installation string `json:"installation_name"`
    23  	// The revision ID for this installation
    24  	Revision string `json:"revision"`
    25  	// Action is the action to be performed
    26  	Action string `json:"action"`
    27  	// Parameters are the parameters to be injected into the container
    28  	Parameters map[string]interface{} `json:"parameters"`
    29  	// Image is the invocation image
    30  	Image bundle.InvocationImage `json:"image"`
    31  	// Environment contains environment variables that should be injected into the invocation image
    32  	Environment map[string]string `json:"environment"`
    33  	// Files contains files that should be injected into the invocation image.
    34  	Files map[string]string `json:"files"`
    35  	// Outputs is a list of paths starting with `/cnab/app/outputs` that the driver should return the contents of in the OperationResult.
    36  	Outputs []string `json:"outputs"`
    37  	// Output stream for log messages from the driver
    38  	Out io.Writer `json:"-"`
    39  	// Bundle represents the bundle information for use by the operation
    40  	Bundle *bundle.Bundle
    41  }
    42  
    43  // ResolvedCred is a credential that has been resolved and is ready for injection into the runtime.
    44  type ResolvedCred struct {
    45  	Type  string `json:"type"`
    46  	Name  string `json:"name"`
    47  	Value string `json:"value"`
    48  }
    49  
    50  // OperationResult is the output of the Driver running an Operation.
    51  type OperationResult struct {
    52  	// Outputs is a map from the container path of an output file to its contents (i.e. /cnab/app/outputs/...).
    53  	Outputs map[string]string
    54  }
    55  
    56  // Driver is capable of running a invocation image
    57  type Driver interface {
    58  	// Run executes the operation inside of the invocation image
    59  	Run(*Operation) (OperationResult, error)
    60  	// Handles receives an ImageType* and answers whether this driver supports that type
    61  	Handles(string) bool
    62  }
    63  
    64  // Configurable drivers can explain their configuration, and have it explicitly set
    65  type Configurable interface {
    66  	// Config returns a map of configuration names and values that can be set via environment variable
    67  	Config() map[string]string
    68  	// SetConfig allows setting configuration, where name corresponds to the key in Config, and value is
    69  	// the value to be set.
    70  	SetConfig(map[string]string)
    71  }
    72  
    73  // DebugDriver prints the information passed to a driver
    74  //
    75  // It does not ever run the image.
    76  type DebugDriver struct {
    77  	config map[string]string
    78  }
    79  
    80  // Run executes the operation on the Debug driver
    81  func (d *DebugDriver) Run(op *Operation) (OperationResult, error) {
    82  	data, err := json.MarshalIndent(op, "", "  ")
    83  	if err != nil {
    84  		return OperationResult{}, err
    85  	}
    86  	fmt.Fprintln(op.Out, string(data))
    87  	return OperationResult{}, nil
    88  }
    89  
    90  // Handles always returns true, effectively claiming to work for any image type
    91  func (d *DebugDriver) Handles(dt string) bool {
    92  	return true
    93  }
    94  
    95  // Config returns the configuration help text
    96  func (d *DebugDriver) Config() map[string]string {
    97  	return map[string]string{
    98  		"VERBOSE": "Increase verbosity. true, false are supported values",
    99  	}
   100  }
   101  
   102  // SetConfig sets configuration for this driver
   103  func (d *DebugDriver) SetConfig(settings map[string]string) {
   104  	d.config = settings
   105  }