2015-09-06 20:26:40 +03:00
|
|
|
package daemonbuilder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/api"
|
2015-12-04 23:34:43 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-12-18 21:36:17 +03:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2015-09-06 20:26:40 +03:00
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"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 21:35:48 +03:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-09-06 20:26:40 +03:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2015-12-05 00:55:15 +03:00
|
|
|
"github.com/docker/docker/reference"
|
2015-09-06 20:26:40 +03:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
)
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
// Docker implements builder.Backend for the docker Daemon object.
|
2015-09-06 20:26:40 +03:00
|
|
|
type Docker struct {
|
2015-12-10 17:35:53 +03:00
|
|
|
*daemon.Daemon
|
2015-09-06 20:26:40 +03:00
|
|
|
OutOld io.Writer
|
2015-12-12 07:11:42 +03:00
|
|
|
AuthConfigs map[string]types.AuthConfig
|
2015-10-08 18:51:41 +03:00
|
|
|
Archiver *archive.Archiver
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
// ensure Docker implements builder.Backend
|
|
|
|
var _ builder.Backend = Docker{}
|
2015-09-06 20:26:40 +03:00
|
|
|
|
|
|
|
// Pull tells Docker to pull image referenced by `name`.
|
2015-12-16 19:01:36 +03:00
|
|
|
func (d Docker) Pull(name string) (builder.Image, error) {
|
2015-11-19 01:20:54 +03:00
|
|
|
ref, err := reference.ParseNamed(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-10 22:01:34 +03:00
|
|
|
ref = reference.WithDefaultTag(ref)
|
2015-09-06 20:26:40 +03:00
|
|
|
|
2015-12-12 07:11:42 +03:00
|
|
|
pullRegistryAuth := &types.AuthConfig{}
|
2015-09-06 20:26:40 +03:00
|
|
|
if len(d.AuthConfigs) > 0 {
|
|
|
|
// The request came with a full auth config file, we prefer to use that
|
2015-11-19 01:20:54 +03:00
|
|
|
repoInfo, err := d.Daemon.RegistryService.ResolveRepository(ref)
|
2015-09-06 20:26:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvedConfig := registry.ResolveAuthConfig(
|
2015-12-12 06:11:20 +03:00
|
|
|
d.AuthConfigs,
|
2015-09-06 20:26:40 +03:00
|
|
|
repoInfo.Index,
|
|
|
|
)
|
|
|
|
pullRegistryAuth = &resolvedConfig
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:20:54 +03:00
|
|
|
if err := d.Daemon.PullImage(ref, nil, pullRegistryAuth, ioutils.NopWriteCloser(d.OutOld)); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-16 19:01:36 +03:00
|
|
|
return d.GetImage(name)
|
|
|
|
}
|
2015-09-06 20:26:40 +03:00
|
|
|
|
2015-12-16 19:01:36 +03:00
|
|
|
// GetImage looks up a Docker image referenced by `name`.
|
|
|
|
func (d Docker) GetImage(name string) (builder.Image, error) {
|
|
|
|
img, err := d.Daemon.GetImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return imgWrap{img}, nil
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
// ContainerUpdateCmd updates Path and Args for the container with ID cID.
|
|
|
|
func (d Docker) ContainerUpdateCmd(cID string, cmd []string) error {
|
|
|
|
c, err := d.Daemon.GetContainer(cID)
|
2015-09-06 20:26:40 +03:00
|
|
|
if err != nil {
|
2015-12-10 17:35:53 +03:00
|
|
|
return err
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
2015-12-10 17:35:53 +03:00
|
|
|
c.Path = cmd[0]
|
|
|
|
c.Args = cmd[1:]
|
|
|
|
return nil
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
2015-12-16 21:19:12 +03:00
|
|
|
// ContainerAttach attaches streams to the container cID. If stream is true, it streams the output.
|
|
|
|
func (d Docker) ContainerAttach(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error {
|
|
|
|
return d.Daemon.ContainerWsAttachWithLogs(cID, &daemon.ContainerWsAttachWithLogsConfig{
|
|
|
|
InStream: stdin,
|
|
|
|
OutStream: stdout,
|
|
|
|
ErrStream: stderr,
|
|
|
|
Stream: stream,
|
|
|
|
})
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
// BuilderCopy copies/extracts a source FileInfo to a destination path inside a container
|
2015-09-06 20:26:40 +03:00
|
|
|
// specified by a container object.
|
|
|
|
// TODO: make sure callers don't unnecessarily convert destPath with filepath.FromSlash (Copy does it already).
|
2015-12-10 17:35:53 +03:00
|
|
|
// BuilderCopy should take in abstract paths (with slashes) and the implementation should convert it to OS-specific paths.
|
|
|
|
func (d Docker) BuilderCopy(cID string, destPath string, src builder.FileInfo, decompress bool) error {
|
2015-09-06 20:26:40 +03:00
|
|
|
srcPath := src.Path()
|
|
|
|
destExists := true
|
2015-11-23 08:22:22 +03:00
|
|
|
destDir := false
|
2015-10-08 18:51:41 +03:00
|
|
|
rootUID, rootGID := d.Daemon.GetRemappedUIDGID()
|
2015-09-06 20:26:40 +03:00
|
|
|
|
|
|
|
// Work in daemon-local OS specific file paths
|
|
|
|
destPath = filepath.FromSlash(destPath)
|
|
|
|
|
2015-12-10 17:35:53 +03:00
|
|
|
c, err := d.Daemon.GetContainer(cID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-16 20:11:57 +03:00
|
|
|
err = d.Daemon.Mount(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer d.Daemon.Unmount(c)
|
|
|
|
|
2015-09-06 20:26:40 +03:00
|
|
|
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 == "." {
|
2015-11-23 08:22:22 +03:00
|
|
|
destDir = true
|
2015-09-06 20:26:40 +03:00
|
|
|
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 18:51:41 +03:00
|
|
|
if err := d.Archiver.CopyWithTar(srcPath, destPath); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-10-08 18:51:41 +03:00
|
|
|
return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
2015-11-20 19:49:33 +03:00
|
|
|
if decompress && archive.IsArchivePath(srcPath) {
|
2015-09-06 20:26:40 +03: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 19:49:33 +03:00
|
|
|
err := d.Archiver.UntarPath(srcPath, tarDest)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Couldn't untar to %s: %v", tarDest, err)
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
2015-11-20 19:49:33 +03:00
|
|
|
return err
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// only needed for fixPermissions, but might as well put it before CopyFileWithTar
|
2015-11-23 08:22:22 +03:00
|
|
|
if destDir || (destExists && destStat.IsDir()) {
|
2015-11-05 00:42:08 +03:00
|
|
|
destPath = filepath.Join(destPath, src.Name())
|
2015-09-06 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
2015-10-14 21:35:48 +03:00
|
|
|
if err := idtools.MkdirAllNewAs(filepath.Dir(destPath), 0755, rootUID, rootGID); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return err
|
|
|
|
}
|
2015-10-08 18:51:41 +03:00
|
|
|
if err := d.Archiver.CopyFileWithTar(srcPath, destPath); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-08 18:51:41 +03:00
|
|
|
return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
|
2015-09-06 20:26:40 +03: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.
|
2015-12-18 21:36:17 +03:00
|
|
|
func (d Docker) GetCachedImage(imgID string, cfg *container.Config) (string, error) {
|
2015-11-19 01:20:54 +03:00
|
|
|
cache, err := d.Daemon.ImageGetCached(image.ID(imgID), cfg)
|
2015-09-06 20:26:40 +03:00
|
|
|
if cache == nil || err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-11-19 01:20:54 +03:00
|
|
|
return cache.ID().String(), nil
|
2015-09-06 20:26:40 +03: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-14 03:59:01 +03: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 20:26:40 +03: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-14 03:59:01 +03:00
|
|
|
return createProgressReader(rc), nil
|
2015-09-06 20:26:40 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("remoteURL (%s) could not be recognized as URL", remoteURL)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|