use dockerclient

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-03-11 14:09:26 -07:00 committed by Victor Vieux
parent b4a88ad622
commit c969fcdae6
3 changed files with 23 additions and 46 deletions

View File

@ -5,7 +5,6 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
@ -339,13 +338,9 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
}
var name = mux.Vars(r)["name"]
client, scheme := newClientAndScheme(c.tlsConfig)
defer closeIdleConnections(client)
matchedImages := []*cluster.Image{}
for _, image := range c.cluster.Images() {
if image.Match(name) {
fmt.Println("matched", image)
matchedImages = append(matchedImages, image)
}
}
@ -355,51 +350,23 @@ func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
httpError(w, fmt.Sprintf("No such image %s", name), http.StatusNotFound)
return
}
wf := NewWriteFlusher(w)
for i, image := range matchedImages {
fmt.Println("delete", image)
req, err := http.NewRequest("DELETE", scheme+"://"+image.Node.Addr()+"/images/"+name, nil)
out := []*dockerclient.ImageDelete{}
for _, image := range matchedImages {
content, err := image.Node.DockerClient().RemoveImage(name)
if err != nil {
if size == 1 {
httpError(w, err.Error(), http.StatusInternalServerError)
}
out = nil
httpError(w, err.Error(), http.StatusInternalServerError)
continue
}
resp, err := client.Do(req)
if err != nil {
if size == 1 {
httpError(w, err.Error(), http.StatusInternalServerError)
}
continue
}
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
if size == 1 {
httpError(w, err.Error(), http.StatusInternalServerError)
}
continue
}
if resp.StatusCode != 200 {
if size == 1 {
w.WriteHeader(resp.StatusCode)
io.Copy(NewWriteFlusher(w), resp.Body)
}
continue
}
sdata := bytes.NewBuffer(data).String()
if i != 0 {
w.Header().Set("Content-Type", "application/json")
sdata = strings.Replace(sdata, "[", ",", -1)
}
if i != size-1 {
sdata = strings.Replace(sdata, "]", "", -1)
}
fmt.Fprintf(wf, sdata)
out = append(out, content...)
}
if out != nil {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(NewWriteFlusher(w)).Encode(out)
}
}
// GET /_ping

View File

@ -1,8 +1,14 @@
package cluster
import "fmt"
import (
"fmt"
"github.com/samalba/dockerclient"
)
type Node interface {
DockerClient() dockerclient.Client
ID() string
Name() string

View File

@ -54,6 +54,10 @@ type node struct {
overcommitRatio int64
}
func (n *node) DockerClient() dockerclient.Client {
return n.client
}
func (n *node) ID() string {
return n.id
}