detect.go 5.2 KB

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