builder.go 5.3 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. // TODO: remove dependency on daemon
  10. "github.com/docker/docker/daemon"
  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. Stat(path string) (FileInfo, error)
  33. // Open opens path from the context and returns a readable stream of it.
  34. Open(path string) (io.ReadCloser, error)
  35. // Walk walks the tree of the context with the function passed to it.
  36. Walk(root string, walkFn WalkFunc) error
  37. }
  38. // WalkFunc is the type of the function called for each file or directory visited by Context.Walk().
  39. type WalkFunc func(path string, fi FileInfo, err error) error
  40. // ModifiableContext represents a modifiable Context.
  41. // TODO: remove this interface once we can get rid of Remove()
  42. type ModifiableContext interface {
  43. Context
  44. // Remove deletes the entry specified by `path`.
  45. // It is usual for directory entries to delete all its subentries.
  46. Remove(path string) error
  47. }
  48. // FileInfo extends os.FileInfo to allow retrieving an absolute path to the file.
  49. // TODO: remove this interface once pkg/archive exposes a walk function that Context can use.
  50. type FileInfo interface {
  51. os.FileInfo
  52. Path() string
  53. }
  54. // PathFileInfo is a convenience struct that implements the FileInfo interface.
  55. type PathFileInfo struct {
  56. os.FileInfo
  57. // FilePath holds the absolute path to the file.
  58. FilePath string
  59. }
  60. // Path returns the absolute path to the file.
  61. func (fi PathFileInfo) Path() string {
  62. return fi.FilePath
  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. // Docker abstracts calls to a Docker Daemon.
  85. type Docker interface {
  86. // TODO: use digest reference instead of name
  87. // LookupImage looks up a Docker image referenced by `name`.
  88. LookupImage(name string) (*image.Image, error)
  89. // Pull tells Docker to pull image referenced by `name`.
  90. Pull(name string) (*image.Image, error)
  91. // TODO: move daemon.Container to its own package
  92. // Container looks up a Docker container referenced by `id`.
  93. Container(id string) (*daemon.Container, error)
  94. // Create creates a new Docker container and returns potential warnings
  95. // TODO: put warnings in the error
  96. Create(*runconfig.Config, *runconfig.HostConfig) (*daemon.Container, []string, error)
  97. // Remove removes a container specified by `id`.
  98. Remove(id string, cfg *daemon.ContainerRmConfig) error
  99. // Commit creates a new Docker image from an existing Docker container.
  100. Commit(string, *daemon.ContainerCommitConfig) (*image.Image, error)
  101. // Copy copies/extracts a source FileInfo to a destination path inside a container
  102. // specified by a container object.
  103. // TODO: make an Extract method instead of passing `decompress`
  104. // TODO: do not pass a FileInfo, instead refactor the archive package to export a Walk function that can be used
  105. // with Context.Walk
  106. Copy(c *daemon.Container, destPath string, src FileInfo, decompress bool) error
  107. // Retain retains an image avoiding it to be removed or overwritten until a corresponding Release() call.
  108. // TODO: remove
  109. Retain(sessionID, imgID string)
  110. // Release releases a list of images that were retained for the time of a build.
  111. // TODO: remove
  112. Release(sessionID string, activeImages []string)
  113. // Kill stops the container execution abruptly.
  114. Kill(c *daemon.Container) error
  115. // Mount mounts the root filesystem for the container.
  116. Mount(c *daemon.Container) error
  117. // Unmount unmounts the root filesystem for the container.
  118. Unmount(c *daemon.Container) error
  119. // Start starts a new container
  120. Start(c *daemon.Container) error
  121. }
  122. // ImageCache abstracts an image cache store.
  123. // (parent image, child runconfig) -> child image
  124. type ImageCache interface {
  125. // GetCachedImage returns a reference to a cached image whose parent equals `parent`
  126. // and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
  127. GetCachedImage(parentID string, cfg *runconfig.Config) (imageID string, err error)
  128. }