2013-05-23 21:33:31 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-12-26 19:42:05 -05:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2013-05-23 21:33:31 -04:00
|
|
|
"encoding/json"
|
2013-12-20 07:26:11 -05:00
|
|
|
"errors"
|
2013-05-23 21:33:31 -04:00
|
|
|
"fmt"
|
2013-10-31 19:57:45 -04:00
|
|
|
"github.com/dotcloud/docker/archive"
|
2013-12-06 17:27:10 -05:00
|
|
|
"github.com/dotcloud/docker/auth"
|
2013-05-23 21:33:31 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"io"
|
2013-05-28 16:37:49 -04:00
|
|
|
"io/ioutil"
|
2013-07-08 03:43:22 -04:00
|
|
|
"net/url"
|
2013-05-23 21:33:31 -04:00
|
|
|
"os"
|
2013-05-28 16:37:49 -04:00
|
|
|
"path"
|
2013-12-12 00:33:15 -05:00
|
|
|
"path/filepath"
|
2013-05-23 21:33:31 -04:00
|
|
|
"reflect"
|
2013-07-09 03:01:45 -04:00
|
|
|
"regexp"
|
2013-12-26 19:02:15 -05:00
|
|
|
"sort"
|
2013-05-23 21:33:31 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-12-20 07:26:11 -05:00
|
|
|
var (
|
|
|
|
ErrDockerfileEmpty = errors.New("Dockerfile cannot be empty")
|
|
|
|
)
|
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
type BuildFile interface {
|
2013-06-15 12:38:18 -04:00
|
|
|
Build(io.Reader) (string, error)
|
2013-05-23 21:33:31 -04:00
|
|
|
CmdFrom(string) error
|
|
|
|
CmdRun(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type buildFile struct {
|
|
|
|
runtime *Runtime
|
|
|
|
srv *Server
|
|
|
|
|
2013-12-26 19:02:15 -05:00
|
|
|
image string
|
|
|
|
maintainer string
|
|
|
|
config *Config
|
|
|
|
|
|
|
|
contextPath string
|
|
|
|
context *utils.TarSum
|
|
|
|
|
2013-08-02 12:18:54 -04:00
|
|
|
verbose bool
|
|
|
|
utilizeCache bool
|
2013-09-10 14:39:47 -04:00
|
|
|
rm bool
|
2013-05-23 21:33:31 -04:00
|
|
|
|
2013-12-06 17:27:10 -05:00
|
|
|
authConfig *auth.AuthConfig
|
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
tmpContainers map[string]struct{}
|
|
|
|
tmpImages map[string]struct{}
|
|
|
|
|
2013-12-02 14:43:41 -05:00
|
|
|
outStream io.Writer
|
|
|
|
errStream io.Writer
|
|
|
|
|
|
|
|
// Deprecated, original writer used for ImagePull. To be removed.
|
|
|
|
outOld io.Writer
|
|
|
|
sf *utils.StreamFormatter
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-09-10 14:39:47 -04:00
|
|
|
func (b *buildFile) clearTmp(containers map[string]struct{}) {
|
2013-05-23 21:33:31 -04:00
|
|
|
for c := range containers {
|
|
|
|
tmp := b.runtime.Get(c)
|
|
|
|
b.runtime.Destroy(tmp)
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, "Removing intermediate container %s\n", utils.TruncateID(c))
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdFrom(name string) error {
|
|
|
|
image, err := b.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
if b.runtime.graph.IsNotExist(err) {
|
2013-07-09 11:06:10 -04:00
|
|
|
remote, tag := utils.ParseRepositoryTag(name)
|
2013-12-06 17:27:10 -05:00
|
|
|
if err := b.srv.ImagePull(remote, tag, b.outOld, b.sf, b.authConfig, nil, true); err != nil {
|
2013-05-23 21:33:31 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
image, err = b.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
b.image = image.ID
|
2013-05-23 21:33:31 -04:00
|
|
|
b.config = &Config{}
|
2013-11-04 16:20:14 -05:00
|
|
|
if image.Config != nil {
|
|
|
|
b.config = image.Config
|
|
|
|
}
|
2013-07-09 03:01:45 -04:00
|
|
|
if b.config.Env == nil || len(b.config.Env) == 0 {
|
|
|
|
b.config.Env = append(b.config.Env, "HOME=/", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
|
|
|
|
}
|
2013-05-23 21:33:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdMaintainer(name string) error {
|
|
|
|
b.maintainer = name
|
2013-05-29 18:03:00 -04:00
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("MAINTAINER %s", name))
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-12-12 00:33:15 -05: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 00:33:15 -05:00
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
func (b *buildFile) CmdRun(args string) error {
|
|
|
|
if b.image == "" {
|
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
2013-05-13 19:39:54 -04:00
|
|
|
config, _, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", args}, nil)
|
2013-05-23 21:33:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-29 17:37:03 -04:00
|
|
|
cmd := b.config.Cmd
|
2013-05-23 21:33:31 -04:00
|
|
|
b.config.Cmd = nil
|
|
|
|
MergeConfig(b.config, config)
|
|
|
|
|
2013-08-04 20:11:23 -04:00
|
|
|
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
|
|
|
|
|
2013-05-29 21:14:50 -04:00
|
|
|
utils.Debugf("Command to be executed: %v", b.config.Cmd)
|
2013-05-29 17:37:03 -04:00
|
|
|
|
2013-12-12 00:33:15 -05:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := b.run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-29 18:03:00 -04:00
|
|
|
if err := b.commit(cid, cmd, "run"); err != nil {
|
2013-05-29 17:37:03 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-08-04 20:11:23 -04:00
|
|
|
|
2013-05-29 17:37:03 -04:00
|
|
|
return nil
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-07-09 03:01:45 -04: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-23 21:33:31 -04: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 12:31:59 -04:00
|
|
|
key := strings.Trim(tmp[0], " \t")
|
|
|
|
value := strings.Trim(tmp[1], " \t")
|
2013-05-23 21:33:31 -04:00
|
|
|
|
2013-07-09 03:01:45 -04:00
|
|
|
envKey := b.FindEnvKey(key)
|
|
|
|
replacedValue, err := b.ReplaceEnvMatches(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
2013-07-09 03:01:45 -04:00
|
|
|
replacedVar := fmt.Sprintf("%s=%s", key, replacedValue)
|
|
|
|
|
|
|
|
if envKey >= 0 {
|
|
|
|
b.config.Env[envKey] = replacedVar
|
2013-08-07 20:23:49 -04:00
|
|
|
} else {
|
|
|
|
b.config.Env = append(b.config.Env, replacedVar)
|
2013-07-09 03:01:45 -04:00
|
|
|
}
|
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("ENV %s", replacedVar))
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-12-27 21:06:26 -05:00
|
|
|
func (b *buildFile) buildCmdFromJson(args string) []string {
|
2013-05-23 21:33:31 -04:00
|
|
|
var cmd []string
|
|
|
|
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
|
2013-12-27 21:06:26 -05:00
|
|
|
utils.Debugf("Error unmarshalling: %s, setting to /bin/sh -c", err)
|
2013-05-29 18:03:00 -04:00
|
|
|
cmd = []string{"/bin/sh", "-c", args}
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
2013-12-27 21:06:26 -05: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 15:21:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdExpose(args string) error {
|
|
|
|
ports := strings.Split(args, " ")
|
|
|
|
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
|
2013-05-29 18:03:00 -04:00
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("EXPOSE %v", ports))
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 08:02:17 -04: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 15:10:54 -04: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 {
|
|
|
|
return fmt.Errorf("COPY has been deprecated. Please use ADD instead")
|
|
|
|
}
|
|
|
|
|
2013-08-18 14:30:19 -04:00
|
|
|
func (b *buildFile) CmdWorkdir(workdir string) error {
|
|
|
|
b.config.WorkingDir = workdir
|
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("WORKDIR %v", workdir))
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:33:30 -04: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 12:46:06 -05:00
|
|
|
b.config.Volumes = map[string]struct{}{}
|
2013-07-03 22:33:30 -04: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 00:33:15 -05:00
|
|
|
func (b *buildFile) checkPathForAddition(orig string) error {
|
2013-12-26 19:02:15 -05:00
|
|
|
origPath := path.Join(b.contextPath, orig)
|
|
|
|
if !strings.HasPrefix(origPath, b.contextPath) {
|
2013-12-12 00:33:15 -05:00
|
|
|
return fmt.Errorf("Forbidden path outside the build context: %s (%s)", orig, origPath)
|
2013-05-28 16:38:26 -04:00
|
|
|
}
|
2013-12-12 00:33:15 -05:00
|
|
|
_, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
2013-07-08 03:43:22 -04:00
|
|
|
}
|
2013-12-12 00:33:15 -05:00
|
|
|
return nil
|
2013-06-06 18:40:46 -04:00
|
|
|
}
|
2013-05-28 16:38:26 -04:00
|
|
|
|
2013-06-06 18:40:46 -04:00
|
|
|
func (b *buildFile) addContext(container *Container, orig, dest string) error {
|
2013-12-26 19:02:15 -05:00
|
|
|
var (
|
|
|
|
origPath = path.Join(b.contextPath, orig)
|
|
|
|
destPath = path.Join(container.RootfsPath(), dest)
|
|
|
|
)
|
2013-06-18 23:28:49 -04:00
|
|
|
// Preserve the trailing '/'
|
2013-07-08 03:43:22 -04:00
|
|
|
if strings.HasSuffix(dest, "/") {
|
2013-06-18 23:28:49 -04:00
|
|
|
destPath = destPath + "/"
|
|
|
|
}
|
2013-05-28 18:22:01 -04:00
|
|
|
fi, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
2013-09-05 08:28:02 -04:00
|
|
|
return fmt.Errorf("%s: no such file or directory", orig)
|
2013-05-28 16:38:26 -04:00
|
|
|
}
|
2013-05-28 18:22:01 -04:00
|
|
|
if fi.IsDir() {
|
2013-10-31 19:57:45 -04:00
|
|
|
if err := archive.CopyWithTar(origPath, destPath); err != nil {
|
2013-05-28 18:22:01 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-06-14 19:43:39 -04:00
|
|
|
// First try to unpack the source as an archive
|
2013-10-31 19:57:45 -04:00
|
|
|
} else if err := archive.UntarPath(origPath, destPath); err != nil {
|
2013-10-15 19:12:27 -04:00
|
|
|
utils.Debugf("Couldn't untar %s to %s: %s", origPath, destPath, err)
|
2013-06-14 19:43:39 -04:00
|
|
|
// If that fails, just copy it as a regular file
|
2013-07-26 17:57:16 -04:00
|
|
|
if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil {
|
2013-05-29 21:55:00 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-10-31 19:57:45 -04:00
|
|
|
if err := archive.CopyWithTar(origPath, destPath); err != nil {
|
2013-05-28 18:22:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 18:40:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdAdd(args string) error {
|
2013-12-26 19:02:15 -05:00
|
|
|
if b.context == nil {
|
2013-06-06 18:40:46 -04:00
|
|
|
return fmt.Errorf("No context given. Impossible to use ADD")
|
|
|
|
}
|
|
|
|
tmp := strings.SplitN(args, " ", 2)
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
return fmt.Errorf("Invalid ADD format")
|
|
|
|
}
|
2013-07-09 03:01:45 -04: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 18:40:46 -04:00
|
|
|
|
|
|
|
cmd := b.config.Cmd
|
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) ADD %s in %s", orig, dest)}
|
2013-06-13 21:42:27 -04:00
|
|
|
b.config.Image = b.image
|
2013-12-12 00:33:15 -05:00
|
|
|
|
2013-12-26 19:02:15 -05:00
|
|
|
// FIXME: do we really need this?
|
|
|
|
var (
|
2014-01-08 17:20:50 -05:00
|
|
|
origPath = orig
|
|
|
|
destPath = dest
|
|
|
|
remoteHash string
|
2013-12-26 19:02:15 -05:00
|
|
|
)
|
2013-12-12 00:33:15 -05:00
|
|
|
|
|
|
|
if utils.IsURL(orig) {
|
|
|
|
resp, err := utils.Download(orig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-26 19:02:15 -05:00
|
|
|
tmpDirName, err := ioutil.TempDir(b.contextPath, "docker-remote")
|
2013-12-12 00:33:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
if _, err = io.Copy(tmpFile, resp.Body); err != nil {
|
2014-01-08 17:20:50 -05:00
|
|
|
tmpFile.Close()
|
2013-12-12 00:33:15 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
origPath = path.Join(filepath.Base(tmpDirName), filepath.Base(tmpFileName))
|
|
|
|
tmpFile.Close()
|
|
|
|
|
2014-01-08 17:20:50 -05:00
|
|
|
// Process the checksum
|
|
|
|
r, err := archive.Tar(tmpFileName, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tarSum := utils.TarSum{Reader: r, DisableCompression: true}
|
|
|
|
remoteHash = tarSum.Sum(nil)
|
|
|
|
|
2013-12-12 00:33:15 -05: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-26 19:02:15 -05:00
|
|
|
var (
|
|
|
|
hash string
|
|
|
|
sums = b.context.GetSums()
|
|
|
|
)
|
2014-01-07 19:53:55 -05:00
|
|
|
|
2014-01-08 17:20:50 -05:00
|
|
|
if remoteHash != "" {
|
|
|
|
hash = remoteHash
|
|
|
|
} else if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil {
|
2013-12-12 00:33:15 -05:00
|
|
|
return err
|
2013-12-26 19:02:15 -05:00
|
|
|
} else if fi.IsDir() {
|
|
|
|
var subfiles []string
|
|
|
|
for file, sum := range sums {
|
2014-01-06 22:42:57 -05:00
|
|
|
absFile := path.Join(b.contextPath, file)
|
|
|
|
absOrigPath := path.Join(b.contextPath, origPath)
|
|
|
|
if strings.HasPrefix(absFile, absOrigPath) {
|
2013-12-26 19:02:15 -05:00
|
|
|
subfiles = append(subfiles, sum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(subfiles)
|
2013-12-26 19:42:05 -05:00
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(strings.Join(subfiles, ",")))
|
|
|
|
hash = "dir:" + hex.EncodeToString(hasher.Sum(nil))
|
2013-12-26 19:02:15 -05:00
|
|
|
} else {
|
2014-01-08 15:16:23 -05: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 00:33:15 -05:00
|
|
|
}
|
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) ADD %s in %s", hash, dest)}
|
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-07 19:53:55 -05:00
|
|
|
// If we do not have a hash, never use the cache
|
|
|
|
if hit && hash != "" {
|
2013-12-12 00:33:15 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 17:57:50 -04:00
|
|
|
// Create the container and start it
|
2013-10-28 19:58:59 -04:00
|
|
|
container, _, err := b.runtime.Create(b.config, "")
|
2013-06-06 18:40:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-13 18:08:38 -04:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2013-06-06 18:40:46 -04:00
|
|
|
|
|
|
|
if err := container.EnsureMounted(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
2013-12-12 00:33:15 -05:00
|
|
|
if err := b.addContext(container, origPath, destPath); err != nil {
|
|
|
|
return err
|
2013-06-06 18:40:46 -04:00
|
|
|
}
|
|
|
|
|
2013-06-13 18:08:38 -04:00
|
|
|
if err := b.commit(container.ID, cmd, fmt.Sprintf("ADD %s in %s", orig, dest)); err != nil {
|
2013-05-30 15:21:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.config.Cmd = cmd
|
|
|
|
return nil
|
2013-05-28 16:38:26 -04:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:43:41 -05:00
|
|
|
type StdoutFormater struct {
|
|
|
|
io.Writer
|
|
|
|
*utils.StreamFormatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *StdoutFormater) Write(buf []byte) (int, error) {
|
2013-12-06 14:55:56 -05:00
|
|
|
formattedBuf := sf.StreamFormatter.FormatStream(string(buf))
|
2013-12-02 14:43:41 -05:00
|
|
|
n, err := sf.Writer.Write(formattedBuf)
|
|
|
|
if n != len(formattedBuf) {
|
|
|
|
return n, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return len(buf), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type StderrFormater struct {
|
|
|
|
io.Writer
|
|
|
|
*utils.StreamFormatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *StderrFormater) Write(buf []byte) (int, error) {
|
2013-12-06 14:55:56 -05:00
|
|
|
formattedBuf := sf.StreamFormatter.FormatStream("\033[91m" + string(buf) + "\033[0m")
|
2013-12-02 14:43:41 -05:00
|
|
|
n, err := sf.Writer.Write(formattedBuf)
|
|
|
|
if n != len(formattedBuf) {
|
|
|
|
return n, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return len(buf), err
|
|
|
|
}
|
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
func (b *buildFile) run() (string, error) {
|
|
|
|
if b.image == "" {
|
|
|
|
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
|
|
|
b.config.Image = b.image
|
|
|
|
|
|
|
|
// Create the container and start it
|
2013-10-28 19:58:59 -04:00
|
|
|
c, _, err := b.runtime.Create(b.config, "")
|
2013-05-23 21:33:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
b.tmpContainers[c.ID] = struct{}{}
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, " ---> Running in %s\n", utils.TruncateID(c.ID))
|
|
|
|
|
2013-07-08 03:11:45 -04: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:]
|
|
|
|
|
2013-10-22 18:01:06 -04:00
|
|
|
var errCh chan error
|
|
|
|
|
|
|
|
if b.verbose {
|
|
|
|
errCh = utils.Go(func() error {
|
2013-12-02 14:43:41 -05:00
|
|
|
return <-c.Attach(nil, nil, b.outStream, b.errStream)
|
2013-10-22 18:01:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
//start the container
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := c.Start(); err != nil {
|
2013-05-23 21:33:31 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-10-22 18:01:06 -04:00
|
|
|
if errCh != nil {
|
|
|
|
if err := <-errCh; err != nil {
|
2013-07-11 20:12:25 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
// Wait for it to finish
|
|
|
|
if ret := c.Wait(); ret != 0 {
|
2013-10-12 08:14:52 -04:00
|
|
|
err := &utils.JSONError{
|
|
|
|
Message: fmt.Sprintf("The command %v returned a non-zero code: %d", b.config.Cmd, ret),
|
|
|
|
Code: ret,
|
|
|
|
}
|
|
|
|
return "", err
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
return c.ID, nil
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-05-29 17:37:03 -04:00
|
|
|
// Commit the container <id> with the autorun command <autoCmd>
|
2013-05-29 18:03:00 -04:00
|
|
|
func (b *buildFile) commit(id string, autoCmd []string, comment string) error {
|
2013-05-23 21:33:31 -04: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 18:48:12 -04:00
|
|
|
cmd := b.config.Cmd
|
2013-05-29 18:03:00 -04:00
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
|
2013-06-06 18:48:12 -04:00
|
|
|
defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
|
2013-05-29 19:10:11 -04:00
|
|
|
|
2013-12-12 00:33:15 -05:00
|
|
|
hit, err := b.probeCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
2013-05-29 19:10:11 -04:00
|
|
|
}
|
2013-08-02 12:18:54 -04:00
|
|
|
|
2013-10-30 14:13:10 -04:00
|
|
|
container, warnings, err := b.runtime.Create(b.config, "")
|
2013-06-04 09:51:12 -04:00
|
|
|
if err != nil {
|
2013-05-23 21:33:31 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-10-30 14:13:10 -04:00
|
|
|
for _, warning := range warnings {
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, " ---> [Warning] %s\n", warning)
|
2013-10-30 14:13:10 -04:00
|
|
|
}
|
2013-06-13 18:08:38 -04:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, " ---> Running in %s\n", utils.TruncateID(container.ID))
|
2013-06-13 18:08:38 -04:00
|
|
|
id = container.ID
|
|
|
|
if err := container.EnsureMounted(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
container := b.runtime.Get(id)
|
|
|
|
if container == nil {
|
|
|
|
return fmt.Errorf("An error occured while creating the container")
|
|
|
|
}
|
|
|
|
|
2013-05-29 17:37:03 -04:00
|
|
|
// Note: Actually copy the struct
|
|
|
|
autoConfig := *b.config
|
|
|
|
autoConfig.Cmd = autoCmd
|
2013-05-23 21:33:31 -04:00
|
|
|
// Commit the container
|
2013-09-06 20:33:05 -04:00
|
|
|
image, err := b.runtime.Commit(container, "", "", "", b.maintainer, &autoConfig)
|
2013-05-23 21:33:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
b.tmpImages[image.ID] = struct{}{}
|
|
|
|
b.image = image.ID
|
2013-05-23 21:33:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:02:45 -04:00
|
|
|
// Long lines can be split with a backslash
|
2013-09-09 21:23:42 -04:00
|
|
|
var lineContinuation = regexp.MustCompile(`\s*\\\s*\n`)
|
2013-09-09 19:42:04 -04:00
|
|
|
|
2013-06-15 12:38:18 -04:00
|
|
|
func (b *buildFile) Build(context io.Reader) (string, error) {
|
2013-12-26 19:02:15 -05:00
|
|
|
tmpdirPath, err := ioutil.TempDir("", "docker-build")
|
2013-06-15 12:38:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-01-07 20:46:04 -05:00
|
|
|
b.context = &utils.TarSum{Reader: context, DisableCompression: true}
|
2013-12-26 19:02:15 -05:00
|
|
|
if err := archive.Untar(b.context, tmpdirPath, nil); err != nil {
|
2013-06-15 12:38:18 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2013-12-26 19:02:15 -05:00
|
|
|
defer os.RemoveAll(tmpdirPath)
|
2014-01-07 20:46:04 -05:00
|
|
|
|
2013-12-26 19:02:15 -05:00
|
|
|
b.contextPath = tmpdirPath
|
|
|
|
filename := path.Join(tmpdirPath, "Dockerfile")
|
2013-09-09 19:42:04 -04:00
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
2013-06-15 12:38:18 -04:00
|
|
|
return "", fmt.Errorf("Can't build a directory with no Dockerfile")
|
2013-05-28 16:38:26 -04:00
|
|
|
}
|
2013-09-09 19:42:04 -04:00
|
|
|
fileBytes, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-20 07:26:11 -05:00
|
|
|
if len(fileBytes) == 0 {
|
|
|
|
return "", ErrDockerfileEmpty
|
|
|
|
}
|
2013-09-09 19:42:04 -04:00
|
|
|
dockerfile := string(fileBytes)
|
2013-09-11 19:43:55 -04:00
|
|
|
dockerfile = lineContinuation.ReplaceAllString(dockerfile, "")
|
2013-06-18 15:26:56 -04:00
|
|
|
stepN := 0
|
2013-09-09 19:42:04 -04:00
|
|
|
for _, line := range strings.Split(dockerfile, "\n") {
|
2013-06-10 12:31:59 -04:00
|
|
|
line = strings.Trim(strings.Replace(line, "\t", " ", -1), " \t\r\n")
|
2013-05-23 21:33:31 -04:00
|
|
|
// Skip comments and empty line
|
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmp := strings.SplitN(line, " ", 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 {
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.errStream, "# Skipping unknown instruction %s\n", strings.ToUpper(instruction))
|
2013-05-29 13:53:24 -04:00
|
|
|
continue
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
2013-08-09 15:56:37 -04:00
|
|
|
|
|
|
|
stepN += 1
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, "Step %d : %s %s\n", stepN, strings.ToUpper(instruction), arguments)
|
2013-08-09 15:56:37 -04:00
|
|
|
|
2013-05-23 21:33:31 -04:00
|
|
|
ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface()
|
|
|
|
if ret != nil {
|
|
|
|
return "", ret.(error)
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, " ---> %s\n", utils.TruncateID(b.image))
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
if b.image != "" {
|
2013-12-02 14:43:41 -05:00
|
|
|
fmt.Fprintf(b.outStream, "Successfully built %s\n", utils.TruncateID(b.image))
|
2013-09-10 14:39:47 -04:00
|
|
|
if b.rm {
|
|
|
|
b.clearTmp(b.tmpContainers)
|
|
|
|
}
|
2013-05-23 21:33:31 -04:00
|
|
|
return b.image, nil
|
|
|
|
}
|
2013-12-20 08:02:10 -05:00
|
|
|
return "", fmt.Errorf("No image was generated. This may be because the Dockerfile does not, like, do anything.\n")
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-12-06 17:27:10 -05:00
|
|
|
func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *auth.AuthConfig) BuildFile {
|
2013-05-23 21:33:31 -04:00
|
|
|
return &buildFile{
|
|
|
|
runtime: srv.runtime,
|
|
|
|
srv: srv,
|
|
|
|
config: &Config{},
|
2013-12-02 14:43:41 -05:00
|
|
|
outStream: outStream,
|
|
|
|
errStream: errStream,
|
2013-05-23 21:33:31 -04:00
|
|
|
tmpContainers: make(map[string]struct{}),
|
|
|
|
tmpImages: make(map[string]struct{}),
|
2013-07-11 20:12:25 -04:00
|
|
|
verbose: verbose,
|
2013-08-02 12:18:54 -04:00
|
|
|
utilizeCache: utilizeCache,
|
2013-09-10 14:39:47 -04:00
|
|
|
rm: rm,
|
2013-10-12 08:14:52 -04:00
|
|
|
sf: sf,
|
2013-12-06 17:27:10 -05:00
|
|
|
authConfig: auth,
|
2013-12-02 14:43:41 -05:00
|
|
|
outOld: outOld,
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
}
|