2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-07 01:56:44 -04:00
|
|
|
// internals for handling commands. Covers many areas and a lot of
|
|
|
|
// non-contiguous functionality. Please read the comments.
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2016-03-22 13:49:03 -04:00
|
|
|
"errors"
|
2014-08-05 18:41:09 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-10-22 14:16:42 -04:00
|
|
|
"net/http"
|
2014-08-05 18:41:09 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-05-07 11:39:42 -04:00
|
|
|
"runtime"
|
2014-08-05 18:41:09 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-03-22 13:49:03 -04:00
|
|
|
"sync"
|
2014-08-05 18:41:09 -04:00
|
|
|
"time"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-16 19:07:41 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2015-09-05 15:49:06 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 17:17:23 -04:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-01-26 23:56:34 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-03-24 07:25:26 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-08-05 18:41:09 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
2014-11-24 18:47:42 -05:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2016-01-03 22:45:06 -05:00
|
|
|
"github.com/docker/docker/runconfig/opts"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
|
|
|
"github.com/docker/engine-api/types/strslice"
|
2014-08-05 18:41:09 -04:00
|
|
|
)
|
|
|
|
|
2016-02-29 06:28:37 -05:00
|
|
|
func (b *Builder) commit(id string, autoCmd strslice.StrSlice, comment string) error {
|
2015-01-09 16:07:00 -05:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2014-10-28 17:06:23 -04:00
|
|
|
if b.image == "" && !b.noBaseImage {
|
2014-08-05 16:17:40 -04:00
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
b.runConfig.Image = b.image
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
if id == "" {
|
2015-09-06 13:26:40 -04:00
|
|
|
cmd := b.runConfig.Cmd
|
2015-05-07 11:39:42 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
2016-02-29 06:28:37 -05:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"/bin/sh", "-c", "#(nop) " + comment}
|
2015-05-07 11:39:42 -04:00
|
|
|
} else {
|
2016-02-29 06:28:37 -05:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"cmd", "/S /C", "REM (nop) " + comment}
|
2015-05-07 11:39:42 -04:00
|
|
|
}
|
2016-02-29 06:28:37 -05:00
|
|
|
defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
2014-08-05 16:17:40 -04:00
|
|
|
return err
|
2015-09-06 13:26:40 -04:00
|
|
|
} else if hit {
|
2014-08-05 16:17:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-10 09:35:53 -05:00
|
|
|
id, err = b.create()
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
// Note: Actually copy the struct
|
2015-09-06 13:26:40 -04:00
|
|
|
autoConfig := *b.runConfig
|
2014-08-05 16:17:40 -04:00
|
|
|
autoConfig.Cmd = autoCmd
|
2014-08-30 07:34:09 -04:00
|
|
|
|
2016-03-16 19:07:41 -04:00
|
|
|
commitCfg := &backend.ContainerCommitConfig{
|
|
|
|
ContainerCommitConfig: types.ContainerCommitConfig{
|
|
|
|
Author: b.maintainer,
|
|
|
|
Pause: true,
|
|
|
|
Config: &autoConfig,
|
|
|
|
},
|
2015-06-20 06:40:37 -04:00
|
|
|
}
|
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
// Commit the container
|
2015-11-18 17:20:54 -05:00
|
|
|
imageID, err := b.docker.Commit(id, commitCfg)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
b.image = imageID
|
2014-08-05 16:17:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:58:20 -04:00
|
|
|
type copyInfo struct {
|
2015-09-06 13:26:40 -04:00
|
|
|
builder.FileInfo
|
2014-09-16 12:58:20 -04:00
|
|
|
decompress bool
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalDecompression bool, cmdName string) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
if b.context == nil {
|
|
|
|
return fmt.Errorf("No context given. Impossible to use %s", cmdName)
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:58:20 -04:00
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("Invalid %s format - at least two arguments required", cmdName)
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:05:10 -04:00
|
|
|
// Work in daemon-specific filepath semantics
|
|
|
|
dest := filepath.FromSlash(args[len(args)-1]) // last one is always the dest
|
2014-09-16 12:58:20 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
b.runConfig.Image = b.image
|
2014-09-16 12:58:20 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
var infos []copyInfo
|
2014-09-16 12:58:20 -04: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 13:26:40 -04:00
|
|
|
var err error
|
2014-09-22 09:41:02 -04:00
|
|
|
for _, orig := range args[0 : len(args)-1] {
|
2015-09-06 13:26:40 -04: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 12:58:20 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
infos = append(infos, subInfos...)
|
2014-09-22 09:41:02 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
if len(infos) == 0 {
|
2014-09-22 09:41:02 -04:00
|
|
|
return fmt.Errorf("No source files were specified")
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
if len(infos) > 1 && !strings.HasSuffix(dest, string(os.PathSeparator)) {
|
2014-09-22 09:41:02 -04: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 12:58:20 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// For backwards compat, if there's just one info then use it as the
|
2014-09-22 09:41:02 -04:00
|
|
|
// cache look-up string, otherwise hash 'em all into one
|
|
|
|
var srcHash string
|
|
|
|
var origPaths string
|
|
|
|
|
2015-09-06 13:26:40 -04: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 09:41:02 -04:00
|
|
|
} else {
|
|
|
|
var hashs []string
|
|
|
|
var origs []string
|
2015-09-06 13:26:40 -04: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 12:58:20 -04:00
|
|
|
}
|
2014-09-22 09:41:02 -04:00
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(hashs, ",")))
|
|
|
|
srcHash = "multi:" + hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
origPaths = strings.Join(origs, " ")
|
2014-09-16 12:58:20 -04:00
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
cmd := b.runConfig.Cmd
|
2015-05-07 11:39:42 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
2016-02-29 06:28:37 -05:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, srcHash, dest)}
|
2015-05-07 11:39:42 -04:00
|
|
|
} else {
|
2016-02-29 06:28:37 -05:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice{"cmd", "/S", "/C", fmt.Sprintf("REM (nop) %s %s in %s", cmdName, srcHash, dest)}
|
2015-05-07 11:39:42 -04:00
|
|
|
}
|
2016-02-29 06:28:37 -05:00
|
|
|
defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
if hit, err := b.probeCache(); err != nil {
|
2014-09-16 12:58:20 -04:00
|
|
|
return err
|
2015-09-06 13:26:40 -04:00
|
|
|
} else if hit {
|
2014-09-16 12:58:20 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:56:49 -05:00
|
|
|
container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{Config: b.runConfig})
|
2015-09-22 21:06:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2015-09-22 21:06:09 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
comment := fmt.Sprintf("%s %s in %s", cmdName, origPaths, dest)
|
2014-09-16 12:58:20 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Twiddle the destination when its a relative path - meaning, make it
|
|
|
|
// relative to the WORKINGDIR
|
2016-05-02 21:33:59 -04:00
|
|
|
if dest, err = normaliseDest(cmdName, b.runConfig.WorkingDir, dest); err != nil {
|
|
|
|
return err
|
2014-09-16 12:58:20 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
for _, info := range infos {
|
2016-01-20 18:32:02 -05:00
|
|
|
if err := b.docker.CopyOnBuild(container.ID, dest, info.FileInfo, info.decompress); err != nil {
|
2014-09-16 12:58:20 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
return b.commit(container.ID, cmd, comment)
|
2014-09-16 12:58:20 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04: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 09:41:02 -04:00
|
|
|
}
|
2015-10-07 18:57:37 -04:00
|
|
|
path := filepath.FromSlash(u.Path) // Ensure in platform semantics
|
2015-09-06 13:26:40 -04: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 13:32:11 -05:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Initiate the download
|
|
|
|
resp, err := httputils.Download(srcURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Prepare file in a tmp dir
|
|
|
|
tmpDir, err := ioutils.TempDir("", "docker-remote")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
os.RemoveAll(tmpDir)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04: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 16:17:40 -04:00
|
|
|
|
2015-11-13 19:59:01 -05: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 13:26:40 -04:00
|
|
|
// Download and dump result to tmp file
|
2015-11-13 19:59:01 -05:00
|
|
|
if _, err = io.Copy(tmpFile, progressReader); err != nil {
|
2014-08-05 16:17:40 -04:00
|
|
|
tmpFile.Close()
|
2015-09-06 13:26:40 -04: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 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Set the mtime to the Last-Modified header value if present
|
|
|
|
// Otherwise just remove atime and mtime
|
|
|
|
mTime := time.Time{}
|
2014-10-22 14:16:42 -04:00
|
|
|
|
2015-09-06 13:26:40 -04: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 14:16:42 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2014-10-22 14:16:42 -04:00
|
|
|
|
2015-11-05 14:47:18 -05:00
|
|
|
if err = system.Chtimes(tmpFileName, mTime, mTime); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04: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 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
func (b *Builder) calcCopyInfo(cmdName, origPath string, allowLocalDecompression, allowWildcards bool) ([]copyInfo, error) {
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
// Work in daemon-specific OS filepath semantics
|
|
|
|
origPath = filepath.FromSlash(origPath)
|
2014-09-22 09:41:02 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
if origPath != "" && origPath[0] == os.PathSeparator && len(origPath) > 1 {
|
|
|
|
origPath = origPath[1:]
|
2014-09-22 09:41:02 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
origPath = strings.TrimPrefix(origPath, "."+string(os.PathSeparator))
|
2014-09-22 09:41:02 -04:00
|
|
|
|
|
|
|
// Deal with wildcards
|
2015-07-22 01:29:03 -04:00
|
|
|
if allowWildcards && containsWildcards(origPath) {
|
2015-09-06 13:26:40 -04: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 16:17:40 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
if match, _ := filepath.Match(origPath, path); !match {
|
|
|
|
return nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2014-09-22 09:41:02 -04:00
|
|
|
|
2015-04-10 15:39:42 -04:00
|
|
|
// Note we set allowWildcards to false in case the name has
|
|
|
|
// a * in it
|
2015-09-06 13:26:40 -04: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 09:41:02 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
return copyInfos, nil
|
2014-09-22 09:41:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must be a dir or a file
|
|
|
|
|
2015-11-04 16:42:08 -05:00
|
|
|
statPath, fi, err := b.context.Stat(origPath)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-22 09:41:02 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
copyInfos := []copyInfo{{FileInfo: fi, decompress: allowLocalDecompression}}
|
|
|
|
|
|
|
|
hfi, handleHash := fi.(builder.Hashed)
|
|
|
|
if !handleHash {
|
|
|
|
return copyInfos, nil
|
|
|
|
}
|
2014-09-22 09:41:02 -04:00
|
|
|
|
|
|
|
// Deal with the single file case
|
|
|
|
if !fi.IsDir() {
|
2015-09-06 13:26:40 -04:00
|
|
|
hfi.SetHash("file:" + hfi.Hash())
|
|
|
|
return copyInfos, nil
|
2014-09-22 09:41:02 -04:00
|
|
|
}
|
|
|
|
// Must be a dir
|
|
|
|
var subfiles []string
|
2015-11-04 16:42:08 -05:00
|
|
|
err = b.context.Walk(statPath, func(path string, info builder.FileInfo, err error) error {
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-09-22 09:41:02 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
// we already checked handleHash above
|
|
|
|
subfiles = append(subfiles, info.(builder.Hashed).Hash())
|
|
|
|
return nil
|
|
|
|
})
|
2015-11-04 16:42:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2014-09-22 09:41:02 -04:00
|
|
|
sort.Strings(subfiles)
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(subfiles, ",")))
|
2015-09-06 13:26:40 -04:00
|
|
|
hfi.SetHash("dir:" + hex.EncodeToString(hasher.Sum(nil)))
|
2014-09-22 09:41:02 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
return copyInfos, nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-07-22 01:29:03 -04:00
|
|
|
func containsWildcards(name string) bool {
|
2014-09-22 09:41:02 -04: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 11:01:36 -05:00
|
|
|
func (b *Builder) processImageFrom(img builder.Image) error {
|
2016-01-03 22:45:06 -05:00
|
|
|
if img != nil {
|
2016-01-20 18:32:02 -05:00
|
|
|
b.image = img.ImageID()
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2016-01-20 18:32:02 -05:00
|
|
|
if img.RunConfig() != nil {
|
2016-04-23 15:59:17 -04:00
|
|
|
b.runConfig = img.RunConfig()
|
2016-01-03 22:45:06 -05:00
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2016-01-03 22:45:06 -05: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 18:41:09 -04:00
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
// Process ONBUILD triggers if they exist
|
2015-09-06 13:26:40 -04:00
|
|
|
if nTriggers := len(b.runConfig.OnBuild); nTriggers != 0 {
|
2015-08-26 22:45:18 -04:00
|
|
|
word := "trigger"
|
|
|
|
if nTriggers > 1 {
|
|
|
|
word = "triggers"
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
fmt.Fprintf(b.Stderr, "# Executing %d build %s...\n", nTriggers, word)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 16:33:30 -04:00
|
|
|
// Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
|
2015-09-06 13:26:40 -04:00
|
|
|
onBuildTriggers := b.runConfig.OnBuild
|
|
|
|
b.runConfig.OnBuild = []string{}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-10-13 16:14:35 -04:00
|
|
|
// parse the ONBUILD triggers by invoking the parser
|
2015-08-26 22:45:18 -04:00
|
|
|
for _, step := range onBuildTriggers {
|
2014-10-13 16:14:35 -04:00
|
|
|
ast, err := parser.Parse(strings.NewReader(step))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:14:35 -04: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 18:41:09 -04:00
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := b.dispatch(i, n); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04: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 15:49:17 -05:00
|
|
|
if !ok || b.options.NoCache || b.cacheBusted {
|
2015-02-25 13:27:32 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
2016-01-20 18:32:02 -05:00
|
|
|
cache, err := c.GetCachedImageOnBuild(b.image, b.runConfig)
|
2015-02-25 13:27:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
if len(cache) == 0 {
|
2015-10-06 08:43:51 -04:00
|
|
|
logrus.Debugf("[BUILDER] Cache miss: %s", b.runConfig.Cmd)
|
2015-02-25 13:27:32 -05:00
|
|
|
b.cacheBusted = true
|
|
|
|
return false, nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2015-02-25 13:27:32 -05:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
fmt.Fprintf(b.Stdout, " ---> Using cache\n")
|
2015-10-06 08:43:51 -04:00
|
|
|
logrus.Debugf("[BUILDER] Use cached version: %s", b.runConfig.Cmd)
|
2015-09-06 13:26:40 -04:00
|
|
|
b.image = string(cache)
|
|
|
|
|
2015-02-25 13:27:32 -05:00
|
|
|
return true, nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
func (b *Builder) create() (string, error) {
|
2014-10-28 17:06:23 -04:00
|
|
|
if b.image == "" && !b.noBaseImage {
|
2015-12-10 09:35:53 -05:00
|
|
|
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
b.runConfig.Image = b.image
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-12-18 13:36:17 -05:00
|
|
|
resources := container.Resources{
|
2015-12-29 15:49:17 -05: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 14:03:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: why not embed a hostconfig in builder?
|
2015-12-18 13:36:17 -05:00
|
|
|
hostConfig := &container.HostConfig{
|
2016-02-03 15:07:00 -05:00
|
|
|
Isolation: b.options.Isolation,
|
2015-12-29 15:49:17 -05:00
|
|
|
ShmSize: b.options.ShmSize,
|
2015-11-18 14:03:08 -05:00
|
|
|
Resources: resources,
|
2015-02-06 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
config := *b.runConfig
|
2014-09-04 11:43:28 -04:00
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
// Create the container
|
2015-12-16 11:56:49 -05:00
|
|
|
c, err := b.docker.ContainerCreate(types.ContainerCreateConfig{
|
2015-12-10 09:35:53 -05:00
|
|
|
Config: b.runConfig,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
})
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
2015-12-10 09:35:53 -05:00
|
|
|
return "", err
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2015-12-10 09:35:53 -05:00
|
|
|
for _, warning := range c.Warnings {
|
2015-09-06 13:26:40 -04:00
|
|
|
fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
|
2015-09-22 21:06:09 -04:00
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
b.tmpContainers[c.ID] = struct{}{}
|
|
|
|
fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(c.ID))
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2016-03-15 12:12:20 -04: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-08 20:00:00 -05:00
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
return c.ID, nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2016-03-22 13:49:03 -04:00
|
|
|
var errCancelled = errors.New("build cancelled")
|
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
func (b *Builder) run(cID string) (err error) {
|
|
|
|
errCh := make(chan error)
|
2015-10-27 20:29:21 -04:00
|
|
|
go func() {
|
2016-01-05 16:23:24 -05:00
|
|
|
errCh <- b.docker.ContainerAttachRaw(cID, nil, b.Stdout, b.Stderr, true)
|
2015-10-27 20:29:21 -04:00
|
|
|
}()
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2015-03-10 18:10:00 -04:00
|
|
|
finished := make(chan struct{})
|
2016-03-22 13:49:03 -04:00
|
|
|
var once sync.Once
|
|
|
|
finish := func() { close(finished) }
|
|
|
|
cancelErrCh := make(chan error, 1)
|
|
|
|
defer once.Do(finish)
|
2015-03-10 18:10:00 -04:00
|
|
|
go func() {
|
|
|
|
select {
|
2016-03-22 13:49:03 -04:00
|
|
|
case <-b.clientCtx.Done():
|
2015-12-10 09:35:53 -05:00
|
|
|
logrus.Debugln("Build cancelled, killing and removing container:", cID)
|
|
|
|
b.docker.ContainerKill(cID, 0)
|
|
|
|
b.removeContainer(cID)
|
2016-03-22 13:49:03 -04:00
|
|
|
cancelErrCh <- errCancelled
|
2015-03-10 18:10:00 -04:00
|
|
|
case <-finished:
|
2016-03-22 13:49:03 -04:00
|
|
|
cancelErrCh <- nil
|
2015-03-10 18:10:00 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
if err := b.docker.ContainerStart(cID, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-27 20:29:21 -04:00
|
|
|
// Block on reading output from container, stop on err or chan closed
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
return err
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-12-10 09:35:53 -05:00
|
|
|
if ret, _ := b.docker.ContainerWait(cID, -1); ret != 0 {
|
2015-09-06 13:26:40 -04:00
|
|
|
// TODO: change error type, because jsonmessage.JSONError assumes HTTP
|
2015-04-16 15:22:32 -04:00
|
|
|
return &jsonmessage.JSONError{
|
2016-02-29 06:28:37 -05:00
|
|
|
Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", strings.Join(b.runConfig.Cmd, " "), ret),
|
2014-08-05 18:41:09 -04:00
|
|
|
Code: ret,
|
|
|
|
}
|
|
|
|
}
|
2016-03-22 13:49:03 -04:00
|
|
|
once.Do(finish)
|
|
|
|
return <-cancelErrCh
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-10-16 05:18:10 -04:00
|
|
|
func (b *Builder) removeContainer(c string) error {
|
2015-12-04 15:34:43 -05:00
|
|
|
rmConfig := &types.ContainerRmConfig{
|
2015-10-16 05:18:10 -04:00
|
|
|
ForceRemove: true,
|
|
|
|
RemoveVolume: true,
|
|
|
|
}
|
2015-12-10 09:35:53 -05:00
|
|
|
if err := b.docker.ContainerRm(c, rmConfig); err != nil {
|
2015-10-16 05:18:10 -04:00
|
|
|
fmt.Fprintf(b.Stdout, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
func (b *Builder) clearTmp() {
|
|
|
|
for c := range b.tmpContainers {
|
2015-10-16 05:18:10 -04:00
|
|
|
if err := b.removeContainer(c); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
delete(b.tmpContainers, c)
|
|
|
|
fmt.Fprintf(b.Stdout, "Removing intermediate container %s\n", stringid.TruncateID(c))
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04: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 15:49:17 -05:00
|
|
|
if b.options.Dockerfile == "" {
|
2016-02-11 14:59:59 -05:00
|
|
|
b.options.Dockerfile = builder.DefaultDockerfileName
|
2015-12-29 15:49:17 -05:00
|
|
|
if _, _, err := b.context.Stat(b.options.Dockerfile); os.IsNotExist(err) {
|
|
|
|
lowercase := strings.ToLower(b.options.Dockerfile)
|
2015-11-04 16:42:08 -05:00
|
|
|
if _, _, err := b.context.Stat(lowercase); err == nil {
|
2015-12-29 15:49:17 -05:00
|
|
|
b.options.Dockerfile = lowercase
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 12:33:16 -04:00
|
|
|
err := b.parseDockerfile()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
dockerIgnore.Process([]string{b.options.Dockerfile})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) parseDockerfile() error {
|
2015-12-29 15:49:17 -05:00
|
|
|
f, err := b.context.Open(b.options.Dockerfile)
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2015-12-29 15:49:17 -05:00
|
|
|
return fmt.Errorf("Cannot locate specified Dockerfile: %s", b.options.Dockerfile)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-05-21 12:33:16 -04:00
|
|
|
defer f.Close()
|
2015-09-06 13:26:40 -04: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 18:41:09 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
if fi.Size() == 0 {
|
2015-12-29 15:49:17 -05:00
|
|
|
return fmt.Errorf("The Dockerfile (%s) cannot be empty", b.options.Dockerfile)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
b.dockerfile, err = parser.Parse(f)
|
|
|
|
if err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:26:40 -04: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 18:41:09 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
if _, ok := b.allowedBuildArgs[arg]; ok {
|
|
|
|
return true
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
return false
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|