2013-04-11 02:48:21 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-04-23 16:20:53 +00:00
|
|
|
_ "bytes"
|
2013-04-18 01:13:43 +00:00
|
|
|
"encoding/json"
|
2013-04-19 13:24:37 +00:00
|
|
|
"fmt"
|
2013-04-11 02:48:21 +00:00
|
|
|
"github.com/gorilla/mux"
|
2013-04-23 16:20:53 +00:00
|
|
|
"io"
|
2013-04-18 16:56:22 +00:00
|
|
|
"log"
|
2013-04-23 16:20:53 +00:00
|
|
|
"net"
|
2013-04-11 02:48:21 +00:00
|
|
|
"net/http"
|
2013-04-30 15:04:31 +00:00
|
|
|
"net/url"
|
2013-04-19 13:24:37 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2013-04-22 16:17:47 +00:00
|
|
|
"strconv"
|
2013-04-19 13:24:37 +00:00
|
|
|
"strings"
|
2013-04-11 02:48:21 +00:00
|
|
|
)
|
|
|
|
|
2013-04-19 13:24:37 +00:00
|
|
|
func ListenAndServe(addr string, rtime *Runtime) error {
|
2013-04-18 01:13:43 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
log.Printf("Listening for HTTP on %s\n", addr)
|
2013-04-11 02:48:21 +00:00
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/version").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
m := ApiVersion{VERSION, GIT_COMMIT, NO_MEMORY_LIMIT}
|
2013-04-18 01:13:43 +00:00
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2013-04-22 16:17:47 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-18 01:13:43 +00:00
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
2013-04-11 02:48:21 +00:00
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/containers/{name:.*}/kill").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00:00
|
|
|
vars := mux.Vars(r)
|
2013-04-23 16:20:53 +00: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 12:01:40 +00:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-23 16:20:53 +00:00
|
|
|
return
|
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-24 12:01:40 +00:00
|
|
|
})
|
|
|
|
|
2013-04-24 14:32:51 +00:00
|
|
|
r.Path("/containers/{name:.*}/export").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 {
|
|
|
|
|
|
|
|
data, err := container.Export()
|
|
|
|
if 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-05-02 03:07:06 +00:00
|
|
|
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
2013-04-24 14:32:51 +00:00
|
|
|
// Stream the entire contents of the container (basically a volatile snapshot)
|
|
|
|
if _, err := io.Copy(file, data); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/images").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-04-22 16:17:47 +00:00
|
|
|
All := r.Form.Get("all")
|
2013-04-23 16:20:53 +00:00
|
|
|
NameFilter := r.Form.Get("filter")
|
|
|
|
Quiet := r.Form.Get("quiet")
|
2013-04-18 16:56:22 +00:00
|
|
|
|
|
|
|
var allImages map[string]*Image
|
|
|
|
var err error
|
2013-04-26 13:08:33 +00:00
|
|
|
if All == "1" {
|
2013-04-19 13:24:37 +00:00
|
|
|
allImages, err = rtime.graph.Map()
|
2013-04-18 16:56:22 +00:00
|
|
|
} else {
|
2013-04-19 13:24:37 +00:00
|
|
|
allImages, err = rtime.graph.Heads()
|
2013-04-18 16:56:22 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
2013-04-30 15:04:31 +00:00
|
|
|
var outs []ApiImages = []ApiImages{} //produce [] when empty instead of 'null'
|
2013-04-19 13:24:37 +00:00
|
|
|
for name, repository := range rtime.repositories.Repositories {
|
2013-04-22 16:17:47 +00:00
|
|
|
if NameFilter != "" && name != NameFilter {
|
2013-04-18 16:56:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for tag, id := range repository {
|
2013-04-23 16:20:53 +00:00
|
|
|
var out ApiImages
|
2013-04-19 13:24:37 +00:00
|
|
|
image, err := rtime.graph.Get(id)
|
2013-04-18 16:56:22 +00: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-26 13:08:33 +00:00
|
|
|
if Quiet != "1" {
|
2013-04-18 16:56:22 +00:00
|
|
|
out.Repository = name
|
|
|
|
out.Tag = tag
|
|
|
|
out.Id = TruncateId(id)
|
2013-04-30 15:04:31 +00:00
|
|
|
out.Created = image.Created.Unix()
|
2013-04-18 16:56:22 +00:00
|
|
|
} else {
|
|
|
|
out.Id = image.ShortId()
|
|
|
|
}
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Display images which aren't part of a
|
2013-04-22 16:17:47 +00:00
|
|
|
if NameFilter == "" {
|
2013-04-18 16:56:22 +00:00
|
|
|
for id, image := range allImages {
|
2013-04-23 16:20:53 +00:00
|
|
|
var out ApiImages
|
2013-04-26 13:08:33 +00:00
|
|
|
if Quiet != "1" {
|
2013-04-18 16:56:22 +00:00
|
|
|
out.Repository = "<none>"
|
|
|
|
out.Tag = "<none>"
|
|
|
|
out.Id = TruncateId(id)
|
2013-04-30 15:04:31 +00:00
|
|
|
out.Created = image.Created.Unix()
|
2013-04-18 16:56:22 +00:00
|
|
|
} else {
|
|
|
|
out.Id = image.ShortId()
|
|
|
|
}
|
|
|
|
outs = append(outs, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(outs)
|
|
|
|
if err != nil {
|
2013-04-19 13:24:37 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/info").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-19 13:24:37 +00:00
|
|
|
images, _ := rtime.graph.All()
|
|
|
|
var imgcount int
|
|
|
|
if images == nil {
|
|
|
|
imgcount = 0
|
|
|
|
} else {
|
|
|
|
imgcount = len(images)
|
|
|
|
}
|
2013-04-23 16:20:53 +00:00
|
|
|
var out ApiInfo
|
2013-04-19 13:24:37 +00: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 16:17:47 +00:00
|
|
|
r.Path("/images/{name:.*}/history").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-04-19 13:24:37 +00:00
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
image, err := rtime.repositories.LookupImage(name)
|
2013-04-19 13:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2013-04-30 15:04:31 +00:00
|
|
|
|
|
|
|
var outs []ApiHistory = []ApiHistory{} //produce [] when empty instead of 'null'
|
2013-04-19 13:24:37 +00:00
|
|
|
err = image.WalkHistory(func(img *Image) error {
|
2013-04-23 16:20:53 +00:00
|
|
|
var out ApiHistory
|
2013-04-19 13:24:37 +00:00
|
|
|
out.Id = rtime.repositories.ImageName(img.ShortId())
|
2013-04-30 15:04:31 +00:00
|
|
|
out.Created = img.Created.Unix()
|
2013-04-19 13:24:37 +00:00
|
|
|
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-24 12:01:40 +00:00
|
|
|
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 13:24:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-23 16:20:53 +00: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 12:01:40 +00:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-23 16:20:53 +00: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 16:17:47 +00:00
|
|
|
r.Path("/containers").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-04-22 16:17:47 +00:00
|
|
|
All := r.Form.Get("all")
|
2013-04-23 16:20:53 +00:00
|
|
|
NoTrunc := r.Form.Get("notrunc")
|
|
|
|
Quiet := r.Form.Get("quiet")
|
2013-04-22 16:17:47 +00:00
|
|
|
Last := r.Form.Get("n")
|
|
|
|
n, err := strconv.Atoi(Last)
|
|
|
|
if err != nil {
|
|
|
|
n = -1
|
|
|
|
}
|
2013-04-30 15:04:31 +00:00
|
|
|
var outs []ApiContainers = []ApiContainers{} //produce [] when empty instead of 'null'
|
2013-04-19 13:24:37 +00:00
|
|
|
for i, container := range rtime.List() {
|
2013-04-26 13:08:33 +00:00
|
|
|
if !container.State.Running && All != "1" && n == -1 {
|
2013-04-19 13:24:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-04-22 16:17:47 +00:00
|
|
|
if i == n {
|
2013-04-19 13:24:37 +00:00
|
|
|
break
|
|
|
|
}
|
2013-04-23 16:20:53 +00:00
|
|
|
var out ApiContainers
|
2013-04-19 13:24:37 +00:00
|
|
|
out.Id = container.ShortId()
|
2013-04-26 13:08:33 +00:00
|
|
|
if Quiet != "1" {
|
2013-04-19 13:24:37 +00:00
|
|
|
command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
|
2013-04-26 13:08:33 +00:00
|
|
|
if NoTrunc != "1" {
|
2013-04-19 13:24:37 +00:00
|
|
|
command = Trunc(command, 20)
|
|
|
|
}
|
|
|
|
out.Image = rtime.repositories.ImageName(container.Image)
|
|
|
|
out.Command = command
|
2013-04-30 15:04:31 +00:00
|
|
|
out.Created = container.Created.Unix()
|
2013-04-19 13:24:37 +00:00
|
|
|
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 14:06:03 +00: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
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:07:06 +00:00
|
|
|
b, err := json.Marshal(ApiId{img.ShortId()})
|
2013-04-24 14:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-23 16:20:53 +00: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
|
2013-04-26 13:08:33 +00:00
|
|
|
if r.Form.Get("force") == "1" {
|
2013-04-23 16:20:53 +00:00
|
|
|
force = true
|
|
|
|
}
|
2013-04-22 21:37:22 +00:00
|
|
|
|
2013-04-23 16:20:53 +00:00
|
|
|
if err := rtime.repositories.Set(repo, tag, name, force); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-22 21:37:22 +00:00
|
|
|
return
|
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
2013-04-23 16:20:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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-22 21:37:22 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2013-04-23 16:20:53 +00:00
|
|
|
file, err := conn.(*net.TCPConn).File()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2013-04-22 21:37:22 +00:00
|
|
|
|
2013-05-02 03:07:06 +00:00
|
|
|
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
2013-04-23 16:20:53 +00:00
|
|
|
if rtime.graph.LookupRemoteImage(name, rtime.authConfig) {
|
2013-04-29 13:12:18 +00:00
|
|
|
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 16:20:53 +00:00
|
|
|
})
|
2013-04-22 21:37:22 +00:00
|
|
|
|
2013-04-30 15:04:31 +00:00
|
|
|
/* /!\ W.I.P /!\ */
|
|
|
|
r.Path("/images").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)
|
|
|
|
}
|
|
|
|
src := r.Form.Get("src")
|
|
|
|
repo := r.Form.Get("repo")
|
|
|
|
tag := r.Form.Get("tag")
|
|
|
|
|
|
|
|
var archive io.Reader
|
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
fmt.Fprintln(file, "HTTP/1.1 201 Created\r\nContent-Type: application/json\r\n\r\n")
|
|
|
|
if src == "-" {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer Debugf("Closing buffered stdin pipe")
|
|
|
|
io.Copy(w, file)
|
|
|
|
}()
|
|
|
|
archive = r
|
|
|
|
} else {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
|
|
|
fmt.Fprintln(file, "Downloading from", u)
|
|
|
|
// Download with curl (pretty progress bar)
|
|
|
|
// If curl is not available, fallback to http.Get()
|
|
|
|
resp, err = Download(u.String(), file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
archive = ProgressReader(resp.Body, int(resp.ContentLength), file)
|
|
|
|
}
|
|
|
|
img, err := rtime.graph.Create(archive, nil, "Imported from "+src)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Optionally register the image at REPO/TAG
|
|
|
|
if repo != "" {
|
|
|
|
if err := rtime.repositories.Set(repo, tag, img.Id, true); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(file, img.ShortId())
|
|
|
|
})
|
|
|
|
|
2013-04-23 16:20:53 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := rtime.Create(&config)
|
|
|
|
if err != nil {
|
|
|
|
if rtime.graph.IsNotExist(err) {
|
2013-05-02 03:07:06 +00:00
|
|
|
http.Error(w, "No such image: "+config.Image, http.StatusNotFound)
|
2013-04-23 16:20:53 +00:00
|
|
|
} else {
|
2013-05-02 03:07:06 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-22 21:37:22 +00:00
|
|
|
}
|
2013-04-23 16:20:53 +00:00
|
|
|
return
|
|
|
|
}
|
2013-04-26 13:08:33 +00:00
|
|
|
|
2013-05-02 03:07:06 +00:00
|
|
|
b, err := json.Marshal(ApiId{container.ShortId()})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-26 13:08:33 +00:00
|
|
|
} else {
|
2013-05-02 03:07:06 +00:00
|
|
|
w.Write(b)
|
2013-04-26 13:08:33 +00:00
|
|
|
}
|
2013-04-23 16:20:53 +00:00
|
|
|
})
|
2013-04-22 21:37:22 +00:00
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/containers/{name:.*}/restart").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00: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 13:24:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 12:01:40 +00:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 16:17:47 +00:00
|
|
|
return
|
2013-04-19 13:24:37 +00:00
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-19 13:24:37 +00:00
|
|
|
})
|
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/containers/{name:.*}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00: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 13:24:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 12:01:40 +00:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 16:17:47 +00:00
|
|
|
return
|
2013-04-19 13:24:37 +00:00
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-19 13:24:37 +00:00
|
|
|
})
|
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/images/{name:.*}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-04-23 16:20:53 +00:00
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
img, err := rtime.repositories.LookupImage(name)
|
2013-04-19 13:24:37 +00:00
|
|
|
if err != nil {
|
2013-04-24 12:01:40 +00:00
|
|
|
http.Error(w, "No such image: "+name, http.StatusNotFound)
|
2013-04-19 13:24:37 +00:00
|
|
|
return
|
2013-04-22 16:17:47 +00: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 13:24:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-19 13:24:37 +00:00
|
|
|
})
|
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/containers/{name:.*}/start").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00: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 13:24:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-24 12:01:40 +00:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 16:17:47 +00:00
|
|
|
return
|
2013-04-19 13:24:37 +00:00
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-19 13:24:37 +00:00
|
|
|
})
|
|
|
|
|
2013-04-22 16:17:47 +00:00
|
|
|
r.Path("/containers/{name:.*}/stop").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-23 16:20:53 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
2013-04-22 16:17:47 +00: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 13:24:37 +00:00
|
|
|
return
|
|
|
|
}
|
2013-04-18 16:56:22 +00:00
|
|
|
} else {
|
2013-04-24 12:01:40 +00:00
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
2013-04-22 16:17:47 +00:00
|
|
|
return
|
2013-04-18 16:56:22 +00:00
|
|
|
}
|
2013-04-29 15:46:41 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-18 01:13:43 +00:00
|
|
|
})
|
2013-04-11 02:48:21 +00:00
|
|
|
|
2013-04-24 12:01:40 +00: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-05-02 03:07:06 +00:00
|
|
|
|
|
|
|
r.Path("/containers/{name:.*}/attach").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)
|
|
|
|
}
|
|
|
|
logs := r.Form.Get("logs")
|
|
|
|
stream := r.Form.Get("stream")
|
|
|
|
stdin := r.Form.Get("stdin")
|
|
|
|
stdout := r.Form.Get("stdout")
|
|
|
|
stderr := r.Form.Get("stderr")
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
|
|
|
|
|
|
|
if container := rtime.Get(name); container != nil {
|
|
|
|
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()
|
|
|
|
if container.Config.Tty {
|
|
|
|
oldState, err := SetRawTerminal()
|
|
|
|
if err != nil {
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
log.Printf("Can't set the terminal in raw mode: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defer RestoreTerminal(oldState)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// Flush the options to make sure the client sets the raw mode
|
|
|
|
conn.Write([]byte{})
|
|
|
|
|
|
|
|
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
|
|
|
//logs
|
|
|
|
if logs == "1" {
|
|
|
|
if stdout == "1" {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
|
|
|
Debugf(err.Error())
|
|
|
|
} else if _, err := io.Copy(file, cLog); err != nil {
|
|
|
|
Debugf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stderr == "1" {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
|
|
|
Debugf(err.Error())
|
|
|
|
} else if _, err := io.Copy(file, cLog); err != nil {
|
|
|
|
Debugf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//stream
|
|
|
|
if stream == "1" {
|
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
cStdinCloser io.Closer
|
|
|
|
)
|
|
|
|
|
|
|
|
if stdin == "1" {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer Debugf("Closing buffered stdin pipe")
|
|
|
|
io.Copy(w, file)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
cStdinCloser = file
|
|
|
|
}
|
|
|
|
if stdout == "1" {
|
|
|
|
cStdout = file
|
|
|
|
}
|
|
|
|
if stderr == "1" {
|
|
|
|
cStderr = file
|
|
|
|
}
|
|
|
|
|
|
|
|
<-container.Attach(cStdin, cStdinCloser, cStdout, cStderr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(w, "No such container: "+name, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
})
|
2013-04-24 12:01:40 +00:00
|
|
|
|
2013-04-29 15:46:41 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2013-04-18 01:13:43 +00:00
|
|
|
return http.ListenAndServe(addr, r)
|
2013-04-11 02:48:21 +00:00
|
|
|
}
|