builder.go 5.7 KB

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