2017-08-25 20:08:18 +00:00
|
|
|
package dockerfile2llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2017-09-11 01:11:42 +00:00
|
|
|
"encoding/json"
|
2017-09-01 23:57:22 +00:00
|
|
|
"fmt"
|
2017-08-25 20:08:18 +00:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/docker/docker/builder/dockerfile/instructions"
|
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2017-09-11 01:11:42 +00:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
|
"github.com/docker/go-connections/nat"
|
2017-08-25 20:08:18 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-22 17:30:30 +00:00
|
|
|
emptyImageName = "scratch"
|
|
|
|
localNameContext = "context"
|
2017-08-25 20:08:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ConvertOpt struct {
|
|
|
|
Target string
|
|
|
|
MetaResolver llb.ImageMetaResolver
|
2017-09-12 06:29:22 +00:00
|
|
|
BuildArgs map[string]string
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, *Image, error) {
|
2017-08-25 20:08:18 +00:00
|
|
|
if len(dt) == 0 {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, errors.Errorf("the Dockerfile cannot be empty")
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dockerfile, err := parser.Parse(bytes.NewReader(dt))
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stages, metaArgs, err := instructions.Parse(dockerfile.AST)
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 06:29:22 +00:00
|
|
|
for i := range metaArgs {
|
2017-09-22 17:30:30 +00:00
|
|
|
metaArgs[i] = setBuildArgValue(metaArgs[i], opt.BuildArgs)
|
2017-09-12 06:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shlex := NewShellLex(dockerfile.EscapeToken)
|
2017-08-25 20:08:18 +00:00
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
var allDispatchStates []*dispatchState
|
|
|
|
dispatchStatesByName := map[string]*dispatchState{}
|
2017-08-25 20:08:18 +00:00
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
// set base state for every image
|
2017-08-25 20:08:18 +00:00
|
|
|
for _, st := range stages {
|
2017-09-22 17:30:30 +00:00
|
|
|
name, err := shlex.ProcessWord(st.BaseName, toEnvList(metaArgs, nil))
|
2017-09-12 06:29:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
st.BaseName = name
|
2017-08-25 20:08:18 +00:00
|
|
|
state := llb.Scratch()
|
|
|
|
if st.BaseName != emptyImageName {
|
|
|
|
state = llb.Image(st.BaseName)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds := &dispatchState{
|
|
|
|
state: state,
|
|
|
|
stage: st,
|
|
|
|
}
|
2017-09-22 17:30:30 +00:00
|
|
|
if d, ok := dispatchStatesByName[st.BaseName]; ok {
|
2017-08-25 20:08:18 +00:00
|
|
|
ds.base = d
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
allDispatchStates = append(allDispatchStates, ds)
|
2017-08-25 20:08:18 +00:00
|
|
|
if st.Name != "" {
|
2017-09-22 17:30:30 +00:00
|
|
|
dispatchStatesByName[strings.ToLower(st.Name)] = ds
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:47:37 +00:00
|
|
|
metaResolver := opt.MetaResolver
|
|
|
|
if metaResolver == nil {
|
|
|
|
metaResolver = llb.DefaultImageMetaResolver()
|
|
|
|
}
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2017-09-22 17:30:30 +00:00
|
|
|
for i, d := range allDispatchStates {
|
|
|
|
// resolve image config for every stage
|
2017-08-25 20:08:18 +00:00
|
|
|
if d.base == nil && d.stage.BaseName != emptyImageName {
|
|
|
|
func(i int, d *dispatchState) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(d.stage.BaseName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.stage.BaseName = reference.TagNameOnly(ref).String()
|
2017-09-14 05:47:37 +00:00
|
|
|
if metaResolver != nil {
|
2017-09-11 01:11:42 +00:00
|
|
|
dt, err := metaResolver.ResolveImageConfig(ctx, d.stage.BaseName)
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-12 06:29:22 +00:00
|
|
|
return nil // handle the error while builder is actually running
|
|
|
|
// TODO: detect unreachable stages so config is not pulled for them
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-11 01:11:42 +00:00
|
|
|
var img Image
|
|
|
|
if err := json.Unmarshal(dt, &img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-22 17:30:30 +00:00
|
|
|
allDispatchStates[i].image = img
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}(i, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
for _, d := range allDispatchStates {
|
2017-08-25 20:08:18 +00:00
|
|
|
if d.base != nil {
|
|
|
|
d.state = d.base.state
|
2017-09-11 05:35:07 +00:00
|
|
|
d.image = clone(d.base.image)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
var buildArgs []instructions.ArgCommand
|
2017-09-12 06:29:22 +00:00
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
// initialize base metadata from image conf
|
2017-08-25 20:08:18 +00:00
|
|
|
for _, env := range d.image.Config.Env {
|
|
|
|
parts := strings.SplitN(env, "=", 2)
|
|
|
|
v := ""
|
|
|
|
if len(parts) > 1 {
|
|
|
|
v = parts[1]
|
|
|
|
}
|
|
|
|
if err := dispatchEnv(d, &instructions.EnvCommand{Env: []instructions.KeyValuePair{{Key: parts[0], Value: v}}}); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.image.Config.WorkingDir != "" {
|
|
|
|
if err = dispatchWorkdir(d, &instructions.WorkdirCommand{Path: d.image.Config.WorkingDir}); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 17:30:30 +00:00
|
|
|
if d.image.Config.User != "" {
|
|
|
|
if err = dispatchUser(d, &instructions.UserCommand{User: d.image.Config.User}); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
for _, cmd := range d.stage.Commands {
|
2017-09-12 06:29:22 +00:00
|
|
|
if ex, ok := cmd.(instructions.SupportsSingleWordExpansion); ok {
|
|
|
|
err := ex.Expand(func(word string) (string, error) {
|
2017-09-22 17:30:30 +00:00
|
|
|
return shlex.ProcessWord(word, toEnvList(buildArgs, d.image.Config.Env))
|
2017-09-12 06:29:22 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 20:08:18 +00:00
|
|
|
switch c := cmd.(type) {
|
|
|
|
case *instructions.EnvCommand:
|
|
|
|
err = dispatchEnv(d, c)
|
|
|
|
case *instructions.RunCommand:
|
2017-09-22 17:30:30 +00:00
|
|
|
err = dispatchRun(d, c, buildArgs)
|
2017-08-25 20:08:18 +00:00
|
|
|
case *instructions.WorkdirCommand:
|
|
|
|
err = dispatchWorkdir(d, c)
|
|
|
|
case *instructions.AddCommand:
|
2017-09-22 17:30:30 +00:00
|
|
|
err = dispatchCopy(d, c.SourcesAndDest, llb.Local(localNameContext))
|
2017-09-11 01:11:42 +00:00
|
|
|
case *instructions.LabelCommand:
|
|
|
|
err = dispatchLabel(d, c)
|
|
|
|
case *instructions.OnbuildCommand:
|
|
|
|
err = dispatchOnbuild(d, c)
|
|
|
|
case *instructions.CmdCommand:
|
|
|
|
err = dispatchCmd(d, c)
|
|
|
|
case *instructions.EntrypointCommand:
|
|
|
|
err = dispatchEntrypoint(d, c)
|
|
|
|
case *instructions.HealthCheckCommand:
|
|
|
|
err = dispatchHealthcheck(d, c)
|
|
|
|
case *instructions.ExposeCommand:
|
|
|
|
err = dispatchExpose(d, c)
|
|
|
|
case *instructions.UserCommand:
|
|
|
|
err = dispatchUser(d, c)
|
|
|
|
case *instructions.VolumeCommand:
|
|
|
|
err = dispatchVolume(d, c)
|
|
|
|
case *instructions.StopSignalCommand:
|
|
|
|
err = dispatchStopSignal(d, c)
|
|
|
|
case *instructions.ShellCommand:
|
|
|
|
err = dispatchShell(d, c)
|
2017-09-12 06:29:22 +00:00
|
|
|
case *instructions.ArgCommand:
|
2017-09-22 17:30:30 +00:00
|
|
|
buildArgs, err = dispatchArg(d, c, buildArgs, metaArgs, opt.BuildArgs)
|
2017-08-25 20:08:18 +00:00
|
|
|
case *instructions.CopyCommand:
|
|
|
|
l := llb.Local("context")
|
|
|
|
if c.From != "" {
|
|
|
|
index, err := strconv.Atoi(c.From)
|
|
|
|
if err != nil {
|
2017-09-22 17:30:30 +00:00
|
|
|
stn, ok := dispatchStatesByName[strings.ToLower(c.From)]
|
2017-08-25 20:08:18 +00:00
|
|
|
if !ok {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, errors.Errorf("stage %s not found", c.From)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
l = stn.state
|
|
|
|
} else {
|
2017-09-22 17:30:30 +00:00
|
|
|
if index >= len(allDispatchStates) {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, errors.Errorf("invalid stage index %d", index)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-22 17:30:30 +00:00
|
|
|
l = allDispatchStates[index].state
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err = dispatchCopy(d, c.SourcesAndDest, l)
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Target == "" {
|
2017-09-22 17:30:30 +00:00
|
|
|
return &allDispatchStates[len(allDispatchStates)-1].state, &allDispatchStates[len(allDispatchStates)-1].image, nil
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
state, ok := dispatchStatesByName[strings.ToLower(opt.Target)]
|
2017-08-25 20:08:18 +00:00
|
|
|
if !ok {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, errors.Errorf("target stage %s could not be found", opt.Target)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-11 05:35:07 +00:00
|
|
|
return &state.state, &state.image, nil
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dispatchState struct {
|
|
|
|
state llb.State
|
2017-09-11 01:11:42 +00:00
|
|
|
image Image
|
2017-08-25 20:08:18 +00:00
|
|
|
stage instructions.Stage
|
|
|
|
base *dispatchState
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchEnv(d *dispatchState, c *instructions.EnvCommand) error {
|
|
|
|
for _, e := range c.Env {
|
|
|
|
d.state = d.state.AddEnv(e.Key, e.Value)
|
2017-09-12 06:29:22 +00:00
|
|
|
d.image.Config.Env = addEnv(d.image.Config.Env, e.Key, e.Value, true)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:29:22 +00:00
|
|
|
func dispatchRun(d *dispatchState, c *instructions.RunCommand, buildArgs []instructions.ArgCommand) error {
|
2017-08-25 20:08:18 +00:00
|
|
|
var args []string = c.CmdLine
|
|
|
|
if c.PrependShell {
|
|
|
|
args = append(defaultShell(), strings.Join(args, " "))
|
2017-09-01 23:57:22 +00:00
|
|
|
} else if d.image.Config.Entrypoint != nil {
|
|
|
|
args = append(d.image.Config.Entrypoint, args...)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-12 06:29:22 +00:00
|
|
|
opt := []llb.RunOption{llb.Args(args)}
|
|
|
|
for _, arg := range buildArgs {
|
|
|
|
opt = append(opt, llb.AddEnv(arg.Key, getArgValue(arg)))
|
|
|
|
}
|
|
|
|
d.state = d.state.Run(opt...).Root()
|
2017-08-25 20:08:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand) error {
|
|
|
|
d.state = d.state.Dir(c.Path)
|
|
|
|
wd := c.Path
|
|
|
|
if !path.IsAbs(c.Path) {
|
|
|
|
wd = path.Join("/", d.image.Config.WorkingDir, wd)
|
|
|
|
}
|
|
|
|
d.image.Config.WorkingDir = wd
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchCopy(d *dispatchState, c instructions.SourcesAndDest, sourceState llb.State) error {
|
|
|
|
// TODO: this should use CopyOp instead. Current implementation is inefficient and doesn't match Dockerfile path suffixes rules
|
2017-09-01 23:57:22 +00:00
|
|
|
img := llb.Image("tonistiigi/copy@sha256:260a4355be76e0609518ebd7c0e026831c80b8908d4afd3f8e8c942645b1e5cf")
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
dest := path.Join("/dest", pathRelativeToWorkingDir(d.state, c.Dest()))
|
2017-09-01 23:57:22 +00:00
|
|
|
args := []string{"copy"}
|
|
|
|
mounts := make([]llb.RunOption, 0, len(c.Sources()))
|
|
|
|
for i, src := range c.Sources() {
|
|
|
|
d, f := splitWildcards(src)
|
|
|
|
if f == "" {
|
|
|
|
f = path.Base(src)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-01 23:57:22 +00:00
|
|
|
target := path.Join(fmt.Sprintf("/src-%d", i), f)
|
|
|
|
args = append(args, target)
|
|
|
|
mounts = append(mounts, llb.AddMount(target, sourceState, llb.SourcePath(d), llb.Readonly))
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:57:22 +00:00
|
|
|
args = append(args, dest)
|
|
|
|
run := img.Run(append([]llb.RunOption{llb.Args(args)}, mounts...)...)
|
2017-08-25 20:08:18 +00:00
|
|
|
d.state = run.AddMount("/dest", d.state)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchMaintainer(d *dispatchState, c instructions.MaintainerCommand) error {
|
|
|
|
d.image.Author = c.Maintainer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-11 01:11:42 +00:00
|
|
|
func dispatchLabel(d *dispatchState, c *instructions.LabelCommand) error {
|
2017-08-25 20:08:18 +00:00
|
|
|
if d.image.Config.Labels == nil {
|
|
|
|
d.image.Config.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
for _, v := range c.Labels {
|
|
|
|
d.image.Config.Labels[v.Key] = v.Value
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-11 01:11:42 +00:00
|
|
|
func dispatchOnbuild(d *dispatchState, c *instructions.OnbuildCommand) error {
|
|
|
|
d.image.Config.OnBuild = append(d.image.Config.OnBuild, c.Expression)
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
func dispatchCmd(d *dispatchState, c *instructions.CmdCommand) error {
|
|
|
|
var args []string = c.CmdLine
|
|
|
|
if c.PrependShell {
|
|
|
|
args = append(defaultShell(), strings.Join(args, " "))
|
|
|
|
}
|
|
|
|
d.image.Config.Cmd = args
|
2017-09-11 01:11:42 +00:00
|
|
|
d.image.Config.ArgsEscaped = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchEntrypoint(d *dispatchState, c *instructions.EntrypointCommand) error {
|
|
|
|
var args []string = c.CmdLine
|
|
|
|
if c.PrependShell {
|
|
|
|
args = append(defaultShell(), strings.Join(args, " "))
|
|
|
|
}
|
|
|
|
d.image.Config.Entrypoint = args
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchHealthcheck(d *dispatchState, c *instructions.HealthCheckCommand) error {
|
|
|
|
d.image.Config.Healthcheck = &HealthConfig{
|
|
|
|
Test: c.Health.Test,
|
|
|
|
Interval: c.Health.Interval,
|
|
|
|
Timeout: c.Health.Timeout,
|
|
|
|
StartPeriod: c.Health.StartPeriod,
|
|
|
|
Retries: c.Health.Retries,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchExpose(d *dispatchState, c *instructions.ExposeCommand) error {
|
|
|
|
// TODO: custom multi word expansion
|
|
|
|
ps, _, err := nat.ParsePortSpecs(c.Ports)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.image.Config.ExposedPorts == nil {
|
|
|
|
d.image.Config.ExposedPorts = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
for p := range ps {
|
|
|
|
d.image.Config.ExposedPorts[string(p)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func dispatchUser(d *dispatchState, c *instructions.UserCommand) error {
|
|
|
|
d.image.Config.User = c.User
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchVolume(d *dispatchState, c *instructions.VolumeCommand) error {
|
|
|
|
if d.image.Config.Volumes == nil {
|
|
|
|
d.image.Config.Volumes = map[string]struct{}{}
|
|
|
|
}
|
|
|
|
for _, v := range c.Volumes {
|
|
|
|
if v == "" {
|
|
|
|
return errors.New("VOLUME specified can not be an empty string")
|
|
|
|
}
|
|
|
|
d.image.Config.Volumes[v] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchStopSignal(d *dispatchState, c *instructions.StopSignalCommand) error {
|
|
|
|
if _, err := signal.ParseSignal(c.Signal); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.image.Config.StopSignal = c.Signal
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dispatchShell(d *dispatchState, c *instructions.ShellCommand) error {
|
|
|
|
d.image.Config.Shell = c.Shell
|
2017-08-25 20:08:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-09-01 23:57:22 +00:00
|
|
|
|
2017-09-12 06:29:22 +00:00
|
|
|
func dispatchArg(d *dispatchState, c *instructions.ArgCommand, args []instructions.ArgCommand, metaArgs []instructions.ArgCommand, buildArgs map[string]string) ([]instructions.ArgCommand, error) {
|
|
|
|
if c.Value == nil {
|
|
|
|
for _, ma := range metaArgs {
|
|
|
|
if ma.Key == c.Key {
|
|
|
|
c.Value = ma.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
args = append(args, setBuildArgValue(*c, buildArgs))
|
2017-09-12 06:29:22 +00:00
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
func pathRelativeToWorkingDir(s llb.State, p string) string {
|
|
|
|
if path.IsAbs(p) {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
return path.Join(s.GetDir(), p)
|
|
|
|
}
|
|
|
|
|
2017-09-01 23:57:22 +00:00
|
|
|
func splitWildcards(name string) (string, string) {
|
|
|
|
i := 0
|
|
|
|
for ; i < len(name); i++ {
|
|
|
|
ch := name[i]
|
|
|
|
if ch == '\\' {
|
|
|
|
i++
|
|
|
|
} else if ch == '*' || ch == '?' || ch == '[' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == len(name) {
|
|
|
|
return name, ""
|
|
|
|
}
|
|
|
|
return path.Dir(name[:i]), path.Base(name[:i]) + name[i:]
|
|
|
|
}
|
2017-09-11 01:11:42 +00:00
|
|
|
|
2017-09-12 06:29:22 +00:00
|
|
|
func addEnv(env []string, k, v string, override bool) []string {
|
2017-09-11 01:11:42 +00:00
|
|
|
gotOne := false
|
2017-09-12 06:29:22 +00:00
|
|
|
for i, envVar := range env {
|
2017-09-11 01:11:42 +00:00
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
|
|
compareFrom := envParts[0]
|
|
|
|
if equalEnvKeys(compareFrom, k) {
|
2017-09-12 06:29:22 +00:00
|
|
|
if override {
|
|
|
|
env[i] = k + "=" + v
|
|
|
|
}
|
2017-09-11 01:11:42 +00:00
|
|
|
gotOne = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !gotOne {
|
2017-09-12 06:29:22 +00:00
|
|
|
env = append(env, k+"="+v)
|
2017-09-11 01:11:42 +00:00
|
|
|
}
|
2017-09-12 06:29:22 +00:00
|
|
|
return env
|
2017-09-11 01:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func equalEnvKeys(from, to string) bool {
|
|
|
|
return from == to
|
|
|
|
}
|
2017-09-12 06:29:22 +00:00
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
func setBuildArgValue(c instructions.ArgCommand, values map[string]string) instructions.ArgCommand {
|
2017-09-12 06:29:22 +00:00
|
|
|
if v, ok := values[c.Key]; ok {
|
|
|
|
c.Value = &v
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:30:30 +00:00
|
|
|
func toEnvList(args []instructions.ArgCommand, env []string) []string {
|
2017-09-12 06:29:22 +00:00
|
|
|
for _, arg := range args {
|
|
|
|
env = addEnv(env, arg.Key, getArgValue(arg), false)
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
func getArgValue(arg instructions.ArgCommand) string {
|
|
|
|
v := ""
|
|
|
|
if arg.Value != nil {
|
|
|
|
v = *arg.Value
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|