-
Notifications
You must be signed in to change notification settings - Fork 48
/
funcs.go
312 lines (270 loc) · 8.34 KB
/
funcs.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package interpolate
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
"time"
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
commontpl "github.com/hashicorp/packer-plugin-sdk/template"
"github.com/hashicorp/packer-plugin-sdk/uuid"
strftime "github.com/jehiah/go-strftime"
)
// InitTime is the UTC time when this package was initialized. It is
// used as the timestamp for all configuration templates so that they
// match for a single build.
var InitTime time.Time
func init() {
InitTime = time.Now().UTC()
}
// Funcs are the interpolation funcs that are available within interpolations.
var FuncGens = map[string]interface{}{
"build_name": funcGenBuildName,
"build_type": funcGenBuildType,
"env": funcGenEnv,
"isotime": funcGenIsotime,
"strftime": funcGenStrftime,
"pwd": funcGenPwd,
"split": funcGenSplitter,
"template_dir": funcGenTemplateDir,
"timestamp": funcGenTimestamp,
"uuid": funcGenUuid,
"user": funcGenUser,
"packer_version": funcGenPackerVersion,
"consul_key": funcGenConsul,
"vault": funcGenVault,
"sed": funcGenSed,
"build": funcGenBuild,
"aws_secretsmanager": funcGenAwsSecrets,
"replace": replace,
"replace_all": replace_all,
"upper": strings.ToUpper,
"lower": strings.ToLower,
}
var ErrVariableNotSetString = "Error: variable not set:"
// FuncGenerator is a function that given a context generates a template
// function for the template.
type FuncGenerator func(*Context) interface{}
// Funcs returns the functions that can be used for interpolation given
// a context.
func Funcs(ctx *Context) template.FuncMap {
result := make(map[string]interface{})
for k, v := range FuncGens {
switch v := v.(type) {
case func(*Context) interface{}:
result[k] = v(ctx)
default:
result[k] = v
}
}
if ctx != nil {
for k, v := range ctx.Funcs {
result[k] = v
}
}
return template.FuncMap(result)
}
func funcGenSplitter(ctx *Context) interface{} {
return func(k string, s string, i int) (string, error) {
// return func(s string) (string, error) {
split := strings.Split(k, s)
if len(split) <= i {
return "", fmt.Errorf("the substring %d was unavailable using the separator value, %s, only %d values were found", i, s, len(split))
}
return split[i], nil
}
}
func funcGenBuildName(ctx *Context) interface{} {
return func() (string, error) {
if ctx == nil || ctx.BuildName == "" {
return "", errors.New("build_name not available")
}
return ctx.BuildName, nil
}
}
func funcGenBuildType(ctx *Context) interface{} {
return func() (string, error) {
if ctx == nil || ctx.BuildType == "" {
return "", errors.New("build_type not available")
}
return ctx.BuildType, nil
}
}
func funcGenEnv(ctx *Context) interface{} {
return func(k string) (string, error) {
if !ctx.EnableEnv {
// The error message doesn't have to be that detailed since
// semantic checks should catch this.
return "", errors.New("env vars are not allowed here")
}
return os.Getenv(k), nil
}
}
func funcGenIsotime(ctx *Context) interface{} {
return func(format ...string) (string, error) {
if len(format) == 0 {
return InitTime.Format(time.RFC3339), nil
}
if len(format) > 1 {
return "", fmt.Errorf("too many values, 1 needed: %v", format)
}
return InitTime.Format(format[0]), nil
}
}
func funcGenStrftime(ctx *Context) interface{} {
return func(format string) string {
return strftime.Format(format, InitTime)
}
}
func funcGenPwd(ctx *Context) interface{} {
return func() (string, error) {
return os.Getwd()
}
}
func funcGenTemplateDir(ctx *Context) interface{} {
return func() (string, error) {
if ctx == nil || ctx.TemplatePath == "" {
return "", errors.New("template path not available")
}
path, err := filepath.Abs(filepath.Dir(ctx.TemplatePath))
if err != nil {
return "", err
}
return path, nil
}
}
func passthroughOrInterpolate(data map[interface{}]interface{}, s string) (string, error) {
if heldPlace, ok := data[s]; ok {
if hp, ok := heldPlace.(string); ok {
// If we're in the first interpolation pass, the goal is to
// make sure that we pass the value through.
// TODO match against an actual string constant
if strings.Contains(hp, packerbuilderdata.PlaceholderMsg) {
return fmt.Sprintf("{{.%s}}", s), nil
} else {
return hp, nil
}
}
}
return "", fmt.Errorf("loaded data, but couldnt find %s in it.", s)
}
func funcGenBuild(ctx *Context) interface{} {
// Depending on where the context data is coming from, it could take a few
// different map types. The following switch standardizes the map types
// so we can act on them correctly.
return func(s string) (string, error) {
switch data := ctx.Data.(type) {
case map[interface{}]interface{}:
return passthroughOrInterpolate(data, s)
case map[string]interface{}:
// convert to a map[interface{}]interface{} so we can use same
// parsing on it
passed := make(map[interface{}]interface{}, len(data))
for k, v := range data {
passed[k] = v
}
return passthroughOrInterpolate(passed, s)
case map[string]string:
// convert to a map[interface{}]interface{} so we can use same
// parsing on it
passed := make(map[interface{}]interface{}, len(data))
for k, v := range data {
passed[k] = v
}
return passthroughOrInterpolate(passed, s)
default:
return "", fmt.Errorf("Error validating build variable: the given "+
"variable %s will not be passed into your plugin.", s)
}
}
}
func funcGenTimestamp(ctx *Context) interface{} {
return func() string {
return strconv.FormatInt(InitTime.Unix(), 10)
}
}
func funcGenUser(ctx *Context) interface{} {
return func(k string) (string, error) {
if ctx == nil || ctx.UserVariables == nil {
return "", errors.New("test")
}
val, ok := ctx.UserVariables[k]
if ctx.EnableEnv {
// error and retry if we're interpolating UserVariables. But if
// we're elsewhere in the template, just return the empty string.
if !ok {
return "", fmt.Errorf("%s %s", ErrVariableNotSetString, k)
}
}
return val, nil
}
}
func funcGenUuid(ctx *Context) interface{} {
return func() string {
return uuid.TimeOrderedUUID()
}
}
func funcGenPackerVersion(ctx *Context) interface{} {
return func() (string, error) {
if ctx == nil || ctx.CorePackerVersionString == "" {
return "", errors.New("packer_version not available")
}
return ctx.CorePackerVersionString, nil
}
}
func funcGenConsul(ctx *Context) interface{} {
return func(key string) (string, error) {
if !ctx.EnableEnv {
// The error message doesn't have to be that detailed since
// semantic checks should catch this.
return "", errors.New("consul_key is not allowed here")
}
return commontpl.Consul(key)
}
}
func funcGenVault(ctx *Context) interface{} {
return func(path string, key string) (string, error) {
// Only allow interpolation from Vault when env vars are being read.
if !ctx.EnableEnv {
// The error message doesn't have to be that detailed since
// semantic checks should catch this.
return "", errors.New("Vault vars are only allowed in the variables section")
}
return commontpl.Vault(path, key)
}
}
func funcGenAwsSecrets(ctx *Context) interface{} {
return func(secret ...string) (string, error) {
if !ctx.EnableEnv {
// The error message doesn't have to be that detailed since
// semantic checks should catch this.
return "", errors.New("AWS Secrets Manager is only allowed in the variables section")
}
switch len(secret) {
case 0:
return "", errors.New("secret name must be provided")
case 1:
return commontpl.GetAWSSecret(secret[0], "")
case 2:
return commontpl.GetAWSSecret(secret[0], secret[1])
default:
return "", errors.New("only secret name and optional secret key can be provided.")
}
}
}
func funcGenSed(ctx *Context) interface{} {
return func(expression string, inputString string) (string, error) {
return "", errors.New("template function `sed` is deprecated " +
"use `replace` or `replace_all` instead." +
"Documentation: https://www.packer.io/docs/templates/engine")
}
}
func replace_all(old, new, src string) string {
return strings.ReplaceAll(src, old, new)
}
func replace(old, new string, n int, src string) string {
return strings.Replace(src, old, new, n)
}