2013-05-24 01:33:31 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"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-05-24 01:33:31 +00:00
|
|
|
"reflect"
|
2013-07-09 07:01:45 +00:00
|
|
|
"regexp"
|
2013-05-24 01:33:31 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
runtime *Runtime
|
|
|
|
builder *Builder
|
|
|
|
srv *Server
|
|
|
|
|
2013-08-02 16:18:54 +00:00
|
|
|
image string
|
|
|
|
maintainer string
|
|
|
|
config *Config
|
|
|
|
context string
|
|
|
|
verbose bool
|
|
|
|
utilizeCache bool
|
2013-05-24 01:33:31 +00:00
|
|
|
|
|
|
|
tmpContainers map[string]struct{}
|
|
|
|
tmpImages map[string]struct{}
|
|
|
|
|
|
|
|
out io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) clearTmp(containers, images map[string]struct{}) {
|
|
|
|
for c := range containers {
|
|
|
|
tmp := b.runtime.Get(c)
|
|
|
|
b.runtime.Destroy(tmp)
|
|
|
|
utils.Debugf("Removing container %s", c)
|
|
|
|
}
|
|
|
|
for i := range images {
|
|
|
|
b.runtime.graph.Delete(i)
|
|
|
|
utils.Debugf("Removing image %s", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:06:10 +00:00
|
|
|
remote, tag := utils.ParseRepositoryTag(name)
|
2013-08-22 19:15:31 +00:00
|
|
|
if err := b.srv.ImagePull(remote, tag, b.out, utils.NewStreamFormatter(false), nil, nil, true); err != nil {
|
2013-05-24 01:33:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
image, err = b.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
b.image = image.ID
|
2013-05-24 01:33:31 +00:00
|
|
|
b.config = &Config{}
|
2013-07-09 07:01:45 +00: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-24 01:33:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 23:39:54 +00:00
|
|
|
config, _, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", 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
|
|
|
|
MergeConfig(b.config, config)
|
|
|
|
|
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-08-02 16:18:54 +00:00
|
|
|
if b.utilizeCache {
|
|
|
|
if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
|
|
|
|
return err
|
|
|
|
} else if cache != nil {
|
|
|
|
fmt.Fprintf(b.out, " ---> Using cache\n")
|
|
|
|
utils.Debugf("[BUILDER] Use cached version")
|
|
|
|
b.image = cache.ID
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
utils.Debugf("[BUILDER] Cache miss")
|
|
|
|
}
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := b.run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-29 22:03:00 +00:00
|
|
|
if err := b.commit(cid, 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
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdCmd(args string) error {
|
|
|
|
var cmd []string
|
|
|
|
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
|
2013-06-25 02:20:05 +00:00
|
|
|
utils.Debugf("Error unmarshalling: %s, setting cmd 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-05-30 19:21:57 +00:00
|
|
|
if err := b.commit("", cmd, fmt.Sprintf("CMD %v", cmd)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.config.Cmd = cmd
|
|
|
|
return nil
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdExpose(args string) error {
|
|
|
|
ports := strings.Split(args, " ")
|
|
|
|
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
|
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 {
|
|
|
|
return fmt.Errorf("COPY has been deprecated. Please use ADD instead")
|
|
|
|
}
|
|
|
|
|
2013-06-25 02:20:05 +00:00
|
|
|
func (b *buildFile) CmdEntrypoint(args string) error {
|
|
|
|
if args == "" {
|
|
|
|
return fmt.Errorf("Entrypoint cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var entrypoint []string
|
|
|
|
if err := json.Unmarshal([]byte(args), &entrypoint); err != nil {
|
|
|
|
b.config.Entrypoint = []string{"/bin/sh", "-c", args}
|
|
|
|
} else {
|
|
|
|
b.config.Entrypoint = entrypoint
|
|
|
|
}
|
|
|
|
if err := b.commit("", b.config.Cmd, fmt.Sprintf("ENTRYPOINT %s", args)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-18 18:30:19 +00: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-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 {
|
|
|
|
b.config.Volumes = NewPathOpts()
|
|
|
|
}
|
|
|
|
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-06-06 22:40:46 +00:00
|
|
|
func (b *buildFile) addRemote(container *Container, orig, dest string) error {
|
|
|
|
file, err := utils.Download(orig, ioutil.Discard)
|
2013-05-28 20:38:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-06 22:40:46 +00:00
|
|
|
defer file.Body.Close()
|
2013-05-28 20:38:26 +00:00
|
|
|
|
2013-07-08 07:43:22 +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)
|
|
|
|
}
|
|
|
|
dest = dest + filename
|
|
|
|
}
|
|
|
|
|
2013-06-06 22:40:46 +00:00
|
|
|
return container.Inject(file.Body, dest)
|
|
|
|
}
|
2013-05-28 20:38:26 +00:00
|
|
|
|
2013-06-06 22:40:46 +00:00
|
|
|
func (b *buildFile) addContext(container *Container, orig, dest string) error {
|
2013-05-28 22:22:01 +00:00
|
|
|
origPath := path.Join(b.context, orig)
|
2013-05-30 00:58:05 +00:00
|
|
|
destPath := path.Join(container.RootfsPath(), dest)
|
2013-06-19 03:28:49 +00:00
|
|
|
// Preserve the trailing '/'
|
2013-07-08 07:43:22 +00:00
|
|
|
if strings.HasSuffix(dest, "/") {
|
2013-06-19 03:28:49 +00:00
|
|
|
destPath = destPath + "/"
|
|
|
|
}
|
2013-08-06 16:58:52 +00:00
|
|
|
if !strings.HasPrefix(origPath, b.context) {
|
|
|
|
return fmt.Errorf("Forbidden path: %s", origPath)
|
|
|
|
}
|
2013-05-28 22:22:01 +00:00
|
|
|
fi, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
2013-05-28 20:38:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-05-28 22:22:01 +00:00
|
|
|
if fi.IsDir() {
|
2013-06-14 23:43:39 +00:00
|
|
|
if err := CopyWithTar(origPath, destPath); err != nil {
|
2013-05-28 22:22:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-06-14 23:43:39 +00:00
|
|
|
// First try to unpack the source as an archive
|
|
|
|
} else if err := UntarPath(origPath, destPath); err != nil {
|
|
|
|
utils.Debugf("Couldn't untar %s to %s: %s", origPath, destPath, err)
|
|
|
|
// If that fails, just copy it as a regular file
|
2013-07-26 21:57:16 +00:00
|
|
|
if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil {
|
2013-05-30 01:55:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-06-14 23:43:39 +00:00
|
|
|
if err := CopyWithTar(origPath, destPath); err != nil {
|
2013-05-28 22:22:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 22:40:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdAdd(args string) error {
|
|
|
|
if b.context == "" {
|
|
|
|
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 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
|
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) ADD %s in %s", orig, dest)}
|
2013-06-13 21:57:50 +00:00
|
|
|
|
2013-06-14 01:42:27 +00:00
|
|
|
b.config.Image = b.image
|
2013-06-13 21:57:50 +00:00
|
|
|
// Create the container and start it
|
2013-06-13 22:08:38 +00:00
|
|
|
container, err := b.builder.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
|
|
|
|
|
|
|
if err := container.EnsureMounted(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
2013-06-06 22:50:09 +00:00
|
|
|
if utils.IsURL(orig) {
|
2013-06-06 22:40:46 +00:00
|
|
|
if err := b.addRemote(container, orig, dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := b.addContext(container, orig, dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 22:08:38 +00:00
|
|
|
if err := b.commit(container.ID, cmd, fmt.Sprintf("ADD %s in %s", orig, dest)); err != nil {
|
2013-05-30 19:21:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.config.Cmd = cmd
|
|
|
|
return nil
|
2013-05-28 20:38:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 01:33:31 +00: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
|
|
|
|
c, err := b.builder.Create(b.config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
b.tmpContainers[c.ID] = struct{}{}
|
2013-06-18 19:26:56 +00:00
|
|
|
fmt.Fprintf(b.out, " ---> Running in %s\n", utils.TruncateID(c.ID))
|
2013-05-24 01:33:31 +00:00
|
|
|
|
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:]
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
//start the container
|
2013-05-13 23:39:54 +00:00
|
|
|
hostConfig := &HostConfig{}
|
|
|
|
if err := c.Start(hostConfig); err != nil {
|
2013-05-24 01:33:31 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:12:25 +00:00
|
|
|
if b.verbose {
|
|
|
|
err = <-c.Attach(nil, nil, b.out, b.out)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
// Wait for it to finish
|
|
|
|
if ret := c.Wait(); ret != 0 {
|
|
|
|
return "", fmt.Errorf("The command %v returned a non-zero code: %d", b.config.Cmd, ret)
|
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
return c.ID, 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-08-02 16:18:54 +00:00
|
|
|
if b.utilizeCache {
|
|
|
|
if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
|
|
|
|
return err
|
|
|
|
} else if cache != nil {
|
|
|
|
fmt.Fprintf(b.out, " ---> Using cache\n")
|
|
|
|
utils.Debugf("[BUILDER] Use cached version")
|
|
|
|
b.image = cache.ID
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
utils.Debugf("[BUILDER] Cache miss")
|
|
|
|
}
|
2013-05-29 23:10:11 +00:00
|
|
|
}
|
2013-08-02 16:18:54 +00:00
|
|
|
|
2013-06-13 22:08:38 +00:00
|
|
|
container, err := b.builder.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-06-13 22:08:38 +00:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2013-06-18 19:26:56 +00:00
|
|
|
fmt.Fprintf(b.out, " ---> Running in %s\n", utils.TruncateID(container.ID))
|
2013-06-13 22:08:38 +00:00
|
|
|
id = container.ID
|
|
|
|
if err := container.EnsureMounted(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
container := b.runtime.Get(id)
|
|
|
|
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
|
2013-05-29 21:37:03 +00:00
|
|
|
image, err := b.builder.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-06-15 16:38:18 +00:00
|
|
|
func (b *buildFile) Build(context io.Reader) (string, error) {
|
|
|
|
// FIXME: @creack any reason for using /tmp instead of ""?
|
|
|
|
// FIXME: @creack "name" is a terrible variable name
|
|
|
|
name, err := ioutil.TempDir("/tmp", "docker-build")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := Untar(context, name); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(name)
|
|
|
|
b.context = name
|
|
|
|
dockerfile, err := os.Open(path.Join(name, "Dockerfile"))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Can't build a directory with no Dockerfile")
|
2013-05-28 20:38:26 +00:00
|
|
|
}
|
2013-06-15 16:38:18 +00:00
|
|
|
// FIXME: "file" is also a terrible variable name ;)
|
2013-05-24 01:33:31 +00:00
|
|
|
file := bufio.NewReader(dockerfile)
|
2013-06-18 19:26:56 +00:00
|
|
|
stepN := 0
|
2013-05-24 01:33:31 +00:00
|
|
|
for {
|
|
|
|
line, err := file.ReadString('\n')
|
|
|
|
if err != nil {
|
2013-06-11 14:39:06 +00:00
|
|
|
if err == io.EOF && line == "" {
|
2013-05-24 01:33:31 +00:00
|
|
|
break
|
2013-06-11 14:39:06 +00:00
|
|
|
} else if err != io.EOF {
|
|
|
|
return "", err
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-10 16:31:59 +00:00
|
|
|
line = strings.Trim(strings.Replace(line, "\t", " ", -1), " \t\r\n")
|
2013-05-24 01:33:31 +00: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-06-18 19:26:56 +00:00
|
|
|
fmt.Fprintf(b.out, "# Skipping unknown instruction %s\n", strings.ToUpper(instruction))
|
2013-05-29 17:53:24 +00:00
|
|
|
continue
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
2013-08-09 19:56:37 +00:00
|
|
|
|
|
|
|
stepN += 1
|
|
|
|
fmt.Fprintf(b.out, "Step %d : %s %s\n", stepN, strings.ToUpper(instruction), arguments)
|
|
|
|
|
2013-05-24 01:33:31 +00:00
|
|
|
ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface()
|
|
|
|
if ret != nil {
|
|
|
|
return "", ret.(error)
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:26:56 +00:00
|
|
|
fmt.Fprintf(b.out, " ---> %v\n", utils.TruncateID(b.image))
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
if b.image != "" {
|
2013-06-18 19:26:56 +00:00
|
|
|
fmt.Fprintf(b.out, "Successfully built %s\n", utils.TruncateID(b.image))
|
2013-05-24 01:33:31 +00:00
|
|
|
return b.image, nil
|
|
|
|
}
|
2013-08-12 17:53:06 +00:00
|
|
|
return "", fmt.Errorf("An error occurred during the build\n")
|
2013-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 16:18:54 +00:00
|
|
|
func NewBuildFile(srv *Server, out io.Writer, verbose, utilizeCache bool) BuildFile {
|
2013-05-24 01:33:31 +00:00
|
|
|
return &buildFile{
|
|
|
|
builder: NewBuilder(srv.runtime),
|
|
|
|
runtime: srv.runtime,
|
|
|
|
srv: srv,
|
|
|
|
config: &Config{},
|
2013-05-28 20:38:26 +00:00
|
|
|
out: out,
|
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-05-24 01:33:31 +00:00
|
|
|
}
|
|
|
|
}
|