builder.go 5.1 KB

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