2015-09-05 19:49:06 +00:00
|
|
|
package dockerfile
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// internals for handling commands. Covers many areas and a lot of
|
|
|
|
// non-contiguous functionality. Please read the comments.
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2016-03-22 17:49:03 +00:00
|
|
|
"errors"
|
2014-08-05 22:41:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-10-22 18:16:42 +00:00
|
|
|
"net/http"
|
2014-08-05 22:41:09 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-05-07 15:39:42 +00:00
|
|
|
"runtime"
|
2014-08-05 22:41:09 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-03-22 17:49:03 +00:00
|
|
|
"sync"
|
2014-08-05 22:41:09 +00:00
|
|
|
"time"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2015-09-06 17:26:40 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2015-09-05 19:49:06 +00:00
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 21:17:23 +00:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-01-27 04:56:34 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-03-18 02:18:41 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-11-14 00:59:01 +00:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2015-09-06 17:26:40 +00:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-08-05 22:41:09 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
2014-11-24 23:47:42 +00:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2016-01-04 03:45:06 +00:00
|
|
|
"github.com/docker/docker/runconfig/opts"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
|
|
|
"github.com/docker/engine-api/types/strslice"
|
2014-08-05 22:41:09 +00:00
|
|
|
)
|
|
|
|
|
2016-03-16 21:52:34 +00:00
|
|
|
func (b *Builder) addLabels() {
|
|
|
|
// merge labels
|
|
|
|
if len(b.options.Labels) > 0 {
|
|
|
|
logrus.Debugf("[BUILDER] setting labels %v", b.options.Labels)
|
|
|
|
if b.runConfig.Labels == nil {
|
|
|
|
b.runConfig.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
for kL, vL := range b.options.Labels {
|
|
|
|
b.runConfig.Labels[kL] = vL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:28:37 +00:00
|
|
|
func (b *Builder) commit(id string, autoCmd strslice.StrSlice, comment string) error {
|
2015-01-09 21:07:00 +00:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-28 21:06:23 +00:00
|
|
|
if b.image == "" && !b.noBaseImage {
|
2014-08-05 20:17:40 +00:00
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Image = b.image
|
2016-03-16 21:52:34 +00:00
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
if id == "" {
|
2015-09-06 17:26:40 +00:00
|
|
|
cmd := b.runConfig.Cmd
|
2015-05-07 15:39:42 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
2016-02-29 11:28:37 +00:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"/bin/sh", "-c", "#(nop) " + comment}
|
2015-05-07 15:39:42 +00:00
|
|
|
} else {
|
2016-02-29 11:28:37 +00:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"cmd", "/S /C", "REM (nop) " + comment}
|
2015-05-07 15:39:42 +00:00
|
|
|
}
|
2016-02-29 11:28:37 +00:00
|
|
|
defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
2014-08-05 20:17:40 +00:00
|
|
|
return err
|
2015-09-06 17:26:40 +00:00
|
|
|
} else if hit {
|
2014-08-05 20:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-10 14:35:53 +00:00
|
|
|
id, err = b.create()
|
2014-08-05 20:17:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
// Note: Actually copy the struct
|
2015-09-06 17:26:40 +00:00
|
|
|
autoConfig := *b.runConfig
|
2014-08-05 20:17:40 +00:00
|
|
|
autoConfig.Cmd = autoCmd
|
2014-08-30 11:34:09 +00:00
|
|
|
|
2016-03-16 23:07:41 +00:00
|
|
|
commitCfg := &backend.ContainerCommitConfig{
|
|
|
|
ContainerCommitConfig: types.ContainerCommitConfig{
|
|
|
|
Author: b.maintainer,
|
|
|
|
Pause: true,
|
|
|
|
Config: &autoConfig,
|
|
|
|
},
|
2015-06-20 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
// Commit the container
|
2015-11-18 22:20:54 +00:00
|
|
|
imageID, err := b.docker.Commit(id, commitCfg)
|
2014-08-05 20:17:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-16 21:52:34 +00:00
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
b.image = imageID
|
2014-08-05 20:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 16:58:20 +00:00
|
|
|
type copyInfo struct {
|
2015-09-06 17:26:40 +00:00
|
|
|
builder.FileInfo
|
2014-09-16 16:58:20 +00:00
|
|
|
decompress bool
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalDecompression bool, cmdName string) error {
|
2014-08-05 20:17:40 +00:00
|
|
|
if b.context == nil {
|
|
|
|
return fmt.Errorf("No context given. Impossible to use %s", cmdName)
|
|
|
|
}
|
|
|
|
|
2014-09-16 16:58:20 +00:00
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("Invalid %s format - at least two arguments required", cmdName)
|
|
|
|
}
|
|
|
|
|
2015-07-12 16:05:10 +00:00
|
|
|
// Work in daemon-specific filepath semantics
|
|
|
|
dest := filepath.FromSlash(args[len(args)-1]) // last one is always the dest
|
2014-09-16 16:58:20 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Image = b.image
|
2014-09-16 16:58:20 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
var infos []copyInfo
|
2014-09-16 16:58:20 +00:00
|
|
|
|
|
|
|
// Loop through each src file and calculate the info we need to
|
|
|
|
// do the copy (e.g. hash value if cached). Don't actually do
|
|
|
|
// the copy until we've looked at all src files
|
2015-09-06 17:26:40 +00:00
|
|
|
var err error
|
2014-09-22 13:41:02 +00:00
|
|
|
for _, orig := range args[0 : len(args)-1] {
|
2015-09-06 17:26:40 +00:00
|
|
|
var fi builder.FileInfo
|
|
|
|
decompress := allowLocalDecompression
|
|
|
|
if urlutil.IsURL(orig) {
|
|
|
|
if !allowRemote {
|
|
|
|
return fmt.Errorf("Source can't be a URL for %s", cmdName)
|
|
|
|
}
|
|
|
|
fi, err = b.download(orig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(filepath.Dir(fi.Path()))
|
|
|
|
decompress = false
|
|
|
|
infos = append(infos, copyInfo{fi, decompress})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// not a URL
|
|
|
|
subInfos, err := b.calcCopyInfo(cmdName, orig, allowLocalDecompression, true)
|
|
|
|
if err != nil {
|
2014-09-16 16:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
|
|
|
|
infos = append(infos, subInfos...)
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if len(infos) == 0 {
|
2014-09-22 13:41:02 +00:00
|
|
|
return fmt.Errorf("No source files were specified")
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if len(infos) > 1 && !strings.HasSuffix(dest, string(os.PathSeparator)) {
|
2014-09-22 13:41:02 +00:00
|
|
|
return fmt.Errorf("When using %s with more than one source file, the destination must be a directory and end with a /", cmdName)
|
|
|
|
}
|
2014-09-16 16:58:20 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// For backwards compat, if there's just one info then use it as the
|
2014-09-22 13:41:02 +00:00
|
|
|
// cache look-up string, otherwise hash 'em all into one
|
|
|
|
var srcHash string
|
|
|
|
var origPaths string
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if len(infos) == 1 {
|
|
|
|
fi := infos[0].FileInfo
|
|
|
|
origPaths = fi.Name()
|
|
|
|
if hfi, ok := fi.(builder.Hashed); ok {
|
|
|
|
srcHash = hfi.Hash()
|
|
|
|
}
|
2014-09-22 13:41:02 +00:00
|
|
|
} else {
|
|
|
|
var hashs []string
|
|
|
|
var origs []string
|
2015-09-06 17:26:40 +00:00
|
|
|
for _, info := range infos {
|
|
|
|
fi := info.FileInfo
|
|
|
|
origs = append(origs, fi.Name())
|
|
|
|
if hfi, ok := fi.(builder.Hashed); ok {
|
|
|
|
hashs = append(hashs, hfi.Hash())
|
|
|
|
}
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
2014-09-22 13:41:02 +00:00
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(hashs, ",")))
|
|
|
|
srcHash = "multi:" + hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
origPaths = strings.Join(origs, " ")
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
cmd := b.runConfig.Cmd
|
2015-05-07 15:39:42 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
2016-02-29 11:28:37 +00:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, srcHash, dest)}
|
2015-05-07 15:39:42 +00:00
|
|
|
} else {
|
2016-02-29 11:28:37 +00:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"cmd", "/S", "/C", fmt.Sprintf("REM (nop) %s %s in %s", cmdName, srcHash, dest)}
|
2015-05-07 15:39:42 +00:00
|
|
|
}
|
2016-02-29 11:28:37 +00:00
|
|
|
defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if hit, err := b.probeCache(); err != nil {
|
2014-09-16 16:58:20 +00:00
|
|
|
return err
|
2015-09-06 17:26:40 +00:00
|
|
|
} else if hit {
|
2014-09-16 16:58:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 16:56:49 +00:00
|
|
|
container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig})
|
2015-09-23 01:06:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2015-09-23 01:06:09 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
comment := fmt.Sprintf("%s %s in %s", cmdName, origPaths, dest)
|
2014-09-16 16:58:20 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Twiddle the destination when its a relative path - meaning, make it
|
|
|
|
// relative to the WORKINGDIR
|
|
|
|
if !system.IsAbs(dest) {
|
|
|
|
hasSlash := strings.HasSuffix(dest, string(os.PathSeparator))
|
|
|
|
dest = filepath.Join(string(os.PathSeparator), filepath.FromSlash(b.runConfig.WorkingDir), dest)
|
|
|
|
|
|
|
|
// Make sure we preserve any trailing slash
|
|
|
|
if hasSlash {
|
|
|
|
dest += string(os.PathSeparator)
|
|
|
|
}
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
for _, info := range infos {
|
2016-01-20 23:32:02 +00:00
|
|
|
if err := b.docker.CopyOnBuild(container.ID, dest, info.FileInfo, info.decompress); err != nil {
|
2014-09-16 16:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
return b.commit(container.ID, cmd, comment)
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
func (b *Builder) download(srcURL string) (fi builder.FileInfo, err error) {
|
|
|
|
// get filename from URL
|
|
|
|
u, err := url.Parse(srcURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
2015-10-07 22:57:37 +00:00
|
|
|
path := filepath.FromSlash(u.Path) // Ensure in platform semantics
|
2015-09-06 17:26:40 +00:00
|
|
|
if strings.HasSuffix(path, string(os.PathSeparator)) {
|
|
|
|
path = path[:len(path)-1]
|
|
|
|
}
|
|
|
|
parts := strings.Split(path, string(os.PathSeparator))
|
|
|
|
filename := parts[len(parts)-1]
|
|
|
|
if filename == "" {
|
|
|
|
err = fmt.Errorf("cannot determine filename from url: %s", u)
|
|
|
|
return
|
2014-12-12 18:32:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Initiate the download
|
|
|
|
resp, err := httputils.Download(srcURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Prepare file in a tmp dir
|
|
|
|
tmpDir, err := ioutils.TempDir("", "docker-remote")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2014-08-05 20:17:40 +00:00
|
|
|
if err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
os.RemoveAll(tmpDir)
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
}()
|
|
|
|
tmpFileName := filepath.Join(tmpDir, filename)
|
|
|
|
tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
stdoutFormatter := b.Stdout.(*streamformatter.StdoutFormatter)
|
|
|
|
progressOutput := stdoutFormatter.StreamFormatter.NewProgressOutput(stdoutFormatter.Writer, true)
|
|
|
|
progressReader := progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Downloading")
|
2015-09-06 17:26:40 +00:00
|
|
|
// Download and dump result to tmp file
|
2015-11-14 00:59:01 +00:00
|
|
|
if _, err = io.Copy(tmpFile, progressReader); err != nil {
|
2014-08-05 20:17:40 +00:00
|
|
|
tmpFile.Close()
|
2015-09-06 17:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(b.Stdout)
|
|
|
|
// ignoring error because the file was already opened successfully
|
|
|
|
tmpFileSt, err := tmpFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tmpFile.Close()
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Set the mtime to the Last-Modified header value if present
|
|
|
|
// Otherwise just remove atime and mtime
|
|
|
|
mTime := time.Time{}
|
2014-10-22 18:16:42 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
lastMod := resp.Header.Get("Last-Modified")
|
|
|
|
if lastMod != "" {
|
|
|
|
// If we can't parse it then just let it default to 'zero'
|
|
|
|
// otherwise use the parsed time value
|
|
|
|
if parsedMTime, err := http.ParseTime(lastMod); err == nil {
|
|
|
|
mTime = parsedMTime
|
2014-10-22 18:16:42 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
2014-10-22 18:16:42 +00:00
|
|
|
|
2015-11-05 19:47:18 +00:00
|
|
|
if err = system.Chtimes(tmpFileName, mTime, mTime); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Calc the checksum, even if we're using the cache
|
|
|
|
r, err := archive.Tar(tmpFileName, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tarSum, err := tarsum.NewTarSum(r, true, tarsum.Version1)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(ioutil.Discard, tarSum); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hash := tarSum.Sum(nil)
|
|
|
|
r.Close()
|
|
|
|
return &builder.HashedFileInfo{FileInfo: builder.PathFileInfo{FileInfo: tmpFileSt, FilePath: tmpFileName}, FileHash: hash}, nil
|
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
func (b *Builder) calcCopyInfo(cmdName, origPath string, allowLocalDecompression, allowWildcards bool) ([]copyInfo, error) {
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// Work in daemon-specific OS filepath semantics
|
|
|
|
origPath = filepath.FromSlash(origPath)
|
2014-09-22 13:41:02 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if origPath != "" && origPath[0] == os.PathSeparator && len(origPath) > 1 {
|
|
|
|
origPath = origPath[1:]
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
origPath = strings.TrimPrefix(origPath, "."+string(os.PathSeparator))
|
2014-09-22 13:41:02 +00:00
|
|
|
|
|
|
|
// Deal with wildcards
|
2015-07-22 05:29:03 +00:00
|
|
|
if allowWildcards && containsWildcards(origPath) {
|
2015-09-06 17:26:40 +00:00
|
|
|
var copyInfos []copyInfo
|
|
|
|
if err := b.context.Walk("", func(path string, info builder.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Name() == "" {
|
|
|
|
// Why are we doing this check?
|
|
|
|
return nil
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if match, _ := filepath.Match(origPath, path); !match {
|
|
|
|
return nil
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2014-09-22 13:41:02 +00:00
|
|
|
|
2015-04-10 19:39:42 +00:00
|
|
|
// Note we set allowWildcards to false in case the name has
|
|
|
|
// a * in it
|
2015-09-06 17:26:40 +00:00
|
|
|
subInfos, err := b.calcCopyInfo(cmdName, path, allowLocalDecompression, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copyInfos = append(copyInfos, subInfos...)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
return copyInfos, nil
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must be a dir or a file
|
|
|
|
|
2015-11-04 21:42:08 +00:00
|
|
|
statPath, fi, err := b.context.Stat(origPath)
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
copyInfos := []copyInfo{{FileInfo: fi, decompress: allowLocalDecompression}}
|
|
|
|
|
|
|
|
hfi, handleHash := fi.(builder.Hashed)
|
|
|
|
if !handleHash {
|
|
|
|
return copyInfos, nil
|
|
|
|
}
|
2014-09-22 13:41:02 +00:00
|
|
|
|
|
|
|
// Deal with the single file case
|
|
|
|
if !fi.IsDir() {
|
2015-09-06 17:26:40 +00:00
|
|
|
hfi.SetHash("file:" + hfi.Hash())
|
|
|
|
return copyInfos, nil
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
|
|
|
// Must be a dir
|
|
|
|
var subfiles []string
|
2015-11-04 21:42:08 +00:00
|
|
|
err = b.context.Walk(statPath, func(path string, info builder.FileInfo, err error) error {
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
// we already checked handleHash above
|
|
|
|
subfiles = append(subfiles, info.(builder.Hashed).Hash())
|
|
|
|
return nil
|
|
|
|
})
|
2015-11-04 21:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2014-09-22 13:41:02 +00:00
|
|
|
sort.Strings(subfiles)
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(subfiles, ",")))
|
2015-09-06 17:26:40 +00:00
|
|
|
hfi.SetHash("dir:" + hex.EncodeToString(hasher.Sum(nil)))
|
2014-09-22 13:41:02 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
return copyInfos, nil
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
func containsWildcards(name string) bool {
|
2014-09-22 13:41:02 +00:00
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
ch := name[i]
|
|
|
|
if ch == '\\' {
|
|
|
|
i++
|
|
|
|
} else if ch == '*' || ch == '?' || ch == '[' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-16 16:01:36 +00:00
|
|
|
func (b *Builder) processImageFrom(img builder.Image) error {
|
2016-01-04 03:45:06 +00:00
|
|
|
if img != nil {
|
2016-01-20 23:32:02 +00:00
|
|
|
b.image = img.ImageID()
|
2014-08-13 10:07:41 +00:00
|
|
|
|
2016-01-20 23:32:02 +00:00
|
|
|
if img.RunConfig() != nil {
|
2016-03-30 21:26:02 +00:00
|
|
|
imgConfig := *img.RunConfig()
|
|
|
|
// inherit runConfig labels from the current
|
|
|
|
// state if they've been set already.
|
|
|
|
// Ensures that images with only a FROM
|
|
|
|
// get the labels populated properly.
|
|
|
|
if b.runConfig.Labels != nil {
|
|
|
|
if imgConfig.Labels == nil {
|
|
|
|
imgConfig.Labels = make(map[string]string)
|
|
|
|
}
|
|
|
|
for k, v := range b.runConfig.Labels {
|
|
|
|
imgConfig.Labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.runConfig = &imgConfig
|
2016-01-04 03:45:06 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-08-13 10:07:41 +00:00
|
|
|
|
2016-01-04 03:45:06 +00:00
|
|
|
// Check to see if we have a default PATH, note that windows won't
|
|
|
|
// have one as its set by HCS
|
|
|
|
if system.DefaultPathEnv != "" {
|
|
|
|
// Convert the slice of strings that represent the current list
|
|
|
|
// of env vars into a map so we can see if PATH is already set.
|
|
|
|
// If its not set then go ahead and give it our default value
|
|
|
|
configEnv := opts.ConvertKVStringsToMap(b.runConfig.Env)
|
|
|
|
if _, ok := configEnv["PATH"]; !ok {
|
|
|
|
b.runConfig.Env = append(b.runConfig.Env,
|
|
|
|
"PATH="+system.DefaultPathEnv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if img == nil {
|
|
|
|
// Typically this means they used "FROM scratch"
|
|
|
|
return nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-08-13 10:07:41 +00:00
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
// Process ONBUILD triggers if they exist
|
2015-09-06 17:26:40 +00:00
|
|
|
if nTriggers := len(b.runConfig.OnBuild); nTriggers != 0 {
|
2015-08-27 02:45:18 +00:00
|
|
|
word := "trigger"
|
|
|
|
if nTriggers > 1 {
|
|
|
|
word = "triggers"
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
fmt.Fprintf(b.Stderr, "# Executing %d build %s...\n", nTriggers, word)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 20:33:30 +00:00
|
|
|
// Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
|
2015-09-06 17:26:40 +00:00
|
|
|
onBuildTriggers := b.runConfig.OnBuild
|
|
|
|
b.runConfig.OnBuild = []string{}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-10-13 20:14:35 +00:00
|
|
|
// parse the ONBUILD triggers by invoking the parser
|
2015-08-27 02:45:18 +00:00
|
|
|
for _, step := range onBuildTriggers {
|
2014-10-13 20:14:35 +00:00
|
|
|
ast, err := parser.Parse(strings.NewReader(step))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 20:14:35 +00:00
|
|
|
for i, n := range ast.Children {
|
|
|
|
switch strings.ToUpper(n.Value) {
|
|
|
|
case "ONBUILD":
|
|
|
|
return fmt.Errorf("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
case "MAINTAINER", "FROM":
|
|
|
|
return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", n.Value)
|
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := b.dispatch(i, n); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// probeCache checks if `b.docker` implements builder.ImageCache and image-caching
|
|
|
|
// is enabled (`b.UseCache`).
|
|
|
|
// If so attempts to look up the current `b.image` and `b.runConfig` pair with `b.docker`.
|
|
|
|
// If an image is found, probeCache returns `(true, nil)`.
|
|
|
|
// If no image is found, it returns `(false, nil)`.
|
|
|
|
// If there is any error, it returns `(false, err)`.
|
|
|
|
func (b *Builder) probeCache() (bool, error) {
|
|
|
|
c, ok := b.docker.(builder.ImageCache)
|
2015-12-29 20:49:17 +00:00
|
|
|
if !ok || b.options.NoCache || b.cacheBusted {
|
2015-02-25 18:27:32 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2016-01-20 23:32:02 +00:00
|
|
|
cache, err := c.GetCachedImageOnBuild(b.image, b.runConfig)
|
2015-02-25 18:27:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if len(cache) == 0 {
|
2015-10-06 12:43:51 +00:00
|
|
|
logrus.Debugf("[BUILDER] Cache miss: %s", b.runConfig.Cmd)
|
2015-02-25 18:27:32 +00:00
|
|
|
b.cacheBusted = true
|
|
|
|
return false, nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-02-25 18:27:32 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
fmt.Fprintf(b.Stdout, " ---> Using cache\n")
|
2015-10-06 12:43:51 +00:00
|
|
|
logrus.Debugf("[BUILDER] Use cached version: %s", b.runConfig.Cmd)
|
2015-09-06 17:26:40 +00:00
|
|
|
b.image = string(cache)
|
|
|
|
|
2015-02-25 18:27:32 +00:00
|
|
|
return true, nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
func (b *Builder) create() (string, error) {
|
2014-10-28 21:06:23 +00:00
|
|
|
if b.image == "" && !b.noBaseImage {
|
2015-12-10 14:35:53 +00:00
|
|
|
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Image = b.image
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-12-18 18:36:17 +00:00
|
|
|
resources := container.Resources{
|
2015-12-29 20:49:17 +00:00
|
|
|
CgroupParent: b.options.CgroupParent,
|
|
|
|
CPUShares: b.options.CPUShares,
|
|
|
|
CPUPeriod: b.options.CPUPeriod,
|
|
|
|
CPUQuota: b.options.CPUQuota,
|
|
|
|
CpusetCpus: b.options.CPUSetCPUs,
|
|
|
|
CpusetMems: b.options.CPUSetMems,
|
|
|
|
Memory: b.options.Memory,
|
|
|
|
MemorySwap: b.options.MemorySwap,
|
|
|
|
Ulimits: b.options.Ulimits,
|
2015-11-18 19:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: why not embed a hostconfig in builder?
|
2015-12-18 18:36:17 +00:00
|
|
|
hostConfig := &container.HostConfig{
|
2016-02-03 20:07:00 +00:00
|
|
|
Isolation: b.options.Isolation,
|
2015-12-29 20:49:17 +00:00
|
|
|
ShmSize: b.options.ShmSize,
|
2015-11-18 19:03:08 +00:00
|
|
|
Resources: resources,
|
2015-02-06 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
config := *b.runConfig
|
2014-09-04 15:43:28 +00:00
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
// Create the container
|
2015-12-16 16:56:49 +00:00
|
|
|
c, err := b.docker.ContainerCreate(types.ContainerCreateConfig{
|
2015-12-10 14:35:53 +00:00
|
|
|
Config: b.runConfig,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
})
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
2015-12-10 14:35:53 +00:00
|
|
|
return "", err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-12-10 14:35:53 +00:00
|
|
|
for _, warning := range c.Warnings {
|
2015-09-06 17:26:40 +00:00
|
|
|
fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
|
2015-09-23 01:06:09 +00:00
|
|
|
}
|
2014-08-26 19:25:44 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.tmpContainers[c.ID] = struct{}{}
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(c.ID))
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2016-03-15 16:12:20 +00:00
|
|
|
// override the entry point that may have been picked up from the base image
|
|
|
|
if err := b.docker.ContainerUpdateCmdOnBuild(c.ID, config.Cmd); err != nil {
|
|
|
|
return "", err
|
2015-01-09 01:00:00 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
return c.ID, nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 17:49:03 +00:00
|
|
|
var errCancelled = errors.New("build cancelled")
|
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
func (b *Builder) run(cID string) (err error) {
|
|
|
|
errCh := make(chan error)
|
2015-10-28 00:29:21 +00:00
|
|
|
go func() {
|
2016-01-05 21:23:24 +00:00
|
|
|
errCh <- b.docker.ContainerAttachRaw(cID, nil, b.Stdout, b.Stderr, true)
|
2015-10-28 00:29:21 +00:00
|
|
|
}()
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-03-10 22:10:00 +00:00
|
|
|
finished := make(chan struct{})
|
2016-03-22 17:49:03 +00:00
|
|
|
var once sync.Once
|
|
|
|
finish := func() { close(finished) }
|
|
|
|
cancelErrCh := make(chan error, 1)
|
|
|
|
defer once.Do(finish)
|
2015-03-10 22:10:00 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
2016-03-22 17:49:03 +00:00
|
|
|
case <-b.clientCtx.Done():
|
2015-12-10 14:35:53 +00:00
|
|
|
logrus.Debugln("Build cancelled, killing and removing container:", cID)
|
|
|
|
b.docker.ContainerKill(cID, 0)
|
|
|
|
b.removeContainer(cID)
|
2016-03-22 17:49:03 +00:00
|
|
|
cancelErrCh <- errCancelled
|
2015-03-10 22:10:00 +00:00
|
|
|
case <-finished:
|
2016-03-22 17:49:03 +00:00
|
|
|
cancelErrCh <- nil
|
2015-03-10 22:10:00 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
if err := b.docker.ContainerStart(cID, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-28 00:29:21 +00:00
|
|
|
// Block on reading output from container, stop on err or chan closed
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
return err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
if ret, _ := b.docker.ContainerWait(cID, -1); ret != 0 {
|
2015-09-06 17:26:40 +00:00
|
|
|
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
|
2015-04-16 19:22:32 +00:00
|
|
|
return &jsonmessage.JSONError{
|
2016-02-29 11:28:37 +00:00
|
|
|
Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", strings.Join(b.runConfig.Cmd, " "), ret),
|
2014-08-05 22:41:09 +00:00
|
|
|
Code: ret,
|
|
|
|
}
|
|
|
|
}
|
2016-03-22 17:49:03 +00:00
|
|
|
once.Do(finish)
|
|
|
|
return <-cancelErrCh
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 09:18:10 +00:00
|
|
|
func (b *Builder) removeContainer(c string) error {
|
2015-12-04 20:34:43 +00:00
|
|
|
rmConfig := &types.ContainerRmConfig{
|
2015-10-16 09:18:10 +00:00
|
|
|
ForceRemove: true,
|
|
|
|
RemoveVolume: true,
|
|
|
|
}
|
2015-12-10 14:35:53 +00:00
|
|
|
if err := b.docker.ContainerRm(c, rmConfig); err != nil {
|
2015-10-16 09:18:10 +00:00
|
|
|
fmt.Fprintf(b.Stdout, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
func (b *Builder) clearTmp() {
|
|
|
|
for c := range b.tmpContainers {
|
2015-10-16 09:18:10 +00:00
|
|
|
if err := b.removeContainer(c); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
delete(b.tmpContainers, c)
|
|
|
|
fmt.Fprintf(b.Stdout, "Removing intermediate container %s\n", stringid.TruncateID(c))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// readDockerfile reads a Dockerfile from the current context.
|
|
|
|
func (b *Builder) readDockerfile() error {
|
|
|
|
// If no -f was specified then look for 'Dockerfile'. If we can't find
|
|
|
|
// that then look for 'dockerfile'. If neither are found then default
|
|
|
|
// back to 'Dockerfile' and use that in the error message.
|
2015-12-29 20:49:17 +00:00
|
|
|
if b.options.Dockerfile == "" {
|
2016-02-11 19:59:59 +00:00
|
|
|
b.options.Dockerfile = builder.DefaultDockerfileName
|
2015-12-29 20:49:17 +00:00
|
|
|
if _, _, err := b.context.Stat(b.options.Dockerfile); os.IsNotExist(err) {
|
|
|
|
lowercase := strings.ToLower(b.options.Dockerfile)
|
2015-11-04 21:42:08 +00:00
|
|
|
if _, _, err := b.context.Stat(lowercase); err == nil {
|
2015-12-29 20:49:17 +00:00
|
|
|
b.options.Dockerfile = lowercase
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-29 20:49:17 +00:00
|
|
|
f, err := b.context.Open(b.options.Dockerfile)
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2015-12-29 20:49:17 +00:00
|
|
|
return fmt.Errorf("Cannot locate specified Dockerfile: %s", b.options.Dockerfile)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if f, ok := f.(*os.File); ok {
|
|
|
|
// ignoring error because Open already succeeded
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unexpected error reading Dockerfile: %v", err)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if fi.Size() == 0 {
|
2015-12-29 20:49:17 +00:00
|
|
|
return fmt.Errorf("The Dockerfile (%s) cannot be empty", b.options.Dockerfile)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
b.dockerfile, err = parser.Parse(f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// After the Dockerfile has been parsed, we need to check the .dockerignore
|
|
|
|
// file for either "Dockerfile" or ".dockerignore", and if either are
|
|
|
|
// present then erase them from the build context. These files should never
|
|
|
|
// have been sent from the client but we did send them to make sure that
|
|
|
|
// we had the Dockerfile to actually parse, and then we also need the
|
|
|
|
// .dockerignore file to know whether either file should be removed.
|
|
|
|
// Note that this assumes the Dockerfile has been read into memory and
|
|
|
|
// is now safe to be removed.
|
|
|
|
if dockerIgnore, ok := b.context.(builder.DockerIgnoreContext); ok {
|
2015-12-29 20:49:17 +00:00
|
|
|
dockerIgnore.Process([]string{b.options.Dockerfile})
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
// determine if build arg is part of built-in args or user
|
|
|
|
// defined args in Dockerfile at any point in time.
|
|
|
|
func (b *Builder) isBuildArgAllowed(arg string) bool {
|
|
|
|
if _, ok := BuiltinAllowedBuildArgs[arg]; ok {
|
|
|
|
return true
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if _, ok := b.allowedBuildArgs[arg]; ok {
|
|
|
|
return true
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
return false
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|