-
Notifications
You must be signed in to change notification settings - Fork 224
/
builtin_builder.go
115 lines (91 loc) · 2.95 KB
/
builtin_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
package imgsrc
import (
"fmt"
"github.com/pkg/errors"
"github.com/superfly/flyctl/internal/build/imgsrc/builtins"
"github.com/superfly/flyctl/internal/cmdfmt"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/terminal"
"golang.org/x/net/context"
)
type builtinBuilder struct{}
func (ds *builtinBuilder) Name() string {
return "Builtin"
}
func (ds *builtinBuilder) 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.BuiltIn == "" {
terminal.Debug("fly.toml does not include a builtin config")
return nil, nil
}
builtin, err := builtins.GetBuiltin(opts.BuiltIn)
if err != nil {
return nil, err
}
// Expand args
vdockerfile, err := builtin.GetVDockerfile(opts.BuiltInSettings)
if err != nil {
return nil, err
}
docker, err := dockerFactory.buildFn(ctx)
if err != nil {
return nil, errors.Wrap(err, "error connecting to docker")
}
defer clearDeploymentTags(ctx, docker, opts.Tag)
cmdfmt.PrintBegin(streams.ErrOut, "Creating build context")
archiveOpts := archiveOptions{
sourcePath: opts.WorkingDir,
compressed: dockerFactory.IsRemote(),
}
excludes, err := readDockerignore(opts.WorkingDir)
if err != nil {
return nil, errors.Wrap(err, "error reading .dockerignore")
}
archiveOpts.exclusions = excludes
// copy dockerfile into the archive if it's outside the context dir
archiveOpts.additions = map[string][]byte{
"Dockerfile": []byte(vdockerfile),
}
r, err := archiveDirectory(archiveOpts)
if err != nil {
return nil, errors.Wrap(err, "error archiving build context")
}
cmdfmt.PrintDone(streams.ErrOut, "Creating build context done")
var imageID string
serverInfo, err := docker.Info(ctx)
if err != nil {
terminal.Debug("error fetching docker server info:", err)
}
cmdfmt.PrintBegin(streams.ErrOut, "Building image with Docker")
msg := fmt.Sprintf("docker host: %s %s %s", serverInfo.ServerVersion, serverInfo.OSType, serverInfo.Architecture)
cmdfmt.PrintDone(streams.ErrOut, msg)
buildArgs, err := normalizeBuildArgsForDocker(ctx, opts.BuildArgs)
if err != nil {
return nil, fmt.Errorf("error parsing build args: %w", err)
}
imageID, err = runClassicBuild(ctx, streams, docker, r, opts, "", buildArgs)
if err != nil {
return nil, errors.Wrap(err, "error building")
}
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 := docker.ImageInspectWithRaw(ctx, imageID)
if err != nil {
return nil, errors.Wrap(err, "count not find built image")
}
fmt.Println(img)
return &DeploymentImage{
ID: img.ID,
Tag: opts.Tag,
Size: img.Size,
}, nil
}