detect.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package remotecontext // import "github.com/docker/docker/builder/remotecontext"
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "runtime"
  8. "strings"
  9. "github.com/containerd/continuity/driver"
  10. "github.com/docker/docker/api/types/backend"
  11. "github.com/docker/docker/builder"
  12. "github.com/docker/docker/builder/remotecontext/urlutil"
  13. "github.com/docker/docker/errdefs"
  14. "github.com/docker/docker/pkg/containerfs"
  15. "github.com/moby/buildkit/frontend/dockerfile/dockerignore"
  16. "github.com/moby/buildkit/frontend/dockerfile/parser"
  17. "github.com/moby/patternmatcher"
  18. "github.com/pkg/errors"
  19. "github.com/sirupsen/logrus"
  20. )
  21. // ClientSessionRemote is identifier for client-session context transport
  22. const ClientSessionRemote = "client-session"
  23. // Detect returns a context and dockerfile from remote location or local
  24. // archive.
  25. func Detect(config backend.BuildConfig) (remote builder.Source, dockerfile *parser.Result, err error) {
  26. remoteURL := config.Options.RemoteContext
  27. dockerfilePath := config.Options.Dockerfile
  28. switch {
  29. case remoteURL == "":
  30. remote, dockerfile, err = newArchiveRemote(config.Source, dockerfilePath)
  31. case remoteURL == ClientSessionRemote:
  32. return nil, nil, errdefs.InvalidParameter(errors.New("experimental session with v1 builder is no longer supported, use builder version v2 (BuildKit) instead"))
  33. case urlutil.IsGitURL(remoteURL):
  34. remote, dockerfile, err = newGitRemote(remoteURL, dockerfilePath)
  35. case urlutil.IsURL(remoteURL):
  36. remote, dockerfile, err = newURLRemote(remoteURL, dockerfilePath, config.ProgressWriter.ProgressReaderFunc)
  37. default:
  38. err = fmt.Errorf("remoteURL (%s) could not be recognized as URL", remoteURL)
  39. }
  40. return
  41. }
  42. func newArchiveRemote(rc io.ReadCloser, dockerfilePath string) (builder.Source, *parser.Result, error) {
  43. defer rc.Close()
  44. c, err := FromArchive(rc)
  45. if err != nil {
  46. return nil, nil, err
  47. }
  48. return withDockerfileFromContext(c.(modifiableContext), dockerfilePath)
  49. }
  50. func withDockerfileFromContext(c modifiableContext, dockerfilePath string) (builder.Source, *parser.Result, error) {
  51. df, err := openAt(c, dockerfilePath)
  52. if err != nil {
  53. if errors.Is(err, os.ErrNotExist) {
  54. if dockerfilePath == builder.DefaultDockerfileName {
  55. lowercase := strings.ToLower(dockerfilePath)
  56. if _, err := StatAt(c, lowercase); err == nil {
  57. return withDockerfileFromContext(c, lowercase)
  58. }
  59. }
  60. return nil, nil, errors.Errorf("Cannot locate specified Dockerfile: %s", dockerfilePath) // backwards compatible error
  61. }
  62. c.Close()
  63. return nil, nil, err
  64. }
  65. res, err := readAndParseDockerfile(dockerfilePath, df)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. df.Close()
  70. if err := removeDockerfile(c, dockerfilePath); err != nil {
  71. c.Close()
  72. return nil, nil, err
  73. }
  74. return c, res, nil
  75. }
  76. func newGitRemote(gitURL string, dockerfilePath string) (builder.Source, *parser.Result, error) {
  77. c, err := MakeGitContext(gitURL) // TODO: change this to NewLazySource
  78. if err != nil {
  79. return nil, nil, err
  80. }
  81. return withDockerfileFromContext(c.(modifiableContext), dockerfilePath)
  82. }
  83. func newURLRemote(url string, dockerfilePath string, progressReader func(in io.ReadCloser) io.ReadCloser) (builder.Source, *parser.Result, error) {
  84. contentType, content, err := downloadRemote(url)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. defer content.Close()
  89. switch contentType {
  90. case mimeTypeTextPlain:
  91. res, err := parser.Parse(progressReader(content))
  92. return nil, res, errdefs.InvalidParameter(err)
  93. default:
  94. source, err := FromArchive(progressReader(content))
  95. if err != nil {
  96. return nil, nil, err
  97. }
  98. return withDockerfileFromContext(source.(modifiableContext), dockerfilePath)
  99. }
  100. }
  101. func removeDockerfile(c modifiableContext, filesToRemove ...string) error {
  102. f, err := openAt(c, ".dockerignore")
  103. // Note that a missing .dockerignore file isn't treated as an error
  104. switch {
  105. case os.IsNotExist(err):
  106. return nil
  107. case err != nil:
  108. return err
  109. }
  110. excludes, err := dockerignore.ReadAll(f)
  111. if err != nil {
  112. f.Close()
  113. return err
  114. }
  115. f.Close()
  116. filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
  117. for _, fileToRemove := range filesToRemove {
  118. if rm, _ := patternmatcher.MatchesOrParentMatches(fileToRemove, excludes); rm {
  119. if err := c.Remove(fileToRemove); err != nil {
  120. logrus.Errorf("failed to remove %s: %v", fileToRemove, err)
  121. }
  122. }
  123. }
  124. return nil
  125. }
  126. func readAndParseDockerfile(name string, rc io.Reader) (*parser.Result, error) {
  127. br := bufio.NewReader(rc)
  128. if _, err := br.Peek(1); err != nil {
  129. if err == io.EOF {
  130. return nil, errdefs.InvalidParameter(errors.Errorf("the Dockerfile (%s) cannot be empty", name))
  131. }
  132. return nil, errors.Wrap(err, "unexpected error reading Dockerfile")
  133. }
  134. dockerfile, err := parser.Parse(br)
  135. if err != nil {
  136. return nil, errdefs.InvalidParameter(errors.Wrapf(err, "failed to parse %s", name))
  137. }
  138. return dockerfile, nil
  139. }
  140. func openAt(remote builder.Source, path string) (driver.File, error) {
  141. fullPath, err := FullPath(remote, path)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return os.Open(fullPath)
  146. }
  147. // StatAt is a helper for calling Stat on a path from a source
  148. func StatAt(remote builder.Source, path string) (os.FileInfo, error) {
  149. fullPath, err := FullPath(remote, path)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return os.Stat(fullPath)
  154. }
  155. // FullPath is a helper for getting a full path for a path from a source
  156. func FullPath(remote builder.Source, path string) (string, error) {
  157. fullPath, err := containerfs.ResolveScopedPath(remote.Root(), path)
  158. if err != nil {
  159. if runtime.GOOS == "windows" {
  160. return "", fmt.Errorf("failed to resolve scoped path %s (%s): %s. Possible cause is a forbidden path outside the build context", path, fullPath, err)
  161. }
  162. return "", fmt.Errorf("forbidden path outside the build context: %s (%s)", path, fullPath) // backwards compat with old error
  163. }
  164. return fullPath, nil
  165. }