builder.go 5.4 KB

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