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-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-19 13:24:37 +00:00
|
|
|
"os"
|
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-05-06 09:31:22 +00:00
|
|
|
func hijackServer(w http.ResponseWriter) (*os.File, net.Conn, error) {
|
|
|
|
rwc, _, err := w.(http.Hijacker).Hijack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := rwc.(*net.TCPConn).File()
|
|
|
|
if err != nil {
|
|
|
|
return nil, rwc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the options to make sure the client sets the raw mode
|
|
|
|
rwc.Write([]byte{})
|
|
|
|
|
|
|
|
return file, rwc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpError(w http.ResponseWriter, err error) {
|
|
|
|
if strings.HasPrefix(err.Error(), "No such") {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListenAndServe(addr string, srv *Server) 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)
|
2013-05-06 09:31:22 +00:00
|
|
|
m := srv.DockerVersion()
|
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"]
|
2013-05-06 09:31:22 +00:00
|
|
|
if err := srv.ContainerKill(name); err != nil {
|
|
|
|
httpError(w, err)
|
2013-04-23 16:20:53 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-23 16:20:53 +00:00
|
|
|
}
|
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"]
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
file, rwc, err := hijackServer(w)
|
|
|
|
if file != nil {
|
2013-04-24 14:32:51 +00:00
|
|
|
defer file.Close()
|
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
if rwc != nil {
|
|
|
|
defer rwc.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
|
|
|
if err := srv.ContainerExport(name, file); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
}
|
2013-04-24 14:32:51 +00:00
|
|
|
})
|
|
|
|
|
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-05-06 09:31:22 +00:00
|
|
|
all := r.Form.Get("all")
|
|
|
|
filter := r.Form.Get("filter")
|
|
|
|
quiet := r.Form.Get("quiet")
|
2013-04-18 16:56:22 +00:00
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
outs, err := srv.Images(all, filter, quiet)
|
2013-04-18 16:56:22 +00:00
|
|
|
if err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
httpError(w, err)
|
2013-04-18 16:56:22 +00:00
|
|
|
}
|
|
|
|
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-05-06 09:31:22 +00:00
|
|
|
out := srv.DockerInfo()
|
2013-04-19 13:24:37 +00:00
|
|
|
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-05-06 09:31:22 +00:00
|
|
|
outs, err := srv.ImageHistory(name)
|
2013-04-19 13:24:37 +00:00
|
|
|
if err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
httpError(w, err)
|
2013-04-19 13:24:37 +00:00
|
|
|
}
|
|
|
|
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"]
|
2013-05-06 09:31:22 +00:00
|
|
|
changesStr, err := srv.ContainerChanges(name)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(changesStr)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-24 12:01:40 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +00:00
|
|
|
w.Write(b)
|
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)
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-05-06 09:31:22 +00:00
|
|
|
out, err := srv.ContainerPort(name, r.Form.Get("port"))
|
2013-04-22 16:17:47 +00:00
|
|
|
if err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
httpError(w, err)
|
2013-04-19 13:24:37 +00:00
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
b, err := json.Marshal(ApiPort{out})
|
2013-04-19 13:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
|
2013-04-19 13:24:37 +00:00
|
|
|
})
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
r.Path("/containers").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2013-04-24 14:06:03 +00:00
|
|
|
log.Println(r.Method, r.RequestURI)
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
all := r.Form.Get("all")
|
|
|
|
notrunc := r.Form.Get("notrunc")
|
|
|
|
quiet := r.Form.Get("quiet")
|
|
|
|
n, err := strconv.Atoi(r.Form.Get("n"))
|
2013-04-24 14:06:03 +00:00
|
|
|
if err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
n = -1
|
2013-04-24 14:06:03 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
outs := srv.Containers(all, notrunc, quiet, n)
|
|
|
|
b, err := json.Marshal(outs)
|
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)
|
|
|
|
}
|
|
|
|
repo := r.Form.Get("repo")
|
|
|
|
tag := r.Form.Get("tag")
|
2013-05-06 09:31:22 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-04-23 16:20:53 +00:00
|
|
|
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-05-06 09:31:22 +00:00
|
|
|
if err := srv.ContainerTag(name, repo, tag, force); err != nil {
|
2013-04-23 16:20:53 +00:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2013-04-30 15:04:31 +00:00
|
|
|
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)
|
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
|
|
|
|
src := r.Form.Get("fromSrc")
|
|
|
|
image := r.Form.Get("fromImage")
|
|
|
|
container := r.Form.Get("fromContainer")
|
2013-04-30 15:04:31 +00:00
|
|
|
repo := r.Form.Get("repo")
|
|
|
|
tag := r.Form.Get("tag")
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
if container != "" { //commit
|
|
|
|
var config Config
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
author := r.Form.Get("author")
|
|
|
|
comment := r.Form.Get("comment")
|
2013-04-30 15:04:31 +00:00
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
id, err := srv.ContainerCommit(container, repo, tag, author, comment, &config)
|
2013-04-30 15:04:31 +00:00
|
|
|
if err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
httpError(w, err)
|
2013-04-30 15:04:31 +00:00
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
b, err := json.Marshal(ApiId{id})
|
2013-04-30 15:04:31 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-05-06 09:31:22 +00:00
|
|
|
} else {
|
|
|
|
w.Write(b)
|
2013-04-30 15:04:31 +00:00
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
} else if image != "" || src != "" {
|
|
|
|
file, rwc, err := hijackServer(w)
|
|
|
|
if file != nil {
|
|
|
|
defer file.Close()
|
|
|
|
}
|
|
|
|
if rwc != nil {
|
|
|
|
defer rwc.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
2013-04-30 15:04:31 +00:00
|
|
|
return
|
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
2013-04-30 15:04:31 +00:00
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
if image != "" { //pull
|
|
|
|
if err := srv.ImagePull(image, file); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
}
|
|
|
|
} else { //import
|
|
|
|
if err := srv.ImageImport(src, repo, tag, file); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2013-04-30 15:04:31 +00:00
|
|
|
})
|
|
|
|
|
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
|
|
|
|
}
|
2013-05-06 09:31:22 +00:00
|
|
|
id, memoryW, swapW, err := srv.ContainerCreate(config)
|
2013-04-23 16:20:53 +00:00
|
|
|
if err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
httpError(w, err)
|
2013-04-23 16:20:53 +00:00
|
|
|
return
|
|
|
|
}
|
2013-05-02 16:36:23 +00:00
|
|
|
var out ApiRun
|
2013-05-06 09:31:22 +00:00
|
|
|
out.Id = id
|
2013-05-02 16:36:23 +00:00
|
|
|
if memoryW {
|
|
|
|
out.Warnings = append(out.Warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
|
|
|
|
}
|
|
|
|
if swapW {
|
|
|
|
out.Warnings = append(out.Warnings, "Your kernel does not support memory swap capabilities. Limitation discarded.")
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(out)
|
2013-05-02 03:07:06 +00:00
|
|
|
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-05-02 16:36:23 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
t, err := strconv.Atoi(r.Form.Get("t"))
|
|
|
|
if err != nil || t < 0 {
|
|
|
|
t = 10
|
|
|
|
}
|
2013-04-22 16:17:47 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-05-06 09:31:22 +00:00
|
|
|
if err := srv.ContainerRestart(name, t); err != nil {
|
|
|
|
httpError(w, err)
|
2013-04-19 13:24:37 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +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-05-06 09:52:15 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2013-04-22 16:17:47 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-05-06 09:52:15 +00:00
|
|
|
var v bool
|
|
|
|
if r.Form.Get("v") == "1" {
|
|
|
|
v = true
|
|
|
|
}
|
|
|
|
if err := srv.ContainerDestroy(name, v); err != nil {
|
2013-05-06 09:31:22 +00:00
|
|
|
httpError(w, err)
|
2013-04-19 13:24:37 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +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-05-06 09:31:22 +00:00
|
|
|
if err := srv.ImageDelete(name); err != nil {
|
|
|
|
httpError(w, err)
|
2013-04-22 16:17:47 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +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"]
|
2013-05-06 09:31:22 +00:00
|
|
|
if err := srv.ContainerStart(name); err != nil {
|
|
|
|
httpError(w, err)
|
2013-04-19 13:24:37 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +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-05-02 16:36:23 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
t, err := strconv.Atoi(r.Form.Get("t"))
|
|
|
|
if err != nil || t < 0 {
|
|
|
|
t = 10
|
|
|
|
}
|
2013-04-22 16:17:47 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["name"]
|
2013-05-06 09:31:22 +00:00
|
|
|
|
|
|
|
if err := srv.ContainerStop(name, t); err != nil {
|
|
|
|
httpError(w, err)
|
2013-04-18 16:56:22 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2013-04-18 16:56:22 +00:00
|
|
|
}
|
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"]
|
2013-05-06 09:31:22 +00:00
|
|
|
status, err := srv.ContainerWait(name)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(ApiWait{status})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2013-04-24 12:01:40 +00:00
|
|
|
} else {
|
2013-05-06 09:31:22 +00:00
|
|
|
w.Write(b)
|
2013-04-24 12:01:40 +00:00
|
|
|
}
|
|
|
|
})
|
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"]
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
file, rwc, err := hijackServer(w)
|
|
|
|
if file != nil {
|
2013-05-02 03:07:06 +00:00
|
|
|
defer file.Close()
|
2013-05-06 09:31:22 +00:00
|
|
|
}
|
|
|
|
if rwc != nil {
|
|
|
|
defer rwc.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-02 03:07:06 +00:00
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
|
|
|
if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, file); err != nil {
|
|
|
|
fmt.Fprintln(file, "Error: "+err.Error())
|
2013-05-02 03:07:06 +00:00
|
|
|
}
|
|
|
|
})
|
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"]
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
container, err := srv.ContainerInspect(name)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(container)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
2013-04-29 15:46:41 +00: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"]
|
|
|
|
|
2013-05-06 09:31:22 +00:00
|
|
|
image, err := srv.ImageInspect(name)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(image)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
w.Write(b)
|
2013-04-29 15:46:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-18 01:13:43 +00:00
|
|
|
return http.ListenAndServe(addr, r)
|
2013-04-11 02:48:21 +00:00
|
|
|
}
|