refactoring run/attach/logs

This commit is contained in:
Victor Vieux 2013-05-02 05:07:06 +02:00
parent 36b968bb09
commit a4bcf7e1ac
3 changed files with 217 additions and 247 deletions

283
api.go
View File

@ -6,7 +6,6 @@ import (
"fmt"
"github.com/gorilla/mux"
"io"
"io/ioutil"
"log"
"net"
"net/http"
@ -73,7 +72,7 @@ func ListenAndServe(addr string, rtime *Runtime) error {
}
defer file.Close()
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\n\r\n")
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
// 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())
@ -207,52 +206,6 @@ func ListenAndServe(addr string, rtime *Runtime) error {
}
})
r.Path("/containers/{name:.*}/logs").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 {
var out ApiLogs
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 {
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)
@ -368,7 +321,7 @@ func ListenAndServe(addr string, rtime *Runtime) error {
return
}
b, err := json.Marshal(ApiCommit{img.ShortId()})
b, err := json.Marshal(ApiId{img.ShortId()})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
@ -415,7 +368,7 @@ func ListenAndServe(addr string, rtime *Runtime) error {
}
defer file.Close()
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n")
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\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())
@ -506,146 +459,21 @@ func ListenAndServe(addr string, rtime *Runtime) error {
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()
if 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
// or tell the client there is no options
conn.Write([]byte{})
fmt.Fprintln(file, "HTTP/1.1 201 Created\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 rtime.graph.LookupRemoteImage(config.Image, rtime.authConfig) {
if err := rtime.graph.PullImage(file, config.Image, rtime.authConfig); err != nil {
fmt.Fprintln(file, "Error: "+err.Error())
return
}
} else if err := rtime.graph.PullRepository(file, config.Image, "", rtime.repositories, rtime.authConfig); err != nil {
fmt.Fprintln(file, "Error: "+err.Error())
return
}
if container, err = rtime.Create(&config); err != nil {
fmt.Fprintln(file, "Error: "+err.Error())
return
}
http.Error(w, "No such image: "+config.Image, http.StatusNotFound)
} else {
fmt.Fprintln(file, "Error: "+err.Error())
return
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
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: api can't differentiate stdout from stderr
}
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
// If we are in stdinonce mode, wait for the process to end
// otherwise, simply return
if config.StdinOnce && !config.Tty {
container.Wait()
}
})
r.Path("/containers/{name:.*}/attach").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 {
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: application/json\r\n\r\n")
r, w := io.Pipe()
go func() {
defer w.Close()
defer Debugf("Closing buffered stdin pipe")
io.Copy(w, file)
}()
cStdin := r
<-container.Attach(cStdin, nil, file, file)
// Expecting I/O pipe error, discarding
b, err := json.Marshal(ApiId{container.ShortId()})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
http.Error(w, "No such container: "+name, http.StatusNotFound)
return
w.Write(b)
}
})
@ -749,6 +577,99 @@ func ListenAndServe(addr string, rtime *Runtime) error {
}
})
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)
}
})
r.Path("/containers/{name:.*}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.RequestURI)
vars := mux.Vars(r)

View File

@ -30,15 +30,10 @@ type ApiContainers struct {
Status string `json:",omitempty"`
}
type ApiCommit struct {
type ApiId struct {
Id string
}
type ApiLogs struct {
Stdout string
Stderr string
}
type ApiPort struct {
Port string
}

View File

@ -210,7 +210,7 @@ func CmdWait(args []string) error {
return nil
}
for _, name := range cmd.Args() {
body, err := call("POST", "/containers/"+name+"/wait")
body, _, err := call("POST", "/containers/"+name+"/wait", nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -236,7 +236,7 @@ func CmdVersion(args []string) error {
return nil
}
body, err := call("GET", "/version")
body, _, err := call("GET", "/version", nil)
if err != nil {
return err
}
@ -266,7 +266,7 @@ func CmdInfo(args []string) error {
return nil
}
body, err := call("GET", "/info")
body, _, err := call("GET", "/info", nil)
if err != nil {
return err
}
@ -295,7 +295,7 @@ func CmdStop(args []string) error {
}
for _, name := range args {
_, err := call("POST", "/containers/"+name+"/stop")
_, _, err := call("POST", "/containers/"+name+"/stop", nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -316,7 +316,7 @@ func CmdRestart(args []string) error {
}
for _, name := range args {
_, err := call("POST", "/containers/"+name+"/restart")
_, _, err := call("POST", "/containers/"+name+"/restart", nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -337,7 +337,7 @@ func CmdStart(args []string) error {
}
for _, name := range args {
_, err := call("POST", "/containers/"+name+"/start")
_, _, err := call("POST", "/containers/"+name+"/start", nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -356,9 +356,9 @@ func CmdInspect(args []string) error {
cmd.Usage()
return nil
}
obj, err := call("GET", "/containers/"+cmd.Arg(0))
obj, _, err := call("GET", "/containers/"+cmd.Arg(0), nil)
if err != nil {
obj, err = call("GET", "/images/"+cmd.Arg(0))
obj, _, err = call("GET", "/images/"+cmd.Arg(0), nil)
if err != nil {
return err
}
@ -378,7 +378,7 @@ func CmdPort(args []string) error {
}
v := url.Values{}
v.Set("port", cmd.Arg(1))
body, err := call("GET", "/containers/"+cmd.Arg(0)+"/port?"+v.Encode())
body, _, err := call("GET", "/containers/"+cmd.Arg(0)+"/port?"+v.Encode(), nil)
if err != nil {
return err
}
@ -404,7 +404,7 @@ func CmdRmi(args []string) error {
}
for _, name := range args {
_, err := call("DELETE", "/images/"+name)
_, _, err := call("DELETE", "/images/"+name, nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -424,7 +424,7 @@ func CmdHistory(args []string) error {
return nil
}
body, err := call("GET", "/images/"+cmd.Arg(0)+"/history")
body, _, err := call("GET", "/images/"+cmd.Arg(0)+"/history", nil)
if err != nil {
return err
}
@ -455,7 +455,7 @@ func CmdRm(args []string) error {
}
for _, name := range args {
_, err := call("DELETE", "/containers/"+name)
_, _, err := call("DELETE", "/containers/"+name, nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -477,7 +477,7 @@ func CmdKill(args []string) error {
}
for _, name := range args {
_, err := call("POST", "/containers/"+name+"/kill")
_, _, err := call("POST", "/containers/"+name+"/kill", nil)
if err != nil {
fmt.Printf("%s", err)
} else {
@ -504,7 +504,7 @@ func CmdImport(args []string) error {
v.Set("tag", tag)
v.Set("src", src)
err := callStream("POST", "/images?"+v.Encode(), nil, false)
err := hijack("POST", "/images?"+v.Encode(), false)
if err != nil {
return err
}
@ -583,7 +583,7 @@ func CmdPull(args []string) error {
return nil
}
if err := callStream("POST", "/images/"+cmd.Arg(0)+"/pull", nil, false); err != nil {
if err := hijack("POST", "/images/"+cmd.Arg(0)+"/pull", false); err != nil {
return err
}
@ -613,7 +613,7 @@ func CmdImages(args []string) error {
v.Set("all", "1")
}
body, err := call("GET", "/images?"+v.Encode())
body, _, err := call("GET", "/images?"+v.Encode(), nil)
if err != nil {
return err
}
@ -670,7 +670,7 @@ func CmdPs(args []string) error {
v.Set("n", strconv.Itoa(*last))
}
body, err := call("GET", "/containers?"+v.Encode())
body, _, err := call("GET", "/containers?"+v.Encode(), nil)
if err != nil {
return err
}
@ -715,12 +715,12 @@ func CmdCommit(args []string) error {
v.Set("tag", tag)
v.Set("comment", *flComment)
body, err := call("POST", "/containers/"+name+"/commit?"+v.Encode())
body, _, err := call("POST", "/containers/"+name+"/commit?"+v.Encode(), nil)
if err != nil {
return err
}
var out ApiCommit
var out ApiId
err = json.Unmarshal(body, &out)
if err != nil {
return err
@ -741,7 +741,7 @@ func CmdExport(args []string) error {
return nil
}
if err := callStream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, false); err != nil {
if err := hijack("GET", "/containers/"+cmd.Arg(0)+"/export", false); err != nil {
return err
}
return nil
@ -757,7 +757,7 @@ func CmdDiff(args []string) error {
return nil
}
body, err := call("GET", "/containers/"+cmd.Arg(0)+"/changes")
body, _, err := call("GET", "/containers/"+cmd.Arg(0)+"/changes", nil)
if err != nil {
return err
}
@ -782,19 +782,15 @@ func CmdLogs(args []string) error {
cmd.Usage()
return nil
}
body, err := call("GET", "/containers/"+cmd.Arg(0)+"/logs")
if err != nil {
v := url.Values{}
v.Set("logs", "1")
v.Set("stdout", "1")
v.Set("stderr", "1")
if err := hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), false); err != nil {
return err
}
var out ApiLogs
err = json.Unmarshal(body, &out)
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, out.Stdout)
fmt.Fprintln(os.Stderr, out.Stderr)
return nil
}
@ -808,7 +804,7 @@ func CmdAttach(args []string) error {
return nil
}
body, err := call("GET", "/containers/"+cmd.Arg(0))
body, _, err := call("GET", "/containers/"+cmd.Arg(0), nil)
if err != nil {
return err
}
@ -819,7 +815,14 @@ func CmdAttach(args []string) error {
return err
}
if err := callStream("POST", "/containers/"+cmd.Arg(0)+"/attach", nil, container.Config.Tty); err != nil {
v := url.Values{}
v.Set("logs", "1")
v.Set("stream", "1")
v.Set("stdout", "1")
v.Set("stderr", "1")
v.Set("stdin", "1")
if err := hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), container.Config.Tty); err != nil {
return err
}
return nil
@ -903,7 +906,7 @@ func CmdTag(args []string) error {
v.Set("force", "1")
}
if err := callStream("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil, false); err != nil {
if _, _, err := call("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil); err != nil {
return err
}
return nil
@ -923,54 +926,105 @@ func CmdRun(args []string) error {
return nil
}
if err := callStream("POST", "/containers", *config, config.Tty); err != nil {
//create the container
body, statusCode, err := call("POST", "/containers", *config)
//if image not found try to pull it
if statusCode == 404 {
err = hijack("POST", "/images/"+config.Image+"/pull", false)
if err != nil {
return err
}
body, _, err = call("POST", "/containers", *config)
}
if err != nil {
return err
}
var out ApiId
err = json.Unmarshal(body, &out)
if err != nil {
return err
}
v := url.Values{}
v.Set("logs", "1")
v.Set("stream", "1")
if config.AttachStdin {
v.Set("stdin", "1")
}
if config.AttachStdout {
v.Set("stdout", "1")
}
if config.AttachStderr {
v.Set("stderr", "1")
}
/*
attach := Go(func() error {
err := hijack("POST", "/containers/"+out.Id+"/attach?"+v.Encode(), config.Tty)
return err
})*/
//start the container
_, _, err = call("POST", "/containers/"+out.Id+"/start", nil)
if err != nil {
return err
}
if err := hijack("POST", "/containers/"+out.Id+"/attach?"+v.Encode(), config.Tty); err != nil {
return err
}
/*
if err := <-attach; err != nil {
return err
}
*/
return nil
}
func call(method, path string) ([]byte, error) {
req, err := http.NewRequest(method, "http://0.0.0.0:4243"+path, nil)
if err != nil {
return nil, err
func call(method, path string, data interface{}) ([]byte, int, error) {
var params io.Reader
if data != nil {
buf, err := json.Marshal(data)
if err != nil {
return nil, -1, err
}
params = bytes.NewBuffer(buf)
}
if method == "POST" {
req, err := http.NewRequest(method, "http://0.0.0.0:4243"+path, params)
if err != nil {
return nil, -1, err
}
if data != nil {
req.Header.Set("Content-Type", "application/json")
} else if method == "POST" {
req.Header.Set("Content-Type", "plain/text")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
return nil, -1, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, -1, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error: %s", body)
return nil, resp.StatusCode, fmt.Errorf("error: %s", body)
}
return body, nil
return body, resp.StatusCode, nil
}
func callStream(method, path string, data interface{}, setRawTerminal bool) error {
var body io.Reader
if data != nil {
buf, err := json.Marshal(data)
if err != nil {
return err
}
body = bytes.NewBuffer(buf)
}
req, err := http.NewRequest(method, path, body)
func hijack(method, path string, setRawTerminal bool) error {
req, err := http.NewRequest(method, path, nil)
if err != nil {
return err
}
if data != nil {
req.Header.Set("Content-Type", "application/json")
}
dial, err := net.Dial("tcp", "0.0.0.0:4243")
if err != nil {
return err