2017-08-25 20:08:18 +00:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
2017-10-03 06:33:25 +00:00
|
|
|
"encoding/json"
|
2017-08-25 20:08:18 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2017-09-12 06:29:22 +00:00
|
|
|
"strings"
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
|
2017-10-01 00:58:07 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-08-25 20:08:18 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-22 17:30:30 +00:00
|
|
|
keyTarget = "target"
|
|
|
|
keyFilename = "filename"
|
|
|
|
exporterImageConfig = "containerimage.config"
|
|
|
|
defaultDockerfileName = "Dockerfile"
|
|
|
|
localNameDockerfile = "dockerfile"
|
|
|
|
buildArgPrefix = "build-arg:"
|
2017-08-25 20:08:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewDockerfileFrontend() frontend.Frontend {
|
|
|
|
return &dfFrontend{}
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
type dfFrontend struct{}
|
|
|
|
|
2017-10-03 06:33:25 +00:00
|
|
|
func (f *dfFrontend) Solve(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string) (retRef cache.ImmutableRef, exporterAttr map[string][]byte, retErr error) {
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
filename := opts[keyFilename]
|
|
|
|
if filename == "" {
|
2017-09-22 17:30:30 +00:00
|
|
|
filename = defaultDockerfileName
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
if path.Base(filename) != filename {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, errors.Errorf("invalid filename %s", filename)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 00:58:07 +00:00
|
|
|
sid := session.FromContext(ctx)
|
|
|
|
|
|
|
|
src := llb.Local(localNameDockerfile,
|
|
|
|
llb.IncludePatterns([]string{filename}),
|
|
|
|
llb.SessionID(sid),
|
|
|
|
)
|
2017-10-02 04:59:34 +00:00
|
|
|
def, err := src.Marshal()
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 05:31:18 +00:00
|
|
|
ref, _, err := llbBridge.Solve(ctx, frontend.SolveRequest{
|
|
|
|
Definition: def.ToPB(),
|
|
|
|
})
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if ref != nil {
|
|
|
|
ref.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
mount, err := ref.Mount(ctx, false)
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
|
|
|
|
root, err := lm.Mount()
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if lm != nil {
|
|
|
|
lm.Unmount()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
dtDockerfile, err := ioutil.ReadFile(filepath.Join(root, filename))
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := lm.Unmount(); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
lm = nil
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
if err := ref.Release(context.TODO()); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
ref = nil
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
st, img, err := dockerfile2llb.Dockerfile2LLB(ctx, dtDockerfile, dockerfile2llb.ConvertOpt{
|
2017-08-25 20:08:18 +00:00
|
|
|
Target: opts[keyTarget],
|
2017-09-14 05:47:37 +00:00
|
|
|
MetaResolver: llbBridge,
|
2017-09-12 06:29:22 +00:00
|
|
|
BuildArgs: filterBuildArgs(opts),
|
2017-10-01 00:58:07 +00:00
|
|
|
SessionID: sid,
|
2017-08-25 20:08:18 +00:00
|
|
|
})
|
2017-09-11 05:35:07 +00:00
|
|
|
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
def, err = st.Marshal()
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-10-05 05:31:18 +00:00
|
|
|
retRef, _, err = llbBridge.Solve(ctx, frontend.SolveRequest{
|
|
|
|
Definition: def.ToPB(),
|
|
|
|
})
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 06:33:25 +00:00
|
|
|
config, err := json.Marshal(img)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return retRef, map[string][]byte{
|
|
|
|
exporterImageConfig: config,
|
2017-09-11 05:35:07 +00:00
|
|
|
}, nil
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-12 06:29:22 +00:00
|
|
|
|
|
|
|
func filterBuildArgs(opt map[string]string) map[string]string {
|
|
|
|
m := map[string]string{}
|
|
|
|
for k, v := range opt {
|
2017-09-22 17:30:30 +00:00
|
|
|
if strings.HasPrefix(k, buildArgPrefix) {
|
|
|
|
m[strings.TrimPrefix(k, buildArgPrefix)] = v
|
2017-09-12 06:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|