2017-07-23 17:33:19 +00:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "crypto/sha256"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Meta struct {
|
|
|
|
Args []string
|
|
|
|
Env EnvList
|
|
|
|
Cwd string
|
|
|
|
}
|
|
|
|
|
2017-12-06 19:36:47 +00:00
|
|
|
func NewExecOp(root Output, meta Meta, readOnly bool, md OpMetadata) *ExecOp {
|
|
|
|
e := &ExecOp{meta: meta, cachedOpMetadata: md}
|
2017-07-23 17:33:19 +00:00
|
|
|
rootMount := &mount{
|
2017-08-30 00:05:48 +00:00
|
|
|
target: pb.RootMount,
|
|
|
|
source: root,
|
|
|
|
readonly: readOnly,
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
e.mounts = append(e.mounts, rootMount)
|
2017-08-30 00:05:48 +00:00
|
|
|
if readOnly {
|
|
|
|
e.root = root
|
|
|
|
} else {
|
|
|
|
e.root = &output{vertex: e, getIndex: e.getMountIndexFn(rootMount)}
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
rootMount.output = e.root
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
type mount struct {
|
|
|
|
target string
|
|
|
|
readonly bool
|
|
|
|
source Output
|
|
|
|
output Output
|
2017-08-08 01:45:31 +00:00
|
|
|
selector string
|
2017-07-23 17:33:19 +00:00
|
|
|
// hasOutput bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecOp struct {
|
2017-10-02 04:59:34 +00:00
|
|
|
root Output
|
|
|
|
mounts []*mount
|
|
|
|
meta Meta
|
|
|
|
cachedPB []byte
|
2017-12-06 19:36:47 +00:00
|
|
|
cachedOpMetadata OpMetadata
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) AddMount(target string, source Output, opt ...MountOption) Output {
|
|
|
|
m := &mount{
|
|
|
|
target: target,
|
|
|
|
source: source,
|
|
|
|
}
|
|
|
|
for _, o := range opt {
|
|
|
|
o(m)
|
|
|
|
}
|
|
|
|
e.mounts = append(e.mounts, m)
|
2017-08-30 00:05:48 +00:00
|
|
|
if m.readonly {
|
|
|
|
m.output = source
|
|
|
|
} else {
|
|
|
|
m.output = &output{vertex: e, getIndex: e.getMountIndexFn(m)}
|
|
|
|
}
|
2017-08-09 00:02:50 +00:00
|
|
|
e.cachedPB = nil
|
2017-07-23 17:33:19 +00:00
|
|
|
return m.output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) GetMount(target string) Output {
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
if m.target == target {
|
|
|
|
return m.output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) Validate() error {
|
|
|
|
if len(e.meta.Args) == 0 {
|
|
|
|
return errors.Errorf("arguments are required")
|
|
|
|
}
|
|
|
|
if e.meta.Cwd == "" {
|
|
|
|
return errors.Errorf("working directory is required")
|
|
|
|
}
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
if m.source != nil {
|
|
|
|
if err := m.source.Vertex().Validate(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 19:36:47 +00:00
|
|
|
func (e *ExecOp) Marshal() ([]byte, *OpMetadata, error) {
|
2017-08-09 00:02:50 +00:00
|
|
|
if e.cachedPB != nil {
|
2017-12-06 19:36:47 +00:00
|
|
|
return e.cachedPB, &e.cachedOpMetadata, nil
|
2017-08-09 00:02:50 +00:00
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
if err := e.Validate(); err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return nil, nil, err
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
// make sure mounts are sorted
|
|
|
|
sort.Slice(e.mounts, func(i, j int) bool {
|
|
|
|
return e.mounts[i].target < e.mounts[j].target
|
|
|
|
})
|
|
|
|
|
|
|
|
peo := &pb.ExecOp{
|
|
|
|
Meta: &pb.Meta{
|
|
|
|
Args: e.meta.Args,
|
|
|
|
Env: e.meta.Env.ToArray(),
|
|
|
|
Cwd: e.meta.Cwd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pop := &pb.Op{
|
|
|
|
Op: &pb.Op_Exec{
|
|
|
|
Exec: peo,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-30 00:05:48 +00:00
|
|
|
outIndex := 0
|
|
|
|
for _, m := range e.mounts {
|
2017-07-23 17:33:19 +00:00
|
|
|
inputIndex := pb.InputIndex(len(pop.Inputs))
|
|
|
|
if m.source != nil {
|
|
|
|
inp, err := m.source.ToInput()
|
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return nil, nil, err
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newInput := true
|
|
|
|
|
|
|
|
for i, inp2 := range pop.Inputs {
|
|
|
|
if *inp == *inp2 {
|
|
|
|
inputIndex = pb.InputIndex(i)
|
|
|
|
newInput = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if newInput {
|
|
|
|
pop.Inputs = append(pop.Inputs, inp)
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-24 19:33:14 +00:00
|
|
|
inputIndex = pb.Empty
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 00:05:48 +00:00
|
|
|
outputIndex := pb.OutputIndex(-1)
|
|
|
|
if !m.readonly {
|
|
|
|
outputIndex = pb.OutputIndex(outIndex)
|
|
|
|
outIndex++
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
pm := &pb.Mount{
|
|
|
|
Input: inputIndex,
|
|
|
|
Dest: m.target,
|
|
|
|
Readonly: m.readonly,
|
2017-08-30 00:05:48 +00:00
|
|
|
Output: outputIndex,
|
2017-08-08 01:45:31 +00:00
|
|
|
Selector: m.selector,
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
peo.Mounts = append(peo.Mounts, pm)
|
|
|
|
}
|
|
|
|
|
2017-08-09 00:02:50 +00:00
|
|
|
dt, err := pop.Marshal()
|
|
|
|
if err != nil {
|
2017-10-02 04:59:34 +00:00
|
|
|
return nil, nil, err
|
2017-08-09 00:02:50 +00:00
|
|
|
}
|
|
|
|
e.cachedPB = dt
|
2017-12-06 19:36:47 +00:00
|
|
|
return dt, &e.cachedOpMetadata, nil
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) Output() Output {
|
|
|
|
return e.root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) Inputs() (inputs []Output) {
|
|
|
|
mm := map[Output]struct{}{}
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
if m.source != nil {
|
|
|
|
mm[m.source] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for o := range mm {
|
|
|
|
inputs = append(inputs, o)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) getMountIndexFn(m *mount) func() (pb.OutputIndex, error) {
|
|
|
|
return func() (pb.OutputIndex, error) {
|
|
|
|
// make sure mounts are sorted
|
|
|
|
sort.Slice(e.mounts, func(i, j int) bool {
|
|
|
|
return e.mounts[i].target < e.mounts[j].target
|
|
|
|
})
|
|
|
|
|
2017-08-30 00:05:48 +00:00
|
|
|
i := 0
|
|
|
|
for _, m2 := range e.mounts {
|
|
|
|
if m2.readonly {
|
|
|
|
continue
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
if m == m2 {
|
|
|
|
return pb.OutputIndex(i), nil
|
|
|
|
}
|
2017-08-30 00:05:48 +00:00
|
|
|
i++
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
return pb.OutputIndex(0), errors.Errorf("invalid mount")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecState struct {
|
|
|
|
State
|
|
|
|
exec *ExecOp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ExecState) AddMount(target string, source State, opt ...MountOption) State {
|
2017-08-25 20:08:18 +00:00
|
|
|
return source.WithOutput(e.exec.AddMount(target, source.Output(), opt...))
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ExecState) GetMount(target string) State {
|
|
|
|
return NewState(e.exec.GetMount(target))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ExecState) Root() State {
|
|
|
|
return e.State
|
|
|
|
}
|
|
|
|
|
|
|
|
type MountOption func(*mount)
|
|
|
|
|
|
|
|
func Readonly(m *mount) {
|
|
|
|
m.readonly = true
|
|
|
|
}
|
|
|
|
|
2017-08-08 01:45:31 +00:00
|
|
|
func SourcePath(src string) MountOption {
|
|
|
|
return func(m *mount) {
|
|
|
|
m.selector = src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 19:36:47 +00:00
|
|
|
type RunOption interface {
|
|
|
|
SetRunOption(es *ExecInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type runOptionFunc func(*ExecInfo)
|
|
|
|
|
|
|
|
func (fn runOptionFunc) SetRunOption(ei *ExecInfo) {
|
|
|
|
fn(ei)
|
|
|
|
}
|
2017-07-23 17:33:19 +00:00
|
|
|
|
|
|
|
func Shlex(str string) RunOption {
|
|
|
|
return Shlexf(str)
|
|
|
|
}
|
|
|
|
func Shlexf(str string, v ...interface{}) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-07-23 17:33:19 +00:00
|
|
|
ei.State = shlexf(str, v...)(ei.State)
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 20:08:18 +00:00
|
|
|
func Args(a []string) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-08-25 20:08:18 +00:00
|
|
|
ei.State = args(a...)(ei.State)
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 19:33:14 +00:00
|
|
|
func AddEnv(key, value string) RunOption {
|
|
|
|
return AddEnvf(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddEnvf(key, value string, v ...interface{}) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-07-24 19:33:14 +00:00
|
|
|
ei.State = ei.State.AddEnvf(key, value, v...)
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-07-24 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Dir(str string) RunOption {
|
|
|
|
return Dirf(str)
|
|
|
|
}
|
|
|
|
func Dirf(str string, v ...interface{}) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-07-24 19:33:14 +00:00
|
|
|
ei.State = ei.State.Dirf(str, v...)
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-07-24 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Reset(s State) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-07-24 19:33:14 +00:00
|
|
|
ei.State = ei.State.Reset(s)
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-07-24 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func With(so ...StateOption) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-07-24 19:33:14 +00:00
|
|
|
ei.State = ei.State.With(so...)
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-07-24 19:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddMount(dest string, mountState State, opts ...MountOption) RunOption {
|
2017-12-06 19:36:47 +00:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2017-07-23 17:33:19 +00:00
|
|
|
ei.Mounts = append(ei.Mounts, MountInfo{dest, mountState.Output(), opts})
|
2017-12-06 19:36:47 +00:00
|
|
|
})
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 00:05:48 +00:00
|
|
|
func ReadonlyRootFS(ei ExecInfo) ExecInfo {
|
|
|
|
ei.ReadonlyRootFS = true
|
|
|
|
return ei
|
|
|
|
}
|
|
|
|
|
2017-07-23 17:33:19 +00:00
|
|
|
type ExecInfo struct {
|
2017-12-06 19:36:47 +00:00
|
|
|
opMetaWrapper
|
2017-08-30 00:05:48 +00:00
|
|
|
State State
|
|
|
|
Mounts []MountInfo
|
|
|
|
ReadonlyRootFS bool
|
2017-07-23 17:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MountInfo struct {
|
|
|
|
Target string
|
|
|
|
Source Output
|
|
|
|
Opts []MountOption
|
|
|
|
}
|