2013-05-23 21:33:31 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"io"
|
2013-05-28 16:37:49 -04:00
|
|
|
"io/ioutil"
|
2013-05-23 21:33:31 -04:00
|
|
|
"os"
|
2013-05-28 16:37:49 -04:00
|
|
|
"path"
|
2013-05-23 21:33:31 -04:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BuildFile interface {
|
|
|
|
Build(io.Reader, io.Reader) (string, error)
|
|
|
|
CmdFrom(string) error
|
|
|
|
CmdRun(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type buildFile struct {
|
|
|
|
runtime *Runtime
|
|
|
|
builder *Builder
|
|
|
|
srv *Server
|
|
|
|
|
|
|
|
image string
|
|
|
|
maintainer string
|
|
|
|
config *Config
|
2013-05-28 16:38:26 -04:00
|
|
|
context string
|
2013-05-23 21:33:31 -04: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) {
|
|
|
|
|
|
|
|
var tag, remote string
|
|
|
|
if strings.Contains(name, ":") {
|
|
|
|
remoteParts := strings.Split(name, ":")
|
|
|
|
tag = remoteParts[1]
|
|
|
|
remote = remoteParts[0]
|
|
|
|
} else {
|
|
|
|
remote = name
|
|
|
|
}
|
|
|
|
|
2013-06-03 07:06:13 -04:00
|
|
|
if err := b.srv.ImagePull(remote, tag, "", b.out, utils.NewStreamFormatter(false), nil); 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{}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdRun(args string) error {
|
|
|
|
if b.image == "" {
|
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
|
|
|
}
|
|
|
|
config, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", args}, nil)
|
|
|
|
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-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-05-29 19:10:11 -04:00
|
|
|
if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
|
2013-05-23 21:33:31 -04:00
|
|
|
return err
|
|
|
|
} else if cache != nil {
|
2013-05-29 19:10:11 -04:00
|
|
|
utils.Debugf("[BUILDER] Use cached version")
|
2013-06-04 14:00:22 -04:00
|
|
|
b.image = cache.ID
|
2013-05-23 21:33:31 -04:00
|
|
|
return nil
|
2013-05-29 19:10:11 -04:00
|
|
|
} else {
|
|
|
|
utils.Debugf("[BUILDER] Cache miss")
|
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
|
|
|
|
}
|
|
|
|
b.config.Cmd = cmd
|
|
|
|
return 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")
|
|
|
|
}
|
|
|
|
key := strings.Trim(tmp[0], " ")
|
|
|
|
value := strings.Trim(tmp[1], " ")
|
|
|
|
|
|
|
|
for i, elem := range b.config.Env {
|
|
|
|
if strings.HasPrefix(elem, key+"=") {
|
|
|
|
b.config.Env[i] = key + "=" + value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.config.Env = append(b.config.Env, key+"="+value)
|
2013-05-29 18:03:00 -04:00
|
|
|
return b.commit("", b.config.Cmd, fmt.Sprintf("ENV %s=%s", key, value))
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdCmd(args string) error {
|
|
|
|
var cmd []string
|
|
|
|
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
|
|
|
|
utils.Debugf("Error unmarshalling: %s, using /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-05-30 15:21:57 -04:00
|
|
|
if err := b.commit("", cmd, fmt.Sprintf("CMD %v", cmd)); err != nil {
|
|
|
|
return err
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
2013-05-30 15:21:57 -04:00
|
|
|
b.config.Cmd = cmd
|
2013-05-23 21:33:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) CmdInsert(args string) error {
|
2013-05-30 15:10:54 -04:00
|
|
|
return fmt.Errorf("INSERT has been deprecated. Please use ADD instead")
|
|
|
|
}
|
2013-05-23 21:33:31 -04:00
|
|
|
|
2013-05-30 15:10:54 -04:00
|
|
|
func (b *buildFile) CmdCopy(args string) error {
|
|
|
|
return fmt.Errorf("COPY has been deprecated. Please use ADD instead")
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:38:26 -04:00
|
|
|
func (b *buildFile) CmdAdd(args string) error {
|
2013-05-28 16:51:21 -04:00
|
|
|
if b.context == "" {
|
|
|
|
return fmt.Errorf("No context given. Impossible to use ADD")
|
|
|
|
}
|
2013-05-28 16:38:26 -04:00
|
|
|
tmp := strings.SplitN(args, " ", 2)
|
|
|
|
if len(tmp) != 2 {
|
2013-05-29 21:14:50 -04:00
|
|
|
return fmt.Errorf("Invalid ADD format")
|
2013-05-28 16:38:26 -04:00
|
|
|
}
|
|
|
|
orig := strings.Trim(tmp[0], " ")
|
|
|
|
dest := strings.Trim(tmp[1], " ")
|
|
|
|
|
2013-05-29 17:37:03 -04:00
|
|
|
cmd := b.config.Cmd
|
2013-06-13 18:18:15 -04:00
|
|
|
|
|
|
|
// Create the container and start it
|
2013-06-14 19:24:22 -04:00
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(nop) ADD %s in %s", orig, dest)}
|
|
|
|
b.config.Image = b.image
|
2013-06-13 18:18:15 -04:00
|
|
|
container, err := b.builder.Create(b.config)
|
2013-05-28 16:38:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-13 18:18:15 -04:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
2013-05-28 16:38:26 -04:00
|
|
|
|
2013-05-29 20:58:05 -04:00
|
|
|
if err := container.EnsureMounted(); err != nil {
|
2013-05-28 16:38:26 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-29 20:58:05 -04:00
|
|
|
defer container.Unmount()
|
2013-05-28 16:38:26 -04:00
|
|
|
|
2013-05-28 18:22:01 -04:00
|
|
|
origPath := path.Join(b.context, orig)
|
2013-05-29 20:58:05 -04:00
|
|
|
destPath := path.Join(container.RootfsPath(), dest)
|
2013-05-28 18:22:01 -04:00
|
|
|
|
|
|
|
fi, err := os.Stat(origPath)
|
|
|
|
if err != nil {
|
2013-05-28 16:38:26 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-28 18:22:01 -04:00
|
|
|
if fi.IsDir() {
|
2013-05-29 21:55:00 -04:00
|
|
|
if err := os.MkdirAll(destPath, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-14 19:43:39 -04:00
|
|
|
if err := 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
|
|
|
|
} 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-05-29 21:55:00 -04:00
|
|
|
if err := os.MkdirAll(path.Dir(destPath), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-14 19:43:39 -04:00
|
|
|
if err := CopyWithTar(origPath, destPath); err != nil {
|
2013-05-28 18:22:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 18:18:15 -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-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
|
|
|
|
c, err := b.builder.Create(b.config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
b.tmpContainers[c.ID] = struct{}{}
|
2013-05-23 21:33:31 -04:00
|
|
|
|
|
|
|
//start the container
|
|
|
|
if err := c.Start(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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-05-29 18:03:00 -04:00
|
|
|
b.config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
|
2013-05-29 19:10:11 -04:00
|
|
|
|
|
|
|
if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
|
|
|
|
return err
|
|
|
|
} else if cache != nil {
|
|
|
|
utils.Debugf("[BUILDER] Use cached version")
|
2013-06-04 14:00:22 -04:00
|
|
|
b.image = cache.ID
|
2013-05-29 19:10:11 -04:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
utils.Debugf("[BUILDER] Cache miss")
|
|
|
|
}
|
|
|
|
|
2013-06-13 18:18:15 -04:00
|
|
|
// Create the container and start it
|
|
|
|
container, err := b.builder.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-06-13 18:18:15 -04:00
|
|
|
b.tmpContainers[container.ID] = struct{}{}
|
|
|
|
|
|
|
|
if err := container.EnsureMounted(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
|
|
|
|
|
|
|
id = container.ID
|
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-05-29 17:37:03 -04:00
|
|
|
image, err := b.builder.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buildFile) Build(dockerfile, context io.Reader) (string, error) {
|
2013-05-28 16:38:26 -04:00
|
|
|
if context != nil {
|
|
|
|
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
|
|
|
|
}
|
2013-05-23 21:33:31 -04:00
|
|
|
file := bufio.NewReader(dockerfile)
|
|
|
|
for {
|
|
|
|
line, err := file.ReadString('\n')
|
|
|
|
if err != nil {
|
2013-06-11 10:39:06 -04:00
|
|
|
if err == io.EOF && line == "" {
|
2013-05-23 21:33:31 -04:00
|
|
|
break
|
2013-06-11 10:39:06 -04:00
|
|
|
} else if err != io.EOF {
|
|
|
|
return "", err
|
2013-05-23 21:33:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
line = strings.Replace(strings.TrimSpace(line), " ", " ", 1)
|
|
|
|
// 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], " ")
|
|
|
|
|
|
|
|
fmt.Fprintf(b.out, "%s %s (%s)\n", strings.ToUpper(instruction), arguments, b.image)
|
|
|
|
|
|
|
|
method, exists := reflect.TypeOf(b).MethodByName("Cmd" + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
|
|
|
|
if !exists {
|
|
|
|
fmt.Fprintf(b.out, "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
|
|
|
}
|
|
|
|
ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface()
|
|
|
|
if ret != nil {
|
|
|
|
return "", ret.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(b.out, "===> %v\n", b.image)
|
|
|
|
}
|
|
|
|
if b.image != "" {
|
2013-05-29 21:14:50 -04:00
|
|
|
fmt.Fprintf(b.out, "Build successful.\n===> %s\n", b.image)
|
2013-05-23 21:33:31 -04:00
|
|
|
return b.image, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("An error occured during the build\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuildFile(srv *Server, out io.Writer) BuildFile {
|
|
|
|
return &buildFile{
|
|
|
|
builder: NewBuilder(srv.runtime),
|
|
|
|
runtime: srv.runtime,
|
|
|
|
srv: srv,
|
|
|
|
config: &Config{},
|
2013-05-28 16:38:26 -04:00
|
|
|
out: out,
|
2013-05-23 21:33:31 -04:00
|
|
|
tmpContainers: make(map[string]struct{}),
|
|
|
|
tmpImages: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
}
|