2018-04-13 21:03:55 +00:00
|
|
|
package ops
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-10 01:42:46 +00:00
|
|
|
"strings"
|
2018-04-13 21:03:55 +00:00
|
|
|
"sync"
|
|
|
|
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2018-04-13 21:03:55 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/source"
|
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sourceCacheType = "buildkit.source.v0"
|
|
|
|
|
|
|
|
type sourceOp struct {
|
2018-06-24 05:52:19 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
op *pb.Op_Source
|
|
|
|
platform *pb.Platform
|
|
|
|
sm *source.Manager
|
|
|
|
src source.SourceInstance
|
|
|
|
w worker.Worker
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
func NewSourceOp(_ solver.Vertex, op *pb.Op_Source, platform *pb.Platform, sm *source.Manager, w worker.Worker) (solver.Op, error) {
|
2018-04-13 21:03:55 +00:00
|
|
|
return &sourceOp{
|
2018-06-24 05:52:19 +00:00
|
|
|
op: op,
|
|
|
|
sm: sm,
|
|
|
|
w: w,
|
|
|
|
platform: platform,
|
2018-04-13 21:03:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceOp) instance(ctx context.Context) (source.SourceInstance, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.src != nil {
|
|
|
|
return s.src, nil
|
|
|
|
}
|
2018-06-24 05:52:19 +00:00
|
|
|
id, err := source.FromLLB(s.op, s.platform)
|
2018-04-13 21:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
src, err := s.sm.Resolve(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.src = src
|
|
|
|
return s.src, nil
|
|
|
|
}
|
|
|
|
|
2018-04-24 21:00:58 +00:00
|
|
|
func (s *sourceOp) CacheMap(ctx context.Context, index int) (*solver.CacheMap, bool, error) {
|
2018-04-13 21:03:55 +00:00
|
|
|
src, err := s.instance(ctx)
|
|
|
|
if err != nil {
|
2018-04-24 21:00:58 +00:00
|
|
|
return nil, false, err
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|
2018-04-24 21:00:58 +00:00
|
|
|
k, done, err := src.CacheKey(ctx, index)
|
2018-04-13 21:03:55 +00:00
|
|
|
if err != nil {
|
2018-04-24 21:00:58 +00:00
|
|
|
return nil, false, err
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 01:42:46 +00:00
|
|
|
dgst := digest.FromBytes([]byte(sourceCacheType + ":" + k))
|
|
|
|
|
|
|
|
if strings.HasPrefix(k, "session:") {
|
|
|
|
dgst = digest.Digest("random:" + strings.TrimPrefix(dgst.String(), dgst.Algorithm().String()+":"))
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:03:55 +00:00
|
|
|
return &solver.CacheMap{
|
|
|
|
// TODO: add os/arch
|
2019-01-10 01:42:46 +00:00
|
|
|
Digest: dgst,
|
2018-04-24 21:00:58 +00:00
|
|
|
}, done, nil
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sourceOp) Exec(ctx context.Context, _ []solver.Result) (outputs []solver.Result, err error) {
|
|
|
|
src, err := s.instance(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ref, err := src.Snapshot(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []solver.Result{worker.NewWorkerRefResult(ref, s.w)}, nil
|
|
|
|
}
|