github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/builder/dockerfile/evaluator.go (about)

     1  // Package dockerfile is the evaluation step in the Dockerfile parse/evaluate pipeline.
     2  //
     3  // It incorporates a dispatch table based on the parser.Node values (see the
     4  // parser package for more information) that are yielded from the parser itself.
     5  // Calling NewBuilder with the BuildOpts struct can be used to customize the
     6  // experience for execution purposes only. Parsing is controlled in the parser
     7  // package, and this division of responsibility should be respected.
     8  //
     9  // Please see the jump table targets for the actual invocations, most of which
    10  // will call out to the functions in internals.go to deal with their tasks.
    11  //
    12  // ONBUILD is a special case, which is covered in the onbuild() func in
    13  // dispatchers.go.
    14  //
    15  // The evaluator uses the concept of "steps", which are usually each processable
    16  // line in the Dockerfile. Each step is numbered and certain actions are taken
    17  // before and after each step, such as creating an image ID and removing temporary
    18  // containers and images. Note that ONBUILD creates a kinda-sorta "sub run" which
    19  // includes its own set of steps (usually only one of them).
    20  package dockerfile
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"strings"
    26  
    27  	"github.com/docker/docker/builder/dockerfile/command"
    28  	"github.com/docker/docker/builder/dockerfile/parser"
    29  )
    30  
    31  // Environment variable interpolation will happen on these statements only.
    32  var replaceEnvAllowed = map[string]bool{
    33  	command.Env:        true,
    34  	command.Label:      true,
    35  	command.Add:        true,
    36  	command.Copy:       true,
    37  	command.Workdir:    true,
    38  	command.Expose:     true,
    39  	command.Volume:     true,
    40  	command.User:       true,
    41  	command.StopSignal: true,
    42  	command.Arg:        true,
    43  }
    44  
    45  // Certain commands are allowed to have their args split into more
    46  // words after env var replacements. Meaning:
    47  //   ENV foo="123 456"
    48  //   EXPOSE $foo
    49  // should result in the same thing as:
    50  //   EXPOSE 123 456
    51  // and not treat "123 456" as a single word.
    52  // Note that: EXPOSE "$foo" and EXPOSE $foo are not the same thing.
    53  // Quotes will cause it to still be treated as single word.
    54  var allowWordExpansion = map[string]bool{
    55  	command.Expose: true,
    56  }
    57  
    58  var evaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
    59  
    60  func init() {
    61  	evaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
    62  		command.Add:         add,
    63  		command.Arg:         arg,
    64  		command.Cmd:         cmd,
    65  		command.Copy:        dispatchCopy, // copy() is a go builtin
    66  		command.Entrypoint:  entrypoint,
    67  		command.Env:         env,
    68  		command.Expose:      expose,
    69  		command.From:        from,
    70  		command.Healthcheck: healthcheck,
    71  		command.Label:       label,
    72  		command.Maintainer:  maintainer,
    73  		command.Onbuild:     onbuild,
    74  		command.Run:         run,
    75  		command.Shell:       shell,
    76  		command.StopSignal:  stopSignal,
    77  		command.User:        user,
    78  		command.Volume:      volume,
    79  		command.Workdir:     workdir,
    80  	}
    81  }
    82  
    83  // This method is the entrypoint to all statement handling routines.
    84  //
    85  // Almost all nodes will have this structure:
    86  // Child[Node, Node, Node] where Child is from parser.Node.Children and each
    87  // node comes from parser.Node.Next. This forms a "line" with a statement and
    88  // arguments and we process them in this normalized form by hitting
    89  // evaluateTable with the leaf nodes of the command and the Builder object.
    90  //
    91  // ONBUILD is a special case; in this case the parser will emit:
    92  // Child[Node, Child[Node, Node...]] where the first node is the literal
    93  // "onbuild" and the child entrypoint is the command of the ONBUILD statement,
    94  // such as `RUN` in ONBUILD RUN foo. There is special case logic in here to
    95  // deal with that, at least until it becomes more of a general concern with new
    96  // features.
    97  func (b *Builder) dispatch(stepN int, stepTotal int, ast *parser.Node) error {
    98  	cmd := ast.Value
    99  	upperCasedCmd := strings.ToUpper(cmd)
   100  
   101  	// To ensure the user is given a decent error message if the platform
   102  	// on which the daemon is running does not support a builder command.
   103  	if err := platformSupports(strings.ToLower(cmd)); err != nil {
   104  		return err
   105  	}
   106  
   107  	attrs := ast.Attributes
   108  	original := ast.Original
   109  	flags := ast.Flags
   110  	strList := []string{}
   111  	msg := fmt.Sprintf("Step %d/%d : %s", stepN+1, stepTotal, upperCasedCmd)
   112  
   113  	if len(ast.Flags) > 0 {
   114  		msg += " " + strings.Join(ast.Flags, " ")
   115  	}
   116  
   117  	if cmd == "onbuild" {
   118  		if ast.Next == nil {
   119  			return errors.New("ONBUILD requires at least one argument")
   120  		}
   121  		ast = ast.Next.Children[0]
   122  		strList = append(strList, ast.Value)
   123  		msg += " " + ast.Value
   124  
   125  		if len(ast.Flags) > 0 {
   126  			msg += " " + strings.Join(ast.Flags, " ")
   127  		}
   128  
   129  	}
   130  
   131  	// count the number of nodes that we are going to traverse first
   132  	// so we can pre-create the argument and message array. This speeds up the
   133  	// allocation of those list a lot when they have a lot of arguments
   134  	cursor := ast
   135  	var n int
   136  	for cursor.Next != nil {
   137  		cursor = cursor.Next
   138  		n++
   139  	}
   140  	msgList := make([]string, n)
   141  
   142  	var i int
   143  	// Append the build-time args to config-environment.
   144  	// This allows builder config to override the variables, making the behavior similar to
   145  	// a shell script i.e. `ENV foo bar` overrides value of `foo` passed in build
   146  	// context. But `ENV foo $foo` will use the value from build context if one
   147  	// isn't already been defined by a previous ENV primitive.
   148  	// Note, we get this behavior because we know that ProcessWord() will
   149  	// stop on the first occurrence of a variable name and not notice
   150  	// a subsequent one. So, putting the buildArgs list after the Config.Env
   151  	// list, in 'envs', is safe.
   152  	envs := b.runConfig.Env
   153  	for key, val := range b.options.BuildArgs {
   154  		if !b.isBuildArgAllowed(key) {
   155  			// skip build-args that are not in allowed list, meaning they have
   156  			// not been defined by an "ARG" Dockerfile command yet.
   157  			// This is an error condition but only if there is no "ARG" in the entire
   158  			// Dockerfile, so we'll generate any necessary errors after we parsed
   159  			// the entire file (see 'leftoverArgs' processing in evaluator.go )
   160  			continue
   161  		}
   162  		envs = append(envs, fmt.Sprintf("%s=%s", key, *val))
   163  	}
   164  	for ast.Next != nil {
   165  		ast = ast.Next
   166  		var str string
   167  		str = ast.Value
   168  		if replaceEnvAllowed[cmd] {
   169  			var err error
   170  			var words []string
   171  
   172  			if allowWordExpansion[cmd] {
   173  				words, err = ProcessWords(str, envs, b.directive.EscapeToken)
   174  				if err != nil {
   175  					return err
   176  				}
   177  				strList = append(strList, words...)
   178  			} else {
   179  				str, err = ProcessWord(str, envs, b.directive.EscapeToken)
   180  				if err != nil {
   181  					return err
   182  				}
   183  				strList = append(strList, str)
   184  			}
   185  		} else {
   186  			strList = append(strList, str)
   187  		}
   188  		msgList[i] = ast.Value
   189  		i++
   190  	}
   191  
   192  	msg += " " + strings.Join(msgList, " ")
   193  	fmt.Fprintln(b.Stdout, msg)
   194  
   195  	// XXX yes, we skip any cmds that are not valid; the parser should have
   196  	// picked these out already.
   197  	if f, ok := evaluateTable[cmd]; ok {
   198  		b.flags = NewBFlags()
   199  		b.flags.Args = flags
   200  		return f(b, strList, attrs, original)
   201  	}
   202  
   203  	return fmt.Errorf("Unknown instruction: %s", upperCasedCmd)
   204  }
   205  
   206  // checkDispatch does a simple check for syntax errors of the Dockerfile.
   207  // Because some of the instructions can only be validated through runtime,
   208  // arg, env, etc., this syntax check will not be complete and could not replace
   209  // the runtime check. Instead, this function is only a helper that allows
   210  // user to find out the obvious error in Dockerfile earlier on.
   211  // onbuild bool: indicate if instruction XXX is part of `ONBUILD XXX` trigger
   212  func (b *Builder) checkDispatch(ast *parser.Node, onbuild bool) error {
   213  	cmd := ast.Value
   214  	upperCasedCmd := strings.ToUpper(cmd)
   215  
   216  	// To ensure the user is given a decent error message if the platform
   217  	// on which the daemon is running does not support a builder command.
   218  	if err := platformSupports(strings.ToLower(cmd)); err != nil {
   219  		return err
   220  	}
   221  
   222  	// The instruction itself is ONBUILD, we will make sure it follows with at
   223  	// least one argument
   224  	if upperCasedCmd == "ONBUILD" {
   225  		if ast.Next == nil {
   226  			return errors.New("ONBUILD requires at least one argument")
   227  		}
   228  	}
   229  
   230  	// The instruction is part of ONBUILD trigger (not the instruction itself)
   231  	if onbuild {
   232  		switch upperCasedCmd {
   233  		case "ONBUILD":
   234  			return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
   235  		case "MAINTAINER", "FROM":
   236  			return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
   237  		}
   238  	}
   239  
   240  	if _, ok := evaluateTable[cmd]; ok {
   241  		return nil
   242  	}
   243  
   244  	return fmt.Errorf("Unknown instruction: %s", upperCasedCmd)
   245  }