This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
/
flavor.go
133 lines (112 loc) · 3.57 KB
/
flavor.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
124
125
126
127
128
129
130
131
132
133
package vanilla // import "github.com/docker/infrakit/pkg/plugin/flavor/vanilla"
import (
"fmt"
"strings"
logutil "github.com/docker/infrakit/pkg/log"
"github.com/docker/infrakit/pkg/run/scope"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/template"
"github.com/docker/infrakit/pkg/types"
)
var (
log = logutil.New("module", "vanilla")
debugV = logutil.V(300)
)
// Spec is the model of the Properties section of the top level group spec.
type Spec struct {
// Init
Init []string
// InitScriptTemplateURL provides a URL to a template that is used to generaete Init
InitScriptTemplateURL string
// Tags
Tags map[string]string
// Attachments are instructions for external entities that should be attached to the instance.
Attachments []instance.Attachment
}
// NewPlugin creates a Flavor plugin that doesn't do very much. It assumes instances are
// identical (cattles) but can assume specific identities (via the LogicalIDs). The
// instances here are treated identically because we have constant Init that applies
// to all instances
func NewPlugin(scope scope.Scope, opt template.Options) flavor.Plugin {
return vanillaFlavor{scope: scope, options: opt}
}
// DefaultOptions contains the default settings.
var DefaultOptions = template.Options{}
type vanillaFlavor struct {
scope scope.Scope
options template.Options
}
func (f vanillaFlavor) Validate(flavorProperties *types.Any, allocation group.AllocationMethod) error {
spec := Spec{}
err := flavorProperties.Decode(&spec)
if err != nil {
return err
}
if spec.InitScriptTemplateURL != "" && len(spec.Init) > 0 {
return fmt.Errorf("Either \"Init\" or \"InitScriptTemplateURL\" can be specified but not both")
}
if spec.InitScriptTemplateURL != "" {
template, err := f.scope.TemplateEngine(spec.InitScriptTemplateURL, f.options)
if err != nil {
return err
}
_, err = template.Render(nil)
if err != nil {
return err
}
}
return nil
}
func (f vanillaFlavor) Healthy(flavorProperties *types.Any, inst instance.Description) (flavor.Health, error) {
// TODO: We could add support for shell code in the Spec for a command to run for checking health.
return flavor.Healthy, nil
}
func (f vanillaFlavor) Drain(flavorProperties *types.Any, inst instance.Description) error {
// TODO: We could add support for shell code in the Spec for a drain command to run.
return nil
}
func (f vanillaFlavor) Prepare(flavor *types.Any,
instance instance.Spec,
allocation group.AllocationMethod,
index group.Index) (instance.Spec, error) {
s := Spec{}
err := flavor.Decode(&s)
if err != nil {
return instance, err
}
// Handle Init lines, either from templated script or raw input; append to
// and instance.Init lines
lines := []string{}
if instance.Init != "" {
lines = append(lines, instance.Init)
}
if s.InitScriptTemplateURL != "" {
template, err := f.scope.TemplateEngine(s.InitScriptTemplateURL, f.options)
if err != nil {
return instance, err
}
initScript, err := template.Render(nil)
if err != nil {
return instance, err
}
lines = append(lines, initScript)
log.Debug("Init script data:", initScript, "V", debugV)
} else {
lines = append(lines, s.Init...)
}
instance.Init = strings.Join(lines, "\n")
// Append tags
for k, v := range s.Tags {
if instance.Tags == nil {
instance.Tags = map[string]string{}
}
instance.Tags[k] = v
}
// Attachements
for _, a := range s.Attachments {
instance.Attachments = append(instance.Attachments, a)
}
return instance, nil
}