This commit is contained in:
Victor Vieux 2013-04-22 23:37:22 +02:00
Родитель 1aa7f1392d
Коммит 6ce475dbdf
4 изменённых файлов: 50 добавлений и 11 удалений

34
api.go
Просмотреть файл

@ -1,6 +1,7 @@
package docker package docker
import ( import (
_"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gorilla/mux" "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) { r.Path("/containers/{name:.*}/restart").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RequestURI) log.Println(r.RequestURI)
vars := mux.Vars(r) vars := mux.Vars(r)

Просмотреть файл

@ -51,6 +51,10 @@ type PsOut struct {
Status string `json:",omitempty"` Status string `json:",omitempty"`
} }
type PullIn struct {
Name string
}
type LogsIn struct { type LogsIn struct {
Name string Name string
} }

Просмотреть файл

@ -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() // FIXME: CmdPull should be a wrapper around Runtime.Pull()
if srv.runtime.graph.LookupRemoteImage(remote, srv.runtime.authConfig) { if srv.runtime.graph.LookupRemoteImage(remote, srv.runtime.authConfig) {
if err := srv.runtime.graph.PullImage(stdout, remote, srv.runtime.authConfig); err != nil { // if err := srv.runtime.graph.PullImage(stdout, remote, srv.runtime.authConfig); err != nil {
return err // return err
} // }
return nil return nil
} }
// FIXME: Allow pull repo:tag // FIXME: Allow pull repo:tag
if err := srv.runtime.graph.PullRepository(stdout, remote, "", srv.runtime.repositories, srv.runtime.authConfig); err != nil { //if err := srv.runtime.graph.PullRepository(stdout, remote, "", srv.runtime.repositories, srv.runtime.authConfig); err != nil {
return err // return err
} //}
return nil return nil
} }
*/ */

Просмотреть файл

@ -1,6 +1,7 @@
package docker package docker
import ( import (
"bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/dotcloud/docker/auth" "github.com/dotcloud/docker/auth"
@ -94,10 +95,10 @@ func (graph *Graph) LookupRemoteImage(imgId string, authConfig *auth.AuthConfig)
// Retrieve an image from the Registry. // Retrieve an image from the Registry.
// Returns the Image object as well as the layer as an Archive (io.Reader) // 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{} 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 // Get the Json
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil) req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil)
if err != nil { if err != nil {
@ -125,7 +126,7 @@ func (graph *Graph) getRemoteImage(stdout io.Writer, imgId string, authConfig *a
img.Id = imgId img.Id = imgId
// Get the layer // 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) req, err = http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/layer", nil)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err) 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 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) history, err := graph.getRemoteHistory(imgId, authConfig)
if err != nil { if err != nil {
return err return err
@ -161,7 +162,7 @@ func (graph *Graph) PullImage(stdout io.Writer, imgId string, authConfig *auth.A
} }
// FIXME: Handle the askedTag parameter // 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{} client := &http.Client{}
fmt.Fprintf(stdout, "Pulling repository %s\r\n", remote) fmt.Fprintf(stdout, "Pulling repository %s\r\n", remote)