This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
/
userdata.go
164 lines (135 loc) · 4 KB
/
userdata.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package model
import (
"log"
"github.com/coreos/coreos-cloudinit/config/validate"
"github.com/kubernetes-incubator/kube-aws/filereader/texttemplate"
"github.com/kubernetes-incubator/kube-aws/gzipcompressor"
"bytes"
"encoding/base64"
"fmt"
"strings"
"text/template"
)
// UserDataValidateFunc returns error if templated Part content doesn't pass validation
type UserDataValidateFunc func(content []byte) error
const (
USERDATA_S3 = "s3"
USERDATA_INSTANCE = "instance"
USERDATA_INSTANCE_SCRIPT = "instance-script"
)
// UserData represents userdata which might be split across multiple storage types
type UserData struct {
Parts map[string]*UserDataPart
}
type UserDataPart struct {
Asset Asset
tmpl *template.Template
tmplData interface{}
validate UserDataValidateFunc
}
type PartDesc struct {
templateName string
validateFunc UserDataValidateFunc
}
var (
defaultParts = []PartDesc{
{USERDATA_INSTANCE, validateNone},
{USERDATA_INSTANCE_SCRIPT, validateNone},
{USERDATA_S3, validateCoreosCloudInit}}
)
type userDataOpt struct {
Parts []PartDesc // userdata Parts in template file
}
type UserDataOption func(*userDataOpt)
// Parts to find in UserData template file
func UserDataPartsOpt(Parts ...PartDesc) UserDataOption {
return func(o *userDataOpt) {
o.Parts = Parts
}
}
// NewUserData creates userdata struct from template file.
// Template file is expected to have defined subtemplates (Parts) which are of various part and storage types
func NewUserData(templateFile string, context interface{}, opts ...UserDataOption) (UserData, error) {
v := UserData{make(map[string]*UserDataPart)}
funcs := template.FuncMap{
"self": func() UserData { return v },
// we add 'extra' stub so templates can be parsed successfully
"extra": func() (r string) { panic("[bug] Stub 'extra' was not replaced") },
}
tmpl, err := texttemplate.ParseFile(templateFile, funcs)
if err != nil {
return UserData{}, err
}
var o userDataOpt
for _, opt := range opts {
opt(&o)
}
if len(o.Parts) == 0 {
o.Parts = defaultParts
}
for _, p := range o.Parts {
if p.validateFunc == nil {
return UserData{}, fmt.Errorf("ValidateFunc must not be nil. Use 'validateNone' if you don't require part validation")
}
t := tmpl.Lookup(p.templateName)
if t == nil {
return UserData{}, fmt.Errorf("Can't find requested template %s in %s", p.templateName, templateFile)
}
v.Parts[p.templateName] = &UserDataPart{
tmpl: t,
tmplData: context,
validate: p.validateFunc,
}
}
return v, nil
}
func (self UserDataPart) Base64(compress bool, extra ...map[string]interface{}) (string, error) {
content, err := self.Template(extra...)
if err != nil {
return "", err
}
if compress {
return gzipcompressor.CompressString(content)
} else {
return base64.StdEncoding.EncodeToString([]byte(content)), nil
}
}
func (self UserDataPart) Template(extra ...map[string]interface{}) (string, error) {
buf := bytes.Buffer{}
funcs := template.FuncMap{}
switch len(extra) {
case 0:
case 1:
funcs["extra"] = func() map[string]interface{} { return extra[0] }
default:
return "", fmt.Errorf("Provide single extra context")
}
if err := self.tmpl.Funcs(funcs).Execute(&buf, self.tmplData); err != nil {
return "", err
}
result := buf.String()
if len(result) == 0 {
return "", fmt.Errorf("failed to render template: result should'nt be empty for asset: %s", self.Asset.Key)
}
// we validate userdata at render time, because we need to wait for
// optional extra context to produce final output
return result, self.validate(buf.Bytes())
}
func validateCoreosCloudInit(content []byte) error {
report, err := validate.Validate(content)
if err != nil {
return err
}
errors := []string{}
for _, entry := range report.Entries() {
errors = append(errors, fmt.Sprintf("%+v", entry))
}
if len(errors) > 0 {
log.Printf("Bad cloud-config:-\n%s\n", content)
return fmt.Errorf("cloud-config validation errors:\n%s\n", strings.Join(errors, "\n"))
}
return nil
}
func validateNone([]byte) error {
return nil
}