2013-04-11 06:48:21 +04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-04-23 20:20:53 +04:00
|
|
|
_ "bytes"
|
2013-04-18 05:13:43 +04:00
|
|
|
"encoding/json"
|
2013-04-19 17:24:37 +04:00
|
|
|
"fmt"
|
2013-04-11 06:48:21 +04:00
|
|
|
"github.com/gorilla/mux"
|
2013-04-23 20:20:53 +04:00
|
|
|
"io"
|
2013-04-19 17:24:37 +04:00
|
|
|
"io/ioutil"
|
2013-04-18 20:56:22 +04:00
|
|
|
"log"
|
2013-04-23 20:20:53 +04:00
|
|
|
"net"
|
2013-04-11 06:48:21 +04:00
|
|
|
"net/http"
|
2013-04-19 17:24:37 +04:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2013-04-22 20:17:47 +04:00
|
|
|
"strconv"
|
2013-04-19 17:24:37 +04:00
|
|
|
"strings"
|
2013-04-18 20:56:22 +04:00
|
|
|
"time"
|
2013-04-11 06:48:21 +04:00
|
|
|
)
|
|
|
|
|
2013-04-19 17:24:37 +04:00
|
|
|
func ListenAndServe(addr string, rtime *Runtime) error {
|
2013-04-18 05:13:43 +04:00
|
|
|
r := mux.NewRouter()
|
|
|
|
log.Printf("Listening for HTTP on %s\n", addr)
|
2013-04-11 06:48:21 +04:00
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/version").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
m := ApiVersion{VERSION, GIT_COMMIT, NO_MEMORY_LIMIT}
|
2013-04-18 05:13:43 +04:00
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2013-04-22 20:17:47 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-18 05:13:43 +04:00
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
2013-04-11 06:48:21 +04:00
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers/{name:.*}/kill").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
2013-04-23 20:20:53 +04:00
|
|
|
name := vars["name"]
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
http.Error(w, "Error restarting container "+name+": "+err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-23 20:20:53 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(200)
|
2013-04-18 20:56:22 +04:00
|
|
|
})
|
|
|
|
|
2013-04-24 16:01:40 +04:00
|
|
|
r.Path("/images/{name:.*}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
if image, err := rtime.repositories.LookupImage(name); err == nil && image != nil {
|
|
|
|
b, err := json.Marshal(image)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, "No such image: "+name, http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Path("/containers/{name:.*}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
b, err := json.Marshal(container)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/images").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
All := r.Form.Get("all")
|
2013-04-23 20:20:53 +04:00
|
|
|
NameFilter := r.Form.Get("filter")
|
|
|
|
Quiet := r.Form.Get("quiet")
|
2013-04-18 20:56:22 +04:00
|
|
|
|
|
|
|
var allImages map[string]*Image
|
|
|
|
var err error
|
2013-04-22 20:17:47 +04:00
|
|
|
if All == "true" {
|
2013-04-19 17:24:37 +04:00
|
|
|
allImages, err = rtime.graph.Map()
|
2013-04-18 20:56:22 +04:00
|
|
|
} else {
|
2013-04-19 17:24:37 +04:00
|
|
|
allImages, err = rtime.graph.Heads()
|
2013-04-18 20:56:22 +04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
var outs []ApiImages
|
2013-04-19 17:24:37 +04:00
|
|
|
for name, repository := range rtime.repositories.Repositories {
|
2013-04-22 20:17:47 +04:00
|
|
|
if NameFilter != "" && name != NameFilter {
|
2013-04-18 20:56:22 +04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for tag, id := range repository {
|
2013-04-23 20:20:53 +04:00
|
|
|
var out ApiImages
|
2013-04-19 17:24:37 +04:00
|
|
|
image, err := rtime.graph.Get(id)
|
2013-04-18 20:56:22 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(allImages, id)
|
2013-04-22 20:17:47 +04:00
|
|
|
if Quiet != "true" {
|
2013-04-18 20:56:22 +04:00
|
|
|
out.Repository = name
|
|
|
|
out.Tag = tag
|
|
|
|
out.Id = TruncateId(id)
|
|
|
|
out.Created = HumanDuration(time.Now().Sub(image.Created)) + " ago"
|
|
|
|
} else {
|
|
|
|
out.Id = image.ShortId()
|
|
|
|
}
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Display images which aren't part of a
|
2013-04-22 20:17:47 +04:00
|
|
|
if NameFilter == "" {
|
2013-04-18 20:56:22 +04:00
|
|
|
for id, image := range allImages {
|
2013-04-23 20:20:53 +04:00
|
|
|
var out ApiImages
|
2013-04-22 20:17:47 +04:00
|
|
|
if Quiet != "true" {
|
2013-04-18 20:56:22 +04:00
|
|
|
out.Repository = "<none>"
|
|
|
|
out.Tag = "<none>"
|
|
|
|
out.Id = TruncateId(id)
|
|
|
|
out.Created = HumanDuration(time.Now().Sub(image.Created)) + " ago"
|
|
|
|
} else {
|
|
|
|
out.Id = image.ShortId()
|
|
|
|
}
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(outs)
|
|
|
|
if err != nil {
|
2013-04-19 17:24:37 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/info").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-19 17:24:37 +04:00
|
|
|
images, _ := rtime.graph.All()
|
|
|
|
var imgcount int
|
|
|
|
if images == nil {
|
|
|
|
imgcount = 0
|
|
|
|
} else {
|
|
|
|
imgcount = len(images)
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
var out ApiInfo
|
2013-04-19 17:24:37 +04:00
|
|
|
out.Containers = len(rtime.List())
|
|
|
|
out.Version = VERSION
|
|
|
|
out.Images = imgcount
|
|
|
|
if os.Getenv("DEBUG") == "1" {
|
|
|
|
out.Debug = true
|
|
|
|
out.NFd = getTotalUsedFds()
|
|
|
|
out.NGoroutines = runtime.NumGoroutine()
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(out)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/images/{name:.*}/history").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-04-19 17:24:37 +04:00
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
image, err := rtime.repositories.LookupImage(name)
|
2013-04-19 17:24:37 +04:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
var outs []ApiHistory
|
2013-04-19 17:24:37 +04:00
|
|
|
err = image.WalkHistory(func(img *Image) error {
|
2013-04-23 20:20:53 +04:00
|
|
|
var out ApiHistory
|
2013-04-19 17:24:37 +04:00
|
|
|
out.Id = rtime.repositories.ImageName(img.ShortId())
|
|
|
|
out.Created = HumanDuration(time.Now().Sub(img.Created)) + " ago"
|
|
|
|
out.CreatedBy = strings.Join(img.ContainerConfig.Cmd, " ")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
b, err := json.Marshal(outs)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers/{name:.*}/logs").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-04-19 17:24:37 +04:00
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
if container := rtime.Get(name); container != nil {
|
2013-04-23 20:20:53 +04:00
|
|
|
var out ApiLogs
|
2013-04-19 17:24:37 +04:00
|
|
|
|
|
|
|
logStdout, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logStderr, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout, errStdout := ioutil.ReadAll(logStdout)
|
|
|
|
if errStdout != nil {
|
|
|
|
http.Error(w, errStdout.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
out.Stdout = fmt.Sprintf("%s", stdout)
|
|
|
|
}
|
|
|
|
stderr, errStderr := ioutil.ReadAll(logStderr)
|
|
|
|
if errStderr != nil {
|
|
|
|
http.Error(w, errStderr.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
out.Stderr = fmt.Sprintf("%s", stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(out)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Path("/containers/{name:.*}/changes").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
changes, err := container.Changes()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var changesStr []string
|
|
|
|
for _, name := range changes {
|
|
|
|
changesStr = append(changesStr, name.String())
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(changesStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-19 17:24:37 +04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
r.Path("/containers/{name:.*}/port").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
privatePort := r.Form.Get("port")
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
if container := rtime.Get(name); container == nil {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-23 20:20:53 +04:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if frontend, exists := container.NetworkSettings.PortMapping[privatePort]; !exists {
|
|
|
|
http.Error(w, "No private port '"+privatePort+"' allocated on "+name, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
b, err := json.Marshal(ApiPort{frontend})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
All := r.Form.Get("all")
|
2013-04-23 20:20:53 +04:00
|
|
|
NoTrunc := r.Form.Get("notrunc")
|
|
|
|
Quiet := r.Form.Get("quiet")
|
2013-04-22 20:17:47 +04:00
|
|
|
Last := r.Form.Get("n")
|
|
|
|
n, err := strconv.Atoi(Last)
|
|
|
|
if err != nil {
|
|
|
|
n = -1
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
var outs []ApiContainers
|
2013-04-19 17:24:37 +04:00
|
|
|
|
|
|
|
for i, container := range rtime.List() {
|
2013-04-22 20:17:47 +04:00
|
|
|
if !container.State.Running && All != "true" && n == -1 {
|
2013-04-19 17:24:37 +04:00
|
|
|
continue
|
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
if i == n {
|
2013-04-19 17:24:37 +04:00
|
|
|
break
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
var out ApiContainers
|
2013-04-19 17:24:37 +04:00
|
|
|
out.Id = container.ShortId()
|
2013-04-22 20:17:47 +04:00
|
|
|
if Quiet != "true" {
|
2013-04-19 17:24:37 +04:00
|
|
|
command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
|
2013-04-22 20:17:47 +04:00
|
|
|
if NoTrunc != "true" {
|
2013-04-19 17:24:37 +04:00
|
|
|
command = Trunc(command, 20)
|
|
|
|
}
|
|
|
|
out.Image = rtime.repositories.ImageName(container.Image)
|
|
|
|
out.Command = command
|
|
|
|
out.Created = HumanDuration(time.Now().Sub(container.Created)) + " ago"
|
|
|
|
out.Status = container.State.String()
|
|
|
|
}
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(outs)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-24 18:06:03 +04:00
|
|
|
r.Path("/containers/{name:.*}/commit").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
repo := r.Form.Get("repo")
|
|
|
|
tag := r.Form.Get("tag")
|
|
|
|
comment := r.Form.Get("comment")
|
|
|
|
|
|
|
|
img, err := rtime.Commit(name, repo, tag, comment)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(ApiCommit{img.ShortId()})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
r.Path("/images/{name:.*}/tag").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
repo := r.Form.Get("repo")
|
|
|
|
tag := r.Form.Get("tag")
|
|
|
|
var force bool
|
|
|
|
if r.Form.Get("force") == "true" {
|
|
|
|
force = true
|
|
|
|
}
|
2013-04-23 01:37:22 +04:00
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
if err := rtime.repositories.Set(repo, tag, name, force); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-23 01:37:22 +04:00
|
|
|
return
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
w.WriteHeader(200)
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Path("/images/{name:.*}/pull").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
conn, _, err := w.(http.Hijacker).Hijack()
|
2013-04-23 01:37:22 +04:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2013-04-23 20:20:53 +04:00
|
|
|
file, err := conn.(*net.TCPConn).File()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2013-04-23 01:37:22 +04:00
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n")
|
|
|
|
if rtime.graph.LookupRemoteImage(name, rtime.authConfig) {
|
|
|
|
if err := rtime.graph.PullImage(file, name, rtime.authConfig); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := rtime.graph.PullRepository(file, name, "", rtime.repositories, rtime.authConfig); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
}
|
|
|
|
})
|
2013-04-23 01:37:22 +04:00
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
r.Path("/containers").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
var config Config
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, _, err := w.(http.Hijacker).Hijack()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
file, err := conn.(*net.TCPConn).File()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2013-04-23 01:37:22 +04:00
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n")
|
|
|
|
container, err := rtime.Create(&config)
|
|
|
|
if err != nil {
|
|
|
|
// If container not found, try to pull it
|
|
|
|
if rtime.graph.IsNotExist(err) {
|
|
|
|
fmt.Fprintf(file, "Image %s not found, trying to pull it from registry.\r\n", config.Image)
|
|
|
|
//if err = srv.CmdPull(stdin, stdout, config.Image); err != nil {
|
|
|
|
// fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//if container, err = srv.runtime.Create(config); err != nil {
|
|
|
|
// fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
return
|
2013-04-23 01:37:22 +04:00
|
|
|
}
|
|
|
|
}
|
2013-04-23 20:20:53 +04:00
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
)
|
|
|
|
if config.AttachStdin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer Debugf("Closing buffered stdin pipe")
|
|
|
|
io.Copy(w, file)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
}
|
|
|
|
if config.AttachStdout {
|
|
|
|
cStdout = file
|
|
|
|
}
|
|
|
|
if config.AttachStderr {
|
|
|
|
cStderr = file // FIXME: rcli can't differentiate stdout from stderr
|
2013-04-23 01:37:22 +04:00
|
|
|
}
|
|
|
|
|
2013-04-23 20:20:53 +04:00
|
|
|
attachErr := container.Attach(cStdin, file, cStdout, cStderr)
|
|
|
|
Debugf("Starting\n")
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cStdout == nil && cStderr == nil {
|
|
|
|
fmt.Fprintln(file, container.ShortId())
|
|
|
|
}
|
|
|
|
Debugf("Waiting for attach to return\n")
|
|
|
|
<-attachErr
|
|
|
|
// Expecting I/O pipe error, discarding
|
|
|
|
|
|
|
|
})
|
2013-04-23 01:37:22 +04:00
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers/{name:.*}/restart").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
if err := container.Restart(); err != nil {
|
|
|
|
http.Error(w, "Error restarting container "+name+": "+err.Error(), http.StatusInternalServerError)
|
2013-04-19 17:24:37 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 20:17:47 +04:00
|
|
|
return
|
2013-04-19 17:24:37 +04:00
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
w.WriteHeader(200)
|
2013-04-19 17:24:37 +04:00
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers/{name:.*}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
if err := rtime.Destroy(container); err != nil {
|
|
|
|
http.Error(w, "Error destroying container "+name+": "+err.Error(), http.StatusInternalServerError)
|
2013-04-19 17:24:37 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 20:17:47 +04:00
|
|
|
return
|
2013-04-19 17:24:37 +04:00
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
w.WriteHeader(200)
|
2013-04-19 17:24:37 +04:00
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/images/{name:.*}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-04-23 20:20:53 +04:00
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
img, err := rtime.repositories.LookupImage(name)
|
2013-04-19 17:24:37 +04:00
|
|
|
if err != nil {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such image: "+name, http.StatusNotFound)
|
2013-04-19 17:24:37 +04:00
|
|
|
return
|
2013-04-22 20:17:47 +04:00
|
|
|
} else {
|
|
|
|
if err := rtime.graph.Delete(img.Id); err != nil {
|
|
|
|
http.Error(w, "Error deleting image "+name+": "+err.Error(), http.StatusInternalServerError)
|
2013-04-19 17:24:37 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
w.WriteHeader(200)
|
2013-04-19 17:24:37 +04:00
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers/{name:.*}/start").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
http.Error(w, "Error starting container "+name+": "+err.Error(), http.StatusInternalServerError)
|
2013-04-19 17:24:37 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 20:17:47 +04:00
|
|
|
return
|
2013-04-19 17:24:37 +04:00
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
w.WriteHeader(200)
|
2013-04-19 17:24:37 +04:00
|
|
|
})
|
|
|
|
|
2013-04-22 20:17:47 +04:00
|
|
|
r.Path("/containers/{name:.*}/stop").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 20:20:53 +04:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 20:17:47 +04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
if err := container.Stop(); err != nil {
|
|
|
|
http.Error(w, "Error stopping container "+name+": "+err.Error(), http.StatusInternalServerError)
|
2013-04-19 17:24:37 +04:00
|
|
|
return
|
|
|
|
}
|
2013-04-18 20:56:22 +04:00
|
|
|
} else {
|
2013-04-24 16:01:40 +04:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 20:17:47 +04:00
|
|
|
return
|
2013-04-18 20:56:22 +04:00
|
|
|
}
|
2013-04-22 20:17:47 +04:00
|
|
|
w.WriteHeader(200)
|
2013-04-18 05:13:43 +04:00
|
|
|
})
|
2013-04-11 06:48:21 +04:00
|
|
|
|
2013-04-24 16:01:40 +04:00
|
|
|
r.Path("/containers/{name:.*}/wait").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
b, err := json.Marshal(ApiWait{container.Wait()})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-18 05:13:43 +04:00
|
|
|
return http.ListenAndServe(addr, r)
|
2013-04-11 06:48:21 +04:00
|
|
|
}
|