-
Notifications
You must be signed in to change notification settings - Fork 296
/
main.go
157 lines (136 loc) · 3.33 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"io"
"log"
"net"
"os"
"path/filepath"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/filesync"
"github.com/pkg/errors"
"github.com/tonistiigi/fsutil"
fsutiltypes "github.com/tonistiigi/fsutil/types"
)
var useCache bool
var useLegacyAPI bool
var contextDir string
// A small utility for running Buildkit on the dockerfile
// in the current directory printing out all the buildkit api
// response protobufs.
func main() {
flag.BoolVar(&useCache, "cache", false, "Enable docker caching")
flag.BoolVar(&useLegacyAPI, "legacy", false, "Print legacy build events")
flag.StringVar(&contextDir, "context", "", "Context directory")
flag.Parse()
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
d, err := client.NewEnvClient()
if err != nil {
return err
}
d.NegotiateAPIVersion(ctx)
session, err := session.NewSession(ctx, "tilt", identity.NewID())
if err != nil {
return err
}
fileMap := func(path string, s *fsutiltypes.Stat) fsutil.MapResult {
s.Uid = 0
s.Gid = 0
return fsutil.MapResultKeep
}
dir, _ := os.Getwd()
if contextDir == "" {
contextDir = dir
}
if !filepath.IsAbs(contextDir) {
contextDir = filepath.Join(dir, contextDir)
}
session.Allow(filesync.NewFSSyncProvider([]filesync.SyncedDir{
{
Name: "context",
Dir: contextDir,
Map: fileMap,
},
{
Name: "dockerfile",
Dir: dir,
},
}))
go func() {
defer func() {
_ = session.Close()
}()
// Start the server
dialSession := func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
return d.DialHijack(ctx, "/session", proto, meta)
}
_ = session.Run(ctx, dialSession)
}()
opts := types.ImageBuildOptions{}
opts.Version = types.BuilderBuildKit
opts.Dockerfile = "Dockerfile"
opts.RemoteContext = "client-session"
opts.SessionID = session.ID()
if !useCache {
opts.NoCache = true
}
defer session.Close()
response, err := d.ImageBuild(ctx, nil, opts)
if err != nil {
return err
}
defer func() {
_ = response.Body.Close()
}()
return readDockerOutput(ctx, response.Body)
}
func readDockerOutput(ctx context.Context, reader io.Reader) error {
decoder := json.NewDecoder(reader)
for decoder.More() {
message := jsonmessage.JSONMessage{}
err := decoder.Decode(&message)
if err != nil {
return errors.Wrap(err, "decoding docker output")
}
isFromBuildkit := messageIsFromBuildkit(message)
if isFromBuildkit && !useLegacyAPI {
err := writeBuildkitStatus(message.Aux)
if err != nil {
return err
}
} else if !isFromBuildkit && useLegacyAPI {
err := json.NewEncoder(os.Stdout).Encode(message)
if err != nil {
return err
}
}
}
return nil
}
func writeBuildkitStatus(aux *json.RawMessage) error {
var resp controlapi.StatusResponse
var dt []byte
if err := json.Unmarshal(*aux, &dt); err != nil {
return err
}
if err := (&resp).Unmarshal(dt); err != nil {
return err
}
return json.NewEncoder(os.Stdout).Encode(resp)
}
func messageIsFromBuildkit(msg jsonmessage.JSONMessage) bool {
return msg.ID == "moby.buildkit.trace"
}