2018-04-19 13:07:27 -04:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
2020-04-16 11:06:33 -04:00
|
|
|
"context"
|
2020-11-17 08:19:27 -05:00
|
|
|
_ "crypto/sha256" // for opencontainers/go-digest
|
2018-09-26 08:26:12 -04:00
|
|
|
"fmt"
|
2018-08-11 15:04:13 -04:00
|
|
|
"net"
|
2018-04-19 13:07:27 -04:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2018-10-05 05:05:42 -04:00
|
|
|
"github.com/moby/buildkit/util/system"
|
2018-04-19 13:07:27 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func NewExecOp(base State, proxyEnv *ProxyEnv, readOnly bool, c Constraints) *ExecOp {
|
|
|
|
e := &ExecOp{base: base, constraints: c, proxyEnv: proxyEnv}
|
|
|
|
root := base.Output()
|
2018-04-19 13:07:27 -04:00
|
|
|
rootMount := &mount{
|
|
|
|
target: pb.RootMount,
|
|
|
|
source: root,
|
|
|
|
readonly: readOnly,
|
|
|
|
}
|
|
|
|
e.mounts = append(e.mounts, rootMount)
|
|
|
|
if readOnly {
|
|
|
|
e.root = root
|
|
|
|
} else {
|
2018-06-29 11:17:00 -04:00
|
|
|
o := &output{vertex: e, getIndex: e.getMountIndexFn(rootMount)}
|
|
|
|
if p := c.Platform; p != nil {
|
|
|
|
o.platform = p
|
|
|
|
}
|
|
|
|
e.root = o
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
rootMount.output = e.root
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
type mount struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
target string
|
|
|
|
readonly bool
|
|
|
|
source Output
|
|
|
|
output Output
|
|
|
|
selector string
|
|
|
|
cacheID string
|
|
|
|
tmpfs bool
|
|
|
|
cacheSharing CacheMountSharingMode
|
2019-04-03 02:23:23 -04:00
|
|
|
noOutput bool
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecOp struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
MarshalCache
|
2020-04-16 11:06:33 -04:00
|
|
|
proxyEnv *ProxyEnv
|
2018-06-29 11:17:00 -04:00
|
|
|
root Output
|
|
|
|
mounts []*mount
|
2020-04-16 11:06:33 -04:00
|
|
|
base State
|
2018-06-29 11:17:00 -04:00
|
|
|
constraints Constraints
|
|
|
|
isValidated bool
|
2018-08-11 15:04:13 -04:00
|
|
|
secrets []SecretInfo
|
2018-09-26 08:26:12 -04:00
|
|
|
ssh []SSHInfo
|
2018-04-19 13:07:27 -04: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)
|
|
|
|
if m.readonly {
|
|
|
|
m.output = source
|
2018-06-10 13:11:22 -04:00
|
|
|
} else if m.tmpfs {
|
|
|
|
m.output = &output{vertex: e, err: errors.Errorf("tmpfs mount for %s can't be used as a parent", target)}
|
2019-04-03 02:23:23 -04:00
|
|
|
} else if m.noOutput {
|
|
|
|
m.output = &output{vertex: e, err: errors.Errorf("mount marked no-output and %s can't be used as a parent", target)}
|
2018-04-19 13:07:27 -04:00
|
|
|
} else {
|
2018-06-29 11:17:00 -04:00
|
|
|
o := &output{vertex: e, getIndex: e.getMountIndexFn(m)}
|
|
|
|
if p := e.constraints.Platform; p != nil {
|
|
|
|
o.platform = p
|
|
|
|
}
|
|
|
|
m.output = o
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-07-20 09:49:37 -04:00
|
|
|
e.Store(nil, nil, nil, nil)
|
2018-04-19 13:07:27 -04:00
|
|
|
e.isValidated = false
|
|
|
|
return m.output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ExecOp) GetMount(target string) Output {
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
if m.target == target {
|
|
|
|
return m.output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (e *ExecOp) Validate(ctx context.Context) error {
|
2018-04-19 13:07:27 -04:00
|
|
|
if e.isValidated {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
args, err := getArgs(e.base)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(args) == 0 {
|
2018-04-19 13:07:27 -04:00
|
|
|
return errors.Errorf("arguments are required")
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
cwd, err := getDir(e.base)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cwd == "" {
|
2018-04-19 13:07:27 -04:00
|
|
|
return errors.Errorf("working directory is required")
|
|
|
|
}
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
if m.source != nil {
|
2020-04-16 11:06:33 -04:00
|
|
|
if err := m.source.Vertex(ctx).Validate(ctx); err != nil {
|
2018-04-19 13:07:27 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.isValidated = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:49:37 -04:00
|
|
|
func (e *ExecOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
|
2018-06-29 11:17:00 -04:00
|
|
|
if e.Cached(c) {
|
|
|
|
return e.Load()
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
if err := e.Validate(ctx); err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
// make sure mounts are sorted
|
|
|
|
sort.Slice(e.mounts, func(i, j int) bool {
|
|
|
|
return e.mounts[i].target < e.mounts[j].target
|
|
|
|
})
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
env, err := getEnv(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:12 -04:00
|
|
|
if len(e.ssh) > 0 {
|
|
|
|
for i, s := range e.ssh {
|
|
|
|
if s.Target == "" {
|
|
|
|
e.ssh[i].Target = fmt.Sprintf("/run/buildkit/ssh_agent.%d", i)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
if _, ok := env.Get("SSH_AUTH_SOCK"); !ok {
|
|
|
|
env = env.AddOrReplace("SSH_AUTH_SOCK", e.ssh[0].Target)
|
2018-09-26 08:26:12 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-05 05:05:42 -04:00
|
|
|
if c.Caps != nil {
|
|
|
|
if err := c.Caps.Supports(pb.CapExecMetaSetsDefaultPath); err != nil {
|
2020-11-17 08:19:27 -05:00
|
|
|
os := "linux"
|
|
|
|
if c.Platform != nil {
|
|
|
|
os = c.Platform.OS
|
|
|
|
} else if e.constraints.Platform != nil {
|
|
|
|
os = e.constraints.Platform.OS
|
|
|
|
}
|
|
|
|
env = env.SetDefault("PATH", system.DefaultPathEnv(os))
|
2018-10-05 05:05:42 -04:00
|
|
|
} else {
|
|
|
|
addCap(&e.constraints, pb.CapExecMetaSetsDefaultPath)
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 08:26:12 -04:00
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
args, err := getArgs(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cwd, err := getDir(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := getUser(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-17 08:19:27 -05:00
|
|
|
hostname, err := getHostname(e.base)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
meta := &pb.Meta{
|
2020-11-17 08:19:27 -05:00
|
|
|
Args: args,
|
|
|
|
Env: env.ToArray(),
|
|
|
|
Cwd: cwd,
|
|
|
|
User: user,
|
|
|
|
Hostname: hostname,
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
extraHosts, err := getExtraHosts(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
if len(extraHosts) > 0 {
|
|
|
|
hosts := make([]*pb.HostIP, len(extraHosts))
|
|
|
|
for i, h := range extraHosts {
|
2018-08-11 15:04:13 -04:00
|
|
|
hosts[i] = &pb.HostIP{Host: h.Host, IP: h.IP.String()}
|
|
|
|
}
|
|
|
|
meta.ExtraHosts = hosts
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
network, err := getNetwork(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
security, err := getSecurity(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
peo := &pb.ExecOp{
|
2019-04-03 02:23:23 -04:00
|
|
|
Meta: meta,
|
2020-04-16 11:06:33 -04:00
|
|
|
Network: network,
|
|
|
|
Security: security,
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
if network != NetModeSandbox {
|
2018-08-20 13:59:40 -04:00
|
|
|
addCap(&e.constraints, pb.CapExecMetaNetwork)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
if security != SecurityModeSandbox {
|
2019-04-03 02:23:23 -04:00
|
|
|
addCap(&e.constraints, pb.CapExecMetaSecurity)
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
if p := e.proxyEnv; p != nil {
|
2018-04-19 13:07:27 -04:00
|
|
|
peo.Meta.ProxyEnv = &pb.ProxyEnv{
|
2020-11-17 08:19:27 -05:00
|
|
|
HttpProxy: p.HTTPProxy,
|
|
|
|
HttpsProxy: p.HTTPSProxy,
|
|
|
|
FtpProxy: p.FTPProxy,
|
2018-04-19 13:07:27 -04:00
|
|
|
NoProxy: p.NoProxy,
|
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&e.constraints, pb.CapExecMetaProxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
addCap(&e.constraints, pb.CapExecMetaBase)
|
|
|
|
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
if m.selector != "" {
|
|
|
|
addCap(&e.constraints, pb.CapExecMountSelector)
|
|
|
|
}
|
|
|
|
if m.cacheID != "" {
|
|
|
|
addCap(&e.constraints, pb.CapExecMountCache)
|
|
|
|
addCap(&e.constraints, pb.CapExecMountCacheSharing)
|
|
|
|
} else if m.tmpfs {
|
|
|
|
addCap(&e.constraints, pb.CapExecMountTmpfs)
|
|
|
|
} else if m.source != nil {
|
|
|
|
addCap(&e.constraints, pb.CapExecMountBind)
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
if len(e.secrets) > 0 {
|
|
|
|
addCap(&e.constraints, pb.CapExecMountSecret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(e.ssh) > 0 {
|
|
|
|
addCap(&e.constraints, pb.CapExecMountSSH)
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
if e.constraints.Platform == nil {
|
|
|
|
p, err := getPlatform(e.base)(ctx)
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2020-04-16 11:06:33 -04:00
|
|
|
}
|
|
|
|
e.constraints.Platform = p
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
pop, md := MarshalConstraints(c, &e.constraints)
|
|
|
|
pop.Op = &pb.Op_Exec{
|
|
|
|
Exec: peo,
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
outIndex := 0
|
|
|
|
for _, m := range e.mounts {
|
|
|
|
inputIndex := pb.InputIndex(len(pop.Inputs))
|
|
|
|
if m.source != nil {
|
2018-06-10 13:11:22 -04:00
|
|
|
if m.tmpfs {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, errors.Errorf("tmpfs mounts must use scratch")
|
2018-06-10 13:11:22 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
inp, err := m.source.ToInput(ctx, c)
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2018-04-19 13:07:27 -04: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 {
|
|
|
|
inputIndex = pb.Empty
|
|
|
|
}
|
|
|
|
|
|
|
|
outputIndex := pb.OutputIndex(-1)
|
2019-04-03 02:23:23 -04:00
|
|
|
if !m.noOutput && !m.readonly && m.cacheID == "" && !m.tmpfs {
|
2018-04-19 13:07:27 -04:00
|
|
|
outputIndex = pb.OutputIndex(outIndex)
|
|
|
|
outIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
pm := &pb.Mount{
|
|
|
|
Input: inputIndex,
|
|
|
|
Dest: m.target,
|
|
|
|
Readonly: m.readonly,
|
|
|
|
Output: outputIndex,
|
|
|
|
Selector: m.selector,
|
|
|
|
}
|
|
|
|
if m.cacheID != "" {
|
|
|
|
pm.MountType = pb.MountType_CACHE
|
|
|
|
pm.CacheOpt = &pb.CacheOpt{
|
|
|
|
ID: m.cacheID,
|
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
switch m.cacheSharing {
|
|
|
|
case CacheMountShared:
|
|
|
|
pm.CacheOpt.Sharing = pb.CacheSharingOpt_SHARED
|
|
|
|
case CacheMountPrivate:
|
|
|
|
pm.CacheOpt.Sharing = pb.CacheSharingOpt_PRIVATE
|
|
|
|
case CacheMountLocked:
|
|
|
|
pm.CacheOpt.Sharing = pb.CacheSharingOpt_LOCKED
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-06-10 13:11:22 -04:00
|
|
|
if m.tmpfs {
|
|
|
|
pm.MountType = pb.MountType_TMPFS
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
peo.Mounts = append(peo.Mounts, pm)
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
for _, s := range e.secrets {
|
|
|
|
pm := &pb.Mount{
|
|
|
|
Dest: s.Target,
|
|
|
|
MountType: pb.MountType_SECRET,
|
|
|
|
SecretOpt: &pb.SecretOpt{
|
|
|
|
ID: s.ID,
|
|
|
|
Uid: uint32(s.UID),
|
|
|
|
Gid: uint32(s.GID),
|
|
|
|
Optional: s.Optional,
|
|
|
|
Mode: uint32(s.Mode),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
peo.Mounts = append(peo.Mounts, pm)
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:12 -04:00
|
|
|
for _, s := range e.ssh {
|
|
|
|
pm := &pb.Mount{
|
|
|
|
Dest: s.Target,
|
|
|
|
MountType: pb.MountType_SSH,
|
|
|
|
SSHOpt: &pb.SSHOpt{
|
|
|
|
ID: s.ID,
|
|
|
|
Uid: uint32(s.UID),
|
|
|
|
Gid: uint32(s.GID),
|
|
|
|
Mode: uint32(s.Mode),
|
|
|
|
Optional: s.Optional,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
peo.Mounts = append(peo.Mounts, pm)
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
dt, err := pop.Marshal()
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-07-20 09:49:37 -04:00
|
|
|
e.Store(dt, md, e.constraints.SourceLocations, c)
|
2018-06-29 11:17:00 -04:00
|
|
|
return e.Load()
|
2018-04-19 13:07:27 -04: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
|
|
|
|
})
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, m2 := range e.mounts {
|
2020-07-20 09:49:37 -04:00
|
|
|
if m2.noOutput || m2.readonly || m2.tmpfs || m2.cacheID != "" {
|
2018-04-19 13:07:27 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if m == m2 {
|
|
|
|
return pb.OutputIndex(i), nil
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return pb.OutputIndex(0), errors.Errorf("invalid mount: %s", m.target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecState struct {
|
|
|
|
State
|
|
|
|
exec *ExecOp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ExecState) AddMount(target string, source State, opt ...MountOption) State {
|
|
|
|
return source.WithOutput(e.exec.AddMount(target, source.Output(), opt...))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func SourcePath(src string) MountOption {
|
|
|
|
return func(m *mount) {
|
|
|
|
m.selector = src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
func ForceNoOutput(m *mount) {
|
|
|
|
m.noOutput = true
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func AsPersistentCacheDir(id string, sharing CacheMountSharingMode) MountOption {
|
2018-04-19 13:07:27 -04:00
|
|
|
return func(m *mount) {
|
|
|
|
m.cacheID = id
|
2018-06-29 11:17:00 -04:00
|
|
|
m.cacheSharing = sharing
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 13:11:22 -04:00
|
|
|
func Tmpfs() MountOption {
|
|
|
|
return func(m *mount) {
|
|
|
|
m.tmpfs = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
type RunOption interface {
|
|
|
|
SetRunOption(es *ExecInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type runOptionFunc func(*ExecInfo)
|
|
|
|
|
|
|
|
func (fn runOptionFunc) SetRunOption(ei *ExecInfo) {
|
|
|
|
fn(ei)
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (fn StateOption) SetRunOption(ei *ExecInfo) {
|
|
|
|
ei.State = ei.State.With(fn)
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
var _ RunOption = StateOption(func(_ State) State { return State{} })
|
2019-04-03 02:23:23 -04:00
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func Shlex(str string) RunOption {
|
2019-09-20 20:42:32 -04:00
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.State = shlexf(str, false)(ei.State)
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
func Shlexf(str string, v ...interface{}) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
2019-09-20 20:42:32 -04:00
|
|
|
ei.State = shlexf(str, true, v...)(ei.State)
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Args(a []string) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.State = args(a...)(ei.State)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
func AddExtraHost(host string, ip net.IP) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.State = ei.State.AddExtraHost(host, ip)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func With(so ...StateOption) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.State = ei.State.With(so...)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddMount(dest string, mountState State, opts ...MountOption) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.Mounts = append(ei.Mounts, MountInfo{dest, mountState.Output(), opts})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:12 -04:00
|
|
|
func AddSSHSocket(opts ...SSHOption) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
s := &SSHInfo{
|
|
|
|
Mode: 0600,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.SetSSHOption(s)
|
|
|
|
}
|
|
|
|
ei.SSH = append(ei.SSH, *s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type SSHOption interface {
|
|
|
|
SetSSHOption(*SSHInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sshOptionFunc func(*SSHInfo)
|
|
|
|
|
|
|
|
func (fn sshOptionFunc) SetSSHOption(si *SSHInfo) {
|
|
|
|
fn(si)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SSHID(id string) SSHOption {
|
|
|
|
return sshOptionFunc(func(si *SSHInfo) {
|
|
|
|
si.ID = id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
func SSHSocketTarget(target string) SSHOption {
|
|
|
|
return sshOptionFunc(func(si *SSHInfo) {
|
|
|
|
si.Target = target
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:12 -04:00
|
|
|
func SSHSocketOpt(target string, uid, gid, mode int) SSHOption {
|
|
|
|
return sshOptionFunc(func(si *SSHInfo) {
|
|
|
|
si.Target = target
|
|
|
|
si.UID = uid
|
|
|
|
si.GID = gid
|
|
|
|
si.Mode = mode
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var SSHOptional = sshOptionFunc(func(si *SSHInfo) {
|
|
|
|
si.Optional = true
|
|
|
|
})
|
|
|
|
|
|
|
|
type SSHInfo struct {
|
|
|
|
ID string
|
|
|
|
Target string
|
|
|
|
Mode int
|
|
|
|
UID int
|
|
|
|
GID int
|
|
|
|
Optional bool
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
func AddSecret(dest string, opts ...SecretOption) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
s := &SecretInfo{ID: dest, Target: dest, Mode: 0400}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.SetSecretOption(s)
|
|
|
|
}
|
|
|
|
ei.Secrets = append(ei.Secrets, *s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecretOption interface {
|
|
|
|
SetSecretOption(*SecretInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type secretOptionFunc func(*SecretInfo)
|
|
|
|
|
|
|
|
func (fn secretOptionFunc) SetSecretOption(si *SecretInfo) {
|
|
|
|
fn(si)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SecretInfo struct {
|
|
|
|
ID string
|
|
|
|
Target string
|
|
|
|
Mode int
|
|
|
|
UID int
|
|
|
|
GID int
|
|
|
|
Optional bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var SecretOptional = secretOptionFunc(func(si *SecretInfo) {
|
|
|
|
si.Optional = true
|
|
|
|
})
|
|
|
|
|
|
|
|
func SecretID(id string) SecretOption {
|
|
|
|
return secretOptionFunc(func(si *SecretInfo) {
|
|
|
|
si.ID = id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func SecretFileOpt(uid, gid, mode int) SecretOption {
|
|
|
|
return secretOptionFunc(func(si *SecretInfo) {
|
|
|
|
si.UID = uid
|
|
|
|
si.GID = gid
|
|
|
|
si.Mode = mode
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func ReadonlyRootFS() RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.ReadonlyRootFS = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithProxy(ps ProxyEnv) RunOption {
|
|
|
|
return runOptionFunc(func(ei *ExecInfo) {
|
|
|
|
ei.ProxyEnv = &ps
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecInfo struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
constraintsWrapper
|
2018-04-19 13:07:27 -04:00
|
|
|
State State
|
|
|
|
Mounts []MountInfo
|
|
|
|
ReadonlyRootFS bool
|
|
|
|
ProxyEnv *ProxyEnv
|
2018-08-11 15:04:13 -04:00
|
|
|
Secrets []SecretInfo
|
2018-09-26 08:26:12 -04:00
|
|
|
SSH []SSHInfo
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type MountInfo struct {
|
|
|
|
Target string
|
|
|
|
Source Output
|
|
|
|
Opts []MountOption
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyEnv struct {
|
2020-11-17 08:19:27 -05:00
|
|
|
HTTPProxy string
|
|
|
|
HTTPSProxy string
|
|
|
|
FTPProxy string
|
2018-04-19 13:07:27 -04:00
|
|
|
NoProxy string
|
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
|
|
|
|
type CacheMountSharingMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CacheMountShared CacheMountSharingMode = iota
|
|
|
|
CacheMountPrivate
|
|
|
|
CacheMountLocked
|
|
|
|
)
|
2018-08-20 13:59:40 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
NetModeSandbox = pb.NetMode_UNSET
|
|
|
|
NetModeHost = pb.NetMode_HOST
|
|
|
|
NetModeNone = pb.NetMode_NONE
|
|
|
|
)
|
2019-04-03 02:23:23 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
SecurityModeInsecure = pb.SecurityMode_INSECURE
|
|
|
|
SecurityModeSandbox = pb.SecurityMode_SANDBOX
|
|
|
|
)
|