2018-04-19 13:07:27 -04:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
2020-04-16 11:06:33 -04:00
|
|
|
"context"
|
2018-04-19 13:07:27 -04:00
|
|
|
"fmt"
|
2018-08-11 15:04:13 -04:00
|
|
|
"net"
|
2018-04-19 13:07:27 -04:00
|
|
|
"path"
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2018-04-19 13:07:27 -04:00
|
|
|
"github.com/google/shlex"
|
2018-08-20 13:59:40 -04:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2018-06-29 11:17:00 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-04-19 13:07:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type contextKeyT string
|
|
|
|
|
|
|
|
var (
|
2018-08-11 15:04:13 -04:00
|
|
|
keyArgs = contextKeyT("llb.exec.args")
|
|
|
|
keyDir = contextKeyT("llb.exec.dir")
|
|
|
|
keyEnv = contextKeyT("llb.exec.env")
|
|
|
|
keyUser = contextKeyT("llb.exec.user")
|
2020-11-17 08:19:27 -05:00
|
|
|
keyHostname = contextKeyT("llb.exec.hostname")
|
2018-08-11 15:04:13 -04:00
|
|
|
keyExtraHost = contextKeyT("llb.exec.extrahost")
|
|
|
|
keyPlatform = contextKeyT("llb.platform")
|
2018-08-20 13:59:40 -04:00
|
|
|
keyNetwork = contextKeyT("llb.network")
|
2019-04-03 02:23:23 -04:00
|
|
|
keySecurity = contextKeyT("llb.security")
|
2018-04-19 13:07:27 -04:00
|
|
|
)
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func AddEnvf(key, value string, v ...interface{}) StateOption {
|
|
|
|
return addEnvf(key, value, true, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddEnv(key, value string) StateOption {
|
|
|
|
return addEnvf(key, value, false)
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:42:32 -04:00
|
|
|
func addEnvf(key, value string, replace bool, v ...interface{}) StateOption {
|
|
|
|
if replace {
|
|
|
|
value = fmt.Sprintf(value, v...)
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
return func(s State) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return s.withValue(keyEnv, func(ctx context.Context) (interface{}, error) {
|
|
|
|
env, err := getEnv(s)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return env.AddOrReplace(key, value), nil
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func Dir(str string) StateOption {
|
2019-09-20 20:42:32 -04:00
|
|
|
return dirf(str, false)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func Dirf(str string, v ...interface{}) StateOption {
|
|
|
|
return dirf(str, true, v...)
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:42:32 -04:00
|
|
|
func dirf(value string, replace bool, v ...interface{}) StateOption {
|
|
|
|
if replace {
|
|
|
|
value = fmt.Sprintf(value, v...)
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
return func(s State) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return s.withValue(keyDir, func(ctx context.Context) (interface{}, error) {
|
|
|
|
if !path.IsAbs(value) {
|
|
|
|
prev, err := getDir(s)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if prev == "" {
|
|
|
|
prev = "/"
|
|
|
|
}
|
|
|
|
value = path.Join(prev, value)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
return value, nil
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func User(str string) StateOption {
|
2018-04-19 13:07:27 -04:00
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyUser, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func Reset(other State) StateOption {
|
2018-04-19 13:07:27 -04:00
|
|
|
return func(s State) State {
|
|
|
|
s = NewState(s.Output())
|
2020-04-16 11:06:33 -04:00
|
|
|
s.prev = &other
|
2018-04-19 13:07:27 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func getEnv(s State) func(context.Context) (EnvList, error) {
|
|
|
|
return func(ctx context.Context) (EnvList, error) {
|
|
|
|
v, err := s.getValue(keyEnv)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
return v.(EnvList), nil
|
|
|
|
}
|
|
|
|
return EnvList{}, nil
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func getDir(s State) func(context.Context) (string, error) {
|
|
|
|
return func(ctx context.Context) (string, error) {
|
|
|
|
v, err := s.getValue(keyDir)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
return v.(string), nil
|
|
|
|
}
|
|
|
|
return "", nil
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func getArgs(s State) func(context.Context) ([]string, error) {
|
|
|
|
return func(ctx context.Context) ([]string, error) {
|
|
|
|
v, err := s.getValue(keyArgs)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
return v.([]string), nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func getUser(s State) func(context.Context) (string, error) {
|
|
|
|
return func(ctx context.Context) (string, error) {
|
|
|
|
v, err := s.getValue(keyUser)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
return v.(string), nil
|
|
|
|
}
|
|
|
|
return "", nil
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 08:19:27 -05:00
|
|
|
func Hostname(str string) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyHostname, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHostname(s State) func(context.Context) (string, error) {
|
|
|
|
return func(ctx context.Context) (string, error) {
|
|
|
|
v, err := s.getValue(keyHostname)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
return v.(string), nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func args(args ...string) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyArgs, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:42:32 -04:00
|
|
|
func shlexf(str string, replace bool, v ...interface{}) StateOption {
|
|
|
|
if replace {
|
|
|
|
str = fmt.Sprintf(str, v...)
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
return func(s State) State {
|
2019-09-20 20:42:32 -04:00
|
|
|
arg, err := shlex.Split(str)
|
2020-11-17 08:19:27 -05:00
|
|
|
if err != nil { //nolint
|
2018-04-19 13:07:27 -04:00
|
|
|
// TODO: handle error
|
|
|
|
}
|
|
|
|
return args(arg...)(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func platform(p specs.Platform) StateOption {
|
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyPlatform, platforms.Normalize(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func getPlatform(s State) func(context.Context) (*specs.Platform, error) {
|
|
|
|
return func(ctx context.Context) (*specs.Platform, error) {
|
|
|
|
v, err := s.getValue(keyPlatform)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
p := v.(specs.Platform)
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2018-06-29 11:17:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
func extraHost(host string, ip net.IP) StateOption {
|
|
|
|
return func(s State) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return s.withValue(keyExtraHost, func(ctx context.Context) (interface{}, error) {
|
|
|
|
v, err := getExtraHosts(s)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return append(v, HostIP{Host: host, IP: ip}), nil
|
|
|
|
})
|
2018-08-11 15:04:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func getExtraHosts(s State) func(context.Context) ([]HostIP, error) {
|
|
|
|
return func(ctx context.Context) ([]HostIP, error) {
|
|
|
|
v, err := s.getValue(keyExtraHost)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
return v.([]HostIP), nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2018-08-11 15:04:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HostIP struct {
|
|
|
|
Host string
|
|
|
|
IP net.IP
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func Network(v pb.NetMode) StateOption {
|
2018-08-20 13:59:40 -04:00
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keyNetwork, v)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
func getNetwork(s State) func(context.Context) (pb.NetMode, error) {
|
|
|
|
return func(ctx context.Context) (pb.NetMode, error) {
|
|
|
|
v, err := s.getValue(keyNetwork)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
n := v.(pb.NetMode)
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
return NetModeSandbox, nil
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func Security(v pb.SecurityMode) StateOption {
|
2019-04-03 02:23:23 -04:00
|
|
|
return func(s State) State {
|
|
|
|
return s.WithValue(keySecurity, v)
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
func getSecurity(s State) func(context.Context) (pb.SecurityMode, error) {
|
|
|
|
return func(ctx context.Context) (pb.SecurityMode, error) {
|
|
|
|
v, err := s.getValue(keySecurity)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
n := v.(pb.SecurityMode)
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
return SecurityModeSandbox, nil
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
type EnvList []KeyValue
|
|
|
|
|
|
|
|
type KeyValue struct {
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e EnvList) AddOrReplace(k, v string) EnvList {
|
|
|
|
e = e.Delete(k)
|
|
|
|
e = append(e, KeyValue{key: k, value: v})
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
func (e EnvList) SetDefault(k, v string) EnvList {
|
|
|
|
if _, ok := e.Get(k); !ok {
|
|
|
|
e = append(e, KeyValue{key: k, value: v})
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func (e EnvList) Delete(k string) EnvList {
|
|
|
|
e = append([]KeyValue(nil), e...)
|
|
|
|
if i, ok := e.Index(k); ok {
|
|
|
|
return append(e[:i], e[i+1:]...)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e EnvList) Get(k string) (string, bool) {
|
|
|
|
if index, ok := e.Index(k); ok {
|
|
|
|
return e[index].value, true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e EnvList) Index(k string) (int, bool) {
|
|
|
|
for i, kv := range e {
|
|
|
|
if kv.key == k {
|
|
|
|
return i, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e EnvList) ToArray() []string {
|
|
|
|
out := make([]string, 0, len(e))
|
|
|
|
for _, kv := range e {
|
|
|
|
out = append(out, kv.key+"="+kv.value)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|