github.com/skf/moby@v1.13.1/builder/builder.go (about)

     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  
     7  import (
     8  	"io"
     9  	"os"
    10  	"time"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/api/types/backend"
    14  	"github.com/docker/docker/api/types/container"
    15  	"github.com/docker/docker/image"
    16  	"github.com/docker/docker/reference"
    17  	"golang.org/x/net/context"
    18  )
    19  
    20  const (
    21  	// DefaultDockerfileName is the Default filename with Docker commands, read by docker build
    22  	DefaultDockerfileName string = "Dockerfile"
    23  )
    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  
    41  // WalkFunc is the type of the function called for each file or directory visited by Context.Walk().
    42  type WalkFunc func(path string, fi FileInfo, err error) error
    43  
    44  // ModifiableContext represents a modifiable Context.
    45  // TODO: remove this interface once we can get rid of Remove()
    46  type ModifiableContext interface {
    47  	Context
    48  	// Remove deletes the entry specified by `path`.
    49  	// It is usual for directory entries to delete all its subentries.
    50  	Remove(path string) error
    51  }
    52  
    53  // FileInfo extends os.FileInfo to allow retrieving an absolute path to the file.
    54  // TODO: remove this interface once pkg/archive exposes a walk function that Context can use.
    55  type FileInfo interface {
    56  	os.FileInfo
    57  	Path() string
    58  }
    59  
    60  // PathFileInfo is a convenience struct that implements the FileInfo interface.
    61  type PathFileInfo struct {
    62  	os.FileInfo
    63  	// FilePath holds the absolute path to the file.
    64  	FilePath string
    65  	// Name holds the basename for the file.
    66  	FileName string
    67  }
    68  
    69  // Path returns the absolute path to the file.
    70  func (fi PathFileInfo) Path() string {
    71  	return fi.FilePath
    72  }
    73  
    74  // Name returns the basename of the file.
    75  func (fi PathFileInfo) Name() string {
    76  	if fi.FileName != "" {
    77  		return fi.FileName
    78  	}
    79  	return fi.FileInfo.Name()
    80  }
    81  
    82  // Hashed defines an extra method intended for implementations of os.FileInfo.
    83  type Hashed interface {
    84  	// Hash returns the hash of a file.
    85  	Hash() string
    86  	SetHash(string)
    87  }
    88  
    89  // HashedFileInfo is a convenient struct that augments FileInfo with a field.
    90  type HashedFileInfo struct {
    91  	FileInfo
    92  	// FileHash represents the hash of a file.
    93  	FileHash string
    94  }
    95  
    96  // Hash returns the hash of a file.
    97  func (fi HashedFileInfo) Hash() string {
    98  	return fi.FileHash
    99  }
   100  
   101  // SetHash sets the hash of a file.
   102  func (fi *HashedFileInfo) SetHash(h string) {
   103  	fi.FileHash = h
   104  }
   105  
   106  // Backend abstracts calls to a Docker Daemon.
   107  type Backend interface {
   108  	// TODO: use digest reference instead of name
   109  
   110  	// GetImageOnBuild looks up a Docker image referenced by `name`.
   111  	GetImageOnBuild(name string) (Image, error)
   112  	// TagImage tags an image with newTag
   113  	TagImageWithReference(image.ID, reference.Named) error
   114  	// PullOnBuild tells Docker to pull image referenced by `name`.
   115  	PullOnBuild(ctx context.Context, name string, authConfigs map[string]types.AuthConfig, output io.Writer) (Image, error)
   116  	// ContainerAttachRaw attaches to container.
   117  	ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error
   118  	// ContainerCreate creates a new Docker container and returns potential warnings
   119  	ContainerCreate(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error)
   120  	// ContainerRm removes a container specified by `id`.
   121  	ContainerRm(name string, config *types.ContainerRmConfig) error
   122  	// Commit creates a new Docker image from an existing Docker container.
   123  	Commit(string, *backend.ContainerCommitConfig) (string, error)
   124  	// ContainerKill stops the container execution abruptly.
   125  	ContainerKill(containerID string, sig uint64) error
   126  	// ContainerStart starts a new container
   127  	ContainerStart(containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
   128  	// ContainerWait stops processing until the given container is stopped.
   129  	ContainerWait(containerID string, timeout time.Duration) (int, error)
   130  	// ContainerUpdateCmdOnBuild updates container.Path and container.Args
   131  	ContainerUpdateCmdOnBuild(containerID string, cmd []string) error
   132  	// ContainerCreateWorkdir creates the workdir (currently only used on Windows)
   133  	ContainerCreateWorkdir(containerID string) error
   134  
   135  	// ContainerCopy copies/extracts a source FileInfo to a destination path inside a container
   136  	// specified by a container object.
   137  	// TODO: make an Extract method instead of passing `decompress`
   138  	// TODO: do not pass a FileInfo, instead refactor the archive package to export a Walk function that can be used
   139  	// with Context.Walk
   140  	// ContainerCopy(name string, res string) (io.ReadCloser, error)
   141  	// TODO: use copyBackend api
   142  	CopyOnBuild(containerID string, destPath string, src FileInfo, decompress bool) error
   143  
   144  	// HasExperimental checks if the backend supports experimental features
   145  	HasExperimental() bool
   146  
   147  	// SquashImage squashes the fs layers from the provided image down to the specified `to` image
   148  	SquashImage(from string, to string) (string, error)
   149  }
   150  
   151  // Image represents a Docker image used by the builder.
   152  type Image interface {
   153  	ImageID() string
   154  	RunConfig() *container.Config
   155  }
   156  
   157  // ImageCacheBuilder represents a generator for stateful image cache.
   158  type ImageCacheBuilder interface {
   159  	// MakeImageCache creates a stateful image cache.
   160  	MakeImageCache(cacheFrom []string) ImageCache
   161  }
   162  
   163  // ImageCache abstracts an image cache.
   164  // (parent image, child runconfig) -> child image
   165  type ImageCache interface {
   166  	// GetCachedImageOnBuild returns a reference to a cached image whose parent equals `parent`
   167  	// and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
   168  	GetCache(parentID string, cfg *container.Config) (imageID string, err error)
   169  }