2014-08-15 12:29:35 -04:00
|
|
|
package builder
|
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"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/archive"
|
|
|
|
"github.com/docker/docker/daemon"
|
|
|
|
imagepkg "github.com/docker/docker/image"
|
2014-08-13 06:07:41 -04:00
|
|
|
"github.com/docker/docker/pkg/log"
|
2014-08-05 18:41:09 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
|
|
"github.com/docker/docker/pkg/system"
|
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) readContext(context io.Reader) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
tmpdirPath, err := ioutil.TempDir("", "docker-build")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decompressedStream, err := archive.DecompressStream(context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-21 16:12:52 -04:00
|
|
|
if b.context, err = tarsum.NewTarSum(decompressedStream, true, tarsum.Version0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
if err := archive.Untar(b.context, tmpdirPath, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.contextPath = tmpdirPath
|
2014-08-05 18:41:09 -04:00
|
|
|
return nil
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) commit(id string, autoCmd []string, comment string) error {
|
2014-08-05 16:17:40 -04:00
|
|
|
if b.image == "" {
|
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Image = b.image
|
2014-08-05 16:17:40 -04:00
|
|
|
if id == "" {
|
2014-08-11 11:44:31 -04:00
|
|
|
cmd := b.Config.Cmd
|
|
|
|
b.Config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
|
|
|
|
defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-04 11:43:28 -04:00
|
|
|
container, err := b.create()
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
id = container.ID
|
|
|
|
|
|
|
|
if err := container.Mount(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
container := b.Daemon.Get(id)
|
2014-08-05 16:17:40 -04:00
|
|
|
if container == nil {
|
|
|
|
return fmt.Errorf("An error occured while creating the container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Actually copy the struct
|
2014-08-11 11:44:31 -04:00
|
|
|
autoConfig := *b.Config
|
2014-08-05 16:17:40 -04:00
|
|
|
autoConfig.Cmd = autoCmd
|
2014-08-30 07:34:09 -04:00
|
|
|
|
2014-08-05 16:17:40 -04:00
|
|
|
// Commit the container
|
2014-08-26 15:25:44 -04:00
|
|
|
image, err := b.Daemon.Commit(container, "", "", "", b.maintainer, true, &autoConfig)
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.image = image.ID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) runContextCommand(args []string, allowRemote bool, allowDecompression 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-08-05 18:41:09 -04:00
|
|
|
if len(args) != 2 {
|
|
|
|
return fmt.Errorf("Invalid %s format", cmdName)
|
2014-08-05 16:17:40 -04:00
|
|
|
}
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
orig := args[0]
|
|
|
|
dest := args[1]
|
2014-08-05 16:17:40 -04:00
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
cmd := b.Config.Cmd
|
|
|
|
b.Config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, orig, dest)}
|
|
|
|
defer func(cmd []string) { b.Config.Cmd = cmd }(cmd)
|
|
|
|
b.Config.Image = b.image
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
origPath = orig
|
|
|
|
destPath = dest
|
|
|
|
remoteHash string
|
|
|
|
isRemote bool
|
|
|
|
decompress = true
|
|
|
|
)
|
|
|
|
|
|
|
|
isRemote = utils.IsURL(orig)
|
|
|
|
if isRemote && !allowRemote {
|
|
|
|
return fmt.Errorf("Source can't be an URL for %s", cmdName)
|
|
|
|
} else if utils.IsURL(orig) {
|
|
|
|
// Initiate the download
|
|
|
|
resp, err := utils.Download(orig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a tmp dir
|
|
|
|
tmpDirName, err := ioutil.TempDir(b.contextPath, "docker-remote")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a tmp file within our tmp dir
|
|
|
|
tmpFileName := path.Join(tmpDirName, "tmp")
|
|
|
|
tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDirName)
|
|
|
|
|
|
|
|
// Download and dump result to tmp file
|
2014-09-02 20:17:08 -04:00
|
|
|
if _, err := io.Copy(tmpFile, utils.ProgressReader(resp.Body, int(resp.ContentLength), b.OutOld, b.StreamFormatter, true, "", "Downloading")); err != nil {
|
2014-08-05 16:17:40 -04:00
|
|
|
tmpFile.Close()
|
|
|
|
return err
|
|
|
|
}
|
2014-09-02 20:17:08 -04:00
|
|
|
fmt.Fprintf(b.OutStream, "\n")
|
2014-08-05 16:17:40 -04:00
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
// Remove the mtime of the newly created tmp file
|
|
|
|
if err := system.UtimesNano(tmpFileName, make([]syscall.Timespec, 2)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
|
|
|
|
|
|
|
|
// Process the checksum
|
|
|
|
r, err := archive.Tar(tmpFileName, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-10 21:56:20 -04:00
|
|
|
tarSum, err := tarsum.NewTarSum(r, true, tarsum.Version0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 16:17:40 -04:00
|
|
|
if _, err := io.Copy(ioutil.Discard, tarSum); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
remoteHash = tarSum.Sum(nil)
|
|
|
|
r.Close()
|
|
|
|
|
|
|
|
// If the destination is a directory, figure out the filename.
|
|
|
|
if strings.HasSuffix(dest, "/") {
|
|
|
|
u, err := url.Parse(orig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
path := u.Path
|
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
path = path[:len(path)-1]
|
|
|
|
}
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
filename := parts[len(parts)-1]
|
|
|
|
if filename == "" {
|
|
|
|
return fmt.Errorf("cannot determine filename from url: %s", u)
|
|
|
|
}
|
|
|
|
destPath = dest + filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.checkPathForAddition(origPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash path and check the cache
|
2014-08-26 15:25:44 -04:00
|
|
|
if b.UtilizeCache {
|
2014-08-05 16:17:40 -04:00
|
|
|
var (
|
|
|
|
hash string
|
|
|
|
sums = b.context.GetSums()
|
|
|
|
)
|
|
|
|
|
|
|
|
if remoteHash != "" {
|
|
|
|
hash = remoteHash
|
|
|
|
} else if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil {
|
|
|
|
return err
|
|
|
|
} else if fi.IsDir() {
|
|
|
|
var subfiles []string
|
|
|
|
for file, sum := range sums {
|
|
|
|
absFile := path.Join(b.contextPath, file)
|
|
|
|
absOrigPath := path.Join(b.contextPath, origPath)
|
|
|
|
if strings.HasPrefix(absFile, absOrigPath) {
|
|
|
|
subfiles = append(subfiles, sum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(subfiles)
|
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(subfiles, ",")))
|
|
|
|
hash = "dir:" + hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
} else {
|
|
|
|
if origPath[0] == '/' && len(origPath) > 1 {
|
|
|
|
origPath = origPath[1:]
|
|
|
|
}
|
|
|
|
origPath = strings.TrimPrefix(origPath, "./")
|
|
|
|
if h, ok := sums[origPath]; ok {
|
|
|
|
hash = "file:" + h
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, hash, dest)}
|
2014-08-05 16:17:40 -04:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// If we do not have a hash, never use the cache
|
|
|
|
if hit && hash != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the container
|
2014-08-26 15:25:44 -04:00
|
|
|
container, _, err := b.Daemon.Create(b.Config, "")
|
2014-08-05 16:17:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
b.TmpContainers[container.ID] = struct{}{}
|
2014-08-05 16:17:40 -04:00
|
|
|
|
|
|
|
if err := container.Mount(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
|
|
|
if !allowDecompression || isRemote {
|
|
|
|
decompress = false
|
|
|
|
}
|
|
|
|
if err := b.addContext(container, origPath, destPath, decompress); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.commit(container.ID, cmd, fmt.Sprintf("%s %s in %s", cmdName, orig, dest)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) pullImage(name string) (*imagepkg.Image, error) {
|
2014-08-05 18:41:09 -04:00
|
|
|
remote, tag := parsers.ParseRepositoryTag(name)
|
2014-09-03 11:27:25 -04:00
|
|
|
if tag == "" {
|
|
|
|
tag = "latest"
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
pullRegistryAuth := b.AuthConfig
|
|
|
|
if len(b.AuthConfigFile.Configs) > 0 {
|
2014-08-05 18:41:09 -04:00
|
|
|
// The request came with a full auth config file, we prefer to use that
|
|
|
|
endpoint, _, err := registry.ResolveRepositoryName(remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
resolvedAuth := b.AuthConfigFile.ResolveAuthConfig(endpoint)
|
2014-08-05 18:41:09 -04:00
|
|
|
pullRegistryAuth = &resolvedAuth
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
job := b.Engine.Job("pull", remote, tag)
|
|
|
|
job.SetenvBool("json", b.StreamFormatter.Json())
|
2014-08-05 18:41:09 -04:00
|
|
|
job.SetenvBool("parallel", true)
|
|
|
|
job.SetenvJson("authConfig", pullRegistryAuth)
|
2014-08-26 15:25:44 -04:00
|
|
|
job.Stdout.Add(b.OutOld)
|
2014-08-05 18:41:09 -04:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
image, err := b.Daemon.Repositories().LookupImage(name)
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return image, nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) processImageFrom(img *imagepkg.Image) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
b.image = img.ID
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
if img.Config != nil {
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config = img.Config
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
|
2014-08-19 07:14:21 -04:00
|
|
|
if len(b.Config.Env) == 0 {
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Env = append(b.Config.Env, "PATH="+daemon.DefaultPathEnv)
|
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
|
2014-08-11 11:44:31 -04:00
|
|
|
if nTriggers := len(b.Config.OnBuild); nTriggers != 0 {
|
2014-08-26 15:25:44 -04:00
|
|
|
fmt.Fprintf(b.ErrStream, "# Executing %d build triggers\n", nTriggers)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the ONBUILD triggers, and remove them from the config, since the config will be commited.
|
2014-08-11 11:44:31 -04:00
|
|
|
onBuildTriggers := b.Config.OnBuild
|
|
|
|
b.Config.OnBuild = []string{}
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
// FIXME rewrite this so that builder/parser is used; right now steps in
|
|
|
|
// onbuild are muted because we have no good way to represent the step
|
|
|
|
// number
|
|
|
|
for _, step := range onBuildTriggers {
|
|
|
|
splitStep := strings.Split(step, " ")
|
|
|
|
stepInstruction := strings.ToUpper(strings.Trim(splitStep[0], " "))
|
|
|
|
switch stepInstruction {
|
|
|
|
case "ONBUILD":
|
|
|
|
return fmt.Errorf("Source image contains forbidden chained `ONBUILD ONBUILD` trigger: %s", step)
|
|
|
|
case "MAINTAINER", "FROM":
|
|
|
|
return fmt.Errorf("Source image contains forbidden %s trigger: %s", stepInstruction, step)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME we have to run the evaluator manually here. This does not belong
|
2014-08-26 15:25:44 -04:00
|
|
|
// in this function. Once removed, the init() in evaluator.go should no
|
|
|
|
// longer be necessary.
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
if f, ok := evaluateTable[strings.ToLower(stepInstruction)]; ok {
|
2014-08-13 06:07:41 -04:00
|
|
|
if err := f(b, splitStep[1:], nil); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("%s doesn't appear to be a valid Dockerfile instruction", splitStep[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
// probeCache checks to see if image-caching is enabled (`b.UtilizeCache`)
|
2014-08-11 11:44:31 -04:00
|
|
|
// and if so attempts to look up the current `b.image` and `b.Config` pair
|
2014-08-26 15:25:44 -04:00
|
|
|
// in the current server `b.Daemon`. If an image is found, probeCache returns
|
2014-08-05 18:41:09 -04:00
|
|
|
// `(true, nil)`. If no image is found, it returns `(false, nil)`. If there
|
|
|
|
// is any error, it returns `(false, err)`.
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) probeCache() (bool, error) {
|
|
|
|
if b.UtilizeCache {
|
|
|
|
if cache, err := b.Daemon.ImageGetCached(b.image, b.Config); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
return false, err
|
|
|
|
} else if cache != nil {
|
2014-08-26 15:25:44 -04:00
|
|
|
fmt.Fprintf(b.OutStream, " ---> Using cache\n")
|
2014-08-13 06:07:41 -04:00
|
|
|
log.Debugf("[BUILDER] Use cached version")
|
2014-08-05 18:41:09 -04:00
|
|
|
b.image = cache.ID
|
|
|
|
return true, nil
|
|
|
|
} else {
|
2014-08-13 06:07:41 -04:00
|
|
|
log.Debugf("[BUILDER] Cache miss")
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) create() (*daemon.Container, error) {
|
2014-08-05 18:41:09 -04:00
|
|
|
if b.image == "" {
|
|
|
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
b.Config.Image = b.image
|
2014-08-05 18:41:09 -04:00
|
|
|
|
2014-09-04 11:43:28 -04:00
|
|
|
config := *b.Config
|
|
|
|
|
2014-08-05 18:41:09 -04:00
|
|
|
// Create the container
|
2014-09-04 11:43:28 -04:00
|
|
|
c, warnings, err := b.Daemon.Create(b.Config, "")
|
2014-08-05 18:41:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-04 11:43:28 -04:00
|
|
|
for _, warning := range warnings {
|
|
|
|
fmt.Fprintf(b.OutStream, " ---> [Warning] %s\n", warning)
|
|
|
|
}
|
2014-08-26 15:25:44 -04:00
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
b.TmpContainers[c.ID] = struct{}{}
|
2014-08-26 15:25:44 -04:00
|
|
|
fmt.Fprintf(b.OutStream, " ---> Running in %s\n", utils.TruncateID(c.ID))
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
// override the entry point that may have been picked up from the base image
|
2014-09-04 11:43:28 -04:00
|
|
|
c.Path = config.Cmd[0]
|
|
|
|
c.Args = config.Cmd[1:]
|
2014-08-05 18:41:09 -04:00
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) run(c *daemon.Container) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
var errCh chan error
|
2014-08-26 15:25:44 -04:00
|
|
|
if b.Verbose {
|
2014-08-05 18:41:09 -04:00
|
|
|
errCh = utils.Go(func() error {
|
|
|
|
// FIXME: call the 'attach' job so that daemon.Attach can be made private
|
|
|
|
//
|
|
|
|
// FIXME (LK4D4): Also, maybe makes sense to call "logs" job, it is like attach
|
|
|
|
// but without hijacking for stdin. Also, with attach there can be race
|
|
|
|
// condition because of some output already was printed before it.
|
2014-09-09 00:19:32 -04:00
|
|
|
return <-b.Daemon.Attach(&c.StreamConfig, c.Config.OpenStdin, c.Config.StdinOnce, c.Config.Tty, nil, nil, b.OutStream, b.ErrStream)
|
2014-08-05 18:41:09 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//start the container
|
|
|
|
if err := c.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if errCh != nil {
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for it to finish
|
2014-08-31 11:20:35 -04:00
|
|
|
if ret, _ := c.WaitStop(-1 * time.Second); ret != 0 {
|
2014-08-05 18:41:09 -04:00
|
|
|
err := &utils.JSONError{
|
2014-08-11 11:44:31 -04:00
|
|
|
Message: fmt.Sprintf("The command %v returned a non-zero code: %d", b.Config.Cmd, ret),
|
2014-08-05 18:41:09 -04:00
|
|
|
Code: ret,
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) checkPathForAddition(orig string) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
origPath := path.Join(b.contextPath, orig)
|
2014-08-13 06:07:41 -04:00
|
|
|
origPath, err := filepath.EvalSymlinks(origPath)
|
|
|
|
if err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(origPath, b.contextPath) {
|
|
|
|
return fmt.Errorf("Forbidden path outside the build context: %s (%s)", orig, origPath)
|
|
|
|
}
|
2014-08-13 06:07:41 -04:00
|
|
|
if _, err := os.Stat(origPath); err != nil {
|
2014-08-05 18:41:09 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) addContext(container *daemon.Container, orig, dest string, decompress bool) error {
|
2014-08-05 18:41:09 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
destExists = true
|
|
|
|
origPath = path.Join(b.contextPath, orig)
|
|
|
|
destPath = path.Join(container.RootfsPath(), dest)
|
|
|
|
)
|
|
|
|
|
|
|
|
if destPath != container.RootfsPath() {
|
|
|
|
destPath, err = symlink.FollowSymlinkInScope(destPath, container.RootfsPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve the trailing '/'
|
|
|
|
if strings.HasSuffix(dest, "/") || dest == "." {
|
|
|
|
destPath = destPath + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
destStat, err := os.Stat(destPath)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
destExists = false
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
return copyAsDirectory(origPath, destPath, destExists)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are adding a remote file (or we've been told not to decompress), do not try to untar it
|
|
|
|
if decompress {
|
|
|
|
// First try to unpack the source as an archive
|
|
|
|
// to support the untar feature we need to clean up the path a little bit
|
|
|
|
// because tar is very forgiving. First we need to strip off the archive's
|
|
|
|
// filename from the path but this is only added if it does not end in / .
|
|
|
|
tarDest := destPath
|
|
|
|
if strings.HasSuffix(tarDest, "/") {
|
|
|
|
tarDest = filepath.Dir(destPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to successfully untar the orig
|
|
|
|
if err := archive.UntarPath(origPath, tarDest); err == nil {
|
|
|
|
return nil
|
|
|
|
} else if err != io.EOF {
|
2014-08-13 06:07:41 -04:00
|
|
|
log.Debugf("Couldn't untar %s to %s: %s", origPath, tarDest, err)
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := archive.CopyWithTar(origPath, destPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resPath := destPath
|
|
|
|
if destExists && destStat.IsDir() {
|
|
|
|
resPath = path.Join(destPath, path.Base(origPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixPermissions(resPath, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyAsDirectory(source, destination string, destinationExists bool) error {
|
|
|
|
if err := archive.CopyWithTar(source, destination); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if destinationExists {
|
|
|
|
files, err := ioutil.ReadDir(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if err := fixPermissions(filepath.Join(destination, file.Name()), 0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixPermissions(destination, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixPermissions(destination string, uid, gid int) error {
|
|
|
|
return filepath.Walk(destination, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err := os.Lchown(path, uid, gid); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
func (b *Builder) clearTmp() {
|
2014-08-19 07:14:21 -04:00
|
|
|
for c := range b.TmpContainers {
|
2014-08-26 15:25:44 -04:00
|
|
|
tmp := b.Daemon.Get(c)
|
|
|
|
if err := b.Daemon.Destroy(tmp); err != nil {
|
|
|
|
fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %s\n", utils.TruncateID(c), err.Error())
|
2014-08-05 18:41:09 -04:00
|
|
|
} else {
|
2014-08-19 07:14:21 -04:00
|
|
|
delete(b.TmpContainers, c)
|
2014-08-26 15:25:44 -04:00
|
|
|
fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", utils.TruncateID(c))
|
2014-08-05 18:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|