parse.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. package instructions
  2. import (
  3. "fmt"
  4. "regexp"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/api/types/strslice"
  11. "github.com/moby/buildkit/frontend/dockerfile/command"
  12. "github.com/moby/buildkit/frontend/dockerfile/parser"
  13. "github.com/pkg/errors"
  14. )
  15. type parseRequest struct {
  16. command string
  17. args []string
  18. attributes map[string]bool
  19. flags *BFlags
  20. original string
  21. location []parser.Range
  22. }
  23. var parseRunPreHooks []func(*RunCommand, parseRequest) error
  24. var parseRunPostHooks []func(*RunCommand, parseRequest) error
  25. func nodeArgs(node *parser.Node) []string {
  26. result := []string{}
  27. for ; node.Next != nil; node = node.Next {
  28. arg := node.Next
  29. if len(arg.Children) == 0 {
  30. result = append(result, arg.Value)
  31. } else if len(arg.Children) == 1 {
  32. //sub command
  33. result = append(result, arg.Children[0].Value)
  34. result = append(result, nodeArgs(arg.Children[0])...)
  35. }
  36. }
  37. return result
  38. }
  39. func newParseRequestFromNode(node *parser.Node) parseRequest {
  40. return parseRequest{
  41. command: node.Value,
  42. args: nodeArgs(node),
  43. attributes: node.Attributes,
  44. original: node.Original,
  45. flags: NewBFlagsWithArgs(node.Flags),
  46. location: node.Location(),
  47. }
  48. }
  49. // ParseInstruction converts an AST to a typed instruction (either a command or a build stage beginning when encountering a `FROM` statement)
  50. func ParseInstruction(node *parser.Node) (v interface{}, err error) {
  51. defer func() {
  52. err = parser.WithLocation(err, node.Location())
  53. }()
  54. req := newParseRequestFromNode(node)
  55. switch node.Value {
  56. case command.Env:
  57. return parseEnv(req)
  58. case command.Maintainer:
  59. return parseMaintainer(req)
  60. case command.Label:
  61. return parseLabel(req)
  62. case command.Add:
  63. return parseAdd(req)
  64. case command.Copy:
  65. return parseCopy(req)
  66. case command.From:
  67. return parseFrom(req)
  68. case command.Onbuild:
  69. return parseOnBuild(req)
  70. case command.Workdir:
  71. return parseWorkdir(req)
  72. case command.Run:
  73. return parseRun(req)
  74. case command.Cmd:
  75. return parseCmd(req)
  76. case command.Healthcheck:
  77. return parseHealthcheck(req)
  78. case command.Entrypoint:
  79. return parseEntrypoint(req)
  80. case command.Expose:
  81. return parseExpose(req)
  82. case command.User:
  83. return parseUser(req)
  84. case command.Volume:
  85. return parseVolume(req)
  86. case command.StopSignal:
  87. return parseStopSignal(req)
  88. case command.Arg:
  89. return parseArg(req)
  90. case command.Shell:
  91. return parseShell(req)
  92. }
  93. return nil, &UnknownInstruction{Instruction: node.Value, Line: node.StartLine}
  94. }
  95. // ParseCommand converts an AST to a typed Command
  96. func ParseCommand(node *parser.Node) (Command, error) {
  97. s, err := ParseInstruction(node)
  98. if err != nil {
  99. return nil, err
  100. }
  101. if c, ok := s.(Command); ok {
  102. return c, nil
  103. }
  104. return nil, parser.WithLocation(errors.Errorf("%T is not a command type", s), node.Location())
  105. }
  106. // UnknownInstruction represents an error occurring when a command is unresolvable
  107. type UnknownInstruction struct {
  108. Line int
  109. Instruction string
  110. }
  111. func (e *UnknownInstruction) Error() string {
  112. return fmt.Sprintf("unknown instruction: %s", strings.ToUpper(e.Instruction))
  113. }
  114. type parseError struct {
  115. inner error
  116. node *parser.Node
  117. }
  118. func (e *parseError) Error() string {
  119. return fmt.Sprintf("dockerfile parse error line %d: %v", e.node.StartLine, e.inner.Error())
  120. }
  121. func (e *parseError) Unwrap() error {
  122. return e.inner
  123. }
  124. // Parse a Dockerfile into a collection of buildable stages.
  125. // metaArgs is a collection of ARG instructions that occur before the first FROM.
  126. func Parse(ast *parser.Node) (stages []Stage, metaArgs []ArgCommand, err error) {
  127. for _, n := range ast.Children {
  128. cmd, err := ParseInstruction(n)
  129. if err != nil {
  130. return nil, nil, &parseError{inner: err, node: n}
  131. }
  132. if len(stages) == 0 {
  133. // meta arg case
  134. if a, isArg := cmd.(*ArgCommand); isArg {
  135. metaArgs = append(metaArgs, *a)
  136. continue
  137. }
  138. }
  139. switch c := cmd.(type) {
  140. case *Stage:
  141. stages = append(stages, *c)
  142. case Command:
  143. stage, err := CurrentStage(stages)
  144. if err != nil {
  145. return nil, nil, parser.WithLocation(err, n.Location())
  146. }
  147. stage.AddCommand(c)
  148. default:
  149. return nil, nil, parser.WithLocation(errors.Errorf("%T is not a command type", cmd), n.Location())
  150. }
  151. }
  152. return stages, metaArgs, nil
  153. }
  154. func parseKvps(args []string, cmdName string) (KeyValuePairs, error) {
  155. if len(args) == 0 {
  156. return nil, errAtLeastOneArgument(cmdName)
  157. }
  158. if len(args)%2 != 0 {
  159. // should never get here, but just in case
  160. return nil, errTooManyArguments(cmdName)
  161. }
  162. var res KeyValuePairs
  163. for j := 0; j < len(args); j += 2 {
  164. if len(args[j]) == 0 {
  165. return nil, errBlankCommandNames(cmdName)
  166. }
  167. name := args[j]
  168. value := args[j+1]
  169. res = append(res, KeyValuePair{Key: name, Value: value})
  170. }
  171. return res, nil
  172. }
  173. func parseEnv(req parseRequest) (*EnvCommand, error) {
  174. if err := req.flags.Parse(); err != nil {
  175. return nil, err
  176. }
  177. envs, err := parseKvps(req.args, "ENV")
  178. if err != nil {
  179. return nil, err
  180. }
  181. return &EnvCommand{
  182. Env: envs,
  183. withNameAndCode: newWithNameAndCode(req),
  184. }, nil
  185. }
  186. func parseMaintainer(req parseRequest) (*MaintainerCommand, error) {
  187. if len(req.args) != 1 {
  188. return nil, errExactlyOneArgument("MAINTAINER")
  189. }
  190. if err := req.flags.Parse(); err != nil {
  191. return nil, err
  192. }
  193. return &MaintainerCommand{
  194. Maintainer: req.args[0],
  195. withNameAndCode: newWithNameAndCode(req),
  196. }, nil
  197. }
  198. func parseLabel(req parseRequest) (*LabelCommand, error) {
  199. if err := req.flags.Parse(); err != nil {
  200. return nil, err
  201. }
  202. labels, err := parseKvps(req.args, "LABEL")
  203. if err != nil {
  204. return nil, err
  205. }
  206. return &LabelCommand{
  207. Labels: labels,
  208. withNameAndCode: newWithNameAndCode(req),
  209. }, nil
  210. }
  211. func parseAdd(req parseRequest) (*AddCommand, error) {
  212. if len(req.args) < 2 {
  213. return nil, errNoDestinationArgument("ADD")
  214. }
  215. flChown := req.flags.AddString("chown", "")
  216. flChmod := req.flags.AddString("chmod", "")
  217. if err := req.flags.Parse(); err != nil {
  218. return nil, err
  219. }
  220. return &AddCommand{
  221. SourcesAndDest: SourcesAndDest(req.args),
  222. withNameAndCode: newWithNameAndCode(req),
  223. Chown: flChown.Value,
  224. Chmod: flChmod.Value,
  225. }, nil
  226. }
  227. func parseCopy(req parseRequest) (*CopyCommand, error) {
  228. if len(req.args) < 2 {
  229. return nil, errNoDestinationArgument("COPY")
  230. }
  231. flChown := req.flags.AddString("chown", "")
  232. flFrom := req.flags.AddString("from", "")
  233. flChmod := req.flags.AddString("chmod", "")
  234. if err := req.flags.Parse(); err != nil {
  235. return nil, err
  236. }
  237. return &CopyCommand{
  238. SourcesAndDest: SourcesAndDest(req.args),
  239. From: flFrom.Value,
  240. withNameAndCode: newWithNameAndCode(req),
  241. Chown: flChown.Value,
  242. Chmod: flChmod.Value,
  243. }, nil
  244. }
  245. func parseFrom(req parseRequest) (*Stage, error) {
  246. stageName, err := parseBuildStageName(req.args)
  247. if err != nil {
  248. return nil, err
  249. }
  250. flPlatform := req.flags.AddString("platform", "")
  251. if err := req.flags.Parse(); err != nil {
  252. return nil, err
  253. }
  254. code := strings.TrimSpace(req.original)
  255. return &Stage{
  256. BaseName: req.args[0],
  257. Name: stageName,
  258. SourceCode: code,
  259. Commands: []Command{},
  260. Platform: flPlatform.Value,
  261. Location: req.location,
  262. }, nil
  263. }
  264. func parseBuildStageName(args []string) (string, error) {
  265. stageName := ""
  266. switch {
  267. case len(args) == 3 && strings.EqualFold(args[1], "as"):
  268. stageName = strings.ToLower(args[2])
  269. if ok, _ := regexp.MatchString("^[a-z][a-z0-9-_\\.]*$", stageName); !ok {
  270. return "", errors.Errorf("invalid name for build stage: %q, name can't start with a number or contain symbols", args[2])
  271. }
  272. case len(args) != 1:
  273. return "", errors.New("FROM requires either one or three arguments")
  274. }
  275. return stageName, nil
  276. }
  277. func parseOnBuild(req parseRequest) (*OnbuildCommand, error) {
  278. if len(req.args) == 0 {
  279. return nil, errAtLeastOneArgument("ONBUILD")
  280. }
  281. if err := req.flags.Parse(); err != nil {
  282. return nil, err
  283. }
  284. triggerInstruction := strings.ToUpper(strings.TrimSpace(req.args[0]))
  285. switch strings.ToUpper(triggerInstruction) {
  286. case "ONBUILD":
  287. return nil, errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
  288. case "MAINTAINER", "FROM":
  289. return nil, fmt.Errorf("%s isn't allowed as an ONBUILD trigger", triggerInstruction)
  290. }
  291. original := regexp.MustCompile(`(?i)^\s*ONBUILD\s*`).ReplaceAllString(req.original, "")
  292. return &OnbuildCommand{
  293. Expression: original,
  294. withNameAndCode: newWithNameAndCode(req),
  295. }, nil
  296. }
  297. func parseWorkdir(req parseRequest) (*WorkdirCommand, error) {
  298. if len(req.args) != 1 {
  299. return nil, errExactlyOneArgument("WORKDIR")
  300. }
  301. err := req.flags.Parse()
  302. if err != nil {
  303. return nil, err
  304. }
  305. return &WorkdirCommand{
  306. Path: req.args[0],
  307. withNameAndCode: newWithNameAndCode(req),
  308. }, nil
  309. }
  310. func parseShellDependentCommand(req parseRequest, emptyAsNil bool) ShellDependantCmdLine {
  311. args := handleJSONArgs(req.args, req.attributes)
  312. cmd := strslice.StrSlice(args)
  313. if emptyAsNil && len(cmd) == 0 {
  314. cmd = nil
  315. }
  316. return ShellDependantCmdLine{
  317. CmdLine: cmd,
  318. PrependShell: !req.attributes["json"],
  319. }
  320. }
  321. func parseRun(req parseRequest) (*RunCommand, error) {
  322. cmd := &RunCommand{}
  323. for _, fn := range parseRunPreHooks {
  324. if err := fn(cmd, req); err != nil {
  325. return nil, err
  326. }
  327. }
  328. if err := req.flags.Parse(); err != nil {
  329. return nil, err
  330. }
  331. cmd.ShellDependantCmdLine = parseShellDependentCommand(req, false)
  332. cmd.withNameAndCode = newWithNameAndCode(req)
  333. for _, fn := range parseRunPostHooks {
  334. if err := fn(cmd, req); err != nil {
  335. return nil, err
  336. }
  337. }
  338. return cmd, nil
  339. }
  340. func parseCmd(req parseRequest) (*CmdCommand, error) {
  341. if err := req.flags.Parse(); err != nil {
  342. return nil, err
  343. }
  344. return &CmdCommand{
  345. ShellDependantCmdLine: parseShellDependentCommand(req, false),
  346. withNameAndCode: newWithNameAndCode(req),
  347. }, nil
  348. }
  349. func parseEntrypoint(req parseRequest) (*EntrypointCommand, error) {
  350. if err := req.flags.Parse(); err != nil {
  351. return nil, err
  352. }
  353. cmd := &EntrypointCommand{
  354. ShellDependantCmdLine: parseShellDependentCommand(req, true),
  355. withNameAndCode: newWithNameAndCode(req),
  356. }
  357. return cmd, nil
  358. }
  359. // parseOptInterval(flag) is the duration of flag.Value, or 0 if
  360. // empty. An error is reported if the value is given and less than minimum duration.
  361. func parseOptInterval(f *Flag) (time.Duration, error) {
  362. s := f.Value
  363. if s == "" {
  364. return 0, nil
  365. }
  366. d, err := time.ParseDuration(s)
  367. if err != nil {
  368. return 0, err
  369. }
  370. if d < container.MinimumDuration {
  371. return 0, fmt.Errorf("Interval %#v cannot be less than %s", f.name, container.MinimumDuration)
  372. }
  373. return d, nil
  374. }
  375. func parseHealthcheck(req parseRequest) (*HealthCheckCommand, error) {
  376. if len(req.args) == 0 {
  377. return nil, errAtLeastOneArgument("HEALTHCHECK")
  378. }
  379. cmd := &HealthCheckCommand{
  380. withNameAndCode: newWithNameAndCode(req),
  381. }
  382. typ := strings.ToUpper(req.args[0])
  383. args := req.args[1:]
  384. if typ == "NONE" {
  385. if len(args) != 0 {
  386. return nil, errors.New("HEALTHCHECK NONE takes no arguments")
  387. }
  388. test := strslice.StrSlice{typ}
  389. cmd.Health = &container.HealthConfig{
  390. Test: test,
  391. }
  392. } else {
  393. healthcheck := container.HealthConfig{}
  394. flInterval := req.flags.AddString("interval", "")
  395. flTimeout := req.flags.AddString("timeout", "")
  396. flStartPeriod := req.flags.AddString("start-period", "")
  397. flRetries := req.flags.AddString("retries", "")
  398. if err := req.flags.Parse(); err != nil {
  399. return nil, err
  400. }
  401. switch typ {
  402. case "CMD":
  403. cmdSlice := handleJSONArgs(args, req.attributes)
  404. if len(cmdSlice) == 0 {
  405. return nil, errors.New("Missing command after HEALTHCHECK CMD")
  406. }
  407. if !req.attributes["json"] {
  408. typ = "CMD-SHELL"
  409. }
  410. healthcheck.Test = strslice.StrSlice(append([]string{typ}, cmdSlice...))
  411. default:
  412. return nil, fmt.Errorf("Unknown type %#v in HEALTHCHECK (try CMD)", typ)
  413. }
  414. interval, err := parseOptInterval(flInterval)
  415. if err != nil {
  416. return nil, err
  417. }
  418. healthcheck.Interval = interval
  419. timeout, err := parseOptInterval(flTimeout)
  420. if err != nil {
  421. return nil, err
  422. }
  423. healthcheck.Timeout = timeout
  424. startPeriod, err := parseOptInterval(flStartPeriod)
  425. if err != nil {
  426. return nil, err
  427. }
  428. healthcheck.StartPeriod = startPeriod
  429. if flRetries.Value != "" {
  430. retries, err := strconv.ParseInt(flRetries.Value, 10, 32)
  431. if err != nil {
  432. return nil, err
  433. }
  434. if retries < 1 {
  435. return nil, fmt.Errorf("--retries must be at least 1 (not %d)", retries)
  436. }
  437. healthcheck.Retries = int(retries)
  438. } else {
  439. healthcheck.Retries = 0
  440. }
  441. cmd.Health = &healthcheck
  442. }
  443. return cmd, nil
  444. }
  445. func parseExpose(req parseRequest) (*ExposeCommand, error) {
  446. portsTab := req.args
  447. if len(req.args) == 0 {
  448. return nil, errAtLeastOneArgument("EXPOSE")
  449. }
  450. if err := req.flags.Parse(); err != nil {
  451. return nil, err
  452. }
  453. sort.Strings(portsTab)
  454. return &ExposeCommand{
  455. Ports: portsTab,
  456. withNameAndCode: newWithNameAndCode(req),
  457. }, nil
  458. }
  459. func parseUser(req parseRequest) (*UserCommand, error) {
  460. if len(req.args) != 1 {
  461. return nil, errExactlyOneArgument("USER")
  462. }
  463. if err := req.flags.Parse(); err != nil {
  464. return nil, err
  465. }
  466. return &UserCommand{
  467. User: req.args[0],
  468. withNameAndCode: newWithNameAndCode(req),
  469. }, nil
  470. }
  471. func parseVolume(req parseRequest) (*VolumeCommand, error) {
  472. if len(req.args) == 0 {
  473. return nil, errAtLeastOneArgument("VOLUME")
  474. }
  475. if err := req.flags.Parse(); err != nil {
  476. return nil, err
  477. }
  478. cmd := &VolumeCommand{
  479. withNameAndCode: newWithNameAndCode(req),
  480. }
  481. for _, v := range req.args {
  482. v = strings.TrimSpace(v)
  483. if v == "" {
  484. return nil, errors.New("VOLUME specified can not be an empty string")
  485. }
  486. cmd.Volumes = append(cmd.Volumes, v)
  487. }
  488. return cmd, nil
  489. }
  490. func parseStopSignal(req parseRequest) (*StopSignalCommand, error) {
  491. if len(req.args) != 1 {
  492. return nil, errExactlyOneArgument("STOPSIGNAL")
  493. }
  494. sig := req.args[0]
  495. cmd := &StopSignalCommand{
  496. Signal: sig,
  497. withNameAndCode: newWithNameAndCode(req),
  498. }
  499. return cmd, nil
  500. }
  501. func parseArg(req parseRequest) (*ArgCommand, error) {
  502. if len(req.args) != 1 {
  503. return nil, errExactlyOneArgument("ARG")
  504. }
  505. kvpo := KeyValuePairOptional{}
  506. arg := req.args[0]
  507. // 'arg' can just be a name or name-value pair. Note that this is different
  508. // from 'env' that handles the split of name and value at the parser level.
  509. // The reason for doing it differently for 'arg' is that we support just
  510. // defining an arg and not assign it a value (while 'env' always expects a
  511. // name-value pair). If possible, it will be good to harmonize the two.
  512. if strings.Contains(arg, "=") {
  513. parts := strings.SplitN(arg, "=", 2)
  514. if len(parts[0]) == 0 {
  515. return nil, errBlankCommandNames("ARG")
  516. }
  517. kvpo.Key = parts[0]
  518. kvpo.Value = &parts[1]
  519. } else {
  520. kvpo.Key = arg
  521. }
  522. return &ArgCommand{
  523. KeyValuePairOptional: kvpo,
  524. withNameAndCode: newWithNameAndCode(req),
  525. }, nil
  526. }
  527. func parseShell(req parseRequest) (*ShellCommand, error) {
  528. if err := req.flags.Parse(); err != nil {
  529. return nil, err
  530. }
  531. shellSlice := handleJSONArgs(req.args, req.attributes)
  532. switch {
  533. case len(shellSlice) == 0:
  534. // SHELL []
  535. return nil, errAtLeastOneArgument("SHELL")
  536. case req.attributes["json"]:
  537. // SHELL ["powershell", "-command"]
  538. return &ShellCommand{
  539. Shell: strslice.StrSlice(shellSlice),
  540. withNameAndCode: newWithNameAndCode(req),
  541. }, nil
  542. default:
  543. // SHELL powershell -command - not JSON
  544. return nil, errNotJSON("SHELL", req.original)
  545. }
  546. }
  547. func errAtLeastOneArgument(command string) error {
  548. return errors.Errorf("%s requires at least one argument", command)
  549. }
  550. func errExactlyOneArgument(command string) error {
  551. return errors.Errorf("%s requires exactly one argument", command)
  552. }
  553. func errNoDestinationArgument(command string) error {
  554. return errors.Errorf("%s requires at least two arguments, but only one was provided. Destination could not be determined.", command)
  555. }
  556. func errBlankCommandNames(command string) error {
  557. return errors.Errorf("%s names can not be blank", command)
  558. }
  559. func errTooManyArguments(command string) error {
  560. return errors.Errorf("Bad input to %s, too many arguments", command)
  561. }