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