2017-06-26 04:10:02 +00:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
2017-07-23 17:33:19 +00:00
|
|
|
"context"
|
2017-06-26 04:10:02 +00:00
|
|
|
|
2017-07-11 04:54:38 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2017-07-23 17:33:19 +00:00
|
|
|
"github.com/moby/buildkit/util/system"
|
2017-06-26 04:10:02 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
)
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type StateOption func(State) State
|
2017-06-26 04:10:02 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type Output interface {
|
|
|
|
ToInput() (*pb.Input, error)
|
|
|
|
Vertex() Vertex
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type Vertex interface {
|
|
|
|
Validate() error
|
2017-10-02 04:59:34 +00:00
|
|
|
Marshal() ([]byte, *pb.OpMetadata, error)
|
2017-07-23 17:33:19 +00:00
|
|
|
Output() Output
|
|
|
|
Inputs() []Output
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func NewState(o Output) State {
|
|
|
|
s := State{
|
|
|
|
out: o,
|
|
|
|
ctx: context.Background(),
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
s = dir("/")(s)
|
|
|
|
s = addEnv("PATH", system.DefaultPathEnv)(s)
|
2017-06-26 04:10:02 +00:00
|
|
|
return s
|
|
|
|
}
|
2017-06-26 18:51:27 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type State struct {
|
|
|
|
out Output
|
|
|
|
ctx context.Context
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) WithValue(k, v interface{}) State {
|
|
|
|
return State{
|
|
|
|
out: s.out,
|
|
|
|
ctx: context.WithValue(s.ctx, k, v),
|
|
|
|
}
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) Value(k interface{}) interface{} {
|
|
|
|
return s.ctx.Value(k)
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
func (s State) Marshal() (*Definition, error) {
|
|
|
|
def := &Definition{
|
|
|
|
Metadata: make(map[digest.Digest]OpMetadata, 0),
|
|
|
|
}
|
|
|
|
def, err := marshal(s.Output().Vertex(), def, map[digest.Digest]struct{}{}, map[Vertex]struct{}{})
|
2017-07-23 17:33:19 +00:00
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, err
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
inp, err := s.Output().ToInput()
|
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, err
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
proto := &pb.Op{Inputs: []*pb.Input{inp}}
|
|
|
|
dt, err := proto.Marshal()
|
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, err
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-10-02 04:59:34 +00:00
|
|
|
def.Def = append(def.Def, dt)
|
|
|
|
return def, nil
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
func marshal(v Vertex, def *Definition, cache map[digest.Digest]struct{}, vertexCache map[Vertex]struct{}) (*Definition, error) {
|
2017-07-23 17:33:19 +00:00
|
|
|
for _, inp := range v.Inputs() {
|
|
|
|
var err error
|
2017-10-02 04:59:34 +00:00
|
|
|
def, err = marshal(inp.Vertex(), def, cache, vertexCache)
|
2017-07-23 17:33:19 +00:00
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, err
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-08-09 00:02:50 +00:00
|
|
|
if _, ok := vertexCache[v]; ok {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, nil
|
2017-08-09 00:02:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
dt, opMeta, err := v.Marshal()
|
2017-07-23 17:33:19 +00:00
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, err
|
2017-07-18 06:08:22 +00:00
|
|
|
}
|
2017-08-09 00:02:50 +00:00
|
|
|
vertexCache[v] = struct{}{}
|
2017-07-23 17:33:19 +00:00
|
|
|
dgst := digest.FromBytes(dt)
|
|
|
|
if _, ok := cache[dgst]; ok {
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
def.Def = append(def.Def, dt)
|
|
|
|
if opMeta != nil {
|
|
|
|
def.Metadata[dgst] = OpMetadata{*opMeta}
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
cache[dgst] = struct{}{}
|
2017-10-02 04:59:34 +00:00
|
|
|
return def, nil
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) Validate() error {
|
|
|
|
return s.Output().Vertex().Validate()
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) Output() Output {
|
|
|
|
return s.out
|
2017-06-26 04:10:02 +00:00
|
|
|
}
|
2017-07-09 23:58:29 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) WithOutput(o Output) State {
|
|
|
|
return State{
|
|
|
|
out: o,
|
|
|
|
ctx: s.ctx,
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) Run(ro ...RunOption) ExecState {
|
|
|
|
ei := ExecInfo{State: s}
|
|
|
|
for _, o := range ro {
|
|
|
|
ei = o(ei)
|
|
|
|
}
|
|
|
|
meta := Meta{
|
|
|
|
Args: getArgs(ei.State),
|
|
|
|
Cwd: getDir(ei.State),
|
|
|
|
Env: getEnv(ei.State),
|
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
|
2017-08-30 00:05:48 +00:00
|
|
|
exec := NewExecOp(s.Output(), meta, ei.ReadonlyRootFS)
|
2017-07-23 17:33:19 +00:00
|
|
|
for _, m := range ei.Mounts {
|
|
|
|
exec.AddMount(m.Target, m.Source, m.Opts...)
|
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
return ExecState{
|
|
|
|
State: s.WithOutput(exec.Output()),
|
|
|
|
exec: exec,
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) AddEnv(key, value string) State {
|
|
|
|
return s.AddEnvf(key, value)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
|
|
|
|
func (s State) AddEnvf(key, value string, v ...interface{}) State {
|
|
|
|
return addEnvf(key, value, v...)(s)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) Dir(str string) State {
|
|
|
|
return s.Dirf(str)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) Dirf(str string, v ...interface{}) State {
|
|
|
|
return dirf(str, v...)(s)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) GetEnv(key string) (string, bool) {
|
|
|
|
return getEnv(s).Get(key)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
|
|
|
|
func (s State) GetDir() string {
|
|
|
|
return getDir(s)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) GetArgs() []string {
|
|
|
|
return getArgs(s)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
|
|
|
|
func (s State) Reset(s2 State) State {
|
|
|
|
return reset(s2)(s)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (s State) With(so ...StateOption) State {
|
|
|
|
for _, o := range so {
|
|
|
|
s = o(s)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type output struct {
|
|
|
|
vertex Vertex
|
|
|
|
getIndex func() (pb.OutputIndex, error)
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
func (o *output) ToInput() (*pb.Input, error) {
|
|
|
|
var index pb.OutputIndex
|
|
|
|
if o.getIndex != nil {
|
|
|
|
var err error
|
|
|
|
index, err = o.getIndex()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|
2017-10-02 04:59:34 +00:00
|
|
|
dt, opMeta, err := o.vertex.Marshal()
|
|
|
|
_ = opMeta
|
2017-07-23 17:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dgst := digest.FromBytes(dt)
|
|
|
|
return &pb.Input{Digest: dgst, Index: index}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *output) Vertex() Vertex {
|
|
|
|
return o.vertex
|
2017-07-09 23:58:29 +00:00
|
|
|
}
|