-
Notifications
You must be signed in to change notification settings - Fork 227
/
buildpacks_builder.go
193 lines (152 loc) · 4.25 KB
/
buildpacks_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
package imgsrc
import (
"context"
"fmt"
"io"
"os"
"github.com/buildpacks/pack"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/internal/cmdfmt"
"github.com/superfly/flyctl/pkg/iostreams"
"github.com/superfly/flyctl/terminal"
)
type buildpacksBuilder struct{}
func (ds *buildpacksBuilder) Name() string {
return "Buildpacks"
}
func (s *buildpacksBuilder) Run(ctx context.Context, dockerFactory *dockerClientFactory, streams *iostreams.IOStreams, opts ImageOptions) (*DeploymentImage, error) {
if !dockerFactory.mode.IsAvailable() {
terminal.Debug("docker daemon not available, skipping")
return nil, nil
}
if !opts.AppConfig.HasBuilder() {
terminal.Debug("no buildpack builder configured, skipping")
return nil, nil
}
builder := opts.AppConfig.Build.Builder
buildpacks := opts.AppConfig.Build.Buildpacks
docker, err := dockerFactory.buildFn(ctx)
if err != nil {
return nil, err
}
defer clearDeploymentTags(ctx, docker, opts.Tag)
packClient, err := pack.NewClient(pack.WithDockerClient(docker), pack.WithLogger(newPackLogger(streams.Out)))
if err != nil {
return nil, err
}
cmdfmt.PrintBegin(streams.ErrOut, "Building image with Buildpacks")
err = packClient.Build(ctx, pack.BuildOptions{
AppPath: opts.WorkingDir,
Builder: builder,
Image: opts.Tag,
Buildpacks: buildpacks,
Env: normalizeBuildArgs(opts.AppConfig, opts.ExtraBuildArgs),
TrustBuilder: true,
})
if err != nil {
return nil, err
}
cmdfmt.PrintDone(streams.ErrOut, "Building image done")
if opts.Publish {
cmdfmt.PrintBegin(streams.ErrOut, "Pushing image to fly")
if err := pushToFly(ctx, docker, streams, opts.Tag); err != nil {
return nil, err
}
cmdfmt.PrintDone(streams.ErrOut, "Pushing image done")
}
img, err := findImageWithDocker(docker, ctx, opts.Tag)
if err != nil {
return nil, err
}
return &DeploymentImage{
ID: img.ID,
Tag: opts.Tag,
Size: img.Size,
}, nil
}
func normalizeBuildArgs(appConfig *flyctl.AppConfig, extra map[string]string) map[string]string {
var out = map[string]string{}
if appConfig.Build != nil {
for k, v := range appConfig.Build.Args {
out[k] = v
}
}
for k, v := range extra {
out[k] = v
}
return out
}
func newPackLogger(out io.Writer) *packLogger {
// pack blocks writes to the underlying writer for it's lifetime.
// we need to use it too, so instead of giving pack stdout/stderr
// give it a burner writer that we pipe to the target
packR, packW := io.Pipe()
go func() {
io.Copy(out, packR)
defer packR.Close()
}()
return &packLogger{
w: &fdWrapper{
Writer: packW,
src: out,
},
debug: os.Getenv("LOG_LEVEL") == "debug",
}
}
type packLogger struct {
w io.Writer
debug bool
}
func (l *packLogger) Debug(msg string) {
if !l.debug {
return
}
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(msg))
}
func (l *packLogger) Debugf(format string, v ...interface{}) {
if !l.debug {
return
}
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(fmt.Sprintf(format, v...)))
}
func (l *packLogger) Info(msg string) {
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(msg))
}
func (l *packLogger) Infof(format string, v ...interface{}) {
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(fmt.Sprintf(format, v...)))
}
func (l *packLogger) Warn(msg string) {
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(msg))
}
func (l *packLogger) Warnf(format string, v ...interface{}) {
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(fmt.Sprintf(format, v...)))
}
func (l *packLogger) Error(msg string) {
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(msg))
}
func (l *packLogger) Errorf(format string, v ...interface{}) {
fmt.Fprint(l.w, cmdfmt.AppendMissingLineFeed(fmt.Sprintf(format, v...)))
}
func (l *packLogger) Writer() io.Writer {
return l.w
}
func (l *packLogger) IsVerbose() bool {
return l.debug
}
// fdWrapper creates an io.Writer wrapper that writes to one Writer but reads Fd from another.
// this is used so we can pass the correct Fd through for terminal detection while
// still writing to our piped writer
type fdWrapper struct {
io.Writer
src io.Writer
}
type fdWriter interface {
Fd() uintptr
}
func (w *fdWrapper) Fd() uintptr {
fmt.Println("get fd", w.src, w.Writer)
if fd, ok := w.src.(fdWriter); ok {
return fd.Fd()
}
return ^(uintptr(0))
}