-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioner.go
165 lines (126 loc) · 3.76 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
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
package main
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
getter "github.com/hashicorp/go-getter"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
type Config struct {
common.PackerConfig `mapstructure:",squash"`
URL string `mapstructure:"url"`
S3AccessKey string `mapstructure:"s3_access_key"`
S3SecretKey string `mapstructure:"s3_secret_key"`
Destination string `mapstructure:"destination"`
ctx interpolate.Context
}
type Provisioner struct {
config Config
}
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }
func (p *Provisioner) Prepare(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{},
}, raws...)
if err != nil {
return err
}
var errs *packer.MultiError
if len(os.Getenv("AWS_ACCESS_KEY")) == 0 && len(p.config.S3AccessKey) == 0 {
errs = packer.MultiErrorAppend(errs, errors.New("AWS_ACCESS_KEY environment variable or inline s3_access_key is required"))
}
if len(os.Getenv("AWS_SECRET_KEY")) == 0 && len(p.config.S3SecretKey) == 0 {
errs = packer.MultiErrorAppend(errs, errors.New("AWS_SECRET_KEY environment variable required inline s3_secret_key is required"))
}
_, err = getURL(p.config.URL)
if err != nil {
errs = packer.MultiErrorAppend(errs, errors.New("Bad url parameter"))
}
if len(p.config.Destination) == 0 {
errs = packer.MultiErrorAppend(errs, errors.New("Destination for S3 download is required"))
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, generatedData map[string]interface{}) error {
if generatedData == nil {
generatedData = make(map[string]interface{})
}
p.config.ctx.Data = generatedData
s3 := new(getter.S3Getter)
u, _ := getURL(p.config.URL)
if len(p.config.S3AccessKey) > 0 {
u.Query().Add("access_key", p.config.S3AccessKey)
}
if len(p.config.S3SecretKey) > 0 {
u.Query().Add("secret_key", p.config.S3SecretKey)
}
local := getTempLocation(p.config.Destination)
// Download
err := s3.GetFile(local, u)
ui.Say(fmt.Sprintf("Downloading %s => %s", p.config.URL, p.config.Destination))
if err != nil {
return err
}
return p.ProvisionUpload(ui, comm)
}
func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) error {
local := getTempLocation(p.config.Destination)
src, err := interpolate.Render(local, &p.config.ctx)
if err != nil {
return fmt.Errorf("Error interpolating source: %s", err)
}
dst, err := interpolate.Render(p.config.Destination, &p.config.ctx)
if err != nil {
return fmt.Errorf("Error interpolating destination: %s", err)
}
ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))
info, err := os.Stat(src)
if err != nil {
return err
}
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
pf := ui.TrackProgress(filepath.Base(src), 0, info.Size(), f)
defer pf.Close()
// Upload the file
if err = comm.Upload(dst, pf, &fi); err != nil {
ui.Error(fmt.Sprintf("Upload failed: %s", err))
return err
}
// Remove local file
err = os.Remove(local)
if err != nil {
ui.Error(fmt.Sprintf("Clean up failed: %s", err))
return err
}
return nil
}
func getURL(s string) (*url.URL, error) {
u, err := url.Parse(s)
return u, err
}
func getTempLocation(s string) string {
split := strings.Split(s, "/")
last := split[len(split)-1]
return fmt.Sprintf("./%s", last)
}