2017-07-23 17:33:19 +00:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
2017-08-21 22:41:22 +00:00
|
|
|
"context"
|
2017-07-23 17:33:19 +00:00
|
|
|
_ "crypto/sha256"
|
2017-08-25 20:08:18 +00:00
|
|
|
"encoding/json"
|
2017-08-21 22:41:22 +00:00
|
|
|
"strings"
|
2017-07-23 17:33:19 +00:00
|
|
|
|
2017-08-25 20:08:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2017-07-23 17:33:19 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2017-09-11 01:11:42 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-07-23 17:33:19 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SourceOp struct {
|
2017-10-02 04:59:34 +00:00
|
|
|
id string
|
|
|
|
attrs map[string]string
|
|
|
|
output Output
|
|
|
|
cachedPB []byte
|
|
|
|
cachedOpMetadata *pb.OpMetadata
|
|
|
|
err error
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSource(id string, attrs map[string]string) *SourceOp {
|
|
|
|
s := &SourceOp{
|
|
|
|
id: id,
|
|
|
|
attrs: attrs,
|
|
|
|
}
|
|
|
|
s.output = &output{vertex: s}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SourceOp) Validate() error {
|
2017-08-21 22:41:22 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return s.err
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
if s.id == "" {
|
|
|
|
return errors.Errorf("source identifier can't be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
func (s *SourceOp) Marshal() ([]byte, *pb.OpMetadata, error) {
|
2017-08-09 00:02:50 +00:00
|
|
|
if s.cachedPB != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return s.cachedPB, s.cachedOpMetadata, nil
|
2017-08-09 00:02:50 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
if err := s.Validate(); err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return nil, nil, err
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proto := &pb.Op{
|
|
|
|
Op: &pb.Op_Source{
|
|
|
|
Source: &pb.SourceOp{Identifier: s.id, Attrs: s.attrs},
|
|
|
|
},
|
|
|
|
}
|
2017-08-09 00:02:50 +00:00
|
|
|
dt, err := proto.Marshal()
|
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return nil, nil, err
|
2017-08-09 00:02:50 +00:00
|
|
|
}
|
|
|
|
s.cachedPB = dt
|
2017-10-02 04:59:34 +00:00
|
|
|
s.cachedOpMetadata = &pb.OpMetadata{}
|
|
|
|
return dt, s.cachedOpMetadata, nil
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SourceOp) Output() Output {
|
|
|
|
return s.output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SourceOp) Inputs() []Output {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Source(id string) State {
|
|
|
|
return NewState(NewSource(id, nil).Output())
|
|
|
|
}
|
|
|
|
|
2017-08-21 22:41:22 +00:00
|
|
|
func Image(ref string, opts ...ImageOption) State {
|
2017-08-25 20:08:18 +00:00
|
|
|
r, err := reference.ParseNormalizedNamed(ref)
|
|
|
|
if err == nil {
|
|
|
|
ref = reference.TagNameOnly(r).String()
|
|
|
|
}
|
2017-08-21 22:41:22 +00:00
|
|
|
src := NewSource("docker-image://"+ref, nil) // controversial
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
|
|
|
src.err = err
|
|
|
|
}
|
2017-08-21 22:41:22 +00:00
|
|
|
var info ImageInfo
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&info)
|
|
|
|
}
|
|
|
|
if info.metaResolver != nil {
|
2017-09-22 22:49:37 +00:00
|
|
|
_, dt, err := info.metaResolver.ResolveImageConfig(context.TODO(), ref)
|
2017-08-21 22:41:22 +00:00
|
|
|
if err != nil {
|
|
|
|
src.err = err
|
|
|
|
} else {
|
2017-09-11 01:11:42 +00:00
|
|
|
var img ocispec.Image
|
|
|
|
if err := json.Unmarshal(dt, &img); err != nil {
|
|
|
|
src.err = err
|
|
|
|
} else {
|
|
|
|
st := NewState(src.Output())
|
|
|
|
for _, env := range img.Config.Env {
|
|
|
|
parts := strings.SplitN(env, "=", 2)
|
|
|
|
if len(parts[0]) > 0 {
|
|
|
|
var v string
|
|
|
|
if len(parts) > 1 {
|
|
|
|
v = parts[1]
|
|
|
|
}
|
|
|
|
st = st.AddEnv(parts[0], v)
|
2017-08-21 22:41:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-11 01:11:42 +00:00
|
|
|
st = st.Dir(img.Config.WorkingDir)
|
|
|
|
return st
|
2017-08-21 22:41:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NewState(src.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageOption func(*ImageInfo)
|
|
|
|
|
|
|
|
type ImageInfo struct {
|
|
|
|
metaResolver ImageMetaResolver
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Git(remote, ref string, opts ...GitOption) State {
|
|
|
|
id := remote
|
|
|
|
if ref != "" {
|
|
|
|
id += "#" + ref
|
|
|
|
}
|
|
|
|
|
|
|
|
gi := &GitInfo{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(gi)
|
|
|
|
}
|
|
|
|
attrs := map[string]string{}
|
|
|
|
if gi.KeepGitDir {
|
|
|
|
attrs[pb.AttrKeepGitDir] = "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
source := NewSource("git://"+id, attrs)
|
|
|
|
return NewState(source.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitOption func(*GitInfo)
|
|
|
|
|
|
|
|
type GitInfo struct {
|
|
|
|
KeepGitDir bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func KeepGitDir() GitOption {
|
|
|
|
return func(gi *GitInfo) {
|
|
|
|
gi.KeepGitDir = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Scratch() State {
|
|
|
|
return NewState(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Local(name string, opts ...LocalOption) State {
|
|
|
|
gi := &LocalInfo{}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(gi)
|
|
|
|
}
|
|
|
|
attrs := map[string]string{}
|
|
|
|
if gi.SessionID != "" {
|
|
|
|
attrs[pb.AttrLocalSessionID] = gi.SessionID
|
|
|
|
}
|
2017-08-25 20:08:18 +00:00
|
|
|
if gi.IncludePatterns != "" {
|
|
|
|
attrs[pb.AttrIncludePatterns] = gi.IncludePatterns
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
|
|
|
|
source := NewSource("local://"+name, attrs)
|
|
|
|
return NewState(source.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type LocalOption func(*LocalInfo)
|
|
|
|
|
|
|
|
func SessionID(id string) LocalOption {
|
|
|
|
return func(li *LocalInfo) {
|
|
|
|
li.SessionID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 20:08:18 +00:00
|
|
|
func IncludePatterns(p []string) LocalOption {
|
|
|
|
return func(li *LocalInfo) {
|
|
|
|
dt, _ := json.Marshal(p) // empty on error
|
|
|
|
li.IncludePatterns = string(dt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type LocalInfo struct {
|
2017-08-25 20:08:18 +00:00
|
|
|
SessionID string
|
|
|
|
IncludePatterns string
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|