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