2015-09-06 13:26:40 -04:00
|
|
|
package daemonbuilder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/api"
|
2015-12-04 15:34:43 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/cliconfig"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-10-14 14:35:48 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Docker implements builder.Docker for the docker Daemon object.
|
|
|
|
type Docker struct {
|
|
|
|
Daemon *daemon.Daemon
|
|
|
|
OutOld io.Writer
|
|
|
|
AuthConfigs map[string]cliconfig.AuthConfig
|
2015-10-08 11:51:41 -04:00
|
|
|
Archiver *archive.Archiver
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure Docker implements builder.Docker
|
|
|
|
var _ builder.Docker = Docker{}
|
|
|
|
|
|
|
|
// LookupImage looks up a Docker image referenced by `name`.
|
|
|
|
func (d Docker) LookupImage(name string) (*image.Image, error) {
|
2015-10-01 17:51:36 -04:00
|
|
|
return d.Daemon.GetImage(name)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull tells Docker to pull image referenced by `name`.
|
|
|
|
func (d Docker) Pull(name string) (*image.Image, error) {
|
2015-11-18 17:20:54 -05:00
|
|
|
ref, err := reference.ParseNamed(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch ref.(type) {
|
|
|
|
case reference.Tagged:
|
|
|
|
case reference.Digested:
|
|
|
|
default:
|
|
|
|
ref, err = reference.WithTag(ref, "latest")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pullRegistryAuth := &cliconfig.AuthConfig{}
|
|
|
|
if len(d.AuthConfigs) > 0 {
|
|
|
|
// The request came with a full auth config file, we prefer to use that
|
2015-11-18 17:20:54 -05:00
|
|
|
repoInfo, err := d.Daemon.RegistryService.ResolveRepository(ref)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvedConfig := registry.ResolveAuthConfig(
|
|
|
|
&cliconfig.ConfigFile{AuthConfigs: d.AuthConfigs},
|
|
|
|
repoInfo.Index,
|
|
|
|
)
|
|
|
|
pullRegistryAuth = &resolvedConfig
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
if err := d.Daemon.PullImage(ref, nil, pullRegistryAuth, ioutils.NopWriteCloser(d.OutOld)); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-01 17:51:36 -04:00
|
|
|
return d.Daemon.GetImage(name)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Container looks up a Docker container referenced by `id`.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Container(id string) (*container.Container, error) {
|
2015-12-11 12:39:28 -05:00
|
|
|
return d.Daemon.GetContainer(id)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new Docker container and returns potential warnings
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Create(cfg *runconfig.Config, hostCfg *runconfig.HostConfig) (*container.Container, []string, error) {
|
2015-09-09 22:23:06 -04:00
|
|
|
ccr, err := d.Daemon.ContainerCreate(&daemon.ContainerCreateConfig{
|
|
|
|
Name: "",
|
|
|
|
Config: cfg,
|
|
|
|
HostConfig: hostCfg,
|
|
|
|
AdjustCPUShares: true,
|
|
|
|
})
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := d.Container(ccr.ID)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, ccr.Warnings, err
|
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
|
|
|
|
return container, ccr.Warnings, d.Mount(container)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes a container specified by `id`.
|
2015-12-04 15:34:43 -05:00
|
|
|
func (d Docker) Remove(id string, cfg *types.ContainerRmConfig) error {
|
2015-09-06 13:26:40 -04:00
|
|
|
return d.Daemon.ContainerRm(id, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit creates a new Docker image from an existing Docker container.
|
2015-12-04 15:34:43 -05:00
|
|
|
func (d Docker) Commit(name string, cfg *types.ContainerCommitConfig) (string, error) {
|
2015-11-06 13:05:00 -05:00
|
|
|
return d.Daemon.Commit(name, cfg)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retain retains an image avoiding it to be removed or overwritten until a corresponding Release() call.
|
|
|
|
func (d Docker) Retain(sessionID, imgID string) {
|
2015-11-18 17:20:54 -05:00
|
|
|
// FIXME: This will be solved with tags in client-side builder
|
|
|
|
//d.Daemon.Graph().Retain(sessionID, imgID)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release releases a list of images that were retained for the time of a build.
|
|
|
|
func (d Docker) Release(sessionID string, activeImages []string) {
|
2015-11-18 17:20:54 -05:00
|
|
|
// FIXME: This will be solved with tags in client-side builder
|
|
|
|
//d.Daemon.Graph().Release(sessionID, activeImages...)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies/extracts a source FileInfo to a destination path inside a container
|
|
|
|
// specified by a container object.
|
|
|
|
// TODO: make sure callers don't unnecessarily convert destPath with filepath.FromSlash (Copy does it already).
|
|
|
|
// Copy should take in abstract paths (with slashes) and the implementation should convert it to OS-specific paths.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Copy(c *container.Container, destPath string, src builder.FileInfo, decompress bool) error {
|
2015-09-06 13:26:40 -04:00
|
|
|
srcPath := src.Path()
|
|
|
|
destExists := true
|
2015-10-08 11:51:41 -04:00
|
|
|
rootUID, rootGID := d.Daemon.GetRemappedUIDGID()
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
// Work in daemon-local OS specific file paths
|
|
|
|
destPath = filepath.FromSlash(destPath)
|
|
|
|
|
|
|
|
dest, err := c.GetResourcePath(destPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve the trailing slash
|
|
|
|
// TODO: why are we appending another path separator if there was already one?
|
|
|
|
if strings.HasSuffix(destPath, string(os.PathSeparator)) || destPath == "." {
|
|
|
|
dest += string(os.PathSeparator)
|
|
|
|
}
|
|
|
|
|
|
|
|
destPath = dest
|
|
|
|
|
|
|
|
destStat, err := os.Stat(destPath)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
logrus.Errorf("Error performing os.Stat on %s. %s", destPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destExists = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if src.IsDir() {
|
|
|
|
// copy as directory
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := d.Archiver.CopyWithTar(srcPath, destPath); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2015-11-20 11:49:33 -05:00
|
|
|
if decompress && archive.IsArchivePath(srcPath) {
|
2015-09-06 13:26:40 -04:00
|
|
|
// Only try to untar if it is a file and that we've been told to decompress (when ADD-ing a remote file)
|
|
|
|
|
|
|
|
// First try to unpack the source as an archive
|
|
|
|
// to support the untar feature we need to clean up the path a little bit
|
|
|
|
// because tar is very forgiving. First we need to strip off the archive's
|
|
|
|
// filename from the path but this is only added if it does not end in slash
|
|
|
|
tarDest := destPath
|
|
|
|
if strings.HasSuffix(tarDest, string(os.PathSeparator)) {
|
|
|
|
tarDest = filepath.Dir(destPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to successfully untar the orig
|
2015-11-20 11:49:33 -05:00
|
|
|
err := d.Archiver.UntarPath(srcPath, tarDest)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Couldn't untar to %s: %v", tarDest, err)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2015-11-20 11:49:33 -05:00
|
|
|
return err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// only needed for fixPermissions, but might as well put it before CopyFileWithTar
|
|
|
|
if destExists && destStat.IsDir() {
|
2015-11-04 16:42:08 -05:00
|
|
|
destPath = filepath.Join(destPath, src.Name())
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2015-10-14 14:35:48 -04:00
|
|
|
if err := idtools.MkdirAllNewAs(filepath.Dir(destPath), 0755, rootUID, rootGID); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
if err := d.Archiver.CopyFileWithTar(srcPath, destPath); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCachedImage returns a reference to a cached image whose parent equals `parent`
|
|
|
|
// and runconfig equals `cfg`. A cache miss is expected to return an empty ID and a nil error.
|
|
|
|
func (d Docker) GetCachedImage(imgID string, cfg *runconfig.Config) (string, error) {
|
2015-11-18 17:20:54 -05:00
|
|
|
cache, err := d.Daemon.ImageGetCached(image.ID(imgID), cfg)
|
2015-09-06 13:26:40 -04:00
|
|
|
if cache == nil || err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
return cache.ID().String(), nil
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:25:26 -05:00
|
|
|
// Kill stops the container execution abruptly.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Kill(container *container.Container) error {
|
2015-11-02 18:25:26 -05:00
|
|
|
return d.Daemon.Kill(container)
|
|
|
|
}
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
// Mount mounts the root filesystem for the container.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Mount(c *container.Container) error {
|
2015-11-02 20:06:09 -05:00
|
|
|
return d.Daemon.Mount(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount unmounts the root filesystem for the container.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Unmount(c *container.Container) error {
|
2015-11-18 17:20:54 -05:00
|
|
|
d.Daemon.Unmount(c)
|
|
|
|
return nil
|
2015-11-02 20:06:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts a container
|
2015-11-12 14:55:17 -05:00
|
|
|
func (d Docker) Start(c *container.Container) error {
|
2015-11-02 20:06:09 -05:00
|
|
|
return d.Daemon.Start(c)
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Following is specific to builder contexts
|
|
|
|
|
|
|
|
// DetectContextFromRemoteURL returns a context and in certain cases the name of the dockerfile to be used
|
|
|
|
// irrespective of user input.
|
|
|
|
// progressReader is only used if remoteURL is actually a URL (not empty, and not a Git endpoint).
|
2015-11-13 19:59:01 -05:00
|
|
|
func DetectContextFromRemoteURL(r io.ReadCloser, remoteURL string, createProgressReader func(in io.ReadCloser) io.ReadCloser) (context builder.ModifiableContext, dockerfileName string, err error) {
|
2015-09-06 13:26:40 -04:00
|
|
|
switch {
|
|
|
|
case remoteURL == "":
|
|
|
|
context, err = builder.MakeTarSumContext(r)
|
|
|
|
case urlutil.IsGitURL(remoteURL):
|
|
|
|
context, err = builder.MakeGitContext(remoteURL)
|
|
|
|
case urlutil.IsURL(remoteURL):
|
|
|
|
context, err = builder.MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){
|
|
|
|
httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
|
|
|
|
dockerfile, err := ioutil.ReadAll(rc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dockerfileName is set to signal that the remote was interpreted as a single Dockerfile, in which case the caller
|
|
|
|
// should use dockerfileName as the new name for the Dockerfile, irrespective of any other user input.
|
|
|
|
dockerfileName = api.DefaultDockerfileName
|
|
|
|
|
|
|
|
// TODO: return a context without tarsum
|
|
|
|
return archive.Generate(dockerfileName, string(dockerfile))
|
|
|
|
},
|
|
|
|
// fallback handler (tar context)
|
|
|
|
"": func(rc io.ReadCloser) (io.ReadCloser, error) {
|
2015-11-13 19:59:01 -05:00
|
|
|
return createProgressReader(rc), nil
|
2015-09-06 13:26:40 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("remoteURL (%s) could not be recognized as URL", remoteURL)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|