From 6ce475dbdf97fd5e721a150c58d92d6819e5decb Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 22 Apr 2013 23:37:22 +0200 Subject: [PATCH] added push hijack (wip) --- api.go | 34 ++++++++++++++++++++++++++++++++++ api_params.go | 4 ++++ commands.go | 12 ++++++------ registry.go | 11 ++++++----- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/api.go b/api.go index f5c6acf53f..fdf87f3f15 100644 --- a/api.go +++ b/api.go @@ -1,6 +1,7 @@ package docker import ( + _"bytes" "encoding/json" "fmt" "github.com/gorilla/mux" @@ -251,6 +252,39 @@ func ListenAndServe(addr string, rtime *Runtime) error { } }) + r.Path("/pull").Methods("GET", "POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var in PullIn + //json.NewDecoder(r.Body).Decode(&in) + in.Name = "base" + + hj, ok := w.(http.Hijacker) + if !ok { + http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) + return + } + conn, bufrw, err := hj.Hijack() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + // Don't forget to close the connection: + defer conn.Close() + + + + if rtime.graph.LookupRemoteImage(in.Name, rtime.authConfig) { + if err := rtime.graph.PullImage(bufrw, in.Name, rtime.authConfig); err != nil { + //http.Error(w, err.Error(), http.StatusInternalServerError) + } + return + } + if err := rtime.graph.PullRepository(bufrw, in.Name, "", rtime.repositories, rtime.authConfig); err != nil { + //http.Error(w, err.Error(), http.StatusInternalServerError) + } + return + }) + + r.Path("/containers/{name:.*}/restart").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Println(r.RequestURI) vars := mux.Vars(r) diff --git a/api_params.go b/api_params.go index 4a2bbee139..8f8b332a09 100644 --- a/api_params.go +++ b/api_params.go @@ -51,6 +51,10 @@ type PsOut struct { Status string `json:",omitempty"` } +type PullIn struct { + Name string +} + type LogsIn struct { Name string } diff --git a/commands.go b/commands.go index 8bcbec551e..e9b0701580 100644 --- a/commands.go +++ b/commands.go @@ -613,15 +613,15 @@ func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string // FIXME: CmdPull should be a wrapper around Runtime.Pull() if srv.runtime.graph.LookupRemoteImage(remote, srv.runtime.authConfig) { - if err := srv.runtime.graph.PullImage(stdout, remote, srv.runtime.authConfig); err != nil { - return err - } + // if err := srv.runtime.graph.PullImage(stdout, remote, srv.runtime.authConfig); err != nil { + // return err + // } return nil } // FIXME: Allow pull repo:tag - if err := srv.runtime.graph.PullRepository(stdout, remote, "", srv.runtime.repositories, srv.runtime.authConfig); err != nil { - return err - } + //if err := srv.runtime.graph.PullRepository(stdout, remote, "", srv.runtime.repositories, srv.runtime.authConfig); err != nil { + // return err + //} return nil } */ diff --git a/registry.go b/registry.go index 428db1b968..7d13efd88f 100644 --- a/registry.go +++ b/registry.go @@ -1,6 +1,7 @@ package docker import ( + "bufio" "encoding/json" "fmt" "github.com/dotcloud/docker/auth" @@ -94,10 +95,10 @@ func (graph *Graph) LookupRemoteImage(imgId string, authConfig *auth.AuthConfig) // Retrieve an image from the Registry. // Returns the Image object as well as the layer as an Archive (io.Reader) -func (graph *Graph) getRemoteImage(stdout io.Writer, imgId string, authConfig *auth.AuthConfig) (*Image, Archive, error) { +func (graph *Graph) getRemoteImage(stdout *bufio.ReadWriter, imgId string, authConfig *auth.AuthConfig) (*Image, Archive, error) { client := &http.Client{} - fmt.Fprintf(stdout, "Pulling %s metadata\r\n", imgId) + fmt.Fprintf(stdout, "Pulling %s metadata\r\n", imgId);stdout.Flush() // Get the Json req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil) if err != nil { @@ -125,7 +126,7 @@ func (graph *Graph) getRemoteImage(stdout io.Writer, imgId string, authConfig *a img.Id = imgId // Get the layer - fmt.Fprintf(stdout, "Pulling %s fs layer\r\n", imgId) + fmt.Fprintf(stdout, "Pulling %s fs layer\r\n", imgId);stdout.Flush() req, err = http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/layer", nil) if err != nil { return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err) @@ -138,7 +139,7 @@ func (graph *Graph) getRemoteImage(stdout io.Writer, imgId string, authConfig *a return img, ProgressReader(res.Body, int(res.ContentLength), stdout), nil } -func (graph *Graph) PullImage(stdout io.Writer, imgId string, authConfig *auth.AuthConfig) error { +func (graph *Graph) PullImage(stdout *bufio.ReadWriter, imgId string, authConfig *auth.AuthConfig) error { history, err := graph.getRemoteHistory(imgId, authConfig) if err != nil { return err @@ -161,7 +162,7 @@ func (graph *Graph) PullImage(stdout io.Writer, imgId string, authConfig *auth.A } // FIXME: Handle the askedTag parameter -func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, repositories *TagStore, authConfig *auth.AuthConfig) error { +func (graph *Graph) PullRepository(stdout *bufio.ReadWriter, remote, askedTag string, repositories *TagStore, authConfig *auth.AuthConfig) error { client := &http.Client{} fmt.Fprintf(stdout, "Pulling repository %s\r\n", remote)