2017-08-03 22:24:02 +00:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-12-04 22:58:00 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-07-11 05:48:32 +00:00
|
|
|
"strings"
|
2017-08-03 22:24:02 +00:00
|
|
|
"time"
|
|
|
|
|
2019-03-20 21:54:20 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2018-07-11 05:48:32 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-08-03 22:24:02 +00:00
|
|
|
"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"
|
|
|
|
"github.com/pkg/errors"
|
2018-07-10 22:33:44 +00:00
|
|
|
"github.com/tonistiigi/fsutil"
|
2018-10-01 11:16:48 +00:00
|
|
|
fstypes "github.com/tonistiigi/fsutil/types"
|
2018-07-11 05:48:32 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-08-03 22:24:02 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
id := session.FromContext(ctx)
|
|
|
|
if id == "" {
|
|
|
|
return nil, errors.New("could not access local files without session")
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
caller, err := e.opt.SessionManager.Get(timeoutCtx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
li := &localExporterInstance{localExporter: e, caller: caller}
|
|
|
|
return li, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type localExporterInstance struct {
|
|
|
|
*localExporter
|
|
|
|
caller session.Caller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *localExporterInstance) Name() string {
|
|
|
|
return "exporting to client"
|
|
|
|
}
|
|
|
|
|
2018-07-10 18:05:22 +00:00
|
|
|
func (e *localExporterInstance) Export(ctx context.Context, inp exporter.Source) (map[string]string, error) {
|
2018-07-11 05:48:32 +00:00
|
|
|
isMap := len(inp.Refs) > 0
|
|
|
|
|
|
|
|
export := func(ctx context.Context, k string, ref cache.ImmutableRef) func() error {
|
|
|
|
return func() error {
|
|
|
|
var src string
|
|
|
|
var err error
|
2019-03-20 21:54:20 +00:00
|
|
|
var idmap *idtools.IdentityMapping
|
2018-07-11 05:48:32 +00:00
|
|
|
if ref == nil {
|
|
|
|
src, err = ioutil.TempDir("", "buildkit")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(src)
|
|
|
|
} else {
|
|
|
|
mount, err := ref.Mount(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
|
|
|
|
src, err = lm.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-20 21:54:20 +00:00
|
|
|
|
|
|
|
idmap = mount.IdentityMapping()
|
|
|
|
|
2018-07-11 05:48:32 +00:00
|
|
|
defer lm.Unmount()
|
|
|
|
}
|
|
|
|
|
2019-03-20 21:54:20 +00:00
|
|
|
walkOpt := &fsutil.WalkOpt{}
|
|
|
|
|
|
|
|
if idmap != nil {
|
|
|
|
walkOpt.Map = func(p string, st *fstypes.Stat) bool {
|
|
|
|
id, err := idmap.ToHost(idtools.Identity{
|
|
|
|
UID: int(st.Uid),
|
|
|
|
GID: int(st.Gid),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
st.Uid = uint32(id.UID)
|
|
|
|
st.Gid = uint32(id.GID)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := fsutil.NewFS(src, walkOpt)
|
2018-07-11 05:48:32 +00:00
|
|
|
lbl := "copying files"
|
|
|
|
if isMap {
|
|
|
|
lbl += " " + k
|
2019-03-27 06:10:21 +00:00
|
|
|
fs, err = fsutil.SubDirFS([]fsutil.Dir{{FS: fs, Stat: fstypes.Stat{
|
2018-07-11 05:48:32 +00:00
|
|
|
Mode: uint32(os.ModeDir | 0755),
|
|
|
|
Path: strings.Replace(k, "/", "_", -1),
|
2019-03-27 06:10:21 +00:00
|
|
|
}}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-11 05:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
progress := newProgressHandler(ctx, lbl)
|
|
|
|
if err := filesync.CopyToCaller(ctx, fs, e.caller, progress); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-04 22:58:00 +00:00
|
|
|
}
|
2018-07-11 05:48:32 +00:00
|
|
|
}
|
2017-08-03 22:24:02 +00:00
|
|
|
|
2018-07-11 05:48:32 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2017-08-03 22:24:02 +00:00
|
|
|
|
2018-07-11 05:48:32 +00:00
|
|
|
if isMap {
|
|
|
|
for k, ref := range inp.Refs {
|
|
|
|
eg.Go(export(ctx, k, ref))
|
2017-12-04 22:58:00 +00:00
|
|
|
}
|
2018-07-11 05:48:32 +00:00
|
|
|
} else {
|
|
|
|
eg.Go(export(ctx, "", inp.Ref))
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:48:32 +00:00
|
|
|
if err := eg.Wait(); err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newProgressHandler(ctx context.Context, id string) func(int, bool) {
|
|
|
|
limiter := rate.NewLimiter(rate.Every(100*time.Millisecond), 1)
|
|
|
|
pw, _, _ := progress.FromContext(ctx)
|
|
|
|
now := time.Now()
|
|
|
|
st := progress.Status{
|
|
|
|
Started: &now,
|
|
|
|
Action: "transferring",
|
|
|
|
}
|
|
|
|
pw.Write(id, st)
|
|
|
|
return func(s int, last bool) {
|
|
|
|
if last || limiter.Allow() {
|
|
|
|
st.Current = s
|
|
|
|
if last {
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
}
|
|
|
|
pw.Write(id, st)
|
|
|
|
if last {
|
|
|
|
pw.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|