builder.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Package builder defines interfaces for any Docker builder to implement.
  2. //
  3. // Historically, only server-side Dockerfile interpreters existed.
  4. // This package allows for other implementations of Docker builders.
  5. package builder
  6. import (
  7. "io"
  8. "os"
  9. "time"
  10. "github.com/docker/docker/reference"
  11. "github.com/docker/engine-api/types"
  12. "github.com/docker/engine-api/types/container"
  13. "golang.org/x/net/context"
  14. )
  15. const (
  16. // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
  17. DefaultDockerfileName string = "Dockerfile"
  18. )
  19. // Context represents a file system tree.
  20. type Context interface {
  21. // Close allows to signal that the filesystem tree won't be used anymore.
  22. // For Context implementations using a temporary directory, it is recommended to
  23. // delete the temporary directory in Close().
  24. Close() error
  25. // Stat returns an entry corresponding to path if any.
  26. // It is recommended to return an error if path was not found.
  27. // If path is a symlink it also returns the path to the target file.
  28. Stat(path string) (string, FileInfo, error)
  29. // Open opens path from the context and returns a readable stream of it.
  30. Open(path string) (io.ReadCloser, error)
  31. // Walk walks the tree of the context with the function passed to it.
  32. Walk(root string, walkFn WalkFunc) error
  33. }
  34. // WalkFunc is the type of the function called for each file or directory visited by Context.Walk().
  35. type WalkFunc func(path string, fi FileInfo, err error) error
  36. // ModifiableContext represents a modifiable Context.
  37. // TODO: remove this interface once we can get rid of Remove()
  38. type ModifiableContext interface {
  39. Context
  40. // Remove deletes the entry specified by `path`.
  41. // It is usual for directory entries to delete all its subentries.
  42. Remove(path string) error
  43. }
  44. // FileInfo extends os.FileInfo to allow retrieving an absolute path to the file.
  45. // TODO: remove this interface once pkg/archive exposes a walk function that Context can use.
  46. type FileInfo interface {
  47. os.FileInfo
  48. Path() string
  49. }
  50. // PathFileInfo is a convenience struct that implements the FileInfo interface.
  51. type PathFileInfo struct {
  52. os.FileInfo
  53. // FilePath holds the absolute path to the file.
  54. FilePath string
  55. // Name holds the basename for the file.
  56. FileName string
  57. }
  58. // Path returns the absolute path to the file.
  59. func (fi PathFileInfo) Path() string {
  60. return fi.FilePath
  61. }
  62. // Name returns the basename of the file.
  63. func (fi PathFileInfo) Name() string {
  64. if fi.FileName != "" {
  65. return fi.FileName
  66. }
  67. return fi.FileInfo.Name()
  68. }
  69. // Hashed defines an extra method intended for implementations of os.FileInfo.
  70. type Hashed interface {
  71. // Hash returns the hash of a file.
  72. Hash() string
  73. SetHash(string)
  74. }
  75. // HashedFileInfo is a convenient struct that augments FileInfo with a field.
  76. type HashedFileInfo struct {
  77. FileInfo
  78. // FileHash represents the hash of a file.
  79. FileHash string
  80. }
  81. // Hash returns the hash of a file.
  82. func (fi HashedFileInfo) Hash() string {
  83. return fi.FileHash
  84. }
  85. // SetHash sets the hash of a file.
  86. func (fi *HashedFileInfo) SetHash(h string) {
  87. fi.FileHash = h
  88. }
  89. // Backend abstracts calls to a Docker Daemon.
  90. type Backend interface {
  91. // TODO: use digest reference instead of name
  92. // GetImage looks up a Docker image referenced by `name`.
  93. GetImageOnBuild(name string) (Image, error)
  94. // Tag an image with newTag
  95. TagImage(newTag reference.Named, imageName string) error
  96. // Pull tells Docker to pull image referenced by `name`.
  97. PullOnBuild(ctx context.Context, name string, authConfigs map[string]types.AuthConfig, output io.Writer) (Image, error)
  98. // ContainerAttach attaches to container.
  99. ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error
  100. // ContainerCreate creates a new Docker container and returns potential warnings
  101. ContainerCreate(types.ContainerCreateConfig) (types.ContainerCreateResponse, error)
  102. // ContainerRm removes a container specified by `id`.
  103. ContainerRm(name string, config *types.ContainerRmConfig) error
  104. // Commit creates a new Docker image from an existing Docker container.
  105. Commit(string, *types.ContainerCommitConfig) (string, error)
  106. // Kill stops the container execution abruptly.
  107. ContainerKill(containerID string, sig uint64) error
  108. // Start starts a new container
  109. ContainerStart(containerID string, hostConfig *container.HostConfig) error
  110. // ContainerWait stops processing until the given container is stopped.
  111. ContainerWait(containerID string, timeout time.Duration) (int, error)
  112. // ContainerUpdateCmd updates container.Path and container.Args
  113. ContainerUpdateCmdOnBuild(containerID string, cmd []string) error
  114. // ContainerCopy copies/extracts a source FileInfo to a destination path inside a container
  115. // specified by a container object.
  116. // TODO: make an Extract method instead of passing `decompress`
  117. // TODO: do not pass a FileInfo, instead refactor the archive package to export a Walk function that can be used
  118. // with Context.Walk
  119. //ContainerCopy(name string, res string) (io.ReadCloser, error)
  120. // TODO: use copyBackend api
  121. CopyOnBuild(containerID string, destPath string, src FileInfo, decompress bool) error
  122. }
  123. // Image represents a Docker image used by the builder.
  124. type Image interface {
  125. ImageID() string
  126. RunConfig() *container.Config
  127. }
  128. // ImageCache abstracts an image cache store.
  129. // (parent image, child runconfig) -> child image
  130. type ImageCache interface {
  131. // GetCachedImageOnBuild returns a reference to a cached image whose parent equals `parent`
  132. // and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
  133. GetCachedImageOnBuild(parentID string, cfg *container.Config) (imageID string, err error)
  134. }