builder.go 5.7 KB

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