2013-05-06 05:31:22 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"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-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
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
2013-05-07 14:37:35 -04:00
|
|
|
func (srv *Server) ImagesSearch(term string) ([]ApiSearch, error) {
|
|
|
|
results, err := srv.runtime.graph.SearchRepositories(nil, term)
|
|
|
|
if err != nil {
|
2013-05-07 14:59:04 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-05-07 14:37:35 -04:00
|
|
|
var outs []ApiSearch
|
|
|
|
for _, repo := range results.Results {
|
|
|
|
var out ApiSearch
|
|
|
|
out.Description = repo["description"]
|
2013-05-07 14:59:04 -04:00
|
|
|
if len(out.Description) > 45 {
|
|
|
|
out.Description = Trunc(out.Description, 42) + "..."
|
|
|
|
}
|
2013-05-07 14:37:35 -04:00
|
|
|
out.Name = repo["name"]
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
func (srv *Server) ImageInsert(name, url, path string, out io.Writer) error {
|
2013-05-07 13:23:50 -04:00
|
|
|
img, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
file, err := Download(url, out)
|
2013-05-07 13:23:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Body.Close()
|
|
|
|
|
|
|
|
config, _, err := ParseRun([]string{img.Id, "echo", "insert", url, path}, srv.runtime.capabilities)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBuilder(srv.runtime)
|
|
|
|
c, err := b.Create(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
if err := c.Inject(ProgressReader(file.Body, int(file.ContentLength), out, "Downloading %v/%v (%v)"), path); err != nil {
|
2013-05-07 13:23:50 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// FIXME: Handle custom repo, tag comment, author
|
|
|
|
img, err = b.Commit(c, "", "", img.Comment, img.Author, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-07 21:06:49 -04:00
|
|
|
fmt.Fprintf(out, "%s\n", img.Id)
|
2013-05-07 13:23:50 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-05-09 17:10:26 -04:00
|
|
|
out.Write([]byte(" \"" + parentImage.ShortId() + "\" -> \"" + image.ShortId() + "\"\n"))
|
2013-05-07 13:23:50 -04:00
|
|
|
} else {
|
2013-05-09 17:10:26 -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 {
|
|
|
|
reporefs[TruncateId(id)] = append(reporefs[TruncateId(id)], fmt.Sprintf("%s:%s", name, tag))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-08 11:35:50 -04:00
|
|
|
func (srv *Server) Images(all, only_ids bool, filter string) ([]ApiImages, error) {
|
2013-05-06 05:31:22 -04:00
|
|
|
var allImages map[string]*Image
|
|
|
|
var 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
|
|
|
|
}
|
|
|
|
var outs []ApiImages = []ApiImages{} //produce [] when empty instead of 'null'
|
|
|
|
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-08 11:35:50 -04:00
|
|
|
if !only_ids {
|
2013-05-06 05:31:22 -04:00
|
|
|
out.Repository = name
|
|
|
|
out.Tag = tag
|
|
|
|
out.Id = TruncateId(id)
|
|
|
|
out.Created = image.Created.Unix()
|
|
|
|
} else {
|
|
|
|
out.Id = image.ShortId()
|
|
|
|
}
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Display images which aren't part of a
|
|
|
|
if filter == "" {
|
|
|
|
for id, image := range allImages {
|
|
|
|
var out ApiImages
|
2013-05-08 11:35:50 -04:00
|
|
|
if !only_ids {
|
2013-05-06 05:31:22 -04:00
|
|
|
out.Repository = "<none>"
|
|
|
|
out.Tag = "<none>"
|
|
|
|
out.Id = TruncateId(id)
|
|
|
|
out.Created = image.Created.Unix()
|
|
|
|
} else {
|
|
|
|
out.Id = image.ShortId()
|
|
|
|
}
|
|
|
|
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-08 20:20:16 -04:00
|
|
|
out.GoVersion = runtime.Version()
|
2013-05-07 19:47:43 -04:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
2013-05-06 05:31:22 -04:00
|
|
|
out.Debug = true
|
|
|
|
out.NFd = getTotalUsedFds()
|
|
|
|
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 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-05-08 12:28:11 -04:00
|
|
|
func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, before string) []ApiContainers {
|
|
|
|
var foundBefore bool
|
|
|
|
var displayed int
|
2013-05-09 20:50:56 -04:00
|
|
|
retContainers := []ApiContainers{}
|
|
|
|
|
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 != "" {
|
|
|
|
if container.ShortId() == before {
|
|
|
|
foundBefore = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !foundBefore {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if displayed == n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if container.ShortId() == since {
|
2013-05-06 05:31:22 -04:00
|
|
|
break
|
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
displayed++
|
|
|
|
|
|
|
|
c := ApiContainers{
|
2013-05-10 04:18:01 -04:00
|
|
|
Id: container.Id,
|
|
|
|
}
|
|
|
|
if trunc_cmd {
|
|
|
|
c = ApiContainers{
|
|
|
|
Id: container.ShortId(),
|
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 11:35:50 -04:00
|
|
|
if !only_ids {
|
2013-05-06 05:31:22 -04:00
|
|
|
command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
|
2013-05-08 11:35:50 -04:00
|
|
|
if trunc_cmd {
|
2013-05-06 05:31:22 -04:00
|
|
|
command = Trunc(command, 20)
|
|
|
|
}
|
2013-05-09 20:50:56 -04:00
|
|
|
c.Image = srv.runtime.repositories.ImageName(container.Image)
|
|
|
|
c.Command = command
|
|
|
|
c.Created = container.Created.Unix()
|
|
|
|
c.Status = container.State.String()
|
|
|
|
c.Ports = container.NetworkSettings.PortMappingHuman()
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
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-07 21:06:49 -04:00
|
|
|
func (srv *Server) ImagePull(name, tag, registry string, out io.Writer) error {
|
2013-05-07 13:23:50 -04:00
|
|
|
if registry != "" {
|
2013-05-07 21:06:49 -04:00
|
|
|
if err := srv.runtime.graph.PullImage(out, name, registry, nil); 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-05-07 21:06:49 -04:00
|
|
|
if err := srv.runtime.graph.PullRepository(out, name, tag, srv.runtime.repositories, srv.runtime.authConfig); err != nil {
|
2013-05-06 05:31:22 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
func (srv *Server) ImagePush(name, registry string, out io.Writer) error {
|
2013-05-06 07:34:31 -04:00
|
|
|
img, err := srv.runtime.graph.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
Debugf("The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name]))
|
2013-05-07 19:33:12 -04:00
|
|
|
// If it fails, try to get the repository
|
2013-05-06 07:34:31 -04:00
|
|
|
if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists {
|
2013-05-07 21:06:49 -04:00
|
|
|
if err := srv.runtime.graph.PushRepository(out, name, localRepo, srv.runtime.authConfig); err != nil {
|
2013-05-06 07:34:31 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2013-05-07 21:06:49 -04:00
|
|
|
err = srv.runtime.graph.PushImage(out, img, registry, nil)
|
2013-05-06 07:34:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer) 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-07 21:06:49 -04:00
|
|
|
fmt.Fprintf(out, "Error: %s\n", err)
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
2013-05-07 21:06:49 -04:00
|
|
|
fmt.Fprintln(out, "Downloading from", 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-07 21:06:49 -04:00
|
|
|
resp, err = Download(u.String(), out)
|
2013-05-06 05:31:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-07 21:06:49 -04:00
|
|
|
archive = ProgressReader(resp.Body, int(resp.ContentLength), out, "Importing %v/%v (%v)")
|
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 != "" {
|
|
|
|
if err := srv.runtime.repositories.Set(repo, tag, img.Id, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 21:06:49 -04:00
|
|
|
fmt.Fprintln(out, 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
|
|
|
|
|
|
|
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-05-09 20:50:56 -04:00
|
|
|
return container.ShortId(), nil
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
|
2013-05-07 21:06:49 -04:00
|
|
|
func (srv *Server) ImageCreateFromFile(dockerfile io.Reader, out io.Writer) error {
|
|
|
|
img, err := NewBuilder(srv.runtime).Build(dockerfile, out)
|
2013-05-07 13:23:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-07 21:06:49 -04:00
|
|
|
fmt.Fprintf(out, "%s\n", img.ShortId())
|
2013-05-07 13:23:50 -04:00
|
|
|
return 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 {
|
|
|
|
return fmt.Errorf("Error restarting container %s: %s", name, err.Error())
|
|
|
|
}
|
|
|
|
} 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:52:15 -04:00
|
|
|
|
2013-05-06 05:31:22 -04:00
|
|
|
if container := srv.runtime.Get(name); container != nil {
|
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 {
|
|
|
|
return fmt.Errorf("Error destroying container %s: %s", name, err.Error())
|
|
|
|
}
|
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 {
|
|
|
|
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 05:31:22 -04:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) ImageDelete(name string) error {
|
|
|
|
img, err := srv.runtime.repositories.LookupImage(name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("No such image: %s", name)
|
|
|
|
} else {
|
|
|
|
if err := srv.runtime.graph.Delete(img.Id); err != nil {
|
2013-05-08 11:35:50 -04:00
|
|
|
return fmt.Errorf("Error deleting image %s: %s", name, err.Error())
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
}
|
2013-05-10 00:53:56 -04:00
|
|
|
|
2013-05-08 02:32:17 -04:00
|
|
|
//logs
|
|
|
|
if logs {
|
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
|
|
|
Debugf(err.Error())
|
|
|
|
} else if _, err := io.Copy(out, cLog); err != nil {
|
|
|
|
Debugf(err.Error())
|
2013-05-06 05:31:22 -04:00
|
|
|
}
|
2013-05-08 02:32:17 -04:00
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
|
|
|
Debugf(err.Error())
|
|
|
|
} else if _, err := io.Copy(out, cLog); err != nil {
|
|
|
|
Debugf(err.Error())
|
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()
|
|
|
|
defer Debugf("Closing buffered stdin pipe")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
runtime: runtime,
|
|
|
|
}
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
runtime *Runtime
|
|
|
|
}
|