builder.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "github.com/docker/docker/api/types"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/image"
  12. "github.com/docker/docker/runconfig"
  13. )
  14. // Builder abstracts a Docker builder whose only purpose is to build a Docker image referenced by an imageID.
  15. type Builder interface {
  16. // Build builds a Docker image referenced by an imageID string.
  17. //
  18. // Note: Tagging an image should not be done by a Builder, it should instead be done
  19. // by the caller.
  20. //
  21. // TODO: make this return a reference instead of string
  22. Build() (imageID string)
  23. }
  24. // Context represents a file system tree.
  25. type Context interface {
  26. // Close allows to signal that the filesystem tree won't be used anymore.
  27. // For Context implementations using a temporary directory, it is recommended to
  28. // delete the temporary directory in Close().
  29. Close() error
  30. // Stat returns an entry corresponding to path if any.
  31. // It is recommended to return an error if path was not found.
  32. // If path is a symlink it also returns the path to the target file.
  33. Stat(path string) (string, FileInfo, error)
  34. // Open opens path from the context and returns a readable stream of it.
  35. Open(path string) (io.ReadCloser, error)
  36. // Walk walks the tree of the context with the function passed to it.
  37. Walk(root string, walkFn WalkFunc) error
  38. }
  39. // WalkFunc is the type of the function called for each file or directory visited by Context.Walk().
  40. type WalkFunc func(path string, fi FileInfo, err error) error
  41. // ModifiableContext represents a modifiable Context.
  42. // TODO: remove this interface once we can get rid of Remove()
  43. type ModifiableContext interface {
  44. Context
  45. // Remove deletes the entry specified by `path`.
  46. // It is usual for directory entries to delete all its subentries.
  47. Remove(path string) error
  48. }
  49. // FileInfo extends os.FileInfo to allow retrieving an absolute path to the file.
  50. // TODO: remove this interface once pkg/archive exposes a walk function that Context can use.
  51. type FileInfo interface {
  52. os.FileInfo
  53. Path() string
  54. }
  55. // PathFileInfo is a convenience struct that implements the FileInfo interface.
  56. type PathFileInfo struct {
  57. os.FileInfo
  58. // FilePath holds the absolute path to the file.
  59. FilePath string
  60. // Name holds the basename for the file.
  61. FileName string
  62. }
  63. // Path returns the absolute path to the file.
  64. func (fi PathFileInfo) Path() string {
  65. return fi.FilePath
  66. }
  67. // Name returns the basename of the file.
  68. func (fi PathFileInfo) Name() string {
  69. if fi.FileName != "" {
  70. return fi.FileName
  71. }
  72. return fi.FileInfo.Name()
  73. }
  74. // Hashed defines an extra method intended for implementations of os.FileInfo.
  75. type Hashed interface {
  76. // Hash returns the hash of a file.
  77. Hash() string
  78. SetHash(string)
  79. }
  80. // HashedFileInfo is a convenient struct that augments FileInfo with a field.
  81. type HashedFileInfo struct {
  82. FileInfo
  83. // FileHash represents the hash of a file.
  84. FileHash string
  85. }
  86. // Hash returns the hash of a file.
  87. func (fi HashedFileInfo) Hash() string {
  88. return fi.FileHash
  89. }
  90. // SetHash sets the hash of a file.
  91. func (fi *HashedFileInfo) SetHash(h string) {
  92. fi.FileHash = h
  93. }
  94. // Docker abstracts calls to a Docker Daemon.
  95. type Docker interface {
  96. // TODO: use digest reference instead of name
  97. // LookupImage looks up a Docker image referenced by `name`.
  98. LookupImage(name string) (*image.Image, error)
  99. // Pull tells Docker to pull image referenced by `name`.
  100. Pull(name string) (*image.Image, error)
  101. // Container looks up a Docker container referenced by `id`.
  102. Container(id string) (*container.Container, error)
  103. // Create creates a new Docker container and returns potential warnings
  104. // TODO: put warnings in the error
  105. Create(*runconfig.Config, *runconfig.HostConfig) (*container.Container, []string, error)
  106. // Remove removes a container specified by `id`.
  107. Remove(id string, cfg *types.ContainerRmConfig) error
  108. // Commit creates a new Docker image from an existing Docker container.
  109. Commit(string, *types.ContainerCommitConfig) (string, error)
  110. // Copy copies/extracts a source FileInfo to a destination path inside a container
  111. // specified by a container object.
  112. // TODO: make an Extract method instead of passing `decompress`
  113. // TODO: do not pass a FileInfo, instead refactor the archive package to export a Walk function that can be used
  114. // with Context.Walk
  115. Copy(c *container.Container, destPath string, src FileInfo, decompress bool) error
  116. // Retain retains an image avoiding it to be removed or overwritten until a corresponding Release() call.
  117. // TODO: remove
  118. Retain(sessionID, imgID string)
  119. // Release releases a list of images that were retained for the time of a build.
  120. // TODO: remove
  121. Release(sessionID string, activeImages []string)
  122. // Kill stops the container execution abruptly.
  123. Kill(c *container.Container) error
  124. // Mount mounts the root filesystem for the container.
  125. Mount(c *container.Container) error
  126. // Unmount unmounts the root filesystem for the container.
  127. Unmount(c *container.Container) error
  128. // Start starts a new container
  129. Start(c *container.Container) error
  130. }
  131. // ImageCache abstracts an image cache store.
  132. // (parent image, child runconfig) -> child image
  133. type ImageCache interface {
  134. // GetCachedImage returns a reference to a cached image whose parent equals `parent`
  135. // and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
  136. GetCachedImage(parentID string, cfg *runconfig.Config) (imageID string, err error)
  137. }