builder.go 5.1 KB

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