2013-05-06 05:31:22 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-06-28 11:51:58 -04:00
|
|
|
"bufio"
|
2013-07-15 12:17:58 -04:00
|
|
|
"encoding/json"
|
2013-05-30 15:30:21 -04:00
|
|
|
"errors"
|
2013-05-06 05:31:22 -04:00
|
|
|
"fmt"
|
2013-05-16 15:09:06 -04:00
|
|
|
"github.com/dotcloud/docker/auth"
|
2013-05-14 21:41:39 -04:00
|
|
|
"github.com/dotcloud/docker/registry"
|
2013-05-14 18:37:35 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-05-06 05:31:22 -04:00
|
|
|
"io"
|
2013-05-15 14:30:40 -04:00
|
|
|
"io/ioutil"
|
2013-05-06 05:31:22 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2013-06-28 11:51:58 -04:00
|
|
|
"os/exec"
|
2013-05-15 14:30:40 -04:00
|
|
|
"path"
|
2013-05-06 05:31:22 -04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2013-06-17 19:10:00 -04:00
|
|
|
"sync"
|
2013-07-12 11:09:20 -04:00
|
|
|
"time"
|
2013-05-06 05:31:22 -04:00
|
|
|
)
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func (srv *Server) DockerVersion() APIVersion {
|
|
|
|
return APIVersion{
|
2013-05-31 18:53:57 -04:00
|
|
|
Version: VERSION,
|
2013-06-04 14:00:22 -04:00
|
|
|
GitCommit: GITCOMMIT,
|
2013-05-31 18:53:57 -04:00
|
|
|
GoVersion: runtime.Version(),
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
|
2013-07-23 17:05:13 -04:00
|
|
|
// simpleVersionInfo is a simple implementation of
|
|
|
|
// the interface VersionInfo, which is used
|
2013-07-18 14:22:49 -04:00
|
|
|
// to provide version information for some product,
|
|
|
|
// component, etc. It stores the product name and the version
|
|
|
|
// in string and returns them on calls to Name() and Version().
|
2013-07-23 17:05:13 -04:00
|
|
|
type simpleVersionInfo struct {
|
2013-06-28 17:42:04 -04:00
|
|
|
name string
|
|
|
|
version string
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:05:13 -04:00
|
|
|
func (v *simpleVersionInfo) Name() string {
|
2013-06-28 17:42:04 -04:00
|
|
|
return v.name
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:05:13 -04:00
|
|
|
func (v *simpleVersionInfo) Version() string {
|
2013-06-28 17:42:04 -04:00
|
|
|
return v.version
|
|
|
|
}
|
|
|
|
|
2013-07-18 14:22:49 -04:00
|
|
|
// versionCheckers() returns version informations of:
|
|
|
|
// docker, go, git-commit (of the docker) and the host's kernel.
|
|
|
|
//
|
|
|
|
// Such information will be used on call to NewRegistry().
|
2013-07-23 17:17:31 -04:00
|
|
|
func (srv *Server) versionInfos() []registry.VersionInfo {
|
2013-06-28 17:42:04 -04:00
|
|
|
v := srv.DockerVersion()
|
2013-07-23 17:05:13 -04:00
|
|
|
ret := make([]registry.VersionInfo, 0, 4)
|
|
|
|
ret = append(ret, &simpleVersionInfo{"docker", v.Version})
|
2013-06-28 17:42:04 -04:00
|
|
|
|
|
|
|
if len(v.GoVersion) > 0 {
|
2013-07-23 17:05:13 -04:00
|
|
|
ret = append(ret, &simpleVersionInfo{"go", v.GoVersion})
|
2013-06-28 17:42:04 -04:00
|
|
|
}
|
|
|
|
if len(v.GitCommit) > 0 {
|
2013-07-23 17:05:13 -04:00
|
|
|
ret = append(ret, &simpleVersionInfo{"git-commit", v.GitCommit})
|
2013-06-28 17:42:04 -04:00
|
|
|
}
|
2013-06-28 17:48:37 -04:00
|
|
|
kernelVersion, err := utils.GetKernelVersion()
|
|
|
|
if err == nil {
|
2013-07-23 17:05:13 -04:00
|
|
|
ret = append(ret, &simpleVersionInfo{"kernel", kernelVersion.String()})
|
2013-06-28 17:48:37 -04:00
|
|
|
}
|
|
|
|
|
2013-06-28 17:42:04 -04:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2013-05-06 05:31:22 -04:00
|
|
|
func (srv *Server) ContainerKill(name string) error {
|
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
|
|
|
if err := container.Kill(); err != nil {
|
2013-07-10 08:55:05 -04:00
|
|
|
return fmt.Errorf("Error killing container %s: %s", name, err)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("kill", name)
|
2013-05-06 05:31:22 -04:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
func (srv *Server) ContainerExport(name string, out io.Writer) error {
|
2013-05-06 05: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-07 21:06:49 -04:00
|
|
|
if _, err := io.Copy(out, data); err != nil {
|
2013-05-06 05:31:22 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("export", name)
|
2013-05-06 05:31:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func (srv *Server) ImagesSearch(term string) ([]APISearch, error) {
|
2013-07-23 17:17:31 -04:00
|
|
|
r, err := registry.NewRegistry(srv.runtime.root, nil, srv.versionInfos()...)
|
2013-06-17 14:13:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
results, err := r.SearchRepositories(term)
|
2013-05-07 14:37:35 -04:00
|
|
|
if err != nil {
|
2013-05-07 14:59:04 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
var outs []APISearch
|
2013-05-07 14:37:35 -04:00
|
|
|
for _, repo := range results.Results {
|
2013-06-04 14:00:22 -04:00
|
|
|
var out APISearch
|
2013-05-07 14:37:35 -04:00
|
|
|
out.Description = repo["description"]
|
|
|
|
out.Name = repo["name"]
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:52:18 -04:00
|
|
|
func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {
|
2013-05-20 13:58:35 -04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-05-07 13:23:50 -04:00
|
|
|
img, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
2013-05-22 23:07:26 -04:00
|
|
|
return "", err
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
|
2013-05-14 18:37:35 -04:00
|
|
|
file, err := utils.Download(url, out)
|
2013-05-07 13:23:50 -04:00
|
|
|
if err != nil {
|
2013-05-22 23:07:26 -04:00
|
|
|
return "", err
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
defer file.Body.Close()
|
|
|
|
|
2013-05-13 19:39:54 -04:00
|
|
|
config, _, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.capabilities)
|
2013-05-07 13:23:50 -04:00
|
|
|
if err != nil {
|
2013-05-22 23:07:26 -04:00
|
|
|
return "", err
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBuilder(srv.runtime)
|
|
|
|
c, err := b.Create(config)
|
|
|
|
if err != nil {
|
2013-05-22 23:07:26 -04:00
|
|
|
return "", err
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
|
2013-07-24 13:10:59 -04:00
|
|
|
if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("", "Downloading", "%8v/%v (%v)"), sf), path); err != nil {
|
2013-05-28 16:46:52 -04:00
|
|
|
return "", err
|
2013-05-07 13: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-22 23:07:26 -04:00
|
|
|
return "", err
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", img.ID))
|
2013-06-04 14:00:22 -04:00
|
|
|
return img.ShortID(), nil
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
func (srv *Server) ImagesViz(out io.Writer) error {
|
2013-05-07 13:23:50 -04:00
|
|
|
images, _ := srv.runtime.graph.All()
|
|
|
|
if images == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-09 17:10:26 -04:00
|
|
|
out.Write([]byte("digraph docker {\n"))
|
2013-05-07 13:23:50 -04:00
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
var (
|
|
|
|
parentImage *Image
|
|
|
|
err error
|
|
|
|
)
|
2013-05-07 13: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-06-04 14:00:22 -04:00
|
|
|
out.Write([]byte(" \"" + parentImage.ShortID() + "\" -> \"" + image.ShortID() + "\"\n"))
|
2013-05-07 13:23:50 -04:00
|
|
|
} else {
|
2013-06-04 14:00:22 -04:00
|
|
|
out.Write([]byte(" base -> \"" + image.ShortID() + "\" [style=invis]\n"))
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reporefs := make(map[string][]string)
|
|
|
|
|
|
|
|
for name, repository := range srv.runtime.repositories.Repositories {
|
|
|
|
for tag, id := range repository {
|
2013-06-04 14:00:22 -04:00
|
|
|
reporefs[utils.TruncateID(id)] = append(reporefs[utils.TruncateID(id)], fmt.Sprintf("%s:%s", name, tag))
|
2013-05-07 13:23:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, repos := range reporefs {
|
2013-05-09 17: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 13:23:50 -04:00
|
|
|
}
|
2013-05-09 17:10:26 -04:00
|
|
|
out.Write([]byte(" base [style=invisible]\n}\n"))
|
2013-05-07 13:23:50 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func (srv *Server) Images(all bool, filter string) ([]APIImages, error) {
|
2013-05-19 13:46:24 -04:00
|
|
|
var (
|
|
|
|
allImages map[string]*Image
|
|
|
|
err error
|
|
|
|
)
|
2013-05-07 19:47:43 -04:00
|
|
|
if all {
|
2013-05-06 05:31:22 -04:00
|
|
|
allImages, err = srv.runtime.graph.Map()
|
|
|
|
} else {
|
|
|
|
allImages, err = srv.runtime.graph.Heads()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
outs := []APIImages{} //produce [] when empty instead of 'null'
|
2013-05-06 05:31:22 -04:00
|
|
|
for name, repository := range srv.runtime.repositories.Repositories {
|
|
|
|
if filter != "" && name != filter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for tag, id := range repository {
|
2013-06-04 14:00:22 -04:00
|
|
|
var out APIImages
|
2013-05-06 05:31:22 -04:00
|
|
|
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 06:18:55 -04:00
|
|
|
out.Repository = name
|
|
|
|
out.Tag = tag
|
2013-06-04 14:00:22 -04:00
|
|
|
out.ID = image.ID
|
2013-05-13 06:18:55 -04:00
|
|
|
out.Created = image.Created.Unix()
|
2013-05-22 09:41:29 -04:00
|
|
|
out.Size = image.Size
|
2013-06-14 06:05:01 -04:00
|
|
|
out.VirtualSize = image.getParentsSize(0) + image.Size
|
2013-05-06 05:31:22 -04:00
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Display images which aren't part of a
|
|
|
|
if filter == "" {
|
2013-05-13 06:18:55 -04:00
|
|
|
for _, image := range allImages {
|
2013-06-04 14:00:22 -04:00
|
|
|
var out APIImages
|
|
|
|
out.ID = image.ID
|
2013-05-13 06:18:55 -04:00
|
|
|
out.Created = image.Created.Unix()
|
2013-05-22 09:41:29 -04:00
|
|
|
out.Size = image.Size
|
2013-06-14 06:05:01 -04:00
|
|
|
out.VirtualSize = image.getParentsSize(0) + image.Size
|
2013-05-06 05:31:22 -04:00
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func (srv *Server) DockerInfo() *APIInfo {
|
2013-05-06 05:31:22 -04:00
|
|
|
images, _ := srv.runtime.graph.All()
|
|
|
|
var imgcount int
|
|
|
|
if images == nil {
|
|
|
|
imgcount = 0
|
|
|
|
} else {
|
|
|
|
imgcount = len(images)
|
|
|
|
}
|
2013-07-19 12:36:23 -04:00
|
|
|
lxcVersion := ""
|
|
|
|
if output, err := exec.Command("lxc-version").CombinedOutput(); err == nil {
|
|
|
|
outputStr := string(output)
|
|
|
|
if len(strings.SplitN(outputStr, ":", 2)) == 2 {
|
|
|
|
lxcVersion = strings.TrimSpace(strings.SplitN(string(output), ":", 2)[1])
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 09:35:38 -04:00
|
|
|
kernelVersion := "<unknown>"
|
|
|
|
if kv, err := utils.GetKernelVersion(); err == nil {
|
|
|
|
kernelVersion = kv.String()
|
|
|
|
}
|
2013-07-19 12:36:23 -04:00
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
return &APIInfo{
|
2013-07-10 08:55:05 -04:00
|
|
|
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(),
|
2013-07-24 09:28:01 -04:00
|
|
|
LXCVersion: lxcVersion,
|
2013-07-10 08:55:05 -04:00
|
|
|
NEventsListener: len(srv.events),
|
2013-07-24 09:35:38 -04:00
|
|
|
KernelVersion: kernelVersion,
|
2013-05-31 18:53:57 -04:00
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func (srv *Server) ImageHistory(name string) ([]APIHistory, error) {
|
2013-05-06 05:31:22 -04:00
|
|
|
image, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:31:07 -04:00
|
|
|
lookupMap := make(map[string][]string)
|
2013-06-17 21:39:30 -04:00
|
|
|
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"
|
2013-06-18 13:31:07 -04:00
|
|
|
if _, exists := lookupMap[id]; !exists {
|
|
|
|
lookupMap[id] = []string{}
|
2013-06-17 21:39:30 -04:00
|
|
|
}
|
2013-06-18 13:31:07 -04:00
|
|
|
lookupMap[id] = append(lookupMap[id], name+":"+tag)
|
2013-06-17 21:39:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
outs := []APIHistory{} //produce [] when empty instead of 'null'
|
2013-05-06 05:31:22 -04:00
|
|
|
err = image.WalkHistory(func(img *Image) error {
|
2013-06-04 14:00:22 -04:00
|
|
|
var out APIHistory
|
|
|
|
out.ID = srv.runtime.repositories.ImageName(img.ShortID())
|
2013-05-06 05:31:22 -04:00
|
|
|
out.Created = img.Created.Unix()
|
|
|
|
out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
|
2013-06-18 13:31:07 -04:00
|
|
|
out.Tags = lookupMap[img.ID]
|
2013-05-06 05:31:22 -04:00
|
|
|
outs = append(outs, out)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return outs, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-19 06:06:32 -04:00
|
|
|
func (srv *Server) ContainerTop(name, ps_args string) (*APITop, error) {
|
2013-06-28 11:51:58 -04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
2013-07-19 06:06:32 -04:00
|
|
|
output, err := exec.Command("lxc-ps", "--name", container.ID, "--", ps_args).CombinedOutput()
|
2013-06-28 11:51:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error trying to use lxc-ps: %s (%s)", err, output)
|
|
|
|
}
|
2013-07-19 06:06:32 -04:00
|
|
|
procs := APITop{}
|
2013-06-28 11:51:58 -04:00
|
|
|
for i, line := range strings.Split(string(output), "\n") {
|
2013-07-19 06:06:32 -04:00
|
|
|
if len(line) == 0 {
|
2013-06-28 11:51:58 -04:00
|
|
|
continue
|
|
|
|
}
|
2013-07-19 06:06:32 -04:00
|
|
|
words := []string{}
|
2013-06-28 11:51:58 -04:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(line))
|
|
|
|
scanner.Split(bufio.ScanWords)
|
|
|
|
if !scanner.Scan() {
|
|
|
|
return nil, fmt.Errorf("Error trying to use lxc-ps")
|
|
|
|
}
|
|
|
|
// no scanner.Text because we skip container id
|
2013-07-19 06:06:32 -04:00
|
|
|
for scanner.Scan() {
|
|
|
|
words = append(words, scanner.Text())
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
procs.Titles = words
|
|
|
|
} else {
|
|
|
|
procs.Processes = append(procs.Processes, words)
|
|
|
|
}
|
2013-06-28 11:51:58 -04:00
|
|
|
}
|
2013-07-19 06:06:32 -04:00
|
|
|
return &procs, nil
|
2013-06-28 11:51:58 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
2013-05-08 11:35:50 -04:00
|
|
|
func (srv *Server) ContainerChanges(name string) ([]Change, error) {
|
2013-05-06 05:31:22 -04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
return container.Changes()
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
2013-06-20 10:19:50 -04:00
|
|
|
func (srv *Server) Containers(all, size bool, n int, since, before string) []APIContainers {
|
2013-05-08 12:28:11 -04:00
|
|
|
var foundBefore bool
|
|
|
|
var displayed int
|
2013-06-04 14:00:22 -04:00
|
|
|
retContainers := []APIContainers{}
|
2013-05-09 20:50:56 -04:00
|
|
|
|
2013-05-08 12:28:11 -04:00
|
|
|
for _, container := range srv.runtime.List() {
|
|
|
|
if !container.State.Running && !all && n == -1 && since == "" && before == "" {
|
2013-05-06 05:31:22 -04:00
|
|
|
continue
|
|
|
|
}
|
2013-05-08 12:28:11 -04:00
|
|
|
if before != "" {
|
2013-06-04 14:00:22 -04:00
|
|
|
if container.ShortID() == before {
|
2013-05-08 12:28:11 -04:00
|
|
|
foundBefore = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !foundBefore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if displayed == n {
|
|
|
|
break
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
if container.ShortID() == since {
|
2013-05-06 05:31:22 -04:00
|
|
|
break
|
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
displayed++
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
c := APIContainers{
|
|
|
|
ID: container.ID,
|
2013-05-10 04:18:01 -04:00
|
|
|
}
|
2013-05-13 06: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-06-20 10:19:50 -04:00
|
|
|
if size {
|
|
|
|
c.SizeRw, c.SizeRootFs = container.GetSize()
|
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
retContainers = append(retContainers, c)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
return retContainers
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
|
2013-05-07 13: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 05:31:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
return img.ShortID(), err
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-02 18:27:22 -04:00
|
|
|
func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgID, endpoint string, token []string, sf *utils.StreamFormatter) error {
|
|
|
|
history, err := r.GetRemoteHistory(imgID, endpoint, token)
|
2013-05-14 21:41:39 -04:00
|
|
|
if err != nil {
|
2013-05-06 05:31:22 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-14 21: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-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus(utils.TruncateID(id), "Pulling metadata"))
|
2013-06-06 21:16:16 -04:00
|
|
|
imgJSON, imgSize, err := r.GetRemoteImageJSON(id, endpoint, token)
|
2013-05-14 21:41:39 -04:00
|
|
|
if err != nil {
|
|
|
|
// FIXME: Keep goging in case of error?
|
|
|
|
return err
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
img, err := NewImgJSON(imgJSON)
|
2013-05-14 21:41:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse json: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the layer
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus(utils.TruncateID(id), "Pulling fs layer"))
|
2013-06-06 21:16:16 -04:00
|
|
|
layer, err := r.GetRemoteImageLayer(img.ID, endpoint, token)
|
2013-05-14 21:41:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-03 15:14:57 -04:00
|
|
|
defer layer.Close()
|
2013-07-24 13:10:59 -04:00
|
|
|
if err := srv.runtime.graph.Register(utils.ProgressReader(layer, imgSize, out, sf.FormatProgress(utils.TruncateID(id), "Downloading", "%8v/%v (%v)"), sf), false, img); err != nil {
|
2013-05-14 21:41:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:09:07 -04:00
|
|
|
func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, localName, remoteName, askedTag, indexEp string, sf *utils.StreamFormatter, parallel bool) error {
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Pulling repository %s", localName))
|
2013-05-14 21:41:39 -04:00
|
|
|
|
2013-07-08 20:26:50 -04:00
|
|
|
repoData, err := r.GetRepositoryData(indexEp, remoteName)
|
2013-05-06 07:34:31 -04:00
|
|
|
if err != nil {
|
2013-05-14 21:41:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Debugf("Updating checksums")
|
|
|
|
// Reload the json file to make sure not to overwrite faster sums
|
2013-05-15 20:17:33 -04:00
|
|
|
if err := srv.runtime.graph.UpdateChecksums(repoData.ImgList); err != nil {
|
2013-05-14 21:41:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Debugf("Retrieving the tag list")
|
2013-07-08 20:26:50 -04:00
|
|
|
tagsList, err := r.GetRemoteTags(repoData.Endpoints, remoteName, repoData.Tokens)
|
2013-05-14 21:41:39 -04:00
|
|
|
if err != nil {
|
2013-06-25 15:03:42 -04:00
|
|
|
utils.Debugf("%v", err)
|
2013-05-14 21:41:39 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-24 13:37:34 -04:00
|
|
|
|
2013-07-05 15:20:58 -04:00
|
|
|
for tag, id := range tagsList {
|
|
|
|
repoData.ImgList[id] = ®istry.ImgData{
|
|
|
|
ID: id,
|
|
|
|
Tag: tag,
|
|
|
|
Checksum: "",
|
2013-05-24 13:37:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 15:29:16 -04:00
|
|
|
utils.Debugf("Registering tags")
|
2013-05-24 13:37:34 -04:00
|
|
|
// If no tag has been specified, pull them all
|
2013-05-16 15:29:16 -04:00
|
|
|
if askedTag == "" {
|
|
|
|
for tag, id := range tagsList {
|
|
|
|
repoData.ImgList[id].Tag = tag
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, check that the tag exists and use only that one
|
2013-06-04 09:51:12 -04:00
|
|
|
id, exists := tagsList[askedTag]
|
|
|
|
if !exists {
|
2013-07-08 20:26:50 -04:00
|
|
|
return fmt.Errorf("Tag %s not found in repository %s", askedTag, localName)
|
2013-05-16 15:29:16 -04:00
|
|
|
}
|
2013-06-04 09:51:12 -04:00
|
|
|
repoData.ImgList[id].Tag = askedTag
|
2013-05-14 21:41:39 -04:00
|
|
|
}
|
|
|
|
|
2013-07-24 11:41:34 -04:00
|
|
|
errors := make(chan error)
|
|
|
|
for _, image := range repoData.ImgList {
|
2013-07-30 08:09:07 -04:00
|
|
|
downloadImage := func(img *registry.ImgData) {
|
2013-07-24 11:41:34 -04:00
|
|
|
if askedTag != "" && img.Tag != askedTag {
|
|
|
|
utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.ID)
|
|
|
|
errors <- nil
|
|
|
|
return
|
|
|
|
}
|
2013-07-02 12:25:06 -04:00
|
|
|
|
2013-07-24 11:41:34 -04:00
|
|
|
if img.Tag == "" {
|
|
|
|
utils.Debugf("Image (id: %s) present in this repository but untagged, skipping", img.ID)
|
|
|
|
errors <- nil
|
|
|
|
return
|
2013-05-14 21:41:39 -04:00
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus(utils.TruncateID(img.ID), "Pulling image (%s) from %s", img.Tag, localName))
|
2013-07-24 11:41:34 -04:00
|
|
|
success := false
|
|
|
|
for _, ep := range repoData.Endpoints {
|
|
|
|
if err := srv.pullImage(r, out, img.ID, ep, repoData.Tokens, sf); err != nil {
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus(utils.TruncateID(img.ID), "Error while retrieving image for tag: %s (%s); checking next endpoint", askedTag, err))
|
2013-07-24 11:41:34 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
success = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !success {
|
|
|
|
errors <- fmt.Errorf("Could not find repository on any of the indexed registries.")
|
|
|
|
}
|
|
|
|
errors <- nil
|
2013-07-30 08:09:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if parallel {
|
|
|
|
go downloadImage(image)
|
|
|
|
} else {
|
|
|
|
downloadImage(image)
|
|
|
|
}
|
2013-07-24 11:41:34 -04:00
|
|
|
}
|
|
|
|
|
2013-07-30 08:09:07 -04:00
|
|
|
if parallel {
|
|
|
|
for i := 0; i < len(repoData.ImgList); i++ {
|
|
|
|
if err := <-errors; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-14 21:41:39 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 11:41:34 -04:00
|
|
|
|
2013-05-14 21:41:39 -04:00
|
|
|
for tag, id := range tagsList {
|
2013-05-20 20:30:33 -04:00
|
|
|
if askedTag != "" && tag != askedTag {
|
|
|
|
continue
|
|
|
|
}
|
2013-07-08 20:26:50 -04:00
|
|
|
if err := srv.runtime.repositories.Set(localName, tag, id, true); err != nil {
|
2013-05-14 21:41:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := srv.runtime.repositories.Save(); err != nil {
|
2013-05-06 07:34:31 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-14 21:41:39 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-17 19:10:00 -04:00
|
|
|
func (srv *Server) poolAdd(kind, key string) error {
|
2013-07-02 18:46:32 -04:00
|
|
|
srv.Lock()
|
|
|
|
defer srv.Unlock()
|
2013-06-17 19:10:00 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-07-30 08:09:07 -04:00
|
|
|
func (srv *Server) ImagePull(localName string, tag string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig, parallel bool) error {
|
2013-07-23 17:17:31 -04:00
|
|
|
r, err := registry.NewRegistry(srv.runtime.root, authConfig, srv.versionInfos()...)
|
2013-06-17 14:13:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-08 20:26:50 -04:00
|
|
|
if err := srv.poolAdd("pull", localName+":"+tag); err != nil {
|
2013-06-17 19:10:00 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-07-08 20:26:50 -04:00
|
|
|
defer srv.poolRemove("pull", localName+":"+tag)
|
2013-06-17 19:10:00 -04:00
|
|
|
|
2013-07-05 15:20:58 -04:00
|
|
|
// Resolve the Repository name from fqn to endpoint + name
|
2013-07-08 20:26:50 -04:00
|
|
|
endpoint, remoteName, err := registry.ResolveRepositoryName(localName)
|
2013-07-05 15:20:58 -04:00
|
|
|
if err != nil {
|
2013-06-17 19:10:00 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-07-05 15:20:58 -04:00
|
|
|
|
2013-07-09 14:30:12 -04:00
|
|
|
if endpoint == auth.IndexServerAddress() {
|
|
|
|
// If pull "index.docker.io/foo/bar", it's stored locally under "foo/bar"
|
|
|
|
localName = remoteName
|
|
|
|
}
|
2013-06-17 19:10:00 -04:00
|
|
|
|
2013-05-23 11:16:35 -04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-07-30 08:09:07 -04:00
|
|
|
err = srv.pullRepository(r, out, localName, remoteName, tag, endpoint, sf, parallel)
|
2013-07-05 15:20:58 -04:00
|
|
|
if err != nil {
|
2013-07-08 20:26:50 -04:00
|
|
|
if err := srv.pullImage(r, out, remoteName, endpoint, nil, sf); err != nil {
|
2013-05-06 05:31:22 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-07 13:23:50 -04:00
|
|
|
return nil
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-06-05 18:13:01 -04:00
|
|
|
|
2013-05-14 21:41:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-14 23: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
|
2013-07-02 18:27:22 -04:00
|
|
|
func (srv *Server) getChecksum(imageID string) (string, error) {
|
2013-05-14 23:27:15 -04:00
|
|
|
// FIXME: Use in-memory map instead of reading the file each time
|
|
|
|
if sums, err := srv.runtime.graph.getStoredChecksums(); err != nil {
|
|
|
|
return "", err
|
2013-07-02 18:27:22 -04:00
|
|
|
} else if checksum, exists := sums[imageID]; exists {
|
2013-05-14 23:27:15 -04:00
|
|
|
return checksum, nil
|
|
|
|
}
|
|
|
|
|
2013-07-02 18:27:22 -04:00
|
|
|
img, err := srv.runtime.graph.Get(imageID)
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-07-02 18:27:22 -04:00
|
|
|
if _, err := os.Stat(layerArchivePath(srv.runtime.graph.imageRoot(imageID))); err != nil {
|
2013-05-14 23: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 14:30:40 -04:00
|
|
|
func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgData, error) {
|
|
|
|
var imgList []*registry.ImgData
|
2013-05-14 23:27:15 -04:00
|
|
|
|
|
|
|
imageSet := make(map[string]struct{})
|
|
|
|
for tag, id := range localRepo {
|
2013-05-15 14:30:40 -04:00
|
|
|
img, err := srv.runtime.graph.Get(id)
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
2013-05-15 14:30:40 -04:00
|
|
|
return nil, err
|
2013-05-14 23:27:15 -04:00
|
|
|
}
|
|
|
|
img.WalkHistory(func(img *Image) error {
|
2013-06-04 14:00:22 -04:00
|
|
|
if _, exists := imageSet[img.ID]; exists {
|
2013-05-14 23:27:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
imageSet[img.ID] = struct{}{}
|
|
|
|
checksum, err := srv.getChecksum(img.ID)
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-15 14:30:40 -04:00
|
|
|
imgList = append([]*registry.ImgData{{
|
2013-06-04 14:00:22 -04:00
|
|
|
ID: img.ID,
|
2013-05-14 23:27:15 -04:00
|
|
|
Checksum: checksum,
|
|
|
|
Tag: tag,
|
|
|
|
}}, imgList...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2013-05-15 14:30:40 -04:00
|
|
|
return imgList, nil
|
2013-05-14 23:27:15 -04:00
|
|
|
}
|
|
|
|
|
2013-07-08 20:26:50 -04:00
|
|
|
func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, localName, remoteName string, localRepo map[string]string, indexEp string, sf *utils.StreamFormatter) error {
|
2013-05-20 13:58:35 -04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Processing checksums"))
|
2013-05-15 14:30:40 -04:00
|
|
|
imgList, err := srv.getImageList(localRepo)
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Sending image list"))
|
2013-05-14 23:27:15 -04:00
|
|
|
|
2013-05-24 13:37:34 -04:00
|
|
|
var repoData *registry.RepositoryData
|
2013-07-08 20:26:50 -04:00
|
|
|
repoData, err = r.PushImageJSONIndex(indexEp, remoteName, imgList, false, nil)
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range repoData.Endpoints {
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Pushing repository %s (%d tags)", localName, len(localRepo)))
|
2013-05-14 23:27:15 -04:00
|
|
|
// For each image within the repo, push them
|
|
|
|
for _, elem := range imgList {
|
2013-06-04 14:00:22 -04:00
|
|
|
if _, exists := repoData.ImgList[elem.ID]; exists {
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Image %s already pushed, skipping", elem.ID))
|
2013-05-15 16:22:57 -04:00
|
|
|
continue
|
2013-07-02 18:07:02 -04:00
|
|
|
} else if r.LookupRemoteImage(elem.ID, ep, repoData.Tokens) {
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Image %s already pushed, skipping", elem.ID))
|
2013-05-15 16:22:57 -04:00
|
|
|
continue
|
|
|
|
}
|
2013-07-08 20:26:50 -04:00
|
|
|
if err := srv.pushImage(r, out, remoteName, elem.ID, ep, repoData.Tokens, sf); err != nil {
|
2013-05-14 23:27:15 -04:00
|
|
|
// FIXME: Continue on error?
|
|
|
|
return err
|
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Pushing tags for rev [%s] on {%s}", elem.ID, ep+"repositories/"+remoteName+"/tags/"+elem.Tag))
|
2013-07-08 20:26:50 -04:00
|
|
|
if err := r.PushRegistryTag(remoteName, elem.ID, elem.Tag, ep, repoData.Tokens); err != nil {
|
2013-05-15 14:30:40 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-14 23:27:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 20:26:50 -04:00
|
|
|
if _, err := r.PushImageJSONIndex(indexEp, remoteName, imgList, true, repoData.Endpoints); err != nil {
|
2013-05-14 23:27:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-24 13:37:34 -04:00
|
|
|
|
2013-05-15 14:30:40 -04:00
|
|
|
return nil
|
2013-05-14 23:27:15 -04:00
|
|
|
}
|
|
|
|
|
2013-07-02 18:27:22 -04:00
|
|
|
func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgID, ep string, token []string, sf *utils.StreamFormatter) error {
|
2013-05-20 13:58:35 -04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-07-02 18:27:22 -04:00
|
|
|
jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgID, "json"))
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
2013-07-02 18:27:22 -04:00
|
|
|
return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgID, err)
|
2013-05-14 23:27:15 -04:00
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Pushing %s", imgID))
|
2013-05-14 23:27:15 -04:00
|
|
|
|
2013-05-15 14:30:40 -04:00
|
|
|
// Make sure we have the image's checksum
|
2013-07-02 18:27:22 -04:00
|
|
|
checksum, err := srv.getChecksum(imgID)
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imgData := ®istry.ImgData{
|
2013-07-02 18:27:22 -04:00
|
|
|
ID: imgID,
|
2013-05-14 23:27:15 -04:00
|
|
|
Checksum: checksum,
|
|
|
|
}
|
|
|
|
|
2013-05-15 16:22:57 -04:00
|
|
|
// Send the json
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := r.PushImageJSONRegistry(imgData, jsonRaw, ep, token); err != nil {
|
2013-05-15 16:22:57 -04:00
|
|
|
if err == registry.ErrAlreadyExists {
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Image %s already pushed, skipping", imgData.ID))
|
2013-05-15 16:22:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-14 23:27:15 -04:00
|
|
|
// Retrieve the tarball to be sent
|
|
|
|
var layerData *TempArchive
|
|
|
|
// If the archive exists, use it
|
2013-07-02 18:27:22 -04:00
|
|
|
file, err := os.Open(layerArchivePath(srv.runtime.graph.imageRoot(imgID)))
|
2013-05-14 23:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// If the archive does not exist, create one from the layer
|
2013-07-04 04:50:37 -04:00
|
|
|
layerData, err = srv.runtime.graph.TempLayerArchive(imgID, Xz, sf, out)
|
2013-05-14 23:27:15 -04:00
|
|
|
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 14:30:40 -04:00
|
|
|
|
|
|
|
// Send the layer
|
2013-07-24 13:10:59 -04:00
|
|
|
if err := r.PushImageLayerRegistry(imgData.ID, utils.ProgressReader(layerData, int(layerData.Size), out, sf.FormatProgress("", "Pushing", "%8v/%v (%v)"), sf), ep, token); err != nil {
|
2013-05-06 05:31:22 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-17 19:10:00 -04:00
|
|
|
// FIXME: Allow to interupt current push when new push of same image is done.
|
2013-07-08 20:26:50 -04:00
|
|
|
func (srv *Server) ImagePush(localName string, out io.Writer, sf *utils.StreamFormatter, authConfig *auth.AuthConfig) error {
|
|
|
|
if err := srv.poolAdd("push", localName); err != nil {
|
2013-06-17 19:10:00 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-07-08 20:26:50 -04:00
|
|
|
defer srv.poolRemove("push", localName)
|
2013-06-17 19:10:00 -04:00
|
|
|
|
2013-07-05 15:20:58 -04:00
|
|
|
// Resolve the Repository name from fqn to endpoint + name
|
2013-07-08 20:26:50 -04:00
|
|
|
endpoint, remoteName, err := registry.ResolveRepositoryName(localName)
|
2013-07-05 18:26:08 -04:00
|
|
|
if err != nil {
|
2013-06-17 19:10:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:58:35 -04:00
|
|
|
out = utils.NewWriteFlusher(out)
|
2013-07-08 20:26:50 -04:00
|
|
|
img, err := srv.runtime.graph.Get(localName)
|
2013-07-23 17:17:31 -04:00
|
|
|
r, err2 := registry.NewRegistry(srv.runtime.root, authConfig, srv.versionInfos()...)
|
2013-06-17 14:13:40 -04:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2013-06-05 18:12:50 -04:00
|
|
|
|
2013-05-06 07:34:31 -04:00
|
|
|
if err != nil {
|
2013-07-08 20:26:50 -04:00
|
|
|
reposLen := len(srv.runtime.repositories.Repositories[localName])
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "The push refers to a repository [%s] (len: %d)", localName, reposLen))
|
2013-05-07 19:33:12 -04:00
|
|
|
// If it fails, try to get the repository
|
2013-07-08 20:26:50 -04:00
|
|
|
if localRepo, exists := srv.runtime.repositories.Repositories[localName]; exists {
|
|
|
|
if err := srv.pushRepository(r, out, localName, remoteName, localRepo, endpoint, sf); err != nil {
|
2013-05-06 07:34:31 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2013-07-05 15:20:58 -04:00
|
|
|
|
|
|
|
var token []string
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "The push refers to an image: [%s]", localName))
|
2013-07-08 20:26:50 -04:00
|
|
|
if err := srv.pushImage(r, out, remoteName, img.ID, endpoint, token, sf); err != nil {
|
2013-05-06 07:34:31 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-25 11:09:46 -04:00
|
|
|
func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer, sf *utils.StreamFormatter) error {
|
2013-05-06 05:31:22 -04:00
|
|
|
var archive io.Reader
|
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
if src == "-" {
|
2013-05-07 21:06:49 -04:00
|
|
|
archive = in
|
2013-05-06 05:31:22 -04:00
|
|
|
} else {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
2013-05-25 11:09:46 -04:00
|
|
|
return err
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", "Downloading from %s", u))
|
2013-05-07 19:33:12 -04:00
|
|
|
// Download with curl (pretty progress bar)
|
|
|
|
// If curl is not available, fallback to http.Get()
|
2013-05-14 18:37:35 -04:00
|
|
|
resp, err = utils.Download(u.String(), out)
|
2013-05-06 05:31:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, sf.FormatProgress("", "Importing", "%8v/%v (%v)"), sf)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-07 19:33:12 -04:00
|
|
|
// Optionally register the image at REPO/TAG
|
2013-05-06 05:31:22 -04:00
|
|
|
if repo != "" {
|
2013-06-04 14:00:22 -04:00
|
|
|
if err := srv.runtime.repositories.Set(repo, tag, img.ID, true); err != nil {
|
2013-05-06 05:31:22 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 13:10:59 -04:00
|
|
|
out.Write(sf.FormatStatus("", img.ShortID()))
|
2013-05-06 05:31:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-09 20:50:56 -04:00
|
|
|
func (srv *Server) ContainerCreate(config *Config) (string, error) {
|
2013-05-06 05:31:22 -04:00
|
|
|
|
2013-06-14 12:46:04 -04:00
|
|
|
if config.Memory != 0 && config.Memory < 524288 {
|
|
|
|
return "", fmt.Errorf("Memory limit must be given in bytes (minimum 524288 bytes)")
|
|
|
|
}
|
|
|
|
|
2013-05-06 05: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 13:23:50 -04:00
|
|
|
b := NewBuilder(srv.runtime)
|
2013-05-09 20:50:56 -04:00
|
|
|
container, err := b.Create(config)
|
2013-05-06 05:31:22 -04:00
|
|
|
if err != nil {
|
|
|
|
if srv.runtime.graph.IsNotExist(err) {
|
2013-05-09 20:50:56 -04:00
|
|
|
return "", fmt.Errorf("No such image: %s", config.Image)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
return "", err
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("create", container.ShortID())
|
2013-06-04 14:00:22 -04:00
|
|
|
return container.ShortID(), nil
|
2013-05-06 05: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 {
|
2013-07-04 17:28:49 -04:00
|
|
|
return fmt.Errorf("Error restarting container %s: %s", name, err)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("restart", name)
|
2013-05-06 05:31:22 -04:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:19:55 -04:00
|
|
|
func (srv *Server) ContainerDestroy(name string, removeVolume bool) error {
|
2013-05-06 05:31:22 -04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
2013-06-20 11:45:30 -04:00
|
|
|
if container.State.Running {
|
|
|
|
return fmt.Errorf("Impossible to remove a running container, please stop it first")
|
|
|
|
}
|
2013-05-06 05: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 05:31:22 -04:00
|
|
|
if err := srv.runtime.Destroy(container); err != nil {
|
2013-07-04 17:28:49 -04:00
|
|
|
return fmt.Errorf("Error destroying container %s: %s", name, err)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("destroy", name)
|
2013-05-06 05:52:15 -04:00
|
|
|
|
2013-05-09 22:19:55 -04:00
|
|
|
if removeVolume {
|
2013-05-06 05: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 {
|
2013-06-04 14:00:22 -04:00
|
|
|
log.Printf("The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.ID)
|
2013-05-06 05:52:15 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := srv.runtime.volumes.Delete(volumeId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:30:21 -04:00
|
|
|
var ErrImageReferenced = errors.New("Image referenced by a repository")
|
|
|
|
|
2013-06-10 17:05:54 -04:00
|
|
|
func (srv *Server) deleteImageAndChildren(id string, imgs *[]APIRmi) error {
|
2013-05-30 15:30:21 -04:00
|
|
|
// If the image is referenced by a repo, do not delete
|
2013-06-10 17:05:54 -04:00
|
|
|
if len(srv.runtime.repositories.ByID()[id]) != 0 {
|
2013-05-30 15:30:21 -04:00
|
|
|
return ErrImageReferenced
|
|
|
|
}
|
|
|
|
// If the image is not referenced but has children, go recursive
|
|
|
|
referenced := false
|
2013-05-30 18:53:45 -04:00
|
|
|
byParents, err := srv.runtime.graph.ByParent()
|
2013-05-06 05:31:22 -04:00
|
|
|
if err != nil {
|
2013-05-30 18:53:45 -04:00
|
|
|
return err
|
2013-06-04 09:51:12 -04:00
|
|
|
}
|
2013-05-30 15:30:21 -04:00
|
|
|
for _, img := range byParents[id] {
|
2013-06-10 17:05:54 -04:00
|
|
|
if err := srv.deleteImageAndChildren(img.ID, imgs); err != nil {
|
2013-05-30 15:30:21 -04:00
|
|
|
if err != ErrImageReferenced {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-10 17:05:54 -04:00
|
|
|
referenced = true
|
2013-05-30 15:30:21 -04:00
|
|
|
}
|
2013-06-04 09:51:12 -04:00
|
|
|
}
|
2013-05-30 15:30:21 -04:00
|
|
|
if referenced {
|
|
|
|
return ErrImageReferenced
|
|
|
|
}
|
2013-05-30 18:53:45 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2013-05-31 10:37:02 -04:00
|
|
|
err := srv.runtime.graph.Delete(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-10 17:05:54 -04:00
|
|
|
*imgs = append(*imgs, APIRmi{Deleted: utils.TruncateID(id)})
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("delete", utils.TruncateID(id))
|
2013-05-31 10:37:02 -04:00
|
|
|
return nil
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-10 17:05:54 -04:00
|
|
|
func (srv *Server) deleteImageParents(img *Image, imgs *[]APIRmi) error {
|
2013-05-30 15:30:21 -04:00
|
|
|
if img.Parent != "" {
|
|
|
|
parent, err := srv.runtime.graph.Get(img.Parent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove all children images
|
2013-05-31 10:37:02 -04:00
|
|
|
if err := srv.deleteImageAndChildren(img.Parent, imgs); err != nil {
|
2013-05-30 15:30:21 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-31 10:37:02 -04:00
|
|
|
return srv.deleteImageParents(parent, imgs)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-28 15:41:09 -04:00
|
|
|
func (srv *Server) deleteImage(img *Image, repoName, tag string) ([]APIRmi, error) {
|
2013-07-05 12:58:39 -04:00
|
|
|
imgs := []APIRmi{}
|
2013-07-17 11:48:53 -04:00
|
|
|
|
|
|
|
//If delete by id, see if the id belong only to one repository
|
|
|
|
if strings.Contains(img.ID, repoName) && tag == "" {
|
|
|
|
for _, repoAndTag := range srv.runtime.repositories.ByID()[img.ID] {
|
|
|
|
parsedRepo := strings.Split(repoAndTag, ":")[0]
|
|
|
|
if strings.Contains(img.ID, repoName) {
|
|
|
|
repoName = parsedRepo
|
2013-07-26 06:01:41 -04:00
|
|
|
if len(srv.runtime.repositories.ByID()[img.ID]) == 1 && len(strings.Split(repoAndTag, ":")) > 1 {
|
2013-07-26 05:19:26 -04:00
|
|
|
tag = strings.Split(repoAndTag, ":")[1]
|
|
|
|
}
|
2013-07-17 11:48:53 -04:00
|
|
|
} else if repoName != parsedRepo {
|
|
|
|
// the id belongs to multiple repos, like base:latest and user:test,
|
|
|
|
// in that case return conflict
|
|
|
|
return imgs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Untag the current image
|
2013-05-31 10:37:02 -04:00
|
|
|
tagDeleted, err := srv.runtime.repositories.Delete(repoName, tag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tagDeleted {
|
2013-06-10 17:05:54 -04:00
|
|
|
imgs = append(imgs, APIRmi{Untagged: img.ShortID()})
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("untag", img.ShortID())
|
2013-05-30 15:30:21 -04:00
|
|
|
}
|
2013-06-10 17:05:54 -04:00
|
|
|
if len(srv.runtime.repositories.ByID()[img.ID]) == 0 {
|
|
|
|
if err := srv.deleteImageAndChildren(img.ID, &imgs); err != nil {
|
2013-05-30 18:53:45 -04:00
|
|
|
if err != ErrImageReferenced {
|
2013-06-28 15:41:09 -04:00
|
|
|
return imgs, err
|
2013-05-30 18:53:45 -04:00
|
|
|
}
|
2013-05-31 10:37:02 -04:00
|
|
|
} else if err := srv.deleteImageParents(img, &imgs); err != nil {
|
2013-05-30 15:30:21 -04:00
|
|
|
if err != ErrImageReferenced {
|
2013-06-28 15:41:09 -04:00
|
|
|
return imgs, err
|
2013-05-30 15:30:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 15:41:09 -04:00
|
|
|
return imgs, nil
|
2013-05-30 15:30:21 -04:00
|
|
|
}
|
|
|
|
|
2013-06-28 15:41:09 -04:00
|
|
|
func (srv *Server) ImageDelete(name string, autoPrune bool) ([]APIRmi, error) {
|
2013-05-30 18:53:45 -04:00
|
|
|
img, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
2013-05-31 10:37:02 -04:00
|
|
|
return nil, fmt.Errorf("No such image: %s", name)
|
|
|
|
}
|
|
|
|
if !autoPrune {
|
2013-06-10 17:05:54 -04:00
|
|
|
if err := srv.runtime.graph.Delete(img.ID); err != nil {
|
2013-07-04 17:28:49 -04:00
|
|
|
return nil, fmt.Errorf("Error deleting image %s: %s", name, err)
|
2013-05-31 10:37:02 -04:00
|
|
|
}
|
|
|
|
return nil, nil
|
2013-05-30 18:53:45 -04:00
|
|
|
}
|
|
|
|
|
2013-05-16 14:27:50 -04:00
|
|
|
var tag string
|
|
|
|
if strings.Contains(name, ":") {
|
|
|
|
nameParts := strings.Split(name, ":")
|
|
|
|
name = nameParts[0]
|
|
|
|
tag = nameParts[1]
|
|
|
|
}
|
|
|
|
|
2013-05-30 18:53:45 -04:00
|
|
|
return srv.deleteImage(img, name, tag)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
|
2013-07-02 18:27:22 -04:00
|
|
|
func (srv *Server) ImageGetCached(imgID string, config *Config) (*Image, error) {
|
2013-05-19 13:46:24 -04:00
|
|
|
|
|
|
|
// 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{})
|
|
|
|
}
|
2013-06-04 14:00:22 -04:00
|
|
|
imageMap[img.Parent][img.ID] = struct{}{}
|
2013-05-19 13:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop on the children of the given image and check the config
|
2013-07-02 18:27:22 -04:00
|
|
|
for elem := range imageMap[imgID] {
|
2013-05-19 13:46:24 -04:00
|
|
|
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-13 19:39:54 -04:00
|
|
|
func (srv *Server) ContainerStart(name string, hostConfig *HostConfig) error {
|
2013-05-06 05:31:22 -04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
2013-05-13 19:39:54 -04:00
|
|
|
if err := container.Start(hostConfig); err != nil {
|
2013-07-04 17:28:49 -04:00
|
|
|
return fmt.Errorf("Error starting container %s: %s", name, err)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("start", name)
|
2013-05-06 05:31:22 -04:00
|
|
|
} 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 {
|
2013-07-04 17:28:49 -04:00
|
|
|
return fmt.Errorf("Error stopping container %s: %s", name, err)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-12 12:29:23 -04:00
|
|
|
srv.LogEvent("stop", name)
|
2013-05-06 05:31:22 -04:00
|
|
|
} 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-23 22: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-07 21: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 02:32:17 -04:00
|
|
|
container := srv.runtime.Get(name)
|
|
|
|
if container == nil {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
//logs
|
|
|
|
if logs {
|
2013-07-15 12:17:58 -04:00
|
|
|
cLog, err := container.ReadLog("json")
|
2013-07-18 09:25:47 -04:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// Legacy logs
|
2013-07-18 09:29:40 -04:00
|
|
|
utils.Debugf("Old logs format")
|
2013-07-18 09:25:47 -04:00
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
|
|
|
utils.Debugf("Error reading logs (stdout): %s", err)
|
|
|
|
} else if _, err := io.Copy(out, cLog); err != nil {
|
|
|
|
utils.Debugf("Error streaming logs (stdout): %s", err)
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-18 09:25:47 -04:00
|
|
|
if stderr {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
|
|
|
utils.Debugf("Error reading logs (stderr): %s", err)
|
|
|
|
} else if _, err := io.Copy(out, cLog); err != nil {
|
|
|
|
utils.Debugf("Error streaming logs (stderr): %s", err)
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-07-18 09:25:47 -04:00
|
|
|
} else if err != nil {
|
|
|
|
utils.Debugf("Error reading logs (json): %s", err)
|
|
|
|
} else {
|
|
|
|
dec := json.NewDecoder(cLog)
|
|
|
|
for {
|
|
|
|
var l utils.JSONLog
|
|
|
|
if err := dec.Decode(&l); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
utils.Debugf("Error streaming logs: %s", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if (l.Stream == "stdout" && stdout) || (l.Stream == "stderr" && stderr) {
|
|
|
|
fmt.Fprintf(out, "%s", l.Log)
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-08 02:32:17 -04:00
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
|
2013-05-08 02:32:17 -04:00
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
if container.State.Ghost {
|
|
|
|
return fmt.Errorf("Impossible to attach to a ghost container")
|
|
|
|
}
|
2013-05-06 05:31:22 -04:00
|
|
|
|
2013-05-08 02:32:17 -04:00
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
cStdinCloser io.Closer
|
|
|
|
)
|
2013-05-06 05:31:22 -04:00
|
|
|
|
2013-05-08 02:32:17 -04:00
|
|
|
if stdin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2013-05-14 18:37:35 -04:00
|
|
|
defer utils.Debugf("Closing buffered stdin pipe")
|
2013-05-08 02:32:17 -04:00
|
|
|
io.Copy(w, in)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
cStdinCloser = in
|
|
|
|
}
|
|
|
|
if stdout {
|
|
|
|
cStdout = out
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cStderr = out
|
|
|
|
}
|
2013-05-07 17:15:42 -04:00
|
|
|
|
2013-05-08 02: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 05: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)
|
|
|
|
}
|
|
|
|
|
2013-06-20 17:29:54 -04:00
|
|
|
func NewServer(flGraphPath string, autoRestart, enableCors bool, dns ListOpts) (*Server, error) {
|
2013-05-06 05:31:22 -04:00
|
|
|
if runtime.GOARCH != "amd64" {
|
|
|
|
log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
|
|
|
|
}
|
2013-06-20 17:29:54 -04:00
|
|
|
runtime, err := NewRuntime(flGraphPath, autoRestart, dns)
|
2013-05-06 05:31:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv := &Server{
|
2013-06-17 19:10:00 -04:00
|
|
|
runtime: runtime,
|
|
|
|
enableCors: enableCors,
|
|
|
|
pullingPool: make(map[string]struct{}),
|
|
|
|
pushingPool: make(map[string]struct{}),
|
2013-07-12 12:29:23 -04:00
|
|
|
events: make([]utils.JSONMessage, 0, 64), //only keeps the 64 last events
|
|
|
|
listeners: make(map[string]chan utils.JSONMessage),
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-05-15 20:17:33 -04:00
|
|
|
runtime.srv = srv
|
2013-05-06 05:31:22 -04:00
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:29:23 -04:00
|
|
|
func (srv *Server) LogEvent(action, id string) {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
jm := utils.JSONMessage{Status: action, ID: id, Time: now}
|
|
|
|
srv.events = append(srv.events, jm)
|
|
|
|
for _, c := range srv.listeners {
|
2013-07-18 10:35:14 -04:00
|
|
|
select { // non blocking channel
|
|
|
|
case c <- jm:
|
|
|
|
default:
|
|
|
|
}
|
2013-07-10 08:55:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 05:31:22 -04:00
|
|
|
type Server struct {
|
2013-07-02 18:46:32 -04:00
|
|
|
sync.Mutex
|
2013-06-17 19:10:00 -04:00
|
|
|
runtime *Runtime
|
|
|
|
enableCors bool
|
|
|
|
pullingPool map[string]struct{}
|
|
|
|
pushingPool map[string]struct{}
|
2013-07-12 12:29:23 -04:00
|
|
|
events []utils.JSONMessage
|
|
|
|
listeners map[string]chan utils.JSONMessage
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|