2019-03-27 06:10:21 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2022-01-14 22:15:50 +00:00
|
|
|
"strconv"
|
2019-03-27 06:10:21 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-04-03 01:29:32 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2019-03-27 06:10:21 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/filesync"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/moby/buildkit/util/progress"
|
2022-01-14 22:15:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-03-27 06:10:21 +00:00
|
|
|
"github.com/tonistiigi/fsutil"
|
|
|
|
fstypes "github.com/tonistiigi/fsutil/types"
|
|
|
|
)
|
|
|
|
|
2022-01-14 22:15:50 +00:00
|
|
|
const (
|
2022-02-10 00:02:24 +00:00
|
|
|
// preferNondistLayersKey is an exporter option which can be used to mark a layer as non-distributable if the layer reference was
|
2022-01-14 22:15:50 +00:00
|
|
|
// already found to use a non-distributable media type.
|
|
|
|
// When this option is not set, the exporter will change the media type of the layer to a distributable one.
|
2022-02-10 00:02:24 +00:00
|
|
|
preferNondistLayersKey = "prefer-nondist-layers"
|
2022-01-14 22:15:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-27 06:10:21 +00:00
|
|
|
type Opt struct {
|
|
|
|
SessionManager *session.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
type localExporter struct {
|
|
|
|
opt Opt
|
|
|
|
// session manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(opt Opt) (exporter.Exporter, error) {
|
|
|
|
le := &localExporter{opt: opt}
|
|
|
|
return le, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *localExporter) Resolve(ctx context.Context, opt map[string]string) (exporter.ExporterInstance, error) {
|
2020-06-30 01:06:02 +00:00
|
|
|
li := &localExporterInstance{localExporter: e}
|
2022-01-14 22:15:50 +00:00
|
|
|
|
2022-02-10 00:02:24 +00:00
|
|
|
v, ok := opt[preferNondistLayersKey]
|
2022-01-14 22:15:50 +00:00
|
|
|
if ok {
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
2022-02-10 00:02:24 +00:00
|
|
|
return nil, errors.Wrapf(err, "non-bool value for %s: %s", preferNondistLayersKey, v)
|
2022-01-14 22:15:50 +00:00
|
|
|
}
|
2022-02-10 00:02:24 +00:00
|
|
|
li.preferNonDist = b
|
2022-01-14 22:15:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 06:10:21 +00:00
|
|
|
return li, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type localExporterInstance struct {
|
|
|
|
*localExporter
|
2022-02-10 00:02:24 +00:00
|
|
|
preferNonDist bool
|
2019-03-27 06:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *localExporterInstance) Name() string {
|
|
|
|
return "exporting to client"
|
|
|
|
}
|
|
|
|
|
2021-10-05 06:43:22 +00:00
|
|
|
func (e *localExporterInstance) Config() exporter.Config {
|
|
|
|
return exporter.Config{}
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (e *localExporterInstance) Export(ctx context.Context, inp exporter.Source, sessionID string) (map[string]string, error) {
|
2019-03-27 06:10:21 +00:00
|
|
|
var defers []func()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
for i := len(defers) - 1; i >= 0; i-- {
|
|
|
|
defers[i]()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
getDir := func(ctx context.Context, k string, ref cache.ImmutableRef) (*fsutil.Dir, error) {
|
|
|
|
var src string
|
|
|
|
var err error
|
2019-04-03 01:29:32 +00:00
|
|
|
var idmap *idtools.IdentityMapping
|
2019-03-27 06:10:21 +00:00
|
|
|
if ref == nil {
|
|
|
|
src, err = ioutil.TempDir("", "buildkit")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defers = append(defers, func() { os.RemoveAll(src) })
|
|
|
|
} else {
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err := ref.Mount(ctx, true, session.NewGroup(sessionID))
|
2019-03-27 06:10:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
|
|
|
|
src, err = lm.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-03 01:29:32 +00:00
|
|
|
|
|
|
|
idmap = mount.IdentityMapping()
|
|
|
|
|
2019-03-27 06:10:21 +00:00
|
|
|
defers = append(defers, func() { lm.Unmount() })
|
|
|
|
}
|
|
|
|
|
2019-04-03 01:29:32 +00:00
|
|
|
walkOpt := &fsutil.WalkOpt{}
|
|
|
|
|
|
|
|
if idmap != nil {
|
|
|
|
walkOpt.Map = func(p string, st *fstypes.Stat) bool {
|
|
|
|
uid, gid, err := idmap.ToContainer(idtools.Identity{
|
|
|
|
UID: int(st.Uid),
|
|
|
|
GID: int(st.Gid),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
st.Uid = uint32(uid)
|
|
|
|
st.Gid = uint32(gid)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 06:10:21 +00:00
|
|
|
return &fsutil.Dir{
|
2019-04-03 01:29:32 +00:00
|
|
|
FS: fsutil.NewFS(src, walkOpt),
|
2019-03-27 06:10:21 +00:00
|
|
|
Stat: fstypes.Stat{
|
|
|
|
Mode: uint32(os.ModeDir | 0755),
|
|
|
|
Path: strings.Replace(k, "/", "_", -1),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var fs fsutil.FS
|
|
|
|
|
|
|
|
if len(inp.Refs) > 0 {
|
|
|
|
dirs := make([]fsutil.Dir, 0, len(inp.Refs))
|
|
|
|
for k, ref := range inp.Refs {
|
|
|
|
d, err := getDir(ctx, k, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dirs = append(dirs, *d)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
fs, err = fsutil.SubDirFS(dirs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d, err := getDir(ctx, "", inp.Ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fs = d.FS
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-11 00:11:24 +00:00
|
|
|
caller, err := e.opt.SessionManager.Get(timeoutCtx, sessionID, false)
|
2020-06-30 01:06:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := filesync.CopyFileWriter(ctx, nil, caller)
|
2019-03-27 06:10:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
report := oneOffProgress(ctx, "sending tarball")
|
|
|
|
if err := fsutil.WriteTar(ctx, fs, w); err != nil {
|
|
|
|
w.Close()
|
|
|
|
return nil, report(err)
|
|
|
|
}
|
|
|
|
return nil, report(w.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func oneOffProgress(ctx context.Context, id string) func(err error) error {
|
2021-06-28 16:49:32 +00:00
|
|
|
pw, _, _ := progress.NewFromContext(ctx)
|
2019-03-27 06:10:21 +00:00
|
|
|
now := time.Now()
|
|
|
|
st := progress.Status{
|
|
|
|
Started: &now,
|
|
|
|
}
|
|
|
|
pw.Write(id, st)
|
|
|
|
return func(err error) error {
|
|
|
|
// TODO: set error on status
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
pw.Write(id, st)
|
|
|
|
pw.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|