1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/builder_client.go

315 lines
7.4 KiB
Go
Raw Normal View History

2013-05-19 13:46:24 -04:00
package docker
import (
"bufio"
"encoding/json"
"fmt"
"github.com/dotcloud/docker/utils"
"io"
"net/url"
"os"
"reflect"
"strings"
)
2013-05-20 19:00:16 -04:00
type builderClient struct {
cli *DockerCli
2013-05-19 13:46:24 -04:00
image string
maintainer string
config *Config
tmpContainers map[string]struct{}
tmpImages map[string]struct{}
needCommit bool
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) clearTmp(containers, images map[string]struct{}) {
2013-05-19 13:46:24 -04:00
for i := range images {
if _, _, err := b.cli.call("DELETE", "/images/"+i, nil); err != nil {
utils.Debugf("%s", err)
}
2013-05-19 13:46:24 -04:00
utils.Debugf("Removing image %s", i)
}
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdFrom(name string) error {
2013-05-19 13:46:24 -04:00
obj, statusCode, err := b.cli.call("GET", "/images/"+name+"/json", nil)
if statusCode == 404 {
remote := name
var tag string
if strings.Contains(remote, ":") {
remoteParts := strings.Split(remote, ":")
tag = remoteParts[1]
remote = remoteParts[0]
}
var out io.Writer
if os.Getenv("DEBUG") != "" {
out = os.Stdout
} else {
out = &utils.NopWriter{}
}
if err := b.cli.stream("POST", "/images/create?fromImage="+remote+"&tag="+tag, nil, out); err != nil {
2013-05-19 13:46:24 -04:00
return err
}
obj, _, err = b.cli.call("GET", "/images/"+name+"/json", nil)
if err != nil {
return err
}
}
if err != nil {
return err
}
2013-06-04 14:00:22 -04:00
img := &APIID{}
2013-05-19 13:46:24 -04:00
if err := json.Unmarshal(obj, img); err != nil {
return err
}
2013-06-04 14:00:22 -04:00
b.image = img.ID
2013-05-20 19:35:28 -04:00
utils.Debugf("Using image %s", b.image)
2013-05-19 13:46:24 -04:00
return nil
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdMaintainer(name string) error {
2013-05-19 13:46:24 -04:00
b.needCommit = true
b.maintainer = name
return nil
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdRun(args string) error {
2013-05-19 13:46:24 -04:00
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)
2013-05-19 13:46:24 -04:00
if err != nil {
return err
}
2013-05-20 18:02:32 -04:00
cmd, env := b.config.Cmd, b.config.Env
b.config.Cmd = nil
2013-05-19 13:46:24 -04:00
MergeConfig(b.config, config)
2013-05-20 18:02:32 -04:00
2013-06-04 14:00:22 -04:00
body, statusCode, err := b.cli.call("POST", "/images/getCache", &APIImageConfig{ID: b.image, Config: b.config})
2013-05-19 13:46:24 -04:00
if err != nil {
if statusCode != 404 {
return err
}
}
if statusCode != 404 {
2013-06-04 14:00:22 -04:00
apiID := &APIID{}
if err := json.Unmarshal(body, apiID); err != nil {
2013-05-19 13:46:24 -04:00
return err
}
2013-05-20 18:02:32 -04:00
utils.Debugf("Use cached version")
2013-06-04 14:00:22 -04:00
b.image = apiID.ID
2013-05-19 13:46:24 -04:00
return nil
}
2013-05-20 18:02:32 -04:00
cid, err := b.run()
if err != nil {
return err
}
b.config.Cmd, b.config.Env = cmd, env
return b.commit(cid)
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdEnv(args string) error {
b.needCommit = true
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)
return nil
}
2013-05-19 13:46:24 -04:00
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdCmd(args string) error {
b.needCommit = true
2013-05-20 19:00:51 -04:00
var cmd []string
if err := json.Unmarshal([]byte(args), &cmd); err != nil {
2013-05-20 19:35:28 -04:00
utils.Debugf("Error unmarshalling: %s, using /bin/sh -c", err)
2013-05-20 19:00:51 -04:00
b.config.Cmd = []string{"/bin/sh", "-c", args}
} else {
b.config.Cmd = cmd
}
return nil
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdExpose(args string) error {
ports := strings.Split(args, " ")
b.config.PortSpecs = append(ports, b.config.PortSpecs...)
return nil
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) CmdInsert(args string) error {
// tmp := strings.SplitN(args, "\t ", 2)
// sourceUrl, destPath := tmp[0], tmp[1]
// v := url.Values{}
// v.Set("url", sourceUrl)
// v.Set("path", destPath)
// body, _, err := b.cli.call("POST", "/images/insert?"+v.Encode(), nil)
// if err != nil {
// return err
// }
2013-06-04 14:00:22 -04:00
// apiId := &APIId{}
// if err := json.Unmarshal(body, apiId); err != nil {
// return err
// }
// FIXME: Reimplement this, we need to retrieve the resulting Id
return fmt.Errorf("INSERT not implemented")
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) run() (string, error) {
2013-05-20 18:02:32 -04:00
if b.image == "" {
return "", fmt.Errorf("Please provide a source image with `from` prior to run")
}
2013-05-20 18:02:32 -04:00
b.config.Image = b.image
body, _, err := b.cli.call("POST", "/containers/create", b.config)
2013-05-19 13:46:24 -04:00
if err != nil {
2013-05-20 18:02:32 -04:00
return "", err
2013-05-19 13:46:24 -04:00
}
2013-06-04 14:00:22 -04:00
apiRun := &APIRun{}
2013-05-20 18:02:32 -04:00
if err := json.Unmarshal(body, apiRun); err != nil {
return "", err
2013-05-19 13:46:24 -04:00
}
2013-05-20 18:02:32 -04:00
for _, warning := range apiRun.Warnings {
2013-05-19 13:46:24 -04:00
fmt.Fprintln(os.Stderr, "WARNING: ", warning)
}
//start the container
2013-06-04 14:00:22 -04:00
_, _, err = b.cli.call("POST", "/containers/"+apiRun.ID+"/start", nil)
2013-05-19 13:46:24 -04:00
if err != nil {
2013-05-20 18:02:32 -04:00
return "", err
2013-05-19 13:46:24 -04:00
}
2013-06-04 14:00:22 -04:00
b.tmpContainers[apiRun.ID] = struct{}{}
2013-05-19 13:46:24 -04:00
// Wait for it to finish
2013-06-04 14:00:22 -04:00
body, _, err = b.cli.call("POST", "/containers/"+apiRun.ID+"/wait", nil)
2013-05-19 13:46:24 -04:00
if err != nil {
2013-05-20 18:02:32 -04:00
return "", err
}
2013-06-04 14:00:22 -04:00
apiWait := &APIWait{}
2013-05-20 18:02:32 -04:00
if err := json.Unmarshal(body, apiWait); err != nil {
return "", err
}
if apiWait.StatusCode != 0 {
return "", fmt.Errorf("The command %v returned a non-zero code: %d", b.config.Cmd, apiWait.StatusCode)
}
2013-06-04 14:00:22 -04:00
return apiRun.ID, nil
2013-05-20 18:02:32 -04:00
}
2013-05-20 19:35:28 -04:00
func (b *builderClient) commit(id string) error {
2013-05-20 18:02:32 -04:00
if b.image == "" {
return fmt.Errorf("Please provide a source image with `from` prior to run")
}
b.config.Image = b.image
if id == "" {
cmd := b.config.Cmd
b.config.Cmd = []string{"true"}
2013-06-04 09:51:12 -04:00
cid, err := b.run()
if err != nil {
2013-05-20 18:02:32 -04:00
return err
}
2013-06-04 09:51:12 -04:00
id = cid
2013-05-20 18:02:32 -04:00
b.config.Cmd = cmd
2013-05-19 13:46:24 -04:00
}
// Commit the container
v := url.Values{}
2013-05-20 18:02:32 -04:00
v.Set("container", id)
2013-05-19 13:46:24 -04:00
v.Set("author", b.maintainer)
2013-05-20 18:02:32 -04:00
body, _, err := b.cli.call("POST", "/commit?"+v.Encode(), b.config)
2013-05-19 13:46:24 -04:00
if err != nil {
return err
}
2013-06-04 14:00:22 -04:00
apiID := &APIID{}
if err := json.Unmarshal(body, apiID); err != nil {
2013-05-19 13:46:24 -04:00
return err
}
2013-06-04 14:00:22 -04:00
b.tmpImages[apiID.ID] = struct{}{}
b.image = apiID.ID
2013-05-20 18:02:32 -04:00
b.needCommit = false
2013-05-19 13:46:24 -04:00
return nil
}
func (b *builderClient) Build(dockerfile, context io.Reader) (string, error) {
defer b.clearTmp(b.tmpContainers, b.tmpImages)
2013-05-19 13:46:24 -04:00
file := bufio.NewReader(dockerfile)
for {
line, err := file.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return "", err
}
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(os.Stderr, "%s %s (%s)\n", strings.ToUpper(instruction), arguments, b.image)
2013-05-19 13:46:24 -04:00
method, exists := reflect.TypeOf(b).MethodByName("Cmd" + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
2013-05-19 13:46:24 -04:00
if !exists {
fmt.Fprintf(os.Stderr, "Skipping unknown instruction %s\n", strings.ToUpper(instruction))
2013-05-19 13:46:24 -04:00
}
ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface()
if ret != nil {
return "", ret.(error)
}
fmt.Fprintf(os.Stderr, "===> %v\n", b.image)
2013-05-19 13:46:24 -04:00
}
if b.needCommit {
2013-05-20 18:02:32 -04:00
if err := b.commit(""); err != nil {
return "", err
}
2013-05-19 13:46:24 -04:00
}
if b.image != "" {
// The build is successful, keep the temporary containers and images
for i := range b.tmpImages {
delete(b.tmpImages, i)
}
for i := range b.tmpContainers {
delete(b.tmpContainers, i)
}
fmt.Fprintf(os.Stderr, "Build finished. image id: %s\n", b.image)
2013-05-19 13:46:24 -04:00
return b.image, nil
}
return "", fmt.Errorf("An error occured during the build\n")
}
func NewBuilderClient(addr string, port int) BuildFile {
2013-05-20 19:00:16 -04:00
return &builderClient{
cli: NewDockerCli(addr, port),
config: &Config{},
tmpContainers: make(map[string]struct{}),
tmpImages: make(map[string]struct{}),
}
}