2014-03-11 17:40:06 +00:00
|
|
|
package server
|
2013-05-24 01:33:31 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-27 00:42:05 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2013-05-24 01:33:31 +00:00
|
|
|
"encoding/json"
|
2013-12-20 12:26:11 +00:00
|
|
|
"errors"
|
2013-05-24 01:33:31 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-05-28 20:37:49 +00:00
|
|
|
"io/ioutil"
|
2013-07-08 07:43:22 +00:00
|
|
|
"net/url"
|
2013-05-24 01:33:31 +00:00
|
|
|
"os"
|
2013-05-28 20:37:49 +00:00
|
|
|
"path"
|
2013-12-12 05:33:15 +00:00
|
|
|
"path/filepath"
|
2013-05-24 01:33:31 +00:00
|
|
|
"reflect"
|
2013-07-09 07:01:45 +00:00
|
|
|
"regexp"
|
2013-12-27 00:02:15 +00:00
|
|
|
"sort"
|
2013-05-24 01:33:31 +00:00
|
|
|
"strings"
|
2014-05-17 21:09:05 +00:00
|
|
|
"syscall"
|
2014-05-05 23:48:56 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/archive"
|
|
|
|
"github.com/dotcloud/docker/daemon"
|
|
|
|
"github.com/dotcloud/docker/nat"
|
2014-05-13 17:54:08 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/symlink"
|
2014-05-17 21:09:05 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-05-05 23:48:56 +00:00
|
|
|
"github.com/dotcloud/docker/registry"
|
|
|
|
"github.com/dotcloud/docker/runconfig"
|
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-05-24 01:33:31 +00:00
|
|
|
)
|
|
|
|
|
2013-12-20 12:26:11 +00:00
|
|
|
var (
|
|
|
|
ErrDockerfileEmpty = errors.New("Dockerfile cannot be empty")
|
|
|
|
)
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
type BuildFile interface {
|
2013-06-15 16:38:18 +00:00
|
|
|
Build(io.Reader) (string, error)
|
2013-05-24 01:33:31 +00:00
|
|
|
CmdFrom(string) error
|
|
|
|
CmdRun(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type buildFile struct {
|
2014-04-17 21:43:01 +00:00
|
|
|
daemon *daemon.Daemon
|
|
|
|
srv *Server
|
2013-05-24 01:33:31 +00:00
|
|
|
|
2013-12-27 00:02:15 +00:00
|
|
|
image string
|
|
|
|
maintainer string
|
2014-02-12 04:04:39 +00:00
|
|
|
config *runconfig.Config
|
2013-12-27 00:02:15 +00:00
|
|
|
|
|
|
|
contextPath string
|
|
|
|
context *utils.TarSum
|
|
|
|
|
2013-08-02 16:18:54 +00:00
|
|
|
verbose bool
|
|
|
|
utilizeCache bool
|
2013-09-10 18:39:47 +00:00
|
|
|
rm bool
|
2014-05-16 11:47:33 +00:00
|
|
|
forceRm bool
|
2013-05-24 01:33:31 +00:00
|
|
|
|
2014-05-07 22:59:13 +00:00
|
|
|
authConfig *registry.AuthConfig
|
2014-03-11 00:16:58 +00:00
|
|
|
configFile *registry.ConfigFile
|
2013-12-06 22:27:10 +00:00
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
tmpContainers map[string]struct{}
|
|
|
|
tmpImages map[string]struct{}
|
|
|
|
|
2013-12-02 19:43:41 +00:00
|
|
|
outStream io.Writer
|
|
|
|
errStream io.Writer
|
|
|
|
|
|
|
|
// Deprecated, original writer used for ImagePull. To be removed.
|
|
|
|
outOld io.Writer
|
|
|
|
sf *utils.StreamFormatter
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:39:47 +00:00
|
|
|
func (b *buildFile) clearTmp(containers map[string]struct{}) {
|
2013-05-24 01:33:31 +00:00
|
|
|
for c := range containers {
|
2014-04-17 21:43:01 +00:00
|
|
|
tmp := b.daemon.Get(c)
|
|
|
|
if err := b.daemon.Destroy(tmp); err != nil {
|
2014-02-12 23:23:42 +00:00
|
|
|
fmt.Fprintf(b.outStream, "Error removing intermediate container %s: %s\n", utils.TruncateID(c), err.Error())
|
|
|
|
} else {
|
2014-04-10 11:43:25 +00:00
|
|
|
delete(containers, c)
|
2014-02-12 23:23:42 +00:00
|
|
|
fmt.Fprintf(b.outStream, "Removing intermediate container %s\n", utils.TruncateID(c))
|
|
|
|
}
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdFrom(name string) error {
|
2014-04-17 21:43:01 +00:00
|
|
|
image, err := b.daemon.Repositories().LookupImage(name)
|
2013-05-24 01:33:31 +00:00
|
|
|
if err != nil {
|
2014-04-17 21:43:01 +00:00
|
|
|
if b.daemon.Graph().IsNotExist(err) {
|
2013-07-09 15:06:10 +00:00
|
|
|
remote, tag := utils.ParseRepositoryTag(name)
|
2014-05-07 22:59:13 +00:00
|
|
|
pullRegistryAuth := b.authConfig
|
|
|
|
if len(b.configFile.Configs) > 0 {
|
|
|
|
// The request came with a full auth config file, we prefer to use that
|
|
|
|
endpoint, _, err := registry.ResolveRepositoryName(remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resolvedAuth := b.configFile.ResolveAuthConfig(endpoint)
|
|
|
|
pullRegistryAuth = &resolvedAuth
|
|
|
|
}
|
2014-01-22 21:35:35 +00:00
|
|
|
job := b.srv.Eng.Job("pull", remote, tag)
|
|
|
|
job.SetenvBool("json", b.sf.Json())
|
|
|
|
job.SetenvBool("parallel", true)
|
2014-05-07 22:59:13 +00:00
|
|
|
job.SetenvJson("authConfig", pullRegistryAuth)
|
2014-01-22 21:35:35 +00:00
|
|
|
job.Stdout.Add(b.outOld)
|
|
|
|
if err := job.Run(); err != nil {
|
2013-05-24 01:33:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-04-17 21:43:01 +00:00
|
|
|
image, err = b.daemon.Repositories().LookupImage(name)
|
2013-05-24 01:33:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
b.image = image.ID
|
2014-02-12 04:04:39 +00:00
|
|
|
b.config = &runconfig.Config{}
|
2013-11-04 21:20:14 +00:00
|
|
|
if image.Config != nil {
|
|
|
|
b.config = image.Config
|
|
|
|
}
|
2013-07-09 07:01:45 +00:00
|
|
|
if b.config.Env == nil || len(b.config.Env) == 0 {
|
2014-04-17 21:43:01 +00:00
|
|
|
b.config.Env = append(b.config.Env, "HOME=/", "PATH="+daemon.DefaultPathEnv)
|
2013-07-09 07:01:45 +00:00
|
|
|
}
|
2013-12-04 10:36:21 +00:00
|
|
|
// Process ONBUILD triggers if they exist
|
|
|
|
if nTriggers := len(b.config.OnBuild); nTriggers != 0 {
|
|
|
|
fmt.Fprintf(b.errStream, "# Executing %d build triggers\n", nTriggers)
|
|
|
|
}
|
2014-02-04 18:41:37 +00:00
|
|
|
for n, step := range b.config.OnBuild {
|
2014-02-20 15:16:45 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-02-04 18:41:37 +00:00
|
|
|
if err := b.BuildStep(fmt.Sprintf("onbuild-%d", n), step); err != nil {
|
2013-12-04 10:36:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.config.OnBuild = []string{}
|
2013-05-24 01:33:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-04 10:36:21 +00:00
|
|
|
// The ONBUILD command declares a build instruction to be executed in any future build
|
|
|
|
// using the current image as a base.
|
|
|
|
func (b *buildFile) CmdOnbuild(trigger string) error {
|
2014-02-20 15:16:45 +00:00
|
|
|
splitTrigger := strings.Split(trigger, " ")
|
|
|
|
triggerInstruction := strings.ToUpper(strings.Trim(splitTrigger[0], " "))
|
|
|
|
switch triggerInstruction {
|
|
|
|
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", triggerInstruction)
|
|
|
|
}
|
2013-12-04 10:36:21 +00:00
|
|
|
b.config.OnBuild = append(b.config.OnBuild, trigger)
|
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("ONBUILD %s", trigger))
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
func (b *buildFile) CmdMaintainer(name string) error {
|
|
|
|
b.maintainer = name
|
2013-05-29 22:03:00 +00:00
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("MAINTAINER %s", name))
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 05:33:15 +00:00
|
|
|
// probeCache checks to see if image-caching is enabled (`b.utilizeCache`)
|
|
|
|
// and if so attempts to look up the current `b.image` and `b.config` pair
|
|
|
|
// in the current server `b.srv`. 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 *buildFile) probeCache() (bool, error) {
|
|
|
|
if b.utilizeCache {
|
|
|
|
if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if cache != nil {
|
|
|
|
fmt.Fprintf(b.outStream, " ---> Using cache\n")
|
|
|
|
utils.Debugf("[BUILDER] Use cached version")
|
|
|
|
b.image = cache.ID
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
utils.Debugf("[BUILDER] Cache miss")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2013-12-12 05:33:15 +00:00
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
func (b *buildFile) CmdRun(args string) error {
|
|
|
|
if b.image == "" {
|
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
2014-02-12 04:04:39 +00:00
|
|
|
config, _, _, err := runconfig.Parse(append([]string{b.image}, b.buildCmdFromJson(args)...), nil)
|
2013-05-24 01:33:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:37:03 +00:00
|
|
|
cmd := b.config.Cmd
|
2013-05-24 01:33:31 +00:00
|
|
|
b.config.Cmd = nil
|
2014-02-12 04:04:39 +00:00
|
|
|
runconfig.Merge(b.config, config)
|
2013-05-24 01:33:31 +00:00
|
|
|
|
2013-08-05 00:11:23 +00:00
|
|
|
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
|
|
|
|
|
2013-05-30 01:14:50 +00:00
|
|
|
utils.Debugf("Command to be executed: %v", b.config.Cmd)
|
2013-05-29 21:37:03 +00:00
|
|
|
|
2013-12-12 05:33:15 +00:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 15:02:53 +00:00
|
|
|
c, err := b.create()
|
2013-05-24 01:33:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-12 15:02:53 +00:00
|
|
|
// Ensure that we keep the container mounted until the commit
|
|
|
|
// to avoid unmounting and then mounting directly again
|
|
|
|
c.Mount()
|
|
|
|
defer c.Unmount()
|
|
|
|
|
|
|
|
err = b.run(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.commit(c.ID, cmd, "run"); err != nil {
|
2013-05-29 21:37:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-08-05 00:11:23 +00:00
|
|
|
|
2013-05-29 21:37:03 +00:00
|
|
|
return nil
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 07:01:45 +00:00
|
|
|
func (b *buildFile) FindEnvKey(key string) int {
|
|
|
|
for k, envVar := range b.config.Env {
|
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
|
|
if key == envParts[0] {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) ReplaceEnvMatches(value string) (string, error) {
|
|
|
|
exp, err := regexp.Compile("(\\\\\\\\+|[^\\\\]|\\b|\\A)\\$({?)([[:alnum:]_]+)(}?)")
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
matches := exp.FindAllString(value, -1)
|
|
|
|
for _, match := range matches {
|
|
|
|
match = match[strings.Index(match, "$"):]
|
|
|
|
matchKey := strings.Trim(match, "${}")
|
|
|
|
|
|
|
|
for _, envVar := range b.config.Env {
|
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
|
|
envKey := envParts[0]
|
|
|
|
envValue := envParts[1]
|
|
|
|
|
|
|
|
if envKey == matchKey {
|
|
|
|
value = strings.Replace(value, match, envValue, -1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
func (b *buildFile) CmdEnv(args string) error {
|
|
|
|
tmp := strings.SplitN(args, " ", 2)
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
return fmt.Errorf("Invalid ENV format")
|
|
|
|
}
|
2013-06-10 16:31:59 +00:00
|
|
|
key := strings.Trim(tmp[0], " \t")
|
|
|
|
value := strings.Trim(tmp[1], " \t")
|
2013-05-24 01:33:31 +00:00
|
|
|
|
2013-07-09 07:01:45 +00:00
|
|
|
envKey := b.FindEnvKey(key)
|
|
|
|
replacedValue, err := b.ReplaceEnvMatches(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
2013-07-09 07:01:45 +00:00
|
|
|
replacedVar := fmt.Sprintf("%s=%s", key, replacedValue)
|
|
|
|
|
|
|
|
if envKey >= 0 {
|
|
|
|
b.config.Env[envKey] = replacedVar
|
2013-08-08 00:23:49 +00:00
|
|
|
} else {
|
|
|
|
b.config.Env = append(b.config.Env, replacedVar)
|
2013-07-09 07:01:45 +00:00
|
|
|
}
|
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("ENV %s", replacedVar))
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-28 02:06:26 +00:00
|
|
|
func (b *buildFile) buildCmdFromJson(args string) []string {
|
2013-05-24 01:33:31 +00:00
|
|
|
var cmd []string
|
|
|
|
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
|
2013-12-28 02:06:26 +00:00
|
|
|
utils.Debugf("Error unmarshalling: %s, setting to /bin/sh -c", err)
|
2013-05-29 22:03:00 +00:00
|
|
|
cmd = []string{"/bin/sh", "-c", args}
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
2013-12-28 02:06:26 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdCmd(args string) error {
|
|
|
|
cmd := b.buildCmdFromJson(args)
|
|
|
|
b.config.Cmd = cmd
|
|
|
|
if err := b.commit("", b.config.Cmd, fmt.Sprintf("CMD %v", cmd)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdEntrypoint(args string) error {
|
|
|
|
entrypoint := b.buildCmdFromJson(args)
|
|
|
|
b.config.Entrypoint = entrypoint
|
|
|
|
if err := b.commit("", b.config.Cmd, fmt.Sprintf("ENTRYPOINT %v", entrypoint)); err != nil {
|
2013-05-30 19:21:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdExpose(args string) error {
|
2014-03-13 20:58:09 +00:00
|
|
|
portsTab := strings.Split(args, " ")
|
|
|
|
|
|
|
|
if b.config.ExposedPorts == nil {
|
|
|
|
b.config.ExposedPorts = make(nat.PortSet)
|
|
|
|
}
|
|
|
|
ports, _, err := nat.ParsePortSpecs(append(portsTab, b.config.PortSpecs...))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for port := range ports {
|
|
|
|
if _, exists := b.config.ExposedPorts[port]; !exists {
|
|
|
|
b.config.ExposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.config.PortSpecs = nil
|
|
|
|
|
2013-05-29 22:03:00 +00:00
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("EXPOSE %v", ports))
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 12:02:17 +00:00
|
|
|
func (b *buildFile) CmdUser(args string) error {
|
|
|
|
b.config.User = args
|
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("USER %v", args))
|
|
|
|
}
|
|
|
|
|
2013-05-30 19:10:54 +00:00
|
|
|
func (b *buildFile) CmdInsert(args string) error {
|
|
|
|
return fmt.Errorf("INSERT has been deprecated. Please use ADD instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdCopy(args string) error {
|
2014-05-28 17:53:16 +00:00
|
|
|
return b.runContextCommand(args, false, false, "COPY")
|
2013-05-30 19:10:54 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 18:30:19 +00:00
|
|
|
func (b *buildFile) CmdWorkdir(workdir string) error {
|
2014-03-15 20:00:35 +00:00
|
|
|
if workdir[0] == '/' {
|
|
|
|
b.config.WorkingDir = workdir
|
|
|
|
} else {
|
|
|
|
if b.config.WorkingDir == "" {
|
|
|
|
b.config.WorkingDir = "/"
|
|
|
|
}
|
|
|
|
b.config.WorkingDir = filepath.Join(b.config.WorkingDir, workdir)
|
|
|
|
}
|
2013-08-18 18:30:19 +00:00
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
|
|
|
|
}
|
|
|
|
|
2013-07-04 02:33:30 +00:00
|
|
|
func (b *buildFile) CmdVolume(args string) error {
|
|
|
|
if args == "" {
|
|
|
|
return fmt.Errorf("Volume cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var volume []string
|
|
|
|
if err := json.Unmarshal([]byte(args), &volume); err != nil {
|
|
|
|
volume = []string{args}
|
|
|
|
}
|
|
|
|
if b.config.Volumes == nil {
|
2013-11-26 17:46:06 +00:00
|
|
|
b.config.Volumes = map[string]struct{}{}
|
2013-07-04 02:33:30 +00:00
|
|
|
}
|
|
|
|
for _, v := range volume {
|
|
|
|
b.config.Volumes[v] = struct{}{}
|
|
|
|
}
|
|
|
|
if err := b.commit("", b.config.Cmd, fmt.Sprintf("VOLUME %s", args)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-12 05:33:15 +00:00
|
|
|
func (b *buildFile) checkPathForAddition(orig string) error {
|
2013-12-27 00:02:15 +00:00
|
|
|
origPath := path.Join(b.contextPath, orig)
|
2014-01-11 12:46:11 +00:00
|
|
|
if p, err := filepath.EvalSymlinks(origPath); err != nil {
|
2014-01-14 18:42:03 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
|
|
|
}
|
2014-01-11 12:46:11 +00:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
origPath = p
|
|
|
|
}
|
2013-12-27 00:02:15 +00:00
|
|
|
if !strings.HasPrefix(origPath, b.contextPath) {
|
2013-12-12 05:33:15 +00:00
|
|
|
return fmt.Errorf("Forbidden path outside the build context: %s (%s)", orig, origPath)
|
2013-05-28 20:38:26 +00:00
|
|
|
}
|
2013-12-12 05:33:15 +00:00
|
|
|
_, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
2014-01-14 18:42:03 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
|
|
|
}
|
|
|
|
return err
|
2013-07-08 07:43:22 +00:00
|
|
|
}
|
2013-12-12 05:33:15 +00:00
|
|
|
return nil
|
2013-06-06 22:40:46 +00:00
|
|
|
}
|
2013-05-28 20:38:26 +00:00
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
func (b *buildFile) addContext(container *daemon.Container, orig, dest string, decompress bool) error {
|
2013-12-27 00:02:15 +00:00
|
|
|
var (
|
2014-04-09 14:21:22 +00:00
|
|
|
err error
|
|
|
|
destExists = true
|
|
|
|
origPath = path.Join(b.contextPath, orig)
|
|
|
|
destPath = path.Join(container.RootfsPath(), dest)
|
2013-12-27 00:02:15 +00:00
|
|
|
)
|
2014-03-26 13:55:45 +00:00
|
|
|
|
|
|
|
if destPath != container.RootfsPath() {
|
2014-05-13 17:54:08 +00:00
|
|
|
destPath, err = symlink.FollowSymlinkInScope(destPath, container.RootfsPath())
|
2014-03-26 13:55:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 03:28:49 +00:00
|
|
|
// Preserve the trailing '/'
|
2014-05-23 00:03:00 +00:00
|
|
|
if strings.HasSuffix(dest, "/") || dest == "." {
|
2013-06-19 03:28:49 +00:00
|
|
|
destPath = destPath + "/"
|
|
|
|
}
|
2014-05-23 00:03:00 +00:00
|
|
|
|
2014-04-09 14:21:22 +00:00
|
|
|
destStat, err := os.Stat(destPath)
|
|
|
|
if err != nil {
|
2014-05-23 00:03:00 +00:00
|
|
|
if !os.IsNotExist(err) {
|
2014-04-09 14:21:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-05-23 00:03:00 +00:00
|
|
|
destExists = false
|
2014-04-09 14:21:22 +00:00
|
|
|
}
|
2014-05-23 00:03:00 +00:00
|
|
|
|
2013-05-28 22:22:01 +00:00
|
|
|
fi, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
2014-01-14 18:42:03 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
|
|
|
}
|
|
|
|
return err
|
2013-05-28 20:38:26 +00:00
|
|
|
}
|
2014-02-14 01:18:38 +00:00
|
|
|
|
2013-05-28 22:22:01 +00:00
|
|
|
if fi.IsDir() {
|
2014-05-23 00:03:00 +00:00
|
|
|
return copyAsDirectory(origPath, destPath, destExists)
|
2014-02-14 01:18:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
// If we are adding a remote file (or we've been told not to decompress), do not try to untar it
|
|
|
|
if decompress {
|
2014-05-23 00:03:00 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2014-02-18 01:08:17 +00:00
|
|
|
// try to successfully untar the orig
|
|
|
|
if err := archive.UntarPath(origPath, tarDest); err == nil {
|
|
|
|
return nil
|
2014-05-23 00:03:00 +00:00
|
|
|
} else if err != io.EOF {
|
|
|
|
utils.Debugf("Couldn't untar %s to %s: %s", origPath, tarDest, err)
|
2014-02-18 01:08:17 +00:00
|
|
|
}
|
2014-02-14 01:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := archive.CopyWithTar(origPath, destPath); err != nil {
|
|
|
|
return err
|
2013-05-28 22:22:01 +00:00
|
|
|
}
|
2014-04-01 21:17:31 +00:00
|
|
|
|
2014-04-09 14:21:22 +00:00
|
|
|
resPath := destPath
|
|
|
|
if destExists && destStat.IsDir() {
|
|
|
|
resPath = path.Join(destPath, path.Base(origPath))
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:03:00 +00:00
|
|
|
return fixPermissions(resPath, 0, 0)
|
2013-06-06 22:40:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
func (b *buildFile) runContextCommand(args string, allowRemote bool, allowDecompression bool, cmdName string) error {
|
2013-12-27 00:02:15 +00:00
|
|
|
if b.context == nil {
|
2014-05-28 17:53:16 +00:00
|
|
|
return fmt.Errorf("No context given. Impossible to use %s", cmdName)
|
2013-06-06 22:40:46 +00:00
|
|
|
}
|
|
|
|
tmp := strings.SplitN(args, " ", 2)
|
|
|
|
if len(tmp) != 2 {
|
2014-05-28 17:53:16 +00:00
|
|
|
return fmt.Errorf("Invalid %s format", cmdName)
|
2013-06-06 22:40:46 +00:00
|
|
|
}
|
2013-07-09 07:01:45 +00:00
|
|
|
|
|
|
|
orig, err := b.ReplaceEnvMatches(strings.Trim(tmp[0], " \t"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dest, err := b.ReplaceEnvMatches(strings.Trim(tmp[1], " \t"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-06 22:40:46 +00:00
|
|
|
|
|
|
|
cmd := b.config.Cmd
|
2014-05-28 17:53:16 +00:00
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, orig, dest)}
|
2014-04-28 16:30:43 +00:00
|
|
|
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
|
2013-06-14 01:42:27 +00:00
|
|
|
b.config.Image = b.image
|
2013-12-12 05:33:15 +00:00
|
|
|
|
2013-12-27 00:02:15 +00:00
|
|
|
var (
|
2014-01-08 22:20:50 +00:00
|
|
|
origPath = orig
|
|
|
|
destPath = dest
|
|
|
|
remoteHash string
|
2014-02-18 01:08:17 +00:00
|
|
|
isRemote bool
|
2014-05-28 17:53:16 +00:00
|
|
|
decompress = true
|
2013-12-27 00:02:15 +00:00
|
|
|
)
|
2013-12-12 05:33:15 +00:00
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
isRemote = utils.IsURL(orig)
|
|
|
|
if isRemote && !allowRemote {
|
|
|
|
return fmt.Errorf("Source can't be an URL for %s", cmdName)
|
|
|
|
} else if utils.IsURL(orig) {
|
2014-04-01 21:17:31 +00:00
|
|
|
// Initiate the download
|
2013-12-12 05:33:15 +00:00
|
|
|
resp, err := utils.Download(orig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-01 21:17:31 +00:00
|
|
|
|
|
|
|
// Create a tmp dir
|
2013-12-27 00:02:15 +00:00
|
|
|
tmpDirName, err := ioutil.TempDir(b.contextPath, "docker-remote")
|
2013-12-12 05:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-01 21:17:31 +00:00
|
|
|
|
|
|
|
// Create a tmp file within our tmp dir
|
2013-12-12 05:33:15 +00:00
|
|
|
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)
|
2014-04-01 21:17:31 +00:00
|
|
|
|
|
|
|
// Download and dump result to tmp file
|
|
|
|
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
|
2014-01-08 22:20:50 +00:00
|
|
|
tmpFile.Close()
|
2013-12-12 05:33:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpFile.Close()
|
|
|
|
|
2014-05-17 21:09:05 +00:00
|
|
|
// Remove the mtime of the newly created tmp file
|
|
|
|
if err := system.UtimesNano(tmpFileName, make([]syscall.Timespec, 2)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:17:31 +00:00
|
|
|
origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
|
|
|
|
|
2014-01-08 22:20:50 +00:00
|
|
|
// Process the checksum
|
|
|
|
r, err := archive.Tar(tmpFileName, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-17 21:09:05 +00:00
|
|
|
tarSum := &utils.TarSum{Reader: r, DisableCompression: true}
|
|
|
|
if _, err := io.Copy(ioutil.Discard, tarSum); err != nil {
|
2014-05-19 21:24:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-01-08 22:20:50 +00:00
|
|
|
remoteHash = tarSum.Sum(nil)
|
2014-02-14 11:41:46 +00:00
|
|
|
r.Close()
|
2014-01-08 22:20:50 +00:00
|
|
|
|
2013-12-12 05:33:15 +00:00
|
|
|
// 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
|
|
|
|
if b.utilizeCache {
|
2013-12-27 00:02:15 +00:00
|
|
|
var (
|
|
|
|
hash string
|
|
|
|
sums = b.context.GetSums()
|
|
|
|
)
|
2014-01-08 00:53:55 +00:00
|
|
|
|
2014-01-08 22:20:50 +00:00
|
|
|
if remoteHash != "" {
|
|
|
|
hash = remoteHash
|
|
|
|
} else if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil {
|
2013-12-12 05:33:15 +00:00
|
|
|
return err
|
2013-12-27 00:02:15 +00:00
|
|
|
} else if fi.IsDir() {
|
|
|
|
var subfiles []string
|
|
|
|
for file, sum := range sums {
|
2014-01-07 03:42:57 +00:00
|
|
|
absFile := path.Join(b.contextPath, file)
|
|
|
|
absOrigPath := path.Join(b.contextPath, origPath)
|
|
|
|
if strings.HasPrefix(absFile, absOrigPath) {
|
2013-12-27 00:02:15 +00:00
|
|
|
subfiles = append(subfiles, sum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(subfiles)
|
2013-12-27 00:42:05 +00:00
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(subfiles, ",")))
|
|
|
|
hash = "dir:" + hex.EncodeToString(hasher.Sum(nil))
|
2013-12-27 00:02:15 +00:00
|
|
|
} else {
|
2014-01-08 20:16:23 +00:00
|
|
|
if origPath[0] == '/' && len(origPath) > 1 {
|
|
|
|
origPath = origPath[1:]
|
|
|
|
}
|
|
|
|
origPath = strings.TrimPrefix(origPath, "./")
|
|
|
|
if h, ok := sums[origPath]; ok {
|
|
|
|
hash = "file:" + h
|
|
|
|
}
|
2013-12-12 05:33:15 +00:00
|
|
|
}
|
2014-05-28 17:53:16 +00:00
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) %s %s in %s", cmdName, hash, dest)}
|
2013-12-12 05:33:15 +00:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-08 00:53:55 +00:00
|
|
|
// If we do not have a hash, never use the cache
|
|
|
|
if hit && hash != "" {
|
2013-12-12 05:33:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 10:31:16 +00:00
|
|
|
// Create the container
|
2014-04-17 21:43:01 +00:00
|
|
|
container, _, err := b.daemon.Create(b.config, "")
|
2013-06-06 22:40:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-13 22:08:38 +00:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2013-06-06 22:40:46 +00:00
|
|
|
|
2013-12-06 11:15:14 +00:00
|
|
|
if err := container.Mount(); err != nil {
|
2013-06-06 22:40:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
if !allowDecompression || isRemote {
|
|
|
|
decompress = false
|
|
|
|
}
|
|
|
|
if err := b.addContext(container, origPath, destPath, decompress); err != nil {
|
2013-12-12 05:33:15 +00:00
|
|
|
return err
|
2013-06-06 22:40:46 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
if err := b.commit(container.ID, cmd, fmt.Sprintf("%s %s in %s", cmdName, orig, dest)); err != nil {
|
2013-05-30 19:21:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-05-28 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:53:16 +00:00
|
|
|
func (b *buildFile) CmdAdd(args string) error {
|
|
|
|
return b.runContextCommand(args, true, true, "ADD")
|
|
|
|
}
|
|
|
|
|
2014-04-17 21:43:01 +00:00
|
|
|
func (b *buildFile) create() (*daemon.Container, error) {
|
2013-05-24 01:33:31 +00:00
|
|
|
if b.image == "" {
|
2014-02-12 15:02:53 +00:00
|
|
|
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
b.config.Image = b.image
|
|
|
|
|
2014-05-16 10:31:16 +00:00
|
|
|
// Create the container
|
2014-04-17 21:43:01 +00:00
|
|
|
c, _, err := b.daemon.Create(b.config, "")
|
2013-05-24 01:33:31 +00:00
|
|
|
if err != nil {
|
2014-02-12 15:02:53 +00:00
|
|
|
return nil, err
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
b.tmpContainers[c.ID] = struct{}{}
|
2013-12-02 19:43:41 +00:00
|
|
|
fmt.Fprintf(b.outStream, " ---> Running in %s\n", utils.TruncateID(c.ID))
|
|
|
|
|
2013-07-08 07:11:45 +00:00
|
|
|
// override the entry point that may have been picked up from the base image
|
|
|
|
c.Path = b.config.Cmd[0]
|
|
|
|
c.Args = b.config.Cmd[1:]
|
|
|
|
|
2014-02-12 15:02:53 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2014-04-17 21:43:01 +00:00
|
|
|
func (b *buildFile) run(c *daemon.Container) error {
|
2013-10-22 22:01:06 +00:00
|
|
|
var errCh chan error
|
|
|
|
if b.verbose {
|
|
|
|
errCh = utils.Go(func() error {
|
2014-05-05 23:48:56 +00:00
|
|
|
return <-b.daemon.Attach(c, nil, nil, b.outStream, b.errStream)
|
2013-10-22 22:01:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
//start the container
|
2013-10-31 21:58:43 +00:00
|
|
|
if err := c.Start(); err != nil {
|
2014-02-12 15:02:53 +00:00
|
|
|
return err
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-10-22 22:01:06 +00:00
|
|
|
if errCh != nil {
|
|
|
|
if err := <-errCh; err != nil {
|
2014-02-12 15:02:53 +00:00
|
|
|
return err
|
2013-07-12 00:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
// Wait for it to finish
|
|
|
|
if ret := c.Wait(); ret != 0 {
|
2013-10-12 12:14:52 +00:00
|
|
|
err := &utils.JSONError{
|
|
|
|
Message: fmt.Sprintf("The command %v returned a non-zero code: %d", b.config.Cmd, ret),
|
|
|
|
Code: ret,
|
|
|
|
}
|
2014-02-12 15:02:53 +00:00
|
|
|
return err
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 15:02:53 +00:00
|
|
|
return nil
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:37:03 +00:00
|
|
|
// Commit the container <id> with the autorun command <autoCmd>
|
2013-05-29 22:03:00 +00:00
|
|
|
func (b *buildFile) commit(id string, autoCmd []string, comment string) error {
|
2013-05-24 01:33:31 +00:00
|
|
|
if b.image == "" {
|
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to commit")
|
|
|
|
}
|
|
|
|
b.config.Image = b.image
|
|
|
|
if id == "" {
|
2013-06-06 22:48:12 +00:00
|
|
|
cmd := b.config.Cmd
|
2013-05-29 22:03:00 +00:00
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
|
2013-06-06 22:48:12 +00:00
|
|
|
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
|
2013-05-29 23:10:11 +00:00
|
|
|
|
2013-12-12 05:33:15 +00:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
2013-05-29 23:10:11 +00:00
|
|
|
}
|
2013-08-02 16:18:54 +00:00
|
|
|
|
2014-04-17 21:43:01 +00:00
|
|
|
container, warnings, err := b.daemon.Create(b.config, "")
|
2013-06-04 13:51:12 +00:00
|
|
|
if err != nil {
|
2013-05-24 01:33:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-10-30 18:13:10 +00:00
|
|
|
for _, warning := range warnings {
|
2013-12-02 19:43:41 +00:00
|
|
|
fmt.Fprintf(b.outStream, " ---> [Warning] %s\n", warning)
|
2013-10-30 18:13:10 +00:00
|
|
|
}
|
2013-06-13 22:08:38 +00:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2013-12-02 19:43:41 +00:00
|
|
|
fmt.Fprintf(b.outStream, " ---> Running in %s\n", utils.TruncateID(container.ID))
|
2013-06-13 22:08:38 +00:00
|
|
|
id = container.ID
|
2014-03-13 20:58:09 +00:00
|
|
|
|
2013-12-06 11:15:14 +00:00
|
|
|
if err := container.Mount(); err != nil {
|
2013-06-13 22:08:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
2014-04-17 21:43:01 +00:00
|
|
|
container := b.daemon.Get(id)
|
2013-05-24 01:33:31 +00:00
|
|
|
if container == nil {
|
|
|
|
return fmt.Errorf("An error occured while creating the container")
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:37:03 +00:00
|
|
|
// Note: Actually copy the struct
|
|
|
|
autoConfig := *b.config
|
|
|
|
autoConfig.Cmd = autoCmd
|
2013-05-24 01:33:31 +00:00
|
|
|
// Commit the container
|
2014-04-17 21:43:01 +00:00
|
|
|
image, err := b.daemon.Commit(container, "", "", "", b.maintainer, &autoConfig)
|
2013-05-24 01:33:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
b.tmpImages[image.ID] = struct{}{}
|
|
|
|
b.image = image.ID
|
2013-05-24 01:33:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:02:45 +00:00
|
|
|
// Long lines can be split with a backslash
|
2013-09-10 01:23:42 +00:00
|
|
|
var lineContinuation = regexp.MustCompile(`\s*\\\s*\n`)
|
2013-09-09 23:42:04 +00:00
|
|
|
|
2013-06-15 16:38:18 +00:00
|
|
|
func (b *buildFile) Build(context io.Reader) (string, error) {
|
2013-12-27 00:02:15 +00:00
|
|
|
tmpdirPath, err := ioutil.TempDir("", "docker-build")
|
2013-06-15 16:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-01-17 07:44:18 +00:00
|
|
|
|
|
|
|
decompressedStream, err := archive.DecompressStream(context)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.context = &utils.TarSum{Reader: decompressedStream, DisableCompression: true}
|
2013-12-27 00:02:15 +00:00
|
|
|
if err := archive.Untar(b.context, tmpdirPath, nil); err != nil {
|
2013-06-15 16:38:18 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2013-12-27 00:02:15 +00:00
|
|
|
defer os.RemoveAll(tmpdirPath)
|
2014-01-08 01:46:04 +00:00
|
|
|
|
2013-12-27 00:02:15 +00:00
|
|
|
b.contextPath = tmpdirPath
|
|
|
|
filename := path.Join(tmpdirPath, "Dockerfile")
|
2013-09-09 23:42:04 +00:00
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
2013-06-15 16:38:18 +00:00
|
|
|
return "", fmt.Errorf("Can't build a directory with no Dockerfile")
|
2013-05-28 20:38:26 +00:00
|
|
|
}
|
2013-09-09 23:42:04 +00:00
|
|
|
fileBytes, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-20 12:26:11 +00:00
|
|
|
if len(fileBytes) == 0 {
|
|
|
|
return "", ErrDockerfileEmpty
|
|
|
|
}
|
2014-03-19 00:50:40 +00:00
|
|
|
var (
|
|
|
|
dockerfile = lineContinuation.ReplaceAllString(stripComments(fileBytes), "")
|
|
|
|
stepN = 0
|
|
|
|
)
|
2013-09-09 23:42:04 +00:00
|
|
|
for _, line := range strings.Split(dockerfile, "\n") {
|
2013-06-10 16:31:59 +00:00
|
|
|
line = strings.Trim(strings.Replace(line, "\t", " ", -1), " \t\r\n")
|
2014-03-19 00:50:40 +00:00
|
|
|
if len(line) == 0 {
|
2013-05-24 01:33:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-02-04 18:41:37 +00:00
|
|
|
if err := b.BuildStep(fmt.Sprintf("%d", stepN), line); err != nil {
|
2014-05-16 11:47:33 +00:00
|
|
|
if b.forceRm {
|
|
|
|
b.clearTmp(b.tmpContainers)
|
|
|
|
}
|
2013-12-04 10:35:23 +00:00
|
|
|
return "", err
|
2014-04-10 11:43:25 +00:00
|
|
|
} else if b.rm {
|
|
|
|
b.clearTmp(b.tmpContainers)
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
2013-08-09 19:56:37 +00:00
|
|
|
stepN += 1
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
if b.image != "" {
|
2013-12-02 19:43:41 +00:00
|
|
|
fmt.Fprintf(b.outStream, "Successfully built %s\n", utils.TruncateID(b.image))
|
2013-05-24 01:33:31 +00:00
|
|
|
return b.image, nil
|
|
|
|
}
|
2013-12-20 13:02:10 +00:00
|
|
|
return "", fmt.Errorf("No image was generated. This may be because the Dockerfile does not, like, do anything.\n")
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 10:35:23 +00:00
|
|
|
// BuildStep parses a single build step from `instruction` and executes it in the current context.
|
2014-02-04 18:41:37 +00:00
|
|
|
func (b *buildFile) BuildStep(name, expression string) error {
|
|
|
|
fmt.Fprintf(b.outStream, "Step %s : %s\n", name, expression)
|
2013-12-04 10:35:23 +00:00
|
|
|
tmp := strings.SplitN(expression, " ", 2)
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
return fmt.Errorf("Invalid Dockerfile format")
|
|
|
|
}
|
|
|
|
instruction := strings.ToLower(strings.Trim(tmp[0], " "))
|
|
|
|
arguments := strings.Trim(tmp[1], " ")
|
|
|
|
|
|
|
|
method, exists := reflect.TypeOf(b).MethodByName("Cmd" + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
|
|
|
|
if !exists {
|
|
|
|
fmt.Fprintf(b.errStream, "# Skipping unknown instruction %s\n", strings.ToUpper(instruction))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface()
|
|
|
|
if ret != nil {
|
|
|
|
return ret.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(b.outStream, " ---> %s\n", utils.TruncateID(b.image))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-19 00:50:40 +00:00
|
|
|
func stripComments(raw []byte) string {
|
|
|
|
var (
|
|
|
|
out []string
|
|
|
|
lines = strings.Split(string(raw), "\n")
|
|
|
|
)
|
|
|
|
for _, l := range lines {
|
|
|
|
if len(l) == 0 || l[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, l)
|
|
|
|
}
|
|
|
|
return strings.Join(out, "\n")
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:03:00 +00:00
|
|
|
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-05-16 11:47:33 +00:00
|
|
|
func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, forceRm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *registry.AuthConfig, authConfigFile *registry.ConfigFile) BuildFile {
|
2013-05-24 01:33:31 +00:00
|
|
|
return &buildFile{
|
2014-04-17 21:43:01 +00:00
|
|
|
daemon: srv.daemon,
|
2013-05-24 01:33:31 +00:00
|
|
|
srv: srv,
|
2014-02-12 04:04:39 +00:00
|
|
|
config: &runconfig.Config{},
|
2013-12-02 19:43:41 +00:00
|
|
|
outStream: outStream,
|
|
|
|
errStream: errStream,
|
2013-05-24 01:33:31 +00:00
|
|
|
tmpContainers: make(map[string]struct{}),
|
|
|
|
tmpImages: make(map[string]struct{}),
|
2013-07-12 00:12:25 +00:00
|
|
|
verbose: verbose,
|
2013-08-02 16:18:54 +00:00
|
|
|
utilizeCache: utilizeCache,
|
2013-09-10 18:39:47 +00:00
|
|
|
rm: rm,
|
2014-05-16 11:47:33 +00:00
|
|
|
forceRm: forceRm,
|
2013-10-12 12:14:52 +00:00
|
|
|
sf: sf,
|
2014-05-07 22:59:13 +00:00
|
|
|
authConfig: auth,
|
|
|
|
configFile: authConfigFile,
|
2013-12-02 19:43:41 +00:00
|
|
|
outOld: outOld,
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
}
|