2013-05-06 13:31:22 +04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-05-30 23:30:21 +04:00
|
|
|
"errors"
|
2013-05-06 13:31:22 +04:00
|
|
|
"fmt"
|
2013-05-16 23:09:06 +04:00
|
|
|
"github.com/dotcloud/docker/auth"
|
2013-05-15 05:41:39 +04:00
|
|
|
"github.com/dotcloud/docker/registry"
|
2013-05-15 02:37:35 +04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-05-06 13:31:22 +04:00
|
|
|
"io"
|
2013-05-15 22:30:40 +04:00
|
|
|
"io/ioutil"
|
2013-05-06 13:31:22 +04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2013-05-15 22:30:40 +04:00
|
|
|
"path"
|
2013-05-06 13:31:22 +04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (srv *Server) DockerVersion() ApiVersion {
|
|
|
|
return ApiVersion{VERSION, GIT_COMMIT, srv.runtime.capabilities.MemoryLimit, srv.runtime.capabilities.SwapLimit}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-05-08 05:06:49 +04:00
|
|
|
func (srv *Server) ContainerExport(name string, out io.Writer) error {
|
2013-05-06 13:31:22 +04:00
|
|
|
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)
|
2013-05-08 05:06:49 +04:00
|
|
|
if _, err := io.Copy(out, data); err != nil {
|
2013-05-06 13:31:22 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
2013-05-07 22:37:35 +04:00
|
|
|
func (srv *Server) ImagesSearch(term string) ([]ApiSearch, error) {
|
2013-05-15 22:50:52 +04:00
|
|
|
results, err := srv.registry.SearchRepositories(term)
|
2013-05-07 22:37:35 +04:00
|
|
|
if err != nil {
|
2013-05-07 22:59:04 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-05-07 22:37:35 +04:00
|
|
|
var outs []ApiSearch
|
|
|
|
for _, repo := range results.Results {
|
|
|
|
var out ApiSearch
|
|
|
|
out.Description = repo["description"]
|
2013-05-07 22:59:04 +04:00
|
|
|
if len(out.Description) > 45 {
|
2013-05-15 02:37:35 +04:00
|
|
|
out.Description = utils.Trunc(out.Description, 42) + "..."
|
2013-05-07 22:59:04 +04:00
|
|
|
}
|
2013-05-07 22:37:35 +04:00
|
|
|
out.Name = repo["name"]
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:07:26 +04:00
|
|
|
func (srv *Server) ImageInsert(name, url, path string, out io.Writer) (string, error) {
|
2013-05-20 21:58:35 +04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-05-07 21:23:50 +04:00
|
|
|
img, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
2013-05-23 07:07:26 +04:00
|
|
|
return "", err
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 02:37:35 +04:00
|
|
|
file, err := utils.Download(url, out)
|
2013-05-07 21:23:50 +04:00
|
|
|
if err != nil {
|
2013-05-23 07:07:26 +04:00
|
|
|
return "", err
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
defer file.Body.Close()
|
|
|
|
|
|
|
|
config, _, err := ParseRun([]string{img.Id, "echo", "insert", url, path}, srv.runtime.capabilities)
|
|
|
|
if err != nil {
|
2013-05-23 07:07:26 +04:00
|
|
|
return "", err
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBuilder(srv.runtime)
|
|
|
|
c, err := b.Create(config)
|
|
|
|
if err != nil {
|
2013-05-23 07:07:26 +04:00
|
|
|
return "", err
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
|
2013-05-24 18:43:52 +04:00
|
|
|
if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, "Downloading %v/%v (%v)\r", false), path); err != nil {
|
2013-05-29 00:46:52 +04:00
|
|
|
return "", err
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
// FIXME: Handle custom repo, tag comment, author
|
|
|
|
img, err = b.Commit(c, "", "", img.Comment, img.Author, nil)
|
|
|
|
if err != nil {
|
2013-05-23 07:07:26 +04:00
|
|
|
return "", err
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
2013-05-08 05:06:49 +04:00
|
|
|
fmt.Fprintf(out, "%s\n", img.Id)
|
2013-05-23 07:07:26 +04:00
|
|
|
return img.ShortId(), nil
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 05:06:49 +04:00
|
|
|
func (srv *Server) ImagesViz(out io.Writer) error {
|
2013-05-07 21:23:50 +04:00
|
|
|
images, _ := srv.runtime.graph.All()
|
|
|
|
if images == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-10 01:10:26 +04:00
|
|
|
out.Write([]byte("digraph docker {\n"))
|
2013-05-07 21:23:50 +04:00
|
|
|
|
2013-05-08 05:06:49 +04:00
|
|
|
var (
|
|
|
|
parentImage *Image
|
|
|
|
err error
|
|
|
|
)
|
2013-05-07 21:23:50 +04:00
|
|
|
for _, image := range images {
|
|
|
|
parentImage, err = image.GetParent()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error while getting parent image: %v", err)
|
|
|
|
}
|
|
|
|
if parentImage != nil {
|
2013-05-10 01:10:26 +04:00
|
|
|
out.Write([]byte(" \"" + parentImage.ShortId() + "\" -> \"" + image.ShortId() + "\"\n"))
|
2013-05-07 21:23:50 +04:00
|
|
|
} else {
|
2013-05-10 01:10:26 +04:00
|
|
|
out.Write([]byte(" base -> \"" + image.ShortId() + "\" [style=invis]\n"))
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reporefs := make(map[string][]string)
|
|
|
|
|
|
|
|
for name, repository := range srv.runtime.repositories.Repositories {
|
|
|
|
for tag, id := range repository {
|
2013-05-15 02:37:35 +04:00
|
|
|
reporefs[utils.TruncateId(id)] = append(reporefs[utils.TruncateId(id)], fmt.Sprintf("%s:%s", name, tag))
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, repos := range reporefs {
|
2013-05-10 01:10:26 +04:00
|
|
|
out.Write([]byte(" \"" + id + "\" [label=\"" + id + "\\n" + strings.Join(repos, "\\n") + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n"))
|
2013-05-07 21:23:50 +04:00
|
|
|
}
|
2013-05-10 01:10:26 +04:00
|
|
|
out.Write([]byte(" base [style=invisible]\n}\n"))
|
2013-05-07 21:23:50 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-13 14:18:55 +04:00
|
|
|
func (srv *Server) Images(all bool, filter string) ([]ApiImages, error) {
|
2013-05-19 21:46:24 +04:00
|
|
|
var (
|
|
|
|
allImages map[string]*Image
|
|
|
|
err error
|
|
|
|
)
|
2013-05-08 03:47:43 +04:00
|
|
|
if all {
|
2013-05-06 13:31:22 +04:00
|
|
|
allImages, err = srv.runtime.graph.Map()
|
|
|
|
} else {
|
|
|
|
allImages, err = srv.runtime.graph.Heads()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-19 21:46:24 +04:00
|
|
|
outs := []ApiImages{} //produce [] when empty instead of 'null'
|
2013-05-06 13:31:22 +04:00
|
|
|
for name, repository := range srv.runtime.repositories.Repositories {
|
|
|
|
if filter != "" && name != filter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for tag, id := range repository {
|
|
|
|
var out ApiImages
|
|
|
|
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)
|
2013-05-13 14:18:55 +04:00
|
|
|
out.Repository = name
|
|
|
|
out.Tag = tag
|
2013-05-13 14:26:18 +04:00
|
|
|
out.Id = image.Id
|
2013-05-13 14:18:55 +04:00
|
|
|
out.Created = image.Created.Unix()
|
2013-05-06 13:31:22 +04:00
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Display images which aren't part of a
|
|
|
|
if filter == "" {
|
2013-05-13 14:18:55 +04:00
|
|
|
for _, image := range allImages {
|
2013-05-06 13:31:22 +04:00
|
|
|
var out ApiImages
|
2013-05-13 14:26:18 +04:00
|
|
|
out.Id = image.Id
|
2013-05-13 14:18:55 +04:00
|
|
|
out.Created = image.Created.Unix()
|
2013-05-06 13:31:22 +04:00
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) DockerInfo() ApiInfo {
|
|
|
|
images, _ := srv.runtime.graph.All()
|
|
|
|
var imgcount int
|
|
|
|
if images == nil {
|
|
|
|
imgcount = 0
|
|
|
|
} else {
|
|
|
|
imgcount = len(images)
|
|
|
|
}
|
|
|
|
var out ApiInfo
|
|
|
|
out.Containers = len(srv.runtime.List())
|
|
|
|
out.Version = VERSION
|
|
|
|
out.Images = imgcount
|
2013-05-09 04:20:16 +04:00
|
|
|
out.GoVersion = runtime.Version()
|
2013-05-08 03:47:43 +04:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
2013-05-06 13:31:22 +04:00
|
|
|
out.Debug = true
|
2013-05-15 02:37:35 +04:00
|
|
|
out.NFd = utils.GetTotalUsedFds()
|
2013-05-06 13:31:22 +04:00
|
|
|
out.NGoroutines = runtime.NumGoroutine()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) ImageHistory(name string) ([]ApiHistory, error) {
|
|
|
|
image, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var outs []ApiHistory = []ApiHistory{} //produce [] when empty instead of 'null'
|
|
|
|
err = image.WalkHistory(func(img *Image) error {
|
|
|
|
var out ApiHistory
|
|
|
|
out.Id = srv.runtime.repositories.ImageName(img.ShortId())
|
|
|
|
out.Created = img.Created.Unix()
|
|
|
|
out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
|
|
|
|
outs = append(outs, out)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return outs, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-08 19:35:50 +04:00
|
|
|
func (srv *Server) ContainerChanges(name string) ([]Change, error) {
|
2013-05-06 13:31:22 +04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
2013-05-08 19:35:50 +04:00
|
|
|
return container.Changes()
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
2013-05-13 14:18:55 +04:00
|
|
|
func (srv *Server) Containers(all bool, n int, since, before string) []ApiContainers {
|
2013-05-08 20:28:11 +04:00
|
|
|
var foundBefore bool
|
|
|
|
var displayed int
|
2013-05-10 04:50:56 +04:00
|
|
|
retContainers := []ApiContainers{}
|
|
|
|
|
2013-05-08 20:28:11 +04:00
|
|
|
for _, container := range srv.runtime.List() {
|
|
|
|
if !container.State.Running && !all && n == -1 && since == "" && before == "" {
|
2013-05-06 13:31:22 +04:00
|
|
|
continue
|
|
|
|
}
|
2013-05-08 20:28:11 +04:00
|
|
|
if before != "" {
|
|
|
|
if container.ShortId() == before {
|
|
|
|
foundBefore = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !foundBefore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if displayed == n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if container.ShortId() == since {
|
2013-05-06 13:31:22 +04:00
|
|
|
break
|
|
|
|
}
|
2013-05-10 04:50:56 +04:00
|
|
|
displayed++
|
|
|
|
|
|
|
|
c := ApiContainers{
|
2013-05-10 12:18:01 +04:00
|
|
|
Id: container.Id,
|
|
|
|
}
|
2013-05-13 14:18:55 +04:00
|
|
|
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()
|
2013-05-10 04:50:56 +04:00
|
|
|
retContainers = append(retContainers, c)
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
2013-05-10 04:50:56 +04:00
|
|
|
return retContainers
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
|
2013-05-07 21:23:50 +04:00
|
|
|
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)
|
2013-05-06 13:31:22 +04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return img.ShortId(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-05-24 18:43:52 +04:00
|
|
|
func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []string, json bool) error {
|
2013-05-15 05:41:39 +04:00
|
|
|
history, err := srv.registry.GetRemoteHistory(imgId, registry, token)
|
|
|
|
if err != nil {
|
2013-05-06 13:31:22 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
|
|
|
|
// FIXME: Try to stream the images?
|
|
|
|
// FIXME: Launch the getRemoteImage() in goroutines
|
|
|
|
for _, id := range history {
|
|
|
|
if !srv.runtime.graph.Exists(id) {
|
2013-05-24 18:43:52 +04:00
|
|
|
fmt.Fprintf(out, utils.FormatStatus("Pulling %s metadata", json), id)
|
2013-05-15 22:50:52 +04:00
|
|
|
imgJson, err := srv.registry.GetRemoteImageJson(id, registry, token)
|
2013-05-15 05:41:39 +04:00
|
|
|
if err != nil {
|
|
|
|
// FIXME: Keep goging in case of error?
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
img, err := NewImgJson(imgJson)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse json: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the layer
|
2013-05-24 18:43:52 +04:00
|
|
|
fmt.Fprintf(out, utils.FormatStatus("Pulling %s fs layer", json), id)
|
2013-05-15 22:50:52 +04:00
|
|
|
layer, contentLength, err := srv.registry.GetRemoteImageLayer(img.Id, registry, token)
|
2013-05-15 05:41:39 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-24 18:43:52 +04:00
|
|
|
if err := srv.runtime.graph.Register(utils.ProgressReader(layer, contentLength, out, utils.FormatProgress("%v/%v (%v)", json), json), false, img); err != nil {
|
2013-05-15 05:41:39 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 13:31:22 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-24 18:43:52 +04:00
|
|
|
func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, json bool) error {
|
|
|
|
fmt.Fprintf(out, utils.FormatStatus("Pulling repository %s from %s", json), remote, auth.IndexServerAddress())
|
2013-05-15 05:41:39 +04:00
|
|
|
repoData, err := srv.registry.GetRepositoryData(remote)
|
2013-05-06 15:34:31 +04:00
|
|
|
if err != nil {
|
2013-05-15 05:41:39 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Debugf("Updating checksums")
|
|
|
|
// Reload the json file to make sure not to overwrite faster sums
|
2013-05-16 04:17:33 +04:00
|
|
|
if err := srv.runtime.graph.UpdateChecksums(repoData.ImgList); err != nil {
|
2013-05-15 05:41:39 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Debugf("Retrieving the tag list")
|
2013-05-15 22:50:52 +04:00
|
|
|
tagsList, err := srv.registry.GetRemoteTags(repoData.Endpoints, remote, repoData.Tokens)
|
2013-05-15 05:41:39 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-16 23:29:16 +04:00
|
|
|
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
|
|
|
|
if id, exists := tagsList[askedTag]; !exists {
|
|
|
|
return fmt.Errorf("Tag %s not found in repositoy %s", askedTag, remote)
|
|
|
|
} else {
|
|
|
|
repoData.ImgList[id].Tag = askedTag
|
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range repoData.ImgList {
|
2013-05-16 23:29:16 +04:00
|
|
|
if askedTag != "" && img.Tag != askedTag {
|
2013-05-21 04:30:33 +04:00
|
|
|
utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id)
|
2013-05-15 05:41:39 +04:00
|
|
|
continue
|
|
|
|
}
|
2013-05-24 18:43:52 +04:00
|
|
|
fmt.Fprintf(out, utils.FormatStatus("Pulling image %s (%s) from %s", json), img.Id, img.Tag, remote)
|
2013-05-15 05:41:39 +04:00
|
|
|
success := false
|
|
|
|
for _, ep := range repoData.Endpoints {
|
2013-05-24 18:43:52 +04:00
|
|
|
if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens, json); err != nil {
|
|
|
|
fmt.Fprintf(out, utils.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint\n", json), askedTag, err)
|
2013-05-15 05:41:39 +04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
success = true
|
|
|
|
break
|
2013-05-06 15:34:31 +04:00
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
if !success {
|
|
|
|
return fmt.Errorf("Could not find repository on any of the indexed registries.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for tag, id := range tagsList {
|
2013-05-21 04:30:33 +04:00
|
|
|
if askedTag != "" && tag != askedTag {
|
|
|
|
continue
|
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
if err := srv.runtime.repositories.Set(remote, tag, id, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := srv.runtime.repositories.Save(); err != nil {
|
2013-05-06 15:34:31 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-24 18:43:52 +04:00
|
|
|
func (srv *Server) ImagePull(name, tag, registry string, out io.Writer, json bool) error {
|
2013-05-23 19:16:35 +04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-05-07 21:23:50 +04:00
|
|
|
if registry != "" {
|
2013-05-24 18:43:52 +04:00
|
|
|
if err := srv.pullImage(out, name, registry, nil, json); err != nil {
|
2013-05-06 13:31:22 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-07 21:23:50 +04:00
|
|
|
return nil
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
|
2013-05-24 18:43:52 +04:00
|
|
|
if err := srv.pullRepository(out, name, tag, json); err != nil {
|
2013-05-06 15:34:31 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-15 05:41:39 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-15 07:27:15 +04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:30:40 +04:00
|
|
|
if _, err := os.Stat(layerArchivePath(srv.runtime.graph.imageRoot(imageId))); err != nil {
|
2013-05-15 07:27:15 +04:00
|
|
|
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
|
2013-05-15 22:30:40 +04:00
|
|
|
func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgData, error) {
|
|
|
|
var imgList []*registry.ImgData
|
2013-05-15 07:27:15 +04:00
|
|
|
|
|
|
|
imageSet := make(map[string]struct{})
|
|
|
|
for tag, id := range localRepo {
|
2013-05-15 22:30:40 +04:00
|
|
|
img, err := srv.runtime.graph.Get(id)
|
2013-05-15 07:27:15 +04:00
|
|
|
if err != nil {
|
2013-05-15 22:30:40 +04:00
|
|
|
return nil, err
|
2013-05-15 07:27:15 +04:00
|
|
|
}
|
|
|
|
img.WalkHistory(func(img *Image) error {
|
|
|
|
if _, exists := imageSet[img.Id]; exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
imageSet[img.Id] = struct{}{}
|
2013-05-15 22:30:40 +04:00
|
|
|
checksum, err := srv.getChecksum(img.Id)
|
2013-05-15 07:27:15 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-15 22:30:40 +04:00
|
|
|
imgList = append([]*registry.ImgData{{
|
2013-05-15 07:27:15 +04:00
|
|
|
Id: img.Id,
|
|
|
|
Checksum: checksum,
|
|
|
|
Tag: tag,
|
|
|
|
}}, imgList...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2013-05-15 22:30:40 +04:00
|
|
|
return imgList, nil
|
2013-05-15 07:27:15 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 22:30:40 +04:00
|
|
|
func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[string]string) error {
|
2013-05-20 21:58:35 +04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-05-15 07:27:15 +04:00
|
|
|
fmt.Fprintf(out, "Processing checksums\n")
|
2013-05-15 22:30:40 +04:00
|
|
|
imgList, err := srv.getImageList(localRepo)
|
2013-05-15 07:27:15 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-15 22:30:40 +04:00
|
|
|
fmt.Fprintf(out, "Sending image list\n")
|
2013-05-15 07:27:15 +04:00
|
|
|
|
2013-05-15 22:30:40 +04:00
|
|
|
repoData, err := srv.registry.PushImageJsonIndex(name, imgList, false)
|
2013-05-15 07:27:15 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Send only needed images
|
|
|
|
for _, ep := range repoData.Endpoints {
|
2013-05-15 22:30:40 +04:00
|
|
|
fmt.Fprintf(out, "Pushing repository %s to %s (%d tags)\r\n", name, ep, len(localRepo))
|
2013-05-15 07:27:15 +04:00
|
|
|
// For each image within the repo, push them
|
|
|
|
for _, elem := range imgList {
|
2013-05-16 00:22:57 +04:00
|
|
|
if _, exists := repoData.ImgList[elem.Id]; exists {
|
|
|
|
fmt.Fprintf(out, "Image %s already on registry, skipping\n", name)
|
|
|
|
continue
|
|
|
|
}
|
2013-05-15 22:30:40 +04:00
|
|
|
if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens); err != nil {
|
2013-05-15 07:27:15 +04:00
|
|
|
// FIXME: Continue on error?
|
|
|
|
return err
|
|
|
|
}
|
2013-05-16 00:22:57 +04:00
|
|
|
fmt.Fprintf(out, "Pushing tags for rev [%s] on {%s}\n", elem.Id, ep+"/users/"+name+"/"+elem.Tag)
|
2013-05-15 22:30:40 +04:00
|
|
|
if err := srv.registry.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-15 07:27:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:30:40 +04:00
|
|
|
if _, err := srv.registry.PushImageJsonIndex(name, imgList, true); err != nil {
|
2013-05-15 07:27:15 +04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-15 22:30:40 +04:00
|
|
|
return nil
|
2013-05-15 07:27:15 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 22:30:40 +04:00
|
|
|
func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []string) error {
|
2013-05-20 21:58:35 +04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-05-15 07:27:15 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "Pushing %s\r\n", imgId)
|
|
|
|
|
2013-05-15 22:30:40 +04:00
|
|
|
// Make sure we have the image's checksum
|
2013-05-15 07:27:15 +04:00
|
|
|
checksum, err := srv.getChecksum(imgId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imgData := ®istry.ImgData{
|
|
|
|
Id: imgId,
|
|
|
|
Checksum: checksum,
|
|
|
|
}
|
|
|
|
|
2013-05-16 00:22:57 +04:00
|
|
|
// Send the json
|
|
|
|
if err := srv.registry.PushImageJsonRegistry(imgData, jsonRaw, ep, token); err != nil {
|
|
|
|
if err == registry.ErrAlreadyExists {
|
|
|
|
fmt.Fprintf(out, "Image %s already uploaded ; skipping\n", imgData.Id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-15 07:27:15 +04:00
|
|
|
// 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(),
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 22:30:40 +04:00
|
|
|
|
|
|
|
// Send the layer
|
2013-05-24 18:43:52 +04:00
|
|
|
if err := srv.registry.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, "", false), ep, token); err != nil {
|
2013-05-06 13:31:22 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 05:06:49 +04:00
|
|
|
func (srv *Server) ImagePush(name, registry string, out io.Writer) error {
|
2013-05-20 21:58:35 +04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-05-06 15:34:31 +04:00
|
|
|
img, err := srv.runtime.graph.Get(name)
|
|
|
|
if err != nil {
|
2013-05-15 23:22:08 +04:00
|
|
|
fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name]))
|
2013-05-08 03:33:12 +04:00
|
|
|
// If it fails, try to get the repository
|
2013-05-06 15:34:31 +04:00
|
|
|
if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
|
2013-05-15 07:27:15 +04:00
|
|
|
if err := srv.pushRepository(out, name, localRepo); err != nil {
|
2013-05-06 15:34:31 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2013-05-15 23:22:08 +04:00
|
|
|
fmt.Fprintf(out, "The push refers to an image: [%s]\n", name)
|
2013-05-15 22:30:40 +04:00
|
|
|
if err := srv.pushImage(out, name, img.Id, registry, nil); err != nil {
|
2013-05-06 15:34:31 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 05:06:49 +04:00
|
|
|
func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer) error {
|
2013-05-06 13:31:22 +04:00
|
|
|
var archive io.Reader
|
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
if src == "-" {
|
2013-05-08 05:06:49 +04:00
|
|
|
archive = in
|
2013-05-06 13:31:22 +04:00
|
|
|
} else {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
2013-05-08 05:06:49 +04:00
|
|
|
fmt.Fprintf(out, "Error: %s\n", err)
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
2013-05-20 21:22:50 +04:00
|
|
|
fmt.Fprintf(out, "Downloading from %s\n", u)
|
2013-05-08 03:33:12 +04:00
|
|
|
// Download with curl (pretty progress bar)
|
|
|
|
// If curl is not available, fallback to http.Get()
|
2013-05-15 02:37:35 +04:00
|
|
|
resp, err = utils.Download(u.String(), out)
|
2013-05-06 13:31:22 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-24 18:43:52 +04:00
|
|
|
archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, "Importing %v/%v (%v)\r", false)
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-08 03:33:12 +04:00
|
|
|
// Optionally register the image at REPO/TAG
|
2013-05-06 13:31:22 +04:00
|
|
|
if repo != "" {
|
|
|
|
if err := srv.runtime.repositories.Set(repo, tag, img.Id, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-05-20 21:22:50 +04:00
|
|
|
fmt.Fprintf(out, "%s\n", img.ShortId())
|
2013-05-06 13:31:22 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-10 04:50:56 +04:00
|
|
|
func (srv *Server) ContainerCreate(config *Config) (string, error) {
|
2013-05-06 13:31:22 +04:00
|
|
|
|
|
|
|
if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
|
|
|
|
config.Memory = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
|
|
|
|
config.MemorySwap = -1
|
|
|
|
}
|
2013-05-07 21:23:50 +04:00
|
|
|
b := NewBuilder(srv.runtime)
|
2013-05-10 04:50:56 +04:00
|
|
|
container, err := b.Create(config)
|
2013-05-06 13:31:22 +04:00
|
|
|
if err != nil {
|
|
|
|
if srv.runtime.graph.IsNotExist(err) {
|
2013-05-10 04:50:56 +04:00
|
|
|
return "", fmt.Errorf("No such image: %s", config.Image)
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
2013-05-10 04:50:56 +04:00
|
|
|
return "", err
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
2013-05-10 04:50:56 +04:00
|
|
|
return container.ShortId(), nil
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-05-10 06:19:55 +04:00
|
|
|
func (srv *Server) ContainerDestroy(name string, removeVolume bool) error {
|
2013-05-06 13:31:22 +04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
2013-05-06 13:52:15 +04:00
|
|
|
volumes := make(map[string]struct{})
|
|
|
|
// Store all the deleted containers volumes
|
|
|
|
for _, volumeId := range container.Volumes {
|
|
|
|
volumes[volumeId] = struct{}{}
|
|
|
|
}
|
2013-05-06 13:31:22 +04:00
|
|
|
if err := srv.runtime.Destroy(container); err != nil {
|
|
|
|
return fmt.Errorf("Error destroying container %s: %s", name, err.Error())
|
|
|
|
}
|
2013-05-06 13:52:15 +04:00
|
|
|
|
2013-05-10 06:19:55 +04:00
|
|
|
if removeVolume {
|
2013-05-06 13:52:15 +04:00
|
|
|
// 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 {
|
|
|
|
log.Printf("The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.Id)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := srv.runtime.volumes.Delete(volumeId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 13:31:22 +04:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-30 23:30:21 +04:00
|
|
|
func (srv *Server) pruneImage(img *Image, repo, tag string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrImageReferenced = errors.New("Image referenced by a repository")
|
|
|
|
|
|
|
|
func (srv *Server) deleteImageChildren(id string, byId map[string][]string, byParents map[string][]*Image) error {
|
|
|
|
// If the image is referenced by a repo, do not delete
|
|
|
|
if len(byId[id]) != 0 {
|
|
|
|
return ErrImageReferenced
|
|
|
|
}
|
|
|
|
// If the image is not referenced and has no children, remove it
|
|
|
|
if len(byParents[id]) == 0 {
|
|
|
|
return srv.runtime.graph.Delete(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the image is not referenced but has children, go recursive
|
|
|
|
referenced := false
|
|
|
|
for _, img := range byParents[id] {
|
|
|
|
if err := srv.deleteImageChildren(img.Id, byId, byParents); err != nil {
|
|
|
|
if err != ErrImageReferenced {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
referenced = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if referenced {
|
|
|
|
return ErrImageReferenced
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) deleteImageParents(img *Image, byId map[string][]string, byParents map[string][]*Image) error {
|
|
|
|
if img.Parent != "" {
|
|
|
|
parent, err := srv.runtime.graph.Get(img.Parent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove all children images
|
|
|
|
if err := srv.deleteImageChildren(img.Id, byId, byParents); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// If no error (no referenced children), then remove the parent
|
|
|
|
if err := srv.runtime.graph.Delete(img.Parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return srv.deleteImageParents(parent, byId, byParents)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) deleteImage(repoName, tag string) error {
|
|
|
|
img, err := srv.runtime.repositories.LookupImage(repoName + ":" + tag)
|
2013-05-06 13:31:22 +04:00
|
|
|
if err != nil {
|
2013-05-30 23:30:21 +04:00
|
|
|
return fmt.Errorf("No such image: %s:%s", repoName, tag)
|
2013-05-16 22:27:50 +04:00
|
|
|
}
|
2013-05-30 23:30:21 +04:00
|
|
|
utils.Debugf("Image %s referenced %d times", img.Id, len(srv.runtime.repositories.ById()[img.Id]))
|
|
|
|
if err := srv.runtime.repositories.Delete(repoName, tag, img.Id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
byId := srv.runtime.repositories.ById()
|
|
|
|
if len(byId[img.Id]) == 0 {
|
|
|
|
byParents, err := srv.runtime.graph.ByParent()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := srv.deleteImageChildren(img.Id, byId, byParents); err != nil {
|
|
|
|
if err != ErrImageReferenced {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
srv.deleteImageParents(img)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) ImageDelete(name string) error {
|
2013-05-16 22:27:50 +04:00
|
|
|
var tag string
|
|
|
|
if strings.Contains(name, ":") {
|
|
|
|
nameParts := strings.Split(name, ":")
|
|
|
|
name = nameParts[0]
|
|
|
|
tag = nameParts[1]
|
|
|
|
}
|
|
|
|
|
2013-05-30 23:30:21 +04:00
|
|
|
srv.deleteImage(name, tag)
|
|
|
|
|
2013-05-16 22:27:50 +04:00
|
|
|
// if the images is referenced several times
|
2013-05-30 23:30:21 +04:00
|
|
|
|
2013-05-18 18:29:32 +04:00
|
|
|
// check is the image to delete isn't parent of another image
|
2013-05-29 21:01:54 +04:00
|
|
|
byParent, _ := srv.runtime.graph.ByParent()
|
|
|
|
if childs, exists := byParent[img.Id]; exists {
|
|
|
|
if strings.Contains(img.Id, name) {
|
|
|
|
return fmt.Errorf("Conflict with %s, %s was not removed", childs[0].ShortId(), name)
|
2013-05-18 18:29:32 +04:00
|
|
|
}
|
2013-05-29 21:01:54 +04:00
|
|
|
if err := srv.runtime.repositories.Delete(name, tag, img.Id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-05-18 18:29:32 +04:00
|
|
|
}
|
2013-05-29 21:27:32 +04:00
|
|
|
parents, _ := img.History()
|
|
|
|
for _, parent := range parents {
|
|
|
|
byParent, _ = srv.runtime.graph.ByParent()
|
|
|
|
//stop if image has children
|
|
|
|
if _, exists := byParent[parent.Id]; exists {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
//stop if image is tagged and it is not the first image we delete
|
|
|
|
if _, hasTags := srv.runtime.repositories.ById()[parent.Id]; hasTags && img.Id != parent.Id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := srv.runtime.graph.Delete(parent.Id); err != nil {
|
|
|
|
return fmt.Errorf("Error deleting image %s: %s", name, err.Error())
|
|
|
|
}
|
|
|
|
if err := srv.runtime.repositories.Delete(name, tag, img.Id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-19 21:46:24 +04:00
|
|
|
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{})
|
|
|
|
}
|
|
|
|
imageMap[img.Parent][img.Id] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:31:22 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-05-24 06:33:28 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-05-08 05:06:49 +04:00
|
|
|
func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, stderr bool, in io.ReadCloser, out io.Writer) error {
|
2013-05-08 10:32:17 +04:00
|
|
|
container := srv.runtime.Get(name)
|
|
|
|
if container == nil {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
2013-05-10 08:53:56 +04:00
|
|
|
|
2013-05-08 10:32:17 +04:00
|
|
|
//logs
|
|
|
|
if logs {
|
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
2013-05-15 02:37:35 +04:00
|
|
|
utils.Debugf(err.Error())
|
2013-05-08 10:32:17 +04:00
|
|
|
} else if _, err := io.Copy(out, cLog); err != nil {
|
2013-05-15 02:37:35 +04:00
|
|
|
utils.Debugf(err.Error())
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
2013-05-08 10:32:17 +04:00
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
2013-05-15 02:37:35 +04:00
|
|
|
utils.Debugf(err.Error())
|
2013-05-08 10:32:17 +04:00
|
|
|
} else if _, err := io.Copy(out, cLog); err != nil {
|
2013-05-15 02:37:35 +04:00
|
|
|
utils.Debugf(err.Error())
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-08 10:32:17 +04:00
|
|
|
}
|
2013-05-06 13:31:22 +04:00
|
|
|
|
2013-05-08 10:32:17 +04:00
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
if container.State.Ghost {
|
|
|
|
return fmt.Errorf("Impossible to attach to a ghost container")
|
|
|
|
}
|
2013-05-06 13:31:22 +04:00
|
|
|
|
2013-05-08 10:32:17 +04:00
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
cStdinCloser io.Closer
|
|
|
|
)
|
2013-05-06 13:31:22 +04:00
|
|
|
|
2013-05-08 10:32:17 +04:00
|
|
|
if stdin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2013-05-15 02:37:35 +04:00
|
|
|
defer utils.Debugf("Closing buffered stdin pipe")
|
2013-05-08 10:32:17 +04:00
|
|
|
io.Copy(w, in)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
cStdinCloser = in
|
|
|
|
}
|
|
|
|
if stdout {
|
|
|
|
cStdout = out
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cStderr = out
|
|
|
|
}
|
2013-05-08 01:15:42 +04:00
|
|
|
|
2013-05-08 10:32:17 +04:00
|
|
|
<-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()
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(autoRestart bool) (*Server, error) {
|
|
|
|
if runtime.GOARCH != "amd64" {
|
|
|
|
log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
|
|
|
|
}
|
|
|
|
runtime, err := NewRuntime(autoRestart)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv := &Server{
|
2013-05-15 05:41:39 +04:00
|
|
|
runtime: runtime,
|
2013-05-16 04:17:33 +04:00
|
|
|
registry: registry.NewRegistry(runtime.root),
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|
2013-05-16 04:17:33 +04:00
|
|
|
runtime.srv = srv
|
2013-05-06 13:31:22 +04:00
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
2013-05-15 05:41:39 +04:00
|
|
|
runtime *Runtime
|
|
|
|
registry *registry.Registry
|
2013-05-06 13:31:22 +04:00
|
|
|
}
|