-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
340 lines (282 loc) · 7.52 KB
/
builder.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
package main
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime/debug"
"strings"
"github.com/fanatic/waypoint-plugin-heroku/heroku"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
herokuSDK "github.com/heroku/heroku-go/v5"
"github.com/paketo-buildpacks/procfile/procfile"
)
type BuildConfig struct {
From string `hcl:"from"`
Source string `hcl:"source,optional"`
Pipeline string `hcl:"pipeline,optional"`
App string `hcl:"app,optional"`
}
type Builder struct {
config BuildConfig
}
// Implement Configurable
func (b *Builder) Config() (interface{}, error) {
return &b.config, nil
}
// Implement Builder
func (b *Builder) BuildFunc() interface{} {
// return a function which will be called by Waypoint
return b.build
}
func (b *Builder) build(ctx context.Context, ui terminal.UI, job *component.JobInfo, src *component.Source, log hclog.Logger) (*Artifact, error) {
defer func() {
if r := recover(); r != nil {
log.Error("Panic: %s %s", r, string(debug.Stack()))
}
}()
h, err := heroku.New()
if err != nil {
return nil, err
}
log.Info(
"Start build",
"src", src,
"config", b.config,
)
if b.config.From == "source" {
sg := ui.StepGroup()
if b.config.Source == "" {
b.config.Source = src.Path
}
step := sg.Add("Archiving source...")
tf, err := b.createLocalArchive(log, b.config.Source)
if err != nil {
step.Abort()
return nil, err
}
defer os.Remove(tf.Name())
defer tf.Close()
step.Done()
step = sg.Add("Sending source to Heroku...")
sourceURL, err := b.createHerokuSource(ctx, h, log, tf)
if err != nil {
step.Abort()
return nil, err
}
step.Done()
step = sg.Add("Building image...")
slugID, err := b.createHerokuBuild(ctx, h, sourceURL, job.Id, step.TermOutput())
if err != nil {
step.Abort()
return nil, err
}
step.Done()
return &Artifact{
SlugID: slugID,
}, nil
} else if b.config.From == "archive" {
sg := ui.StepGroup()
if b.config.Source == "" {
b.config.Source = src.Path
}
step := sg.Add("Archiving slug...")
tf, err := b.createLocalArchive(log, b.config.Source)
if err != nil {
return nil, err
}
defer os.Remove(tf.Name())
defer tf.Close()
step.Done()
step = sg.Add("Sending slug to Heroku...")
slugID, err := b.createHerokuSlug(ctx, h, log, tf)
if err != nil {
return nil, err
}
step.Done()
return &Artifact{
SlugID: slugID,
}, nil
}
return nil, fmt.Errorf("Must supply valid 'from' parameter: source")
}
func (b *Builder) createLocalArchive(log hclog.Logger, source string) (*os.File, error) {
log.Info("Tar started", "source", source)
tf, err := ioutil.TempFile("", "source-tar.")
if err != nil {
return nil, err
}
if err := Tar(source, tf); err != nil {
return nil, err
}
log.Info("Tar finished", "source", source)
return tf, nil
}
func (b *Builder) createHerokuSource(ctx context.Context, h *herokuSDK.Service, log hclog.Logger, tf *os.File) (string, error) {
source, err := h.SourceCreate(ctx)
if err != nil {
return "", err
}
log.Info(
"Source created",
"source", source,
)
if _, err := tf.Seek(0, 0); err != nil {
return "", err
}
req, err := http.NewRequest("PUT", source.SourceBlob.PutURL, tf)
if err != nil {
return "", err
}
stat, err := tf.Stat()
if err != nil {
return "", err
}
req.ContentLength = stat.Size()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
log.Info("Source upload complete", "code", resp.StatusCode, "body", string(body))
return source.SourceBlob.GetURL, nil
}
func (b *Builder) createHerokuBuild(ctx context.Context, h *herokuSDK.Service, sourceURL, sourceVersion string, w io.Writer) (string, error) {
// Force build stack back to heroku-18 in case we set it to container with a previous push
_, err := h.AppUpdate(ctx, b.config.App, herokuSDK.AppUpdateOpts{BuildStack: String("heroku-18")})
if err != nil {
return "", err
}
buildOpts := herokuSDK.BuildCreateOpts{}
buildOpts.SourceBlob.URL = &sourceURL
buildOpts.SourceBlob.Version = &sourceVersion
build, err := h.BuildCreate(ctx, b.config.App, buildOpts)
if err != nil {
return "", err
}
resp, err := http.Get(build.OutputStreamURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if _, err := io.Copy(w, resp.Body); err != nil {
return "", err
}
build, err = h.BuildInfo(ctx, b.config.App, build.ID)
if err != nil {
return "", err
}
return build.Slug.ID, nil
}
func (b *Builder) createHerokuSlug(ctx context.Context, h *herokuSDK.Service, log hclog.Logger, tf *os.File) (string, error) {
processTypesRaw, err := procfile.NewProcfileFromPath(b.config.Source)
if err != nil {
return "", err
}
processTypes := map[string]string{}
for k, v := range processTypesRaw {
processTypes[k], _ = v.(string)
}
o := herokuSDK.SlugCreateOpts{
BuildpackProvidedDescription: String("waypoint-plugin-heroku"),
ProcessTypes: processTypes,
}
slug, err := h.SlugCreate(ctx, b.config.App, o)
if err != nil {
return "", err
}
log.Info(
"Slug created",
"source", slug,
)
if _, err := tf.Seek(0, 0); err != nil {
return "", err
}
req, err := http.NewRequest("PUT", slug.Blob.URL, tf)
if err != nil {
return "", err
}
stat, err := tf.Stat()
if err != nil {
return "", err
}
req.ContentLength = stat.Size()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
log.Info("Slug upload complete", "code", resp.StatusCode, "body", string(body))
return slug.ID, nil
}
// Tar takes a source and variable writers and walks 'source' writing each file
// found to the tar writer; the purpose for accepting multiple writers is to allow
// for multiple outputs (for example a file, or md5 hash)
func Tar(src string, writers ...io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(src); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}
mw := io.MultiWriter(writers...)
gzw := gzip.NewWriter(mw)
defer gzw.Close()
tw := tar.NewWriter(gzw)
defer tw.Close()
// walk path
return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
// return on any error
if err != nil {
return err
}
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
if !fi.Mode().IsRegular() {
return nil
}
// create a new dir/file header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}
// update the name to correctly reflect the desired destination when untaring
header.Name = strings.TrimPrefix(strings.Replace(file, src, "", -1), string(filepath.Separator))
// write the header
if err := tw.WriteHeader(header); err != nil {
return err
}
// open files for taring
f, err := os.Open(file)
if err != nil {
return err
}
// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
return nil
})
}
func String(s string) *string {
return &s
}
var (
_ component.Builder = (*Builder)(nil)
_ component.Configurable = (*Builder)(nil)
)