-
Notifications
You must be signed in to change notification settings - Fork 27
/
post-processor.go
396 lines (332 loc) · 11 KB
/
post-processor.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
385
386
387
388
389
390
391
392
393
394
395
396
//go:generate mapstructure-to-hcl2 -type Config
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type Config struct {
Region string `mapstructure:"region"`
Bucket string `mapstructure:"bucket"`
CloudFront string `mapstructure:"cloudfront"`
ManifestPath string `mapstructure:"manifest"`
BoxName string `mapstructure:"box_name"`
BoxDir string `mapstructure:"box_dir"`
Version string `mapstructure:"version"`
ACL string `mapstructure:"acl"`
CredentialFile string `mapstructure:"credentials"`
CredentialProfile string `mapstructure:"profile"`
AccessKey string `mapstructure:"access_key_id"`
SecretKey string `mapstructure:"secret_key"`
SessionToken string `mapstructure:"session_token"`
SignedExpiry time.Duration `mapstructure:"signed_expiry"`
StorageClass string `mapstructure:"storage_class"`
PartSize int64 `mapstructure:"part_size"`
Concurrency int `mapstructure:"concurrency"`
common.PackerConfig `mapstructure:",squash"`
ctx interpolate.Context
}
type PostProcessor struct {
config Config
session *session.Session
s3 *s3.S3
}
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{"output"},
},
}, raws...)
if err != nil {
return err
}
errs := new(packer.MultiError)
// required configuration
templates := map[string]*string{
"region": &p.config.Region,
"bucket": &p.config.Bucket,
"manifest": &p.config.ManifestPath,
"box_name": &p.config.BoxName,
"box_dir": &p.config.BoxDir,
}
for key, ptr := range templates {
if *ptr == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("vagrant-s3 %s must be set", key))
}
}
// Template process
for key, ptr := range templates {
if err = interpolate.Validate(*ptr, &p.config.ctx); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error parsing %s template: %s", key, err))
}
}
var cred *credentials.Credentials = nil // nil credentials use the default aws sdk credential chain
if p.config.AccessKey != "" && p.config.SecretKey != "" {
// StaticProvider if both access id and secret are defined
// Environmental variables used:
// $AWS_SESSION_TOKEN
cred = credentials.NewCredentials(&credentials.StaticProvider{
Value: credentials.Value{
AccessKeyID: p.config.AccessKey,
SecretAccessKey: p.config.SecretKey,
SessionToken: p.config.SessionToken,
},
})
} else if p.config.CredentialFile != "" || p.config.CredentialProfile != "" {
// SharedCredentialProvider if either credentials file or a profile is defined
// Environmental variables used:
// $AWS_SHARED_CREDENTIALS_FILE ("$HOME/.aws/credentials" if unset)
// $AWS_PROFILE ("default" if unset)
cred = credentials.NewCredentials(&credentials.SharedCredentialsProvider{
Filename: p.config.CredentialFile,
Profile: p.config.CredentialProfile,
})
} else {
// EnvProvider as fallback if none of the above matched
// Environmental variables used:
// $AWS_ACCESS_KEY_ID ($AWS_ACCESS_KEY if unset)
// $AWS_SECRET_ACCESS_KEY ($AWS_SECRET_KEY if unset)
// $AWS_SESSION_TOKEN
cred = credentials.NewCredentials(&credentials.EnvProvider{})
}
p.session = session.New(&aws.Config{
Region: aws.String(p.config.Region),
Credentials: cred,
})
p.s3 = s3.New(p.session)
// check that we have permission to access the bucket
_, err = p.s3.HeadBucket(&s3.HeadBucketInput{
Bucket: aws.String(p.config.Bucket),
})
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Unable to access the bucket %s:\n%s\nMake sure your credentials are valid and have sufficient permissions", p.config.Bucket, err))
}
if p.config.ACL == "" {
p.config.ACL = "public-read"
}
// set default storage class
if p.config.StorageClass == "" {
p.config.StorageClass = "STANDARD"
}
if p.config.PartSize == 0 {
p.config.PartSize = s3manager.DefaultUploadPartSize
}
if p.config.Concurrency == 0 {
p.config.Concurrency = s3manager.DefaultUploadConcurrency
}
if len(errs.Errors) > 0 {
return errs
}
return nil
}
func (p *PostProcessor) PostProcess(context context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
// Only accept input from the vagrant post-processor
if artifact.BuilderId() != "mitchellh.post-processor.vagrant" && artifact.BuilderId() != "vagrant" {
return nil, false, false, fmt.Errorf("Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId())
}
// Assume there is only one .box file to upload
box := artifact.Files()[0]
if !strings.HasSuffix(box, ".box") {
return nil, false, false, fmt.Errorf("Unknown files in artifact from vagrant post-processor: %s", artifact.Files())
}
provider := providerFromBuilderName(artifact.Id())
ui.Say(fmt.Sprintf("Preparing to upload box for '%s' provider to S3 bucket '%s'", provider, p.config.Bucket))
// determine box size
boxStat, err := os.Stat(box)
if err != nil {
return nil, false, false, err
}
ui.Message(fmt.Sprintf("Box to upload: %s (%d bytes)", box, boxStat.Size()))
// determine version
version := p.config.Version
if version == "" {
version, err = p.determineVersion()
if err != nil {
return nil, false, false, err
}
ui.Message(fmt.Sprintf("No version defined, using %s as new version", version))
} else {
ui.Message(fmt.Sprintf("Using %s as new version", version))
}
// generate the path to store the box in S3
boxPath := fmt.Sprintf("%s/%s/%s", p.config.BoxDir, version, path.Base(box))
ui.Message("Generating checksum")
checksum, err := sum256(box)
if err != nil {
return nil, false, false, err
}
ui.Message(fmt.Sprintf("Checksum is %s", checksum))
// upload the box to S3
ui.Message(fmt.Sprintf("Uploading box to S3: %s, PartSize: %d, Concurrency: %d", boxPath, p.config.PartSize, p.config.Concurrency))
start := time.Now()
err = p.uploadBox(box, boxPath)
if err != nil {
return nil, false, false, err
} else {
elapsed := time.Since(start)
ui.Message(fmt.Sprintf("Box upload took: %s", elapsed))
}
// get the latest manifest so we can add to it
ui.Message("Fetching latest manifest")
manifest, err := p.getManifest()
if err != nil {
return nil, false, false, err
}
ui.Message(fmt.Sprintf("Adding %s %s box to manifest", provider, version))
var url string
if p.config.SignedExpiry == 0 {
url = generateS3Url(p.config.Region, p.config.Bucket, p.config.CloudFront, boxPath)
} else {
// fetch the new object
boxObject, _ := p.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(p.config.Bucket),
Key: aws.String(boxPath),
})
url, err = boxObject.Presign(p.config.SignedExpiry)
if err != nil {
return nil, false, false, err
}
}
if err := manifest.add(version, &Provider{
Name: provider,
Url: url,
ChecksumType: "sha256",
Checksum: checksum,
}); err != nil {
return nil, false, false, err
}
ui.Message(fmt.Sprintf("Uploading the manifest: %s", p.config.ManifestPath))
if err := p.putManifest(manifest); err != nil {
return nil, false, false, err
}
return &Artifact{generateS3Url(p.config.Region, p.config.Bucket, p.config.CloudFront, p.config.ManifestPath)}, true, false, nil
}
func (p *PostProcessor) determineVersion() (string, error) {
// get the next version based on the existing manifest
if manifest, err := p.getManifest(); err != nil {
return "", err
} else {
return manifest.getNextVersion(), nil
}
}
func (p *PostProcessor) uploadBox(box, boxPath string) error {
// open the file for reading
file, err := os.Open(box)
if err != nil {
return err
}
defer file.Close()
// upload the file
uploader := s3manager.NewUploader(p.session, func(u *s3manager.Uploader) {
u.PartSize = p.config.PartSize
u.Concurrency = p.config.Concurrency
})
_, err = uploader.Upload(&s3manager.UploadInput{
Body: file,
Bucket: aws.String(p.config.Bucket),
Key: aws.String(boxPath),
ACL: aws.String(p.config.ACL),
StorageClass: aws.String(p.config.StorageClass),
})
return err
}
func (p *PostProcessor) getManifest() (*Manifest, error) {
result, err := p.s3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(p.config.Bucket),
Key: aws.String(p.config.ManifestPath),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NoSuchKey" {
return &Manifest{Name: p.config.BoxName}, nil
}
}
return nil, err
}
defer result.Body.Close()
manifest := &Manifest{}
if err := json.NewDecoder(result.Body).Decode(manifest); err != nil {
return nil, err
}
return manifest, nil
}
func (p *PostProcessor) putManifest(manifest *Manifest) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(manifest); err != nil {
return err
}
_, err := p.s3.PutObject(&s3.PutObjectInput{
Body: strings.NewReader(buf.String()),
Bucket: aws.String(p.config.Bucket),
Key: aws.String(p.config.ManifestPath),
ContentType: aws.String("application/json"),
ACL: aws.String(p.config.ACL),
})
if err != nil {
return err
}
return nil
}
func generateS3Url(region, bucket, cloudFront, key string) string {
if cloudFront != "" {
return fmt.Sprintf("https://%s/%s", cloudFront, key)
}
if region == "us-east-1" {
return fmt.Sprintf("https://s3.amazonaws.com/%s/%s", bucket, key)
}
return fmt.Sprintf("https://s3-%s.amazonaws.com/%s/%s", region, bucket, key)
}
// calculates a sha256 checksum of the file
func sum256(filePath string) (string, error) {
// open the file for reading
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
h := sha256.New()
if _, err := io.Copy(h, file); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// converts a packer builder name to the corresponding vagrant provider
func providerFromBuilderName(name string) string {
switch name {
case "aws":
return "aws"
case "digitalocean":
return "digitalocean"
case "virtualbox":
return "virtualbox"
case "vmware":
return "vmware_desktop"
case "parallels":
return "parallels"
default:
return name
}
}