-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovisioner.go
133 lines (109 loc) · 3.15 KB
/
provisioner.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 main
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
//go:generate go run github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@latest mapstructure-to-hcl2 -type Config,Object
type Config struct {
Profile string `mapstructure:"profile"`
UsePathStyle bool `mapstructure:"use_path_style"`
Objects []Object `mapstructure:"objects"`
ctx interpolate.Context
}
type Object struct {
Source string `mapstructure:"source"`
Destination string `mapstructure:"destination"`
}
type S3Provisioner struct {
conf Config
}
func (p *S3Provisioner) ConfigSpec() hcldec.ObjectSpec {
return p.conf.FlatMapstructure().HCL2Spec()
}
func (p *S3Provisioner) Prepare(raws ...interface{}) error {
if err := config.Decode(&p.conf, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.conf.ctx,
}, raws...); err != nil {
return err
}
for _, o := range p.conf.Objects {
var (
err = fmt.Errorf("invalid source %s: should be in the format of <bucket>/path/to/object", o.Source)
parts = strings.Split(o.Source, "/")
)
if len(parts) < 2 {
return err
}
// handle the following cases
// * /obj
// * obj/
if parts[0] == "" || parts[1] == "" {
return err
}
}
return nil
}
func (p *S3Provisioner) Provision(
ctx context.Context,
ui packer.Ui,
communicator packer.Communicator,
m map[string]interface{},
) error {
p.conf.ctx.Data = m
var cfg aws.Config
if p.conf.Profile != "" {
tmp, err := awsconfig.LoadDefaultConfig(
context.Background(),
awsconfig.WithSharedConfigProfile(p.conf.Profile),
)
if err != nil {
return fmt.Errorf("error loading aws config from profile: %v", err)
}
cfg = tmp
} else {
tmp, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("error loading aws config: %v", err)
}
cfg = tmp
}
downloader := manager.NewDownloader(s3.NewFromConfig(cfg))
for _, o := range p.conf.Objects {
src, err := interpolate.Render(o.Source, &p.conf.ctx)
if err != nil {
return fmt.Errorf("error interpolating source: %v", err)
}
dest, err := interpolate.Render(o.Destination, &p.conf.ctx)
if err != nil {
return fmt.Errorf("error interpolating destination: %v", err)
}
var (
parts = strings.Split(src, "/")
bucket = parts[0]
path = strings.Join(parts[1:], "/")
writeAt = manager.NewWriteAtBuffer(make([]byte, 0))
)
ui.Sayf("retrieving object %s from bucket %s", path, bucket)
if _, err := downloader.Download(ctx, writeAt, &s3.GetObjectInput{
Bucket: &bucket,
Key: &path,
}); err != nil {
return fmt.Errorf("error downloading object %s: %v", path, err)
}
if err := communicator.Upload(dest, bytes.NewReader(writeAt.Bytes()), nil); err != nil {
return fmt.Errorf("error uploading object %s: %v", path, err)
}
}
return nil
}