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"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-10-22 18:16:42 +00:00
|
|
|
"net/http"
|
2014-08-05 22:41:09 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-15 22:28:06 +00:00
|
|
|
"runtime"
|
2014-08-05 22:41:09 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
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"
|
2017-03-20 22:22:29 +00:00
|
|
|
"github.com/docker/docker/builder/remotecontext"
|
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"
|
2014-11-24 23:47:42 +00:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2017-03-15 22:28:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2014-08-05 22:41:09 +00:00
|
|
|
)
|
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
func (b *Builder) commit(comment string) error {
|
2015-01-09 21:07:00 +00:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-24 22:19:45 +00:00
|
|
|
if !b.hasFromImage() {
|
2016-12-25 06:37:31 +00:00
|
|
|
return errors.New("Please provide a source image with `from` prior to commit")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2016-03-16 21:52:34 +00:00
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(b.runConfig, withCmdComment(comment))
|
|
|
|
hit, err := b.probeCache(b.image, runConfigWithCommentCmd)
|
|
|
|
if err != nil || hit {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
id, err := b.create(runConfigWithCommentCmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
return b.commitContainer(id, runConfigWithCommentCmd)
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
func (b *Builder) commitContainer(id string, containerConfig *container.Config) error {
|
2017-04-21 18:11:21 +00:00
|
|
|
if b.disableCommit {
|
|
|
|
return nil
|
|
|
|
}
|
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,
|
2017-04-25 16:21:43 +00:00
|
|
|
// TODO: this should be done by Commit()
|
|
|
|
Config: copyRunConfig(b.runConfig),
|
2016-03-16 23:07:41 +00:00
|
|
|
},
|
2017-04-21 19:08:11 +00:00
|
|
|
ContainerConfig: containerConfig,
|
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
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
// TODO: this function should return imageID and runConfig instead of setting
|
|
|
|
// then on the builder
|
2015-11-18 22:20:54 +00:00
|
|
|
b.image = imageID
|
2017-04-21 19:08:11 +00:00
|
|
|
b.imageContexts.update(imageID, b.runConfig)
|
2014-08-05 20:17:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-16 16:58:20 +00:00
|
|
|
type copyInfo struct {
|
2017-03-20 22:22:29 +00:00
|
|
|
root string
|
|
|
|
path string
|
|
|
|
hash string
|
2014-09-16 16:58:20 +00:00
|
|
|
decompress bool
|
|
|
|
}
|
|
|
|
|
2017-03-23 00:49:39 +00:00
|
|
|
func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalDecompression bool, cmdName string, imageSource *imageMount) error {
|
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
|
|
|
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
|
|
|
if urlutil.IsURL(orig) {
|
|
|
|
if !allowRemote {
|
|
|
|
return fmt.Errorf("Source can't be a URL for %s", cmdName)
|
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
remote, path, err := b.download(orig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(remote.Root())
|
|
|
|
h, err := remote.Hash(path)
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-29 08:17:35 +00:00
|
|
|
infos = append(infos, copyInfo{
|
2017-03-20 22:22:29 +00:00
|
|
|
root: remote.Root(),
|
|
|
|
path: path,
|
|
|
|
hash: h,
|
2016-11-29 08:17:35 +00:00
|
|
|
})
|
2015-09-06 17:26:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// not a URL
|
2017-03-23 00:49:39 +00:00
|
|
|
subInfos, err := b.calcCopyInfo(cmdName, orig, allowLocalDecompression, true, imageSource)
|
2015-09-06 17:26:40 +00:00
|
|
|
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 {
|
2016-12-25 06:37:31 +00:00
|
|
|
return errors.New("No source files were specified")
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
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
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if len(infos) == 1 {
|
2017-03-20 22:22:29 +00:00
|
|
|
info := infos[0]
|
|
|
|
srcHash = info.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 {
|
2017-03-20 22:22:29 +00:00
|
|
|
origs = append(origs, info.path)
|
|
|
|
hashs = append(hashs, info.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))
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
// TODO: should this have been using origPaths instead of srcHash in the comment?
|
2017-04-21 19:08:11 +00:00
|
|
|
runConfigWithCommentCmd := copyRunConfig(
|
|
|
|
b.runConfig,
|
|
|
|
withCmdCommentString(fmt.Sprintf("%s %s in %s ", cmdName, srcHash, dest)))
|
|
|
|
if hit, err := b.probeCache(b.image, runConfigWithCommentCmd); err != nil || hit {
|
2014-09-16 16:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-19 20:18:06 +00:00
|
|
|
container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{
|
2017-04-21 19:08:11 +00:00
|
|
|
Config: runConfigWithCommentCmd,
|
2016-12-19 20:18:06 +00:00
|
|
|
// Set a log config to override any default value set on the daemon
|
|
|
|
HostConfig: &container.HostConfig{LogConfig: defaultLogConfig},
|
|
|
|
})
|
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
|
|
|
|
2016-12-23 12:48:25 +00:00
|
|
|
// Twiddle the destination when it's a relative path - meaning, make it
|
2015-09-06 17:26:40 +00:00
|
|
|
// relative to the WORKINGDIR
|
2016-05-03 01:33:59 +00:00
|
|
|
if dest, err = normaliseDest(cmdName, b.runConfig.WorkingDir, dest); err != nil {
|
|
|
|
return err
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
for _, info := range infos {
|
2017-03-20 22:22:29 +00:00
|
|
|
if err := b.docker.CopyOnBuild(container.ID, dest, info.root, info.path, info.decompress); err != nil {
|
2014-09-16 16:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
return b.commitContainer(container.ID, runConfigWithCommentCmd)
|
2017-04-21 18:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type runConfigModifier func(*container.Config)
|
|
|
|
|
|
|
|
func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
|
|
|
|
copy := *runConfig
|
|
|
|
for _, modifier := range modifiers {
|
|
|
|
modifier(©)
|
|
|
|
}
|
|
|
|
return ©
|
|
|
|
}
|
|
|
|
|
|
|
|
func withCmd(cmd []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
// withCmdComment sets Cmd to a nop comment string. See withCmdCommentString for
|
|
|
|
// why there are two almost identical versions of this.
|
2017-04-21 18:11:21 +00:00
|
|
|
func withCmdComment(comment string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = append(getShell(runConfig), "#(nop) ", comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
// withCmdCommentString exists to maintain compatibility with older versions.
|
|
|
|
// A few instructions (workdir, copy, add) used a nop comment that is a single arg
|
|
|
|
// where as all the other instructions used a two arg comment string. This
|
|
|
|
// function implements the single arg version.
|
|
|
|
func withCmdCommentString(comment string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Cmd = append(getShell(runConfig), "#(nop) "+comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
func withEnv(env []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
runConfig.Env = env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 16:21:43 +00:00
|
|
|
// withEntrypointOverride sets an entrypoint on runConfig if the command is
|
|
|
|
// not empty. The entrypoint is left unmodified if command is empty.
|
|
|
|
//
|
|
|
|
// The dockerfile RUN instruction expect to run without an entrypoint
|
|
|
|
// so the runConfig entrypoint needs to be modified accordingly. ContainerCreate
|
|
|
|
// will change a []string{""} entrypoint to nil, so we probe the cache with the
|
|
|
|
// nil entrypoint.
|
|
|
|
func withEntrypointOverride(cmd []string, entrypoint []string) runConfigModifier {
|
|
|
|
return func(runConfig *container.Config) {
|
|
|
|
if len(cmd) > 0 {
|
|
|
|
runConfig.Entrypoint = entrypoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
// getShell is a helper function which gets the right shell for prefixing the
|
|
|
|
// shell-form of RUN, ENTRYPOINT and CMD instructions
|
|
|
|
func getShell(c *container.Config) []string {
|
|
|
|
if 0 == len(c.Shell) {
|
|
|
|
return append([]string{}, defaultShell[:]...)
|
|
|
|
}
|
|
|
|
return append([]string{}, c.Shell[:]...)
|
2014-09-16 16:58:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
func (b *Builder) download(srcURL string) (remote builder.Source, p string, err error) {
|
2015-09-06 17:26:40 +00:00
|
|
|
// 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
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
progressOutput := streamformatter.NewJSONProgressOutput(b.Output, true)
|
2015-11-14 00:59:01 +00:00
|
|
|
progressReader := progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Downloading")
|
2015-09-06 17:26:40 +00:00
|
|
|
// Download and dump result to tmp file
|
2017-03-20 22:22:29 +00:00
|
|
|
// TODO: add filehash directly
|
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)
|
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
|
|
|
|
2016-06-25 03:57:21 +00:00
|
|
|
tmpFile.Close()
|
|
|
|
|
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
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
lc, err := remotecontext.NewLazyContext(tmpDir)
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
return lc, filename, nil
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2017-03-24 11:07:08 +00:00
|
|
|
var windowsBlacklist = map[string]bool{
|
|
|
|
"c:\\": true,
|
|
|
|
"c:\\windows": true,
|
|
|
|
}
|
|
|
|
|
2017-03-23 00:49:39 +00:00
|
|
|
func (b *Builder) calcCopyInfo(cmdName, origPath string, allowLocalDecompression, allowWildcards bool, imageSource *imageMount) ([]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)
|
2017-03-15 22:28:06 +00:00
|
|
|
// validate windows paths from other images
|
2017-03-23 00:49:39 +00:00
|
|
|
if imageSource != nil && runtime.GOOS == "windows" {
|
2017-03-24 11:07:08 +00:00
|
|
|
p := strings.ToLower(filepath.Clean(origPath))
|
|
|
|
if !filepath.IsAbs(p) {
|
|
|
|
if filepath.VolumeName(p) != "" {
|
|
|
|
if p[len(p)-2:] == ":." { // case where clean returns weird c:. paths
|
|
|
|
p = p[:len(p)-1]
|
|
|
|
}
|
|
|
|
p += "\\"
|
|
|
|
} else {
|
|
|
|
p = filepath.Join("c:\\", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, blacklisted := windowsBlacklist[p]; blacklisted {
|
|
|
|
return nil, errors.New("copy from c:\\ or c:\\windows is not allowed on windows")
|
2017-03-15 22:28:06 +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
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
source := b.source
|
2017-03-15 22:28:06 +00:00
|
|
|
var err error
|
2017-03-23 00:49:39 +00:00
|
|
|
if imageSource != nil {
|
2017-03-20 22:22:29 +00:00
|
|
|
source, err = imageSource.context()
|
2017-03-15 22:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
if source == nil {
|
2017-03-15 22:28:06 +00:00
|
|
|
return nil, errors.Errorf("No context given. Impossible to use %s", cmdName)
|
|
|
|
}
|
|
|
|
|
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
|
2017-03-20 22:22:29 +00:00
|
|
|
if err := filepath.Walk(source.Root(), func(path string, info os.FileInfo, err error) error {
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
rel, err := remotecontext.Rel(source.Root(), path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rel == "." {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
if match, _ := filepath.Match(origPath, rel); !match {
|
2015-09-06 17:26:40 +00:00
|
|
|
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
|
2017-03-20 22:22:29 +00:00
|
|
|
subInfos, err := b.calcCopyInfo(cmdName, rel, allowLocalDecompression, false, imageSource)
|
2015-09-06 17:26:40 +00:00
|
|
|
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
|
2017-03-20 22:22:29 +00:00
|
|
|
hash, err := source.Hash(origPath)
|
2015-09-06 17:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
fi, err := remotecontext.StatAt(source, origPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
// TODO: remove, handle dirs in Hash()
|
|
|
|
copyInfos := []copyInfo{{root: source.Root(), path: origPath, hash: hash, decompress: allowLocalDecompression}}
|
|
|
|
|
2017-03-23 00:49:39 +00:00
|
|
|
if imageSource != nil {
|
2017-03-15 22:28:06 +00:00
|
|
|
// fast-cache based on imageID
|
2017-03-23 00:49:39 +00:00
|
|
|
if h, ok := b.imageContexts.getCache(imageSource.id, origPath); ok {
|
2017-03-20 22:22:29 +00:00
|
|
|
copyInfos[0].hash = h.(string)
|
2017-03-15 22:28:06 +00:00
|
|
|
return copyInfos, nil
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 13:41:02 +00:00
|
|
|
|
|
|
|
// Deal with the single file case
|
|
|
|
if !fi.IsDir() {
|
2017-03-20 22:22:29 +00:00
|
|
|
copyInfos[0].hash = "file:" + copyInfos[0].hash
|
2015-09-06 17:26:40 +00:00
|
|
|
return copyInfos, nil
|
2014-09-22 13:41:02 +00:00
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
|
|
|
|
fp, err := remotecontext.FullPath(source, origPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-22 13:41:02 +00:00
|
|
|
// Must be a dir
|
|
|
|
var subfiles []string
|
2017-03-20 22:22:29 +00:00
|
|
|
err = filepath.Walk(fp, func(path string, info os.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
|
|
|
}
|
2017-03-20 22:22:29 +00:00
|
|
|
rel, err := remotecontext.Rel(source.Root(), path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rel == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hash, err := source.Hash(rel)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
// we already checked handleHash above
|
2017-03-20 22:22:29 +00:00
|
|
|
subfiles = append(subfiles, hash)
|
2015-09-06 17:26:40 +00:00
|
|
|
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, ",")))
|
2017-03-20 22:22:29 +00:00
|
|
|
copyInfos[0].hash = "dir:" + hex.EncodeToString(hasher.Sum(nil))
|
2017-03-23 00:49:39 +00:00
|
|
|
if imageSource != nil {
|
2017-03-20 22:22:29 +00:00
|
|
|
b.imageContexts.setCache(imageSource.id, origPath, copyInfos[0].hash)
|
2017-03-15 22:28:06 +00:00
|
|
|
}
|
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-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-04-23 19:59:17 +00:00
|
|
|
b.runConfig = img.RunConfig()
|
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
|
2016-12-23 12:48:25 +00:00
|
|
|
// have one as it's set by HCS
|
2016-01-04 03:45:06 +00:00
|
|
|
if system.DefaultPathEnv != "" {
|
2017-04-04 16:28:59 +00:00
|
|
|
if _, ok := b.runConfigEnvMapping()["PATH"]; !ok {
|
2016-01-04 03:45:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-29 07:03:26 +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
|
|
|
|
2017-02-15 19:49:17 +00:00
|
|
|
// Reset stdin settings as all build actions run without stdin
|
|
|
|
b.runConfig.OpenStdin = false
|
|
|
|
b.runConfig.StdinOnce = false
|
|
|
|
|
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 {
|
2017-04-12 17:47:19 +00:00
|
|
|
result, err := parser.Parse(strings.NewReader(step))
|
2014-10-13 20:14:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:47:19 +00:00
|
|
|
for _, n := range result.AST.Children {
|
2017-04-04 20:34:19 +00:00
|
|
|
if err := checkDispatch(n); err != nil {
|
2016-09-13 04:06:04 +00:00
|
|
|
return err
|
2014-10-13 20:14:35 +00:00
|
|
|
}
|
2017-04-04 20:34:19 +00:00
|
|
|
|
|
|
|
upperCasedCmd := strings.ToUpper(n.Value)
|
|
|
|
switch upperCasedCmd {
|
|
|
|
case "ONBUILD":
|
|
|
|
return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
case "MAINTAINER", "FROM":
|
|
|
|
return errors.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:47:19 +00:00
|
|
|
if err := dispatchFromDockerfile(b, result); err != nil {
|
2017-04-04 20:34:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:38:00 +00:00
|
|
|
// probeCache checks if cache match can be found for current build instruction.
|
2015-09-06 17:26:40 +00:00
|
|
|
// 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)`.
|
2017-04-22 22:34:04 +00:00
|
|
|
func (b *Builder) probeCache(parentID string, runConfig *container.Config) (bool, error) {
|
2016-09-22 21:38:00 +00:00
|
|
|
c := b.imageCache
|
|
|
|
if c == nil || b.options.NoCache || b.cacheBusted {
|
2015-02-25 18:27:32 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2017-04-22 22:34:04 +00:00
|
|
|
cache, err := c.GetCache(parentID, 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 {
|
2017-04-21 18:11:21 +00:00
|
|
|
logrus.Debugf("[BUILDER] Cache miss: %s", 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
|
|
|
|
2016-12-25 06:37:31 +00:00
|
|
|
fmt.Fprint(b.Stdout, " ---> Using cache\n")
|
2017-04-21 18:11:21 +00:00
|
|
|
logrus.Debugf("[BUILDER] Use cached version: %s", runConfig.Cmd)
|
2015-09-06 17:26:40 +00:00
|
|
|
b.image = string(cache)
|
2017-04-21 18:11:21 +00:00
|
|
|
b.imageContexts.update(b.image, runConfig)
|
2015-09-06 17:26:40 +00:00
|
|
|
|
2015-02-25 18:27:32 +00:00
|
|
|
return true, nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:11:21 +00:00
|
|
|
func (b *Builder) create(runConfig *container.Config) (string, error) {
|
2017-02-24 22:19:45 +00:00
|
|
|
if !b.hasFromImage() {
|
2016-12-25 06:37:31 +00:00
|
|
|
return "", errors.New("Please provide a source image with `from` prior to run")
|
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-06-07 19:15:50 +00:00
|
|
|
SecurityOpt: b.options.SecurityOpt,
|
|
|
|
Isolation: b.options.Isolation,
|
|
|
|
ShmSize: b.options.ShmSize,
|
|
|
|
Resources: resources,
|
2016-03-06 12:29:23 +00:00
|
|
|
NetworkMode: container.NetworkMode(b.options.NetworkMode),
|
2016-12-19 20:18:06 +00:00
|
|
|
// Set a log config to override any default value set on the daemon
|
2017-01-13 15:01:58 +00:00
|
|
|
LogConfig: defaultLogConfig,
|
|
|
|
ExtraHosts: b.options.ExtraHosts,
|
2015-02-06 14:33:01 +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{
|
2017-04-21 18:11:21 +00:00
|
|
|
Config: runConfig,
|
2015-12-10 14:35:53 +00:00
|
|
|
HostConfig: hostConfig,
|
2016-11-30 18:22:07 +00:00
|
|
|
})
|
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))
|
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")
|
|
|
|
|
2017-04-21 19:08:11 +00:00
|
|
|
func (b *Builder) run(cID string, cmd []string) (err error) {
|
2017-03-15 11:33:04 +00:00
|
|
|
attached := make(chan struct{})
|
2015-12-10 14:35:53 +00:00
|
|
|
errCh := make(chan error)
|
2015-10-28 00:29:21 +00:00
|
|
|
go func() {
|
2017-03-15 11:33:04 +00:00
|
|
|
errCh <- b.docker.ContainerAttachRaw(cID, nil, b.Stdout, b.Stderr, true, attached)
|
2015-10-28 00:29:21 +00:00
|
|
|
}()
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2017-03-15 11:33:04 +00:00
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
case <-attached:
|
|
|
|
}
|
|
|
|
|
2015-03-10 22:10:00 +00:00
|
|
|
finished := make(chan struct{})
|
2016-03-22 17:49:03 +00:00
|
|
|
cancelErrCh := make(chan error, 1)
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-11-30 18:22:07 +00:00
|
|
|
if err := b.docker.ContainerStart(cID, nil, "", ""); err != nil {
|
2016-09-19 19:01:44 +00:00
|
|
|
close(finished)
|
|
|
|
if cancelErr := <-cancelErrCh; cancelErr != nil {
|
|
|
|
logrus.Debugf("Build cancelled (%v) and got an error from ContainerStart: %v",
|
|
|
|
cancelErr, err)
|
|
|
|
}
|
2015-12-10 14:35:53 +00:00
|
|
|
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 {
|
2016-09-19 19:01:44 +00:00
|
|
|
close(finished)
|
|
|
|
if cancelErr := <-cancelErrCh; cancelErr != nil {
|
|
|
|
logrus.Debugf("Build cancelled (%v) and got an error from errCh: %v",
|
|
|
|
cancelErr, err)
|
|
|
|
}
|
2015-10-28 00:29:21 +00:00
|
|
|
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 {
|
2016-09-19 19:01:44 +00:00
|
|
|
close(finished)
|
|
|
|
if cancelErr := <-cancelErrCh; cancelErr != nil {
|
|
|
|
logrus.Debugf("Build cancelled (%v) and got a non-zero code from ContainerWait: %d",
|
|
|
|
cancelErr, ret)
|
|
|
|
}
|
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{
|
2017-04-21 19:08:11 +00:00
|
|
|
Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", strings.Join(cmd, " "), ret),
|
2014-08-05 22:41:09 +00:00
|
|
|
Code: ret,
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 19:01:44 +00:00
|
|
|
close(finished)
|
2016-03-22 17:49:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|