forked from enmand/packer-provisioner-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
114 lines (91 loc) · 2.85 KB
/
s3.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
package main
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/plugin"
"github.com/hashicorp/packer/template/interpolate"
"github.com/jbowes/vice"
)
const defaultTimeout = time.Minute * 2 // 2 minute timeout
// Config is the S3 provisioner configuration
type Config struct {
common.PackerConfig `mapstructure:",squash"`
awscommon.AccessConfig `mapstructure:",squash"`
Bucket string `mapstructure:"bucket"`
FileKey string `mapstructure:"key"`
LocalPath string `mapstructure:"local_path"`
Timeout string `mapstructure:"timeout"`
timeout time.Duration
ctx interpolate.Context
}
// Provisioner is a `file`-like Provisioner that can download a file on S3 to a
// remote instance
type Provisioner struct {
packer.Provisioner
config Config
s3 *s3.S3
}
// Prepare prepres
func (p *Provisioner) Prepare(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return vice.Wrap(err, vice.InvalidArgument, "unable to decode options")
}
if p.config.Bucket == "" {
return vice.Wrap(err, vice.InvalidArgument, "`bucket` is required")
}
if p.config.FileKey == "" {
return vice.Wrap(err, vice.InvalidArgument, "`key` is required")
}
if p.config.LocalPath == "" {
return vice.Wrap(err, vice.InvalidArgument, "`local_path` is required")
}
p.config.timeout, _ = time.ParseDuration(p.config.Timeout)
if p.config.Timeout == "" {
p.config.timeout = defaultTimeout
}
packer.LogSecretFilter.Set(p.config.AccessKey, p.config.SecretKey, p.config.Token)
return nil
}
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator) error {
ui.Say(fmt.Sprintf("Provisioning from S3..."))
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, p.config.timeout)
defer cancel()
session, err := p.config.Session()
if err != nil {
return vice.Wrap(err, vice.AuthRequired, "no aws session")
}
s3conn := s3.New(session)
resp, err := s3conn.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: aws.String(p.config.Bucket),
Key: aws.String(p.config.FileKey),
})
if err != nil {
return vice.Wrap(err, vice.Temporary, "unable to download object from s3")
}
if err := comm.Upload(p.config.LocalPath, resp.Body, nil); err != nil {
return vice.Wrap(err, vice.Temporary, "unable to upload file")
}
return nil
}
func main() {
server, err := plugin.Server()
if err != nil {
panic(err)
}
server.RegisterProvisioner(new(Provisioner))
}