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
/
action.go
123 lines (108 loc) · 3.12 KB
/
action.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package action
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"github.com/deislabs/duffle/pkg/bundle"
"github.com/deislabs/duffle/pkg/claim"
"github.com/deislabs/duffle/pkg/credentials"
"github.com/deislabs/duffle/pkg/driver"
)
// notStateless is there just to make callers of opFromClaims more readable
const notStateless = false
// Action describes one of the primary actions that can be executed in CNAB.
//
// The actions are:
// - install
// - upgrade
// - uninstall
// - downgrade
// - status
type Action interface {
// Run an action, and record the status in the given claim
Run(*claim.Claim, credentials.Set) error
}
func selectInvocationImage(d driver.Driver, c *claim.Claim) (bundle.InvocationImage, error) {
if len(c.Bundle.InvocationImages) == 0 {
return bundle.InvocationImage{}, errors.New("no invocationImages are defined in the bundle")
}
for _, ii := range c.Bundle.InvocationImages {
if d.Handles(ii.ImageType) {
return ii, nil
}
}
return bundle.InvocationImage{}, errors.New("driver is not compatible with any of the invocation images in the bundle")
}
func getImageMap(b *bundle.Bundle) ([]byte, error) {
imgs := b.Images
if imgs == nil {
imgs = make(map[string]bundle.Image)
}
return json.Marshal(imgs)
}
func appliesToAction(action string, parameter bundle.ParameterDefinition) bool {
if len(parameter.ApplyTo) == 0 {
return true
}
for _, act := range parameter.ApplyTo {
if action == act {
return true
}
}
return false
}
func opFromClaim(action string, stateless bool, c *claim.Claim, ii bundle.InvocationImage, creds credentials.Set, w io.Writer) (*driver.Operation, error) {
env, files, err := creds.Expand(c.Bundle, stateless)
if err != nil {
return nil, err
}
// Quick verification that no params were passed that are not actual legit params.
for key := range c.Parameters {
if _, ok := c.Bundle.Parameters[key]; !ok {
return nil, fmt.Errorf("undefined parameter %q", key)
}
}
for k, param := range c.Bundle.Parameters {
rawval, ok := c.Parameters[k]
if !ok {
if param.Required && appliesToAction(action, param) {
return nil, fmt.Errorf("missing required parameter %q for action %q", k, action)
}
continue
}
value := fmt.Sprintf("%v", rawval)
if param.Destination == nil {
// env is a CNAB_P_
env[fmt.Sprintf("CNAB_P_%s", strings.ToUpper(k))] = value
continue
}
if param.Destination.Path != "" {
files[param.Destination.Path] = value
}
if param.Destination.EnvironmentVariable != "" {
env[param.Destination.EnvironmentVariable] = value
}
}
imgMap, err := getImageMap(c.Bundle)
if err != nil {
return nil, fmt.Errorf("unable to generate image map: %s", err)
}
files["/cnab/app/image-map.json"] = string(imgMap)
env["CNAB_INSTALLATION_NAME"] = c.Name
env["CNAB_ACTION"] = action
env["CNAB_BUNDLE_NAME"] = c.Bundle.Name
env["CNAB_BUNDLE_VERSION"] = c.Bundle.Version
return &driver.Operation{
Action: action,
Installation: c.Name,
Parameters: c.Parameters,
Image: ii.Image,
ImageType: ii.ImageType,
Revision: c.Revision,
Environment: env,
Files: files,
Out: w,
}, nil
}