-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
vendor_utils.go
384 lines (323 loc) 路 11.3 KB
/
vendor_utils.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package exec
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/hashicorp/go-getter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"os"
"path"
"strconv"
"strings"
"text/template"
"time"
cfg "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
cp "github.com/otiai10/copy"
)
// ExecuteVendorCommand executes `atmos vendor` commands
func ExecuteVendorCommand(cmd *cobra.Command, args []string, vendorCommand string) error {
info, err := processCommandLineArgs("terraform", cmd, args)
if err != nil {
return err
}
// InitCliConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
cliConfig, err := cfg.InitCliConfig(info, true)
if err != nil {
return err
}
flags := cmd.Flags()
dryRun, err := flags.GetBool("dry-run")
if err != nil {
return err
}
component, err := flags.GetString("component")
if err != nil {
return err
}
stack, err := flags.GetString("stack")
if err != nil {
return err
}
if component != "" && stack != "" {
return fmt.Errorf("either '--component' or '--stack' parameter needs to be provided, but not both")
}
if component != "" {
// Process component vendoring
componentType, err := flags.GetString("type")
if err != nil {
return err
}
if componentType == "" {
componentType = "terraform"
}
componentConfig, componentPath, err := ReadAndProcessComponentConfigFile(cliConfig, component, componentType)
if err != nil {
return err
}
return ExecuteComponentVendorCommandInternal(componentConfig.Spec, component, componentPath, dryRun, vendorCommand)
} else {
// Process stack vendoring
return ExecuteStackVendorCommandInternal(stack, dryRun, vendorCommand)
}
}
// ReadAndProcessComponentConfigFile reads and processes `component.yaml` vendor config file
func ReadAndProcessComponentConfigFile(cliConfig cfg.CliConfiguration, component string, componentType string) (cfg.VendorComponentConfig, string, error) {
var componentBasePath string
var componentConfig cfg.VendorComponentConfig
if componentType == "terraform" {
componentBasePath = cliConfig.Components.Terraform.BasePath
} else if componentType == "helmfile" {
componentBasePath = cliConfig.Components.Helmfile.BasePath
} else {
return componentConfig, "", fmt.Errorf("type '%s' is not supported. Valid types are 'terraform' and 'helmfile'", componentType)
}
componentPath := path.Join(cliConfig.BasePath, componentBasePath, component)
dirExists, err := u.IsDirectory(componentPath)
if err != nil {
return componentConfig, "", err
}
if !dirExists {
return componentConfig, "", fmt.Errorf("folder '%s' does not exist", componentPath)
}
componentConfigFile := path.Join(componentPath, cfg.ComponentConfigFileName)
if !u.FileExists(componentConfigFile) {
return componentConfig, "", fmt.Errorf("vendor config file '%s' does not exist in the '%s' folder", cfg.ComponentConfigFileName, componentPath)
}
componentConfigFileContent, err := os.ReadFile(componentConfigFile)
if err != nil {
return componentConfig, "", err
}
if err = yaml.Unmarshal(componentConfigFileContent, &componentConfig); err != nil {
return componentConfig, "", err
}
if componentConfig.Kind != "ComponentVendorConfig" {
return componentConfig, "", fmt.Errorf("invalid 'kind: %s' in the vendor config file '%s'. Supported kinds: 'ComponentVendorConfig'",
componentConfig.Kind,
cfg.ComponentConfigFileName)
}
return componentConfig, componentPath, nil
}
// ExecuteComponentVendorCommandInternal executes a component vendor command
// Supports all protocols (local files, Git, Mercurial, HTTP, HTTPS, Amazon S3, Google GCP),
// URL and archive formats described in https://github.com/hashicorp/go-getter
// https://www.allee.xyz/en/posts/getting-started-with-go-getter
// https://github.com/otiai10/copy
func ExecuteComponentVendorCommandInternal(
vendorComponentSpec cfg.VendorComponentSpec,
component string,
componentPath string,
dryRun bool,
vendorCommand string,
) error {
var tempDir string
var err error
var t *template.Template
var uri string
if vendorCommand == "pull" {
if vendorComponentSpec.Source.Uri == "" {
return errors.New("'uri' must be specified in 'source.uri' in the 'component.yaml' file")
}
// Parse 'uri' template
if vendorComponentSpec.Source.Version != "" {
t, err = template.New(fmt.Sprintf("source-uri-%s", vendorComponentSpec.Source.Version)).Parse(vendorComponentSpec.Source.Uri)
if err != nil {
return err
}
var tpl bytes.Buffer
err = t.Execute(&tpl, vendorComponentSpec.Source)
if err != nil {
return err
}
uri = tpl.String()
} else {
uri = vendorComponentSpec.Source.Uri
}
// Check if `uri` is a file path.
// If it's a file path, check if it's an absolute path.
// If it's not absolute path, join it with the base path (component dir) and convert to absolute path.
if absPath, err := u.JoinAbsolutePathWithPath(componentPath, uri); err == nil {
uri = absPath
}
u.PrintInfo(fmt.Sprintf("Pulling sources for the component '%s' from '%s' and writing to '%s'\n",
component,
uri,
componentPath,
))
if !dryRun {
// Create temp folder
// We are using a temp folder for the following reasons:
// 1. 'git' does not clone into an existing folder (and we have the existing component folder with `component.yaml` in it)
// 2. We have the option to skip some files we don't need and include only the files we need when copying from the temp folder to the destination folder
// ioutil.TempDir is deprecated. As of Go 1.17, this function simply calls os.MkdirTemp
tempDir, err = os.MkdirTemp("", strconv.FormatInt(time.Now().Unix(), 10))
if err != nil {
return err
}
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
u.PrintError(err)
}
}(tempDir)
// Download the source into the temp folder
client := &getter.Client{
Ctx: context.Background(),
// Define the destination to where the files will be stored. This will create the directory if it doesn't exist
Dst: tempDir,
// Source
Src: uri,
Mode: getter.ClientModeDir,
}
if err = client.Get(); err != nil {
return err
}
// Copy from the temp folder to the destination folder with skipping of some files
copyOptions := cp.Options{
// Skip specifies which files should be skipped
Skip: func(info os.FileInfo, src string, dest string) (bool, error) {
if strings.HasSuffix(src, ".git") {
return true, nil
}
trimmedSrc := u.TrimBasePathFromPath(tempDir+"/", src)
// Exclude the files that match the 'excluded_paths' patterns
// It supports POSIX-style Globs for file names/paths (double-star `**` is supported)
// https://en.wikipedia.org/wiki/Glob_(programming)
// https://github.com/bmatcuk/doublestar#patterns
for _, excludePath := range vendorComponentSpec.Source.ExcludedPaths {
excludeMatch, err := u.PathMatch(excludePath, src)
if err != nil {
return true, err
} else if excludeMatch {
// If the file matches ANY of the 'excluded_paths' patterns, exclude the file
fmt.Printf("Excluding the file '%s' since it matches the '%s' pattern from 'excluded_paths'\n",
trimmedSrc,
excludePath,
)
return true, nil
}
}
// Only include the files that match the 'included_paths' patterns (if any pattern is specified)
if len(vendorComponentSpec.Source.IncludedPaths) > 0 {
anyMatches := false
for _, includePath := range vendorComponentSpec.Source.IncludedPaths {
includeMatch, err := u.PathMatch(includePath, src)
if err != nil {
return true, err
} else if includeMatch {
// If the file matches ANY of the 'included_paths' patterns, include the file
fmt.Printf("Including '%s' since it matches the '%s' pattern from 'included_paths'\n",
trimmedSrc,
includePath,
)
anyMatches = true
break
}
}
if anyMatches {
return false, nil
} else {
fmt.Printf("Excluding '%s' since it does not match any pattern from 'included_paths'\n", trimmedSrc)
return true, nil
}
}
// If 'included_paths' is not provided, include all files that were not excluded
fmt.Printf("Including '%s'\n", u.TrimBasePathFromPath(tempDir+"/", src))
return false, nil
},
// Preserve the atime and the mtime of the entries
// On linux we can preserve only up to 1 millisecond accuracy
PreserveTimes: false,
// Preserve the uid and the gid of all entries
PreserveOwner: false,
}
if err = cp.Copy(tempDir, componentPath, copyOptions); err != nil {
return err
}
}
// Process mixins
if len(vendorComponentSpec.Mixins) > 0 {
fmt.Println()
for _, mixin := range vendorComponentSpec.Mixins {
if mixin.Uri == "" {
return errors.New("'uri' must be specified for each 'mixin' in the 'component.yaml' file")
}
if mixin.Filename == "" {
return errors.New("'filename' must be specified for each 'mixin' in the 'component.yaml' file")
}
// Parse 'uri' template
if mixin.Version != "" {
t, err = template.New(fmt.Sprintf("mixin-uri-%s", mixin.Version)).Parse(mixin.Uri)
if err != nil {
return err
}
var tpl bytes.Buffer
err = t.Execute(&tpl, mixin)
if err != nil {
return err
}
uri = tpl.String()
} else {
uri = mixin.Uri
}
// Check if `uri` is a file path.
// If it's a file path, check if it's an absolute path.
// If it's not absolute path, join it with the base path (component dir) and convert to absolute path.
if absPath, err := u.JoinAbsolutePathWithPath(componentPath, uri); err == nil {
uri = absPath
}
u.PrintInfo(fmt.Sprintf("Pulling the mixin '%s' for the component '%s' and writing to '%s'\n",
uri,
component,
path.Join(componentPath, mixin.Filename),
))
if !dryRun {
err = os.RemoveAll(tempDir)
if err != nil {
return err
}
// Download the mixin into the temp file
client := &getter.Client{
Ctx: context.Background(),
Dst: path.Join(tempDir, mixin.Filename),
Src: uri,
Mode: getter.ClientModeFile,
}
if err = client.Get(); err != nil {
return err
}
// Copy from the temp folder to the destination folder
copyOptions := cp.Options{
// Preserve the atime and the mtime of the entries
PreserveTimes: false,
// Preserve the uid and the gid of all entries
PreserveOwner: false,
// OnSymlink specifies what to do on symlink
// Override the destination file if it already exists
// Prevent the error:
// symlink components/terraform/mixins/context.tf components/terraform/infra/vpc-flow-logs-bucket/context.tf: file exists
OnSymlink: func(src string) cp.SymlinkAction {
return cp.Deep
},
}
if err = cp.Copy(tempDir, componentPath, copyOptions); err != nil {
return err
}
}
}
}
}
return nil
}
// ExecuteStackVendorCommandInternal executes a stack vendor command
// TODO: implement this
func ExecuteStackVendorCommandInternal(
stack string,
dryRun bool,
vendorCommand string,
) error {
return fmt.Errorf("command 'atmos vendor %s --stack <stack>' is not implemented yet", vendorCommand)
}