server.go
04cd20fa
 package docker
 
 import (
054451fd
 	"errors"
04cd20fa
 	"fmt"
f29e5dc8
 	"github.com/dotcloud/docker/auth"
9bb3dc98
 	"github.com/dotcloud/docker/registry"
2e69e172
 	"github.com/dotcloud/docker/utils"
04cd20fa
 	"io"
49b61af1
 	"io/ioutil"
04cd20fa
 	"log"
 	"net/http"
 	"net/url"
 	"os"
49b61af1
 	"path"
04cd20fa
 	"runtime"
 	"strings"
fe204e6f
 	"sync"
04cd20fa
 )
 
fd224ee5
 func (srv *Server) DockerVersion() APIVersion {
 	return APIVersion{
1dae7a25
 		Version:   VERSION,
fd224ee5
 		GitCommit: GITCOMMIT,
1dae7a25
 		GoVersion: runtime.Version(),
 	}
04cd20fa
 }
 
 func (srv *Server) ContainerKill(name string) error {
 	if container := srv.runtime.Get(name); container != nil {
 		if err := container.Kill(); err != nil {
 			return fmt.Errorf("Error restarting container %s: %s", name, err.Error())
 		}
 	} else {
 		return fmt.Errorf("No such container: %s", name)
 	}
 	return nil
 }
 
57cfe72e
 func (srv *Server) ContainerExport(name string, out io.Writer) error {
04cd20fa
 	if container := srv.runtime.Get(name); container != nil {
 
 		data, err := container.Export()
 		if err != nil {
 			return err
 		}
 
 		// Stream the entire contents of the container (basically a volatile snapshot)
57cfe72e
 		if _, err := io.Copy(out, data); err != nil {
04cd20fa
 			return err
 		}
 		return nil
 	}
 	return fmt.Errorf("No such container: %s", name)
 }
 
fd224ee5
 func (srv *Server) ImagesSearch(term string) ([]APISearch, error) {
fde82f44
 	r, err := registry.NewRegistry(srv.runtime.root, nil)
 	if err != nil {
 		return nil, err
 	}
 	results, err := r.SearchRepositories(term)
59a6316f
 	if err != nil {
40794113
 		return nil, err
 	}
 
fd224ee5
 	var outs []APISearch
59a6316f
 	for _, repo := range results.Results {
fd224ee5
 		var out APISearch
59a6316f
 		out.Description = repo["description"]
 		out.Name = repo["name"]
 		outs = append(outs, out)
 	}
 	return outs, nil
 }
 
f339fc2e
 func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {
ae9d7a51
 	out = utils.NewWriteFlusher(out)
10c0e990
 	img, err := srv.runtime.repositories.LookupImage(name)
 	if err != nil {
0f135ad7
 		return "", err
10c0e990
 	}
 
2e69e172
 	file, err := utils.Download(url, out)
10c0e990
 	if err != nil {
0f135ad7
 		return "", err
10c0e990
 	}
 	defer file.Body.Close()
 
fd224ee5
 	config, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.capabilities)
10c0e990
 	if err != nil {
0f135ad7
 		return "", err
10c0e990
 	}
 
 	b := NewBuilder(srv.runtime)
 	c, err := b.Create(config)
 	if err != nil {
0f135ad7
 		return "", err
10c0e990
 	}
 
c8c7094b
 	if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), path); err != nil {
a4879901
 		return "", err
10c0e990
 	}
 	// FIXME: Handle custom repo, tag comment, author
 	img, err = b.Commit(c, "", "", img.Comment, img.Author, nil)
 	if err != nil {
0f135ad7
 		return "", err
10c0e990
 	}
fd224ee5
 	out.Write(sf.FormatStatus(img.ID))
 	return img.ShortID(), nil
10c0e990
 }
 
57cfe72e
 func (srv *Server) ImagesViz(out io.Writer) error {
10c0e990
 	images, _ := srv.runtime.graph.All()
 	if images == nil {
 		return nil
 	}
0ecf5e24
 	out.Write([]byte("digraph docker {\n"))
10c0e990
 
57cfe72e
 	var (
 		parentImage *Image
 		err         error
 	)
10c0e990
 	for _, image := range images {
 		parentImage, err = image.GetParent()
 		if err != nil {
 			return fmt.Errorf("Error while getting parent image: %v", err)
 		}
 		if parentImage != nil {
fd224ee5
 			out.Write([]byte(" \"" + parentImage.ShortID() + "\" -> \"" + image.ShortID() + "\"\n"))
10c0e990
 		} else {
fd224ee5
 			out.Write([]byte(" base -> \"" + image.ShortID() + "\" [style=invis]\n"))
10c0e990
 		}
 	}
 
 	reporefs := make(map[string][]string)
 
 	for name, repository := range srv.runtime.repositories.Repositories {
 		for tag, id := range repository {
fd224ee5
 			reporefs[utils.TruncateID(id)] = append(reporefs[utils.TruncateID(id)], fmt.Sprintf("%s:%s", name, tag))
10c0e990
 		}
 	}
 
 	for id, repos := range reporefs {
0ecf5e24
 		out.Write([]byte(" \"" + id + "\" [label=\"" + id + "\\n" + strings.Join(repos, "\\n") + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n"))
10c0e990
 	}
0ecf5e24
 	out.Write([]byte(" base [style=invisible]\n}\n"))
10c0e990
 	return nil
 }
 
fd224ee5
 func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
0f312113
 	var (
 		allImages map[string]*Image
 		err       error
 	)
ab96da8e
 	if all {
04cd20fa
 		allImages, err = srv.runtime.graph.Map()
 	} else {
 		allImages, err = srv.runtime.graph.Heads()
 	}
 	if err != nil {
 		return nil, err
 	}
fd224ee5
 	outs := []APIImages{} //produce [] when empty instead of 'null'
04cd20fa
 	for name, repository := range srv.runtime.repositories.Repositories {
 		if filter != "" && name != filter {
 			continue
 		}
 		for tag, id := range repository {
fd224ee5
 			var out APIImages
04cd20fa
 			image, err := srv.runtime.graph.Get(id)
 			if err != nil {
 				log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err)
 				continue
 			}
 			delete(allImages, id)
1990c49a
 			out.Repository = name
 			out.Tag = tag
fd224ee5
 			out.ID = image.ID
1990c49a
 			out.Created = image.Created.Unix()
6fce89e6
 			out.Size = image.Size
00cf2a1f
 			out.VirtualSize = image.getParentsSize(0) + image.Size
04cd20fa
 			outs = append(outs, out)
 		}
 	}
 	// Display images which aren't part of a
 	if filter == "" {
1990c49a
 		for _, image := range allImages {
fd224ee5
 			var out APIImages
 			out.ID = image.ID
1990c49a
 			out.Created = image.Created.Unix()
6fce89e6
 			out.Size = image.Size
00cf2a1f
 			out.VirtualSize = image.getParentsSize(0) + image.Size
04cd20fa
 			outs = append(outs, out)
 		}
 	}
 	return outs, nil
 }
 
fd224ee5
 func (srv *Server) DockerInfo() *APIInfo {
04cd20fa
 	images, _ := srv.runtime.graph.All()
 	var imgcount int
 	if images == nil {
 		imgcount = 0
 	} else {
 		imgcount = len(images)
 	}
fd224ee5
 	return &APIInfo{
1dae7a25
 		Containers:  len(srv.runtime.List()),
 		Images:      imgcount,
 		MemoryLimit: srv.runtime.capabilities.MemoryLimit,
 		SwapLimit:   srv.runtime.capabilities.SwapLimit,
 		Debug:       os.Getenv("DEBUG") != "",
 		NFd:         utils.GetTotalUsedFds(),
 		NGoroutines: runtime.NumGoroutine(),
 	}
04cd20fa
 }
 
fd224ee5
 func (srv *Server) ImageHistory(name string) ([]APIHistory, error) {
04cd20fa
 	image, err := srv.runtime.repositories.LookupImage(name)
 	if err != nil {
 		return nil, err
 	}
 
808faa63
 	lookupMap := make(map[string][]string)
3bfc8225
 	for name, repository := range srv.runtime.repositories.Repositories {
 		for tag, id := range repository {
 			// If the ID already has a reverse lookup, do not update it unless for "latest"
808faa63
 			if _, exists := lookupMap[id]; !exists {
 				lookupMap[id] = []string{}
3bfc8225
 			}
808faa63
 			lookupMap[id] = append(lookupMap[id], name+":"+tag)
3bfc8225
 		}
 	}
 
fd224ee5
 	outs := []APIHistory{} //produce [] when empty instead of 'null'
04cd20fa
 	err = image.WalkHistory(func(img *Image) error {
fd224ee5
 		var out APIHistory
 		out.ID = srv.runtime.repositories.ImageName(img.ShortID())
04cd20fa
 		out.Created = img.Created.Unix()
 		out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
808faa63
 		out.Tags = lookupMap[img.ID]
04cd20fa
 		outs = append(outs, out)
 		return nil
 	})
 	return outs, nil
 
 }
 
60ddcaa1
 func (srv *Server) ContainerChanges(name string) ([]Change, error) {
04cd20fa
 	if container := srv.runtime.Get(name); container != nil {
60ddcaa1
 		return container.Changes()
04cd20fa
 	}
 	return nil, fmt.Errorf("No such container: %s", name)
 }
 
fd224ee5
 func (srv *Server) Containers(all bool, n int, since, before string) []APIContainers {
bc3fa506
 	var foundBefore bool
 	var displayed int
fd224ee5
 	retContainers := []APIContainers{}
152ebeea
 
bc3fa506
 	for _, container := range srv.runtime.List() {
 		if !container.State.Running && !all && n == -1 && since == "" && before == "" {
04cd20fa
 			continue
 		}
bc3fa506
 		if before != "" {
fd224ee5
 			if container.ShortID() == before {
bc3fa506
 				foundBefore = true
 				continue
 			}
 			if !foundBefore {
 				continue
 			}
 		}
 		if displayed == n {
 			break
 		}
fd224ee5
 		if container.ShortID() == since {
04cd20fa
 			break
 		}
152ebeea
 		displayed++
 
fd224ee5
 		c := APIContainers{
 			ID: container.ID,
82313b1d
 		}
1990c49a
 		c.Image = srv.runtime.repositories.ImageName(container.Image)
 		c.Command = fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
 		c.Created = container.Created.Unix()
 		c.Status = container.State.String()
 		c.Ports = container.NetworkSettings.PortMappingHuman()
6fce89e6
 		c.SizeRw, c.SizeRootFs = container.GetSize()
152ebeea
 
 		retContainers = append(retContainers, c)
04cd20fa
 	}
152ebeea
 	return retContainers
04cd20fa
 }
 
 func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
10c0e990
 	container := srv.runtime.Get(name)
 	if container == nil {
 		return "", fmt.Errorf("No such container: %s", name)
 	}
 	img, err := NewBuilder(srv.runtime).Commit(container, repo, tag, comment, author, config)
04cd20fa
 	if err != nil {
 		return "", err
 	}
fd224ee5
 	return img.ShortID(), err
04cd20fa
 }
 
 func (srv *Server) ContainerTag(name, repo, tag string, force bool) error {
 	if err := srv.runtime.repositories.Set(repo, tag, name, force); err != nil {
 		return err
 	}
 	return nil
 }
 
cd002a4d
 func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgId, endpoint string, token []string, sf *utils.StreamFormatter) error {
cd0de839
 	history, err := r.GetRemoteHistory(imgId, endpoint, token)
9bb3dc98
 	if err != nil {
04cd20fa
 		return err
 	}
9bb3dc98
 
 	// FIXME: Try to stream the images?
 	// FIXME: Launch the getRemoteImage() in goroutines
 	for _, id := range history {
 		if !srv.runtime.graph.Exists(id) {
5a36efb6
 			out.Write(sf.FormatStatus("Pulling %s metadata", id))
f355d33b
 			imgJSON, imgSize, err := r.GetRemoteImageJSON(id, endpoint, token)
9bb3dc98
 			if err != nil {
 				// FIXME: Keep goging in case of error?
 				return err
 			}
fd224ee5
 			img, err := NewImgJSON(imgJSON)
9bb3dc98
 			if err != nil {
 				return fmt.Errorf("Failed to parse json: %s", err)
 			}
 
 			// Get the layer
5a36efb6
 			out.Write(sf.FormatStatus("Pulling %s fs layer", id))
f355d33b
 			layer, err := r.GetRemoteImageLayer(img.ID, endpoint, token)
9bb3dc98
 			if err != nil {
 				return err
 			}
830c458f
 			defer layer.Close()
f355d33b
 			if err := srv.runtime.graph.Register(utils.ProgressReader(layer, imgSize, out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), false, img); err != nil {
9bb3dc98
 				return err
 			}
 		}
 	}
04cd20fa
 	return nil
 }
 
4e180107
 func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, local, remote, askedTag string, sf *utils.StreamFormatter) error {
 	out.Write(sf.FormatStatus("Pulling repository %s from %s", local, auth.IndexServerAddress()))
cd0de839
 	repoData, err := r.GetRepositoryData(remote)
f37399d2
 	if err != nil {
9bb3dc98
 		return err
 	}
 
 	utils.Debugf("Updating checksums")
 	// Reload the json file to make sure not to overwrite faster sums
95dd6d31
 	if err := srv.runtime.graph.UpdateChecksums(repoData.ImgList); err != nil {
9bb3dc98
 		return err
 	}
 
 	utils.Debugf("Retrieving the tag list")
cd0de839
 	tagsList, err := r.GetRemoteTags(repoData.Endpoints, remote, repoData.Tokens)
9bb3dc98
 	if err != nil {
 		return err
 	}
f498dd2a
 	utils.Debugf("Registering tags")
 	// If not specific tag have been asked, take all
 	if askedTag == "" {
 		for tag, id := range tagsList {
 			repoData.ImgList[id].Tag = tag
 		}
 	} else {
 		// Otherwise, check that the tag exists and use only that one
86ada2fa
 		id, exists := tagsList[askedTag]
 		if !exists {
4e180107
 			return fmt.Errorf("Tag %s not found in repositoy %s", askedTag, local)
f498dd2a
 		}
86ada2fa
 		repoData.ImgList[id].Tag = askedTag
9bb3dc98
 	}
 
 	for _, img := range repoData.ImgList {
f498dd2a
 		if askedTag != "" && img.Tag != askedTag {
fd224ee5
 			utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.ID)
9bb3dc98
 			continue
 		}
fd224ee5
 		out.Write(sf.FormatStatus("Pulling image %s (%s) from %s", img.ID, img.Tag, remote))
9bb3dc98
 		success := false
 		for _, ep := range repoData.Endpoints {
fd224ee5
 			if err := srv.pullImage(r, out, img.ID, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil {
5a36efb6
 				out.Write(sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint", askedTag, err))
9bb3dc98
 				continue
 			}
 			success = true
 			break
f37399d2
 		}
9bb3dc98
 		if !success {
 			return fmt.Errorf("Could not find repository on any of the indexed registries.")
 		}
 	}
 	for tag, id := range tagsList {
49505c59
 		if askedTag != "" && tag != askedTag {
 			continue
 		}
4e180107
 		if err := srv.runtime.repositories.Set(local, tag, id, true); err != nil {
9bb3dc98
 			return err
 		}
 	}
 	if err := srv.runtime.repositories.Save(); err != nil {
f37399d2
 		return err
 	}
9bb3dc98
 
 	return nil
 }
 
fe204e6f
 func (srv *Server) poolAdd(kind, key string) error {
 	srv.lock.Lock()
 	defer srv.lock.Unlock()
 
 	if _, exists := srv.pullingPool[key]; exists {
 		return fmt.Errorf("%s %s is already in progress", key, kind)
 	}
 
 	switch kind {
 	case "pull":
 		srv.pullingPool[key] = struct{}{}
 		break
 	case "push":
 		srv.pushingPool[key] = struct{}{}
 		break
 	default:
 		return fmt.Errorf("Unkown pool type")
 	}
 	return nil
 }
 
 func (srv *Server) poolRemove(kind, key string) error {
 	switch kind {
 	case "pull":
 		delete(srv.pullingPool, key)
 		break
 	case "push":
 		delete(srv.pushingPool, key)
 		break
 	default:
 		return fmt.Errorf("Unkown pool type")
 	}
 	return nil
 }
 
62c78696
 func (srv *Server) ImagePull(name, tag, endpoint string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
fde82f44
 	r, err := registry.NewRegistry(srv.runtime.root, authConfig)
 	if err != nil {
 		return err
 	}
fe204e6f
 	if err := srv.poolAdd("pull", name+":"+tag); err != nil {
 		return err
 	}
 	defer srv.poolRemove("pull", name+":"+tag)
 
cf35e8ed
 	out = utils.NewWriteFlusher(out)
cd0de839
 	if endpoint != "" {
cd002a4d
 		if err := srv.pullImage(r, out, name, endpoint, nil, sf); err != nil {
04cd20fa
 			return err
 		}
10c0e990
 		return nil
04cd20fa
 	}
4e180107
 	remote := name
 	parts := strings.Split(name, "/")
 	if len(parts) > 2 {
d227af1e
 		remote = fmt.Sprintf("src/%s", url.QueryEscape(strings.Join(parts, "/")))
4e180107
 	}
 	if err := srv.pullRepository(r, out, name, remote, tag, sf); err != nil {
f37399d2
 		return err
 	}
9bb3dc98
 	return nil
 }
 
828d1aa5
 // Retrieve the checksum of an image
 // Priority:
 // - Check on the stored checksums
 // - Check if the archive exists, if it does not, ask the registry
 // - If the archive does exists, process the checksum from it
 // - If the archive does not exists and not found on registry, process checksum from layer
 func (srv *Server) getChecksum(imageId string) (string, error) {
 	// FIXME: Use in-memory map instead of reading the file each time
 	if sums, err := srv.runtime.graph.getStoredChecksums(); err != nil {
 		return "", err
 	} else if checksum, exists := sums[imageId]; exists {
 		return checksum, nil
 	}
 
 	img, err := srv.runtime.graph.Get(imageId)
 	if err != nil {
 		return "", err
 	}
 
49b61af1
 	if _, err := os.Stat(layerArchivePath(srv.runtime.graph.imageRoot(imageId))); err != nil {
828d1aa5
 		if os.IsNotExist(err) {
 			// TODO: Ask the registry for the checksum
 			//       As the archive is not there, it is supposed to come from a pull.
 		} else {
 			return "", err
 		}
 	}
 
 	checksum, err := img.Checksum()
 	if err != nil {
 		return "", err
 	}
 	return checksum, nil
 }
 
 // Retrieve the all the images to be uploaded in the correct order
 // Note: we can't use a map as it is not ordered
49b61af1
 func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgData, error) {
 	var imgList []*registry.ImgData
828d1aa5
 
 	imageSet := make(map[string]struct{})
 	for tag, id := range localRepo {
49b61af1
 		img, err := srv.runtime.graph.Get(id)
828d1aa5
 		if err != nil {
49b61af1
 			return nil, err
828d1aa5
 		}
 		img.WalkHistory(func(img *Image) error {
fd224ee5
 			if _, exists := imageSet[img.ID]; exists {
828d1aa5
 				return nil
 			}
fd224ee5
 			imageSet[img.ID] = struct{}{}
 			checksum, err := srv.getChecksum(img.ID)
828d1aa5
 			if err != nil {
 				return err
 			}
49b61af1
 			imgList = append([]*registry.ImgData{{
fd224ee5
 				ID:       img.ID,
828d1aa5
 				Checksum: checksum,
 				Tag:      tag,
 			}}, imgList...)
 			return nil
 		})
 	}
49b61af1
 	return imgList, nil
828d1aa5
 }
 
cd002a4d
 func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, name string, localRepo map[string]string, sf *utils.StreamFormatter) error {
ae9d7a51
 	out = utils.NewWriteFlusher(out)
5a36efb6
 	out.Write(sf.FormatStatus("Processing checksums"))
49b61af1
 	imgList, err := srv.getImageList(localRepo)
828d1aa5
 	if err != nil {
 		return err
 	}
5a36efb6
 	out.Write(sf.FormatStatus("Sending image list"))
828d1aa5
 
4e180107
 	srvName := name
 	parts := strings.Split(name, "/")
 	if len(parts) > 2 {
d227af1e
 		srvName = fmt.Sprintf("src/%s", url.QueryEscape(strings.Join(parts, "/")))
4e180107
 	}
 
78e4a385
 	repoData, err := r.PushImageJSONIndex(srvName, imgList, false, nil)
828d1aa5
 	if err != nil {
 		return err
 	}
 
 	for _, ep := range repoData.Endpoints {
5a36efb6
 		out.Write(sf.FormatStatus("Pushing repository %s to %s (%d tags)", name, ep, len(localRepo)))
828d1aa5
 		// For each image within the repo, push them
 		for _, elem := range imgList {
fd224ee5
 			if _, exists := repoData.ImgList[elem.ID]; exists {
5a36efb6
 				out.Write(sf.FormatStatus("Image %s already on registry, skipping", name))
dc9d6c1c
 				continue
 			}
fd224ee5
 			if err := srv.pushImage(r, out, name, elem.ID, ep, repoData.Tokens, sf); err != nil {
828d1aa5
 				// FIXME: Continue on error?
 				return err
 			}
0312bbc5
 			out.Write(sf.FormatStatus("Pushing tags for rev [%s] on {%s}", elem.ID, ep+"/repositories/"+srvName+"/tags/"+elem.Tag))
4e180107
 			if err := r.PushRegistryTag(srvName, elem.ID, elem.Tag, ep, repoData.Tokens); err != nil {
49b61af1
 				return err
 			}
828d1aa5
 		}
 	}
 
78e4a385
 	if _, err := r.PushImageJSONIndex(srvName, imgList, true, repoData.Endpoints); err != nil {
828d1aa5
 		return err
 	}
49b61af1
 	return nil
828d1aa5
 }
 
cd002a4d
 func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgId, ep string, token []string, sf *utils.StreamFormatter) error {
ae9d7a51
 	out = utils.NewWriteFlusher(out)
828d1aa5
 	jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json"))
 	if err != nil {
 		return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err)
 	}
5a36efb6
 	out.Write(sf.FormatStatus("Pushing %s", imgId))
828d1aa5
 
49b61af1
 	// Make sure we have the image's checksum
828d1aa5
 	checksum, err := srv.getChecksum(imgId)
 	if err != nil {
 		return err
 	}
 	imgData := &registry.ImgData{
fd224ee5
 		ID:       imgId,
828d1aa5
 		Checksum: checksum,
 	}
 
dc9d6c1c
 	// Send the json
fd224ee5
 	if err := r.PushImageJSONRegistry(imgData, jsonRaw, ep, token); err != nil {
dc9d6c1c
 		if err == registry.ErrAlreadyExists {
fd224ee5
 			out.Write(sf.FormatStatus("Image %s already uploaded ; skipping", imgData.ID))
dc9d6c1c
 			return nil
 		}
 		return err
 	}
 
828d1aa5
 	// Retrieve the tarball to be sent
 	var layerData *TempArchive
 	// If the archive exists, use it
 	file, err := os.Open(layerArchivePath(srv.runtime.graph.imageRoot(imgId)))
 	if err != nil {
 		if os.IsNotExist(err) {
 			// If the archive does not exist, create one from the layer
 			layerData, err = srv.runtime.graph.TempLayerArchive(imgId, Xz, out)
 			if err != nil {
 				return fmt.Errorf("Failed to generate layer archive: %s", err)
 			}
 		} else {
 			return err
 		}
 	} else {
 		defer file.Close()
 		st, err := file.Stat()
 		if err != nil {
 			return err
 		}
 		layerData = &TempArchive{
 			File: file,
 			Size: st.Size(),
 		}
 	}
49b61af1
 
 	// Send the layer
b4682e67
 	if err := r.PushImageLayerRegistry(imgData.ID, utils.ProgressReader(layerData, int(layerData.Size), out, sf.FormatProgress("Pushing", "%v/%v (%v)"), sf), ep, token); err != nil {
04cd20fa
 		return err
 	}
 	return nil
 }
 
fe204e6f
 // FIXME: Allow to interupt current push when new push of same image is done.
62c78696
 func (srv *Server) ImagePush(name, endpoint string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
fe204e6f
 	if err := srv.poolAdd("push", name); err != nil {
 		return err
 	}
 	defer srv.poolRemove("push", name)
 
ae9d7a51
 	out = utils.NewWriteFlusher(out)
f37399d2
 	img, err := srv.runtime.graph.Get(name)
fde82f44
 	r, err2 := registry.NewRegistry(srv.runtime.root, authConfig)
 	if err2 != nil {
 		return err2
 	}
f37399d2
 	if err != nil {
5a36efb6
 		out.Write(sf.FormatStatus("The push refers to a repository [%s] (len: %d)", name, len(srv.runtime.repositories.Repositories[name])))
b56b2da5
 		// If it fails, try to get the repository
f37399d2
 		if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
cd002a4d
 			if err := srv.pushRepository(r, out, name, localRepo, sf); err != nil {
f37399d2
 				return err
 			}
 			return nil
 		}
 
 		return err
 	}
5a36efb6
 	out.Write(sf.FormatStatus("The push refers to an image: [%s]", name))
fd224ee5
 	if err := srv.pushImage(r, out, name, img.ID, endpoint, nil, sf); err != nil {
f37399d2
 		return err
 	}
 	return nil
 }
 
c8c7094b
 func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer, sf *utils.StreamFormatter) error {
04cd20fa
 	var archive io.Reader
 	var resp *http.Response
 
 	if src == "-" {
57cfe72e
 		archive = in
04cd20fa
 	} else {
 		u, err := url.Parse(src)
 		if err != nil {
c8c7094b
 			return err
04cd20fa
 		}
 		if u.Scheme == "" {
 			u.Scheme = "http"
 			u.Host = src
 			u.Path = ""
 		}
5a36efb6
 		out.Write(sf.FormatStatus("Downloading from %s", u))
b56b2da5
 		// Download with curl (pretty progress bar)
 		// If curl is not available, fallback to http.Get()
2e69e172
 		resp, err = utils.Download(u.String(), out)
04cd20fa
 		if err != nil {
 			return err
 		}
c8c7094b
 		archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, sf.FormatProgress("Importing", "%v/%v (%v)"), sf)
04cd20fa
 	}
 	img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil)
 	if err != nil {
 		return err
 	}
b56b2da5
 	// Optionally register the image at REPO/TAG
04cd20fa
 	if repo != "" {
fd224ee5
 		if err := srv.runtime.repositories.Set(repo, tag, img.ID, true); err != nil {
04cd20fa
 			return err
 		}
 	}
fd224ee5
 	out.Write(sf.FormatStatus(img.ShortID()))
04cd20fa
 	return nil
 }
 
152ebeea
 func (srv *Server) ContainerCreate(config *Config) (string, error) {
04cd20fa
 
9ee11161
 	if config.Memory != 0 && config.Memory < 524288 {
 		return "", fmt.Errorf("Memory limit must be given in bytes (minimum 524288 bytes)")
 	}
 
04cd20fa
 	if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
 		config.Memory = 0
 	}
 
 	if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
 		config.MemorySwap = -1
 	}
10c0e990
 	b := NewBuilder(srv.runtime)
152ebeea
 	container, err := b.Create(config)
04cd20fa
 	if err != nil {
 		if srv.runtime.graph.IsNotExist(err) {
152ebeea
 			return "", fmt.Errorf("No such image: %s", config.Image)
04cd20fa
 		}
152ebeea
 		return "", err
04cd20fa
 	}
fd224ee5
 	return container.ShortID(), nil
04cd20fa
 }
 
 func (srv *Server) ContainerRestart(name string, t int) error {
 	if container := srv.runtime.Get(name); container != nil {
 		if err := container.Restart(t); err != nil {
 			return fmt.Errorf("Error restarting container %s: %s", name, err.Error())
 		}
 	} else {
 		return fmt.Errorf("No such container: %s", name)
 	}
 	return nil
 }
 
0c6380cc
 func (srv *Server) ContainerDestroy(name string, removeVolume bool) error {
04cd20fa
 	if container := srv.runtime.Get(name); container != nil {
1c841d4f
 		if container.State.Running {
 			return fmt.Errorf("Impossible to remove a running container, please stop it first")
 		}
6f9b574f
 		volumes := make(map[string]struct{})
 		// Store all the deleted containers volumes
 		for _, volumeId := range container.Volumes {
 			volumes[volumeId] = struct{}{}
 		}
04cd20fa
 		if err := srv.runtime.Destroy(container); err != nil {
 			return fmt.Errorf("Error destroying container %s: %s", name, err.Error())
 		}
6f9b574f
 
0c6380cc
 		if removeVolume {
6f9b574f
 			// Retrieve all volumes from all remaining containers
 			usedVolumes := make(map[string]*Container)
 			for _, container := range srv.runtime.List() {
 				for _, containerVolumeId := range container.Volumes {
 					usedVolumes[containerVolumeId] = container
 				}
 			}
 
 			for volumeId := range volumes {
 				// If the requested volu
 				if c, exists := usedVolumes[volumeId]; exists {
fd224ee5
 					log.Printf("The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.ID)
6f9b574f
 					continue
 				}
 				if err := srv.runtime.volumes.Delete(volumeId); err != nil {
 					return err
 				}
 			}
 		}
04cd20fa
 	} else {
 		return fmt.Errorf("No such container: %s", name)
 	}
 	return nil
 }
 
054451fd
 var ErrImageReferenced = errors.New("Image referenced by a repository")
 
66d9a733
 func (srv *Server) deleteImageAndChildren(id string, imgs *[]APIRmi) error {
054451fd
 	// If the image is referenced by a repo, do not delete
66d9a733
 	if len(srv.runtime.repositories.ByID()[id]) != 0 {
054451fd
 		return ErrImageReferenced
 	}
 
 	// If the image is not referenced but has children, go recursive
 	referenced := false
5aa95b66
 	byParents, err := srv.runtime.graph.ByParent()
04cd20fa
 	if err != nil {
5aa95b66
 		return err
86ada2fa
 	}
054451fd
 	for _, img := range byParents[id] {
66d9a733
 		if err := srv.deleteImageAndChildren(img.ID, imgs); err != nil {
054451fd
 			if err != ErrImageReferenced {
 				return err
 			}
66d9a733
 			referenced = true
054451fd
 		}
86ada2fa
 	}
054451fd
 	if referenced {
 		return ErrImageReferenced
 	}
5aa95b66
 
 	// If the image is not referenced and has no children, remove it
 	byParents, err = srv.runtime.graph.ByParent()
 	if err != nil {
 		return err
 	}
 	if len(byParents[id]) == 0 {
 		if err := srv.runtime.repositories.DeleteAll(id); err != nil {
 			return err
 		}
9060b5c2
 		err := srv.runtime.graph.Delete(id)
 		if err != nil {
 			return err
 		}
66d9a733
 		*imgs = append(*imgs, APIRmi{Deleted: utils.TruncateID(id)})
9060b5c2
 		return nil
04cd20fa
 	}
 	return nil
 }
 
66d9a733
 func (srv *Server) deleteImageParents(img *Image, imgs *[]APIRmi) error {
054451fd
 	if img.Parent != "" {
 		parent, err := srv.runtime.graph.Get(img.Parent)
 		if err != nil {
 			return err
 		}
 		// Remove all children images
9060b5c2
 		if err := srv.deleteImageAndChildren(img.Parent, imgs); err != nil {
054451fd
 			return err
 		}
9060b5c2
 		return srv.deleteImageParents(parent, imgs)
04cd20fa
 	}
 	return nil
 }
 
66d9a733
 func (srv *Server) deleteImage(img *Image, repoName, tag string) (*[]APIRmi, error) {
5aa95b66
 	//Untag the current image
66d9a733
 	var imgs []APIRmi
9060b5c2
 	tagDeleted, err := srv.runtime.repositories.Delete(repoName, tag)
 	if err != nil {
 		return nil, err
 	}
 	if tagDeleted {
66d9a733
 		imgs = append(imgs, APIRmi{Untagged: img.ShortID()})
054451fd
 	}
66d9a733
 	if len(srv.runtime.repositories.ByID()[img.ID]) == 0 {
 		if err := srv.deleteImageAndChildren(img.ID, &imgs); err != nil {
5aa95b66
 			if err != ErrImageReferenced {
9060b5c2
 				return &imgs, err
5aa95b66
 			}
9060b5c2
 		} else if err := srv.deleteImageParents(img, &imgs); err != nil {
054451fd
 			if err != ErrImageReferenced {
9060b5c2
 				return &imgs, err
054451fd
 			}
 		}
 	}
9060b5c2
 	return &imgs, nil
054451fd
 }
 
66d9a733
 func (srv *Server) ImageDelete(name string, autoPrune bool) (*[]APIRmi, error) {
5aa95b66
 	img, err := srv.runtime.repositories.LookupImage(name)
 	if err != nil {
9060b5c2
 		return nil, fmt.Errorf("No such image: %s", name)
 	}
 	if !autoPrune {
66d9a733
 		if err := srv.runtime.graph.Delete(img.ID); err != nil {
9060b5c2
 			return nil, fmt.Errorf("Error deleting image %s: %s", name, err.Error())
 		}
 		return nil, nil
5aa95b66
 	}
 
db1e965b
 	var tag string
 	if strings.Contains(name, ":") {
 		nameParts := strings.Split(name, ":")
 		name = nameParts[0]
 		tag = nameParts[1]
 	}
 
5aa95b66
 	return srv.deleteImage(img, name, tag)
04cd20fa
 }
 
0f312113
 func (srv *Server) ImageGetCached(imgId string, config *Config) (*Image, error) {
 
 	// Retrieve all images
 	images, err := srv.runtime.graph.All()
 	if err != nil {
 		return nil, err
 	}
 
 	// Store the tree in a map of map (map[parentId][childId])
 	imageMap := make(map[string]map[string]struct{})
 	for _, img := range images {
 		if _, exists := imageMap[img.Parent]; !exists {
 			imageMap[img.Parent] = make(map[string]struct{})
 		}
fd224ee5
 		imageMap[img.Parent][img.ID] = struct{}{}
0f312113
 	}
 
 	// Loop on the children of the given image and check the config
 	for elem := range imageMap[imgId] {
 		img, err := srv.runtime.graph.Get(elem)
 		if err != nil {
 			return nil, err
 		}
 		if CompareConfig(&img.ContainerConfig, config) {
 			return img, nil
 		}
 	}
 	return nil, nil
 }
 
04cd20fa
 func (srv *Server) ContainerStart(name string) error {
 	if container := srv.runtime.Get(name); container != nil {
 		if err := container.Start(); err != nil {
 			return fmt.Errorf("Error starting container %s: %s", name, err.Error())
 		}
 	} else {
 		return fmt.Errorf("No such container: %s", name)
 	}
 	return nil
 }
 
 func (srv *Server) ContainerStop(name string, t int) error {
 	if container := srv.runtime.Get(name); container != nil {
 		if err := container.Stop(t); err != nil {
 			return fmt.Errorf("Error stopping container %s: %s", name, err.Error())
 		}
 	} else {
 		return fmt.Errorf("No such container: %s", name)
 	}
 	return nil
 }
 
 func (srv *Server) ContainerWait(name string) (int, error) {
 	if container := srv.runtime.Get(name); container != nil {
 		return container.Wait(), nil
 	}
 	return 0, fmt.Errorf("No such container: %s", name)
 }
 
70d2123e
 func (srv *Server) ContainerResize(name string, h, w int) error {
 	if container := srv.runtime.Get(name); container != nil {
 		return container.Resize(h, w)
 	}
 	return fmt.Errorf("No such container: %s", name)
 }
 
57cfe72e
 func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, stderr bool, in io.ReadCloser, out io.Writer) error {
cacc7e56
 	container := srv.runtime.Get(name)
 	if container == nil {
 		return fmt.Errorf("No such container: %s", name)
 	}
 	//logs
 	if logs {
 		if stdout {
 			cLog, err := container.ReadLog("stdout")
 			if err != nil {
2e69e172
 				utils.Debugf(err.Error())
cacc7e56
 			} else if _, err := io.Copy(out, cLog); err != nil {
2e69e172
 				utils.Debugf(err.Error())
04cd20fa
 			}
cacc7e56
 		}
 		if stderr {
 			cLog, err := container.ReadLog("stderr")
 			if err != nil {
2e69e172
 				utils.Debugf(err.Error())
cacc7e56
 			} else if _, err := io.Copy(out, cLog); err != nil {
2e69e172
 				utils.Debugf(err.Error())
04cd20fa
 			}
 		}
cacc7e56
 	}
04cd20fa
 
cacc7e56
 	//stream
 	if stream {
 		if container.State.Ghost {
 			return fmt.Errorf("Impossible to attach to a ghost container")
 		}
04cd20fa
 
cacc7e56
 		var (
 			cStdin           io.ReadCloser
 			cStdout, cStderr io.Writer
 			cStdinCloser     io.Closer
 		)
04cd20fa
 
cacc7e56
 		if stdin {
 			r, w := io.Pipe()
 			go func() {
 				defer w.Close()
2e69e172
 				defer utils.Debugf("Closing buffered stdin pipe")
cacc7e56
 				io.Copy(w, in)
 			}()
 			cStdin = r
 			cStdinCloser = in
 		}
 		if stdout {
 			cStdout = out
 		}
 		if stderr {
 			cStderr = out
 		}
4d30a32c
 
cacc7e56
 		<-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
 
 		// If we are in stdinonce mode, wait for the process to end
 		// otherwise, simply return
 		if container.Config.StdinOnce && !container.Config.Tty {
 			container.Wait()
04cd20fa
 		}
 	}
 	return nil
 }
 
 func (srv *Server) ContainerInspect(name string) (*Container, error) {
 	if container := srv.runtime.Get(name); container != nil {
 		return container, nil
 	}
 	return nil, fmt.Errorf("No such container: %s", name)
 }
 
 func (srv *Server) ImageInspect(name string) (*Image, error) {
 	if image, err := srv.runtime.repositories.LookupImage(name); err == nil && image != nil {
 		return image, nil
 	}
 	return nil, fmt.Errorf("No such image: %s", name)
 }
 
250e47e2
 func NewServer(autoRestart, enableCors bool, dns ListOpts) (*Server, error) {
04cd20fa
 	if runtime.GOARCH != "amd64" {
 		log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
 	}
84d68007
 	runtime, err := NewRuntime(autoRestart, dns)
04cd20fa
 	if err != nil {
 		return nil, err
 	}
 	srv := &Server{
fe204e6f
 		runtime:     runtime,
 		enableCors:  enableCors,
 		lock:        &sync.Mutex{},
 		pullingPool: make(map[string]struct{}),
 		pushingPool: make(map[string]struct{}),
04cd20fa
 	}
95dd6d31
 	runtime.srv = srv
04cd20fa
 	return srv, nil
 }
 
 type Server struct {
fe204e6f
 	runtime     *Runtime
 	enableCors  bool
 	lock        *sync.Mutex
 	pullingPool map[string]struct{}
 	pushingPool map[string]struct{}
04cd20fa
 }