detect.go 5.7 KB

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