add docker rmi

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-03-05 11:33:07 -08:00 committed by Victor Vieux
parent 3dfa0816c5
commit 649b6cf577
10 changed files with 104 additions and 27 deletions

View File

@ -22,8 +22,6 @@ POST "/images/create" (pull implemented)
POST "/images/load"
POST "/images/{name:.*}/push"
POST "/images/{name:.*}/tag"
DELETE "/images/{name:.*}"
```
## Endpoints which behave differently

View File

@ -5,6 +5,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
@ -89,7 +90,7 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
accepteds, _ := filters["node"]
images := []*cluster.Image{}
for _, image := range c.cluster.Images() {
for _, image := range c.cluster.Images("") {
if len(accepteds) != 0 {
found := false
for _, accepted := range accepteds {
@ -330,6 +331,69 @@ func postContainersExec(c *context, w http.ResponseWriter, r *http.Request) {
w.Write(data)
}
// DELETE /images/{name:.*}
func deleteImages(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
var name = mux.Vars(r)["name"]
client, scheme := newClientAndScheme(c.tlsConfig)
defer closeIdleConnections(client)
images := c.cluster.Images(name)
size := len(images)
if size == 0 {
httpError(w, fmt.Sprintf("No such image %s", name), http.StatusNotFound)
return
}
for i, image := range images {
req, err := http.NewRequest("DELETE", scheme+"://"+image.Node.Addr()+"/images/"+name, nil)
if err != nil {
if size == 1 {
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 != len(images)-1 {
sdata = strings.Replace(sdata, "]", "", -1)
}
fmt.Fprintf(w, sdata)
}
}
// GET /_ping
func ping(c *context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte{'O', 'K'})
@ -467,7 +531,7 @@ func createRouter(c *context, enableCors bool) *mux.Router {
},
"DELETE": {
"/containers/{name:.*}": deleteContainers,
"/images/{name:.*}": notImplementedHandler,
"/images/{name:.*}": deleteImages,
},
"OPTIONS": {
"": optionsHandler,

View File

@ -23,7 +23,7 @@ func (fn *FakeNode) ID() string { return "node_id" }
func (fn *FakeNode) Name() string { return "node_name" }
func (fn *FakeNode) IP() string { return "node_ip" }
func (fn *FakeNode) Addr() string { return "node_addr" }
func (fn *FakeNode) Images() []*cluster.Image { return nil }
func (fn *FakeNode) Images(_ string) []*cluster.Image { return nil }
func (fn *FakeNode) Image(_ string) *cluster.Image { return nil }
func (fn *FakeNode) Containers() []*cluster.Container { return nil }
func (fn *FakeNode) Container(_ string) *cluster.Container { return nil }

View File

@ -9,8 +9,9 @@ type Cluster interface {
// Remove a container
RemoveContainer(container *Container, force bool) error
// Return all images
Images() []*Image
// Return all images matching `name`
// if `name` == "" return all images in the cluster
Images(name string) []*Image
// Return one image matching `IdOrName`
Image(IdOrName string) *Image

View File

@ -9,7 +9,7 @@ type Node interface {
IP() string //to inject the actual IP of the machine in docker ps (hostname:port or ip:port)
Addr() string //to know where to connect with the proxy
Images() []*Image //used by the API
Images(name string) []*Image //used by the API
Image(IdOrName string) *Image //used by the filters
Containers() []*Container //used by the filters
Container(IdOrName string) *Container //used by the filters

View File

@ -156,13 +156,14 @@ func (c *Cluster) getNode(addr string) *node {
}
// Containers returns all the images in the cluster.
func (c *Cluster) Images() []*cluster.Image {
// If `name` is empty, return all the images
func (c *Cluster) Images(name string) []*cluster.Image {
c.RLock()
defer c.RUnlock()
out := []*cluster.Image{}
for _, n := range c.nodes {
out = append(out, n.Images()...)
out = append(out, n.Images(name)...)
}
return out

View File

@ -459,11 +459,30 @@ func (n *node) Container(IdOrName string) *cluster.Container {
return nil
}
func (n *node) Images() []*cluster.Image {
func matchImage(image *cluster.Image, IdOrName string) bool {
size := len(IdOrName)
if image.Id == IdOrName || (size > 2 && strings.HasPrefix(image.Id, IdOrName)) {
return true
}
for _, repoTag := range image.RepoTags {
if repoTag == IdOrName || (size > 2 && strings.HasPrefix(repoTag, IdOrName)) {
return true
}
}
return false
}
// Images returns a list for images matching name in the node
// If `name` is empty, returns all the images
func (n *node) Images(name string) []*cluster.Image {
images := []*cluster.Image{}
n.RLock()
for _, image := range n.images {
images = append(images, image)
if name == "" || matchImage(image, name) {
images = append(images, image)
}
}
n.RUnlock()
return images
@ -474,16 +493,10 @@ func (n *node) Image(IdOrName string) *cluster.Image {
n.RLock()
defer n.RUnlock()
size := len(IdOrName)
for _, image := range n.Images() {
if image.Id == IdOrName || (size > 2 && strings.HasPrefix(image.Id, IdOrName)) {
for _, image := range n.images {
if matchImage(image, IdOrName) {
return image
}
for _, t := range image.RepoTags {
if t == IdOrName || (size > 2 && strings.HasPrefix(t, IdOrName)) {
return image
}
}
}
return nil
}

View File

@ -35,7 +35,7 @@ func (f *AffinityFilter) Filter(config *dockerclient.ContainerConfig, nodes []cl
}
case "image":
images := []string{}
for _, image := range node.Images() {
for _, image := range node.Images("") {
images = append(images, image.Id)
images = append(images, image.RepoTags...)
for _, tag := range image.RepoTags {

View File

@ -11,11 +11,11 @@ type FakeNode struct {
labels map[string]string
}
func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" }
func (fn *FakeNode) Addr() string { return fn.addr }
func (fn *FakeNode) Images() []*cluster.Image { return fn.images }
func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" }
func (fn *FakeNode) Addr() string { return fn.addr }
func (fn *FakeNode) Images(_ string) []*cluster.Image { return fn.images }
func (fn *FakeNode) Image(id string) *cluster.Image {
for _, image := range fn.images {
if image.Id == id {

View File

@ -21,7 +21,7 @@ func (fn *FakeNode) ID() string { return fn.id }
func (fn *FakeNode) Name() string { return fn.name }
func (fn *FakeNode) IP() string { return "" }
func (fn *FakeNode) Addr() string { return fn.addr }
func (fn *FakeNode) Images() []*cluster.Image { return nil }
func (fn *FakeNode) Images(_ string) []*cluster.Image { return nil }
func (fn *FakeNode) Image(_ string) *cluster.Image { return nil }
func (fn *FakeNode) Containers() []*cluster.Container { return fn.containers }
func (fn *FakeNode) Container(_ string) *cluster.Container { return nil }