Browse Source

removed hijack on viz

Victor Vieux 12 năm trước cách đây
mục cha
commit
0ecf5e245d
4 tập tin đã thay đổi với 64 bổ sung32 xóa
  1. 20 22
      api.go
  2. 3 1
      commands.go
  3. 36 3
      docs/sources/remote-api/api.rst
  4. 5 6
      server.go

+ 20 - 22
api.go

@@ -115,37 +115,34 @@ func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) ([
 }
 }
 
 
 func getImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 func getImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
+	vars := mux.Vars(r)
+	format := vars["format"]
+
 	if err := parseForm(r); err != nil {
 	if err := parseForm(r); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	viz := r.Form.Get("viz") == "1"
-	if viz {
-		in, out, err := hijackServer(w)
-		if err != nil {
+	if format == "viz" {
+		if err := srv.ImagesViz(w); err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
-		defer in.Close()
-		fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
-		if err := srv.ImagesViz(out); err != nil {
-			fmt.Fprintf(out, "Error: %s\n", err)
-		}
 		return nil, nil
 		return nil, nil
-	}
+	} else if format == "" || format == "json" {
+		all := r.Form.Get("all") == "1"
+		filter := r.Form.Get("filter")
+		only_ids := r.Form.Get("only_ids") == "1"
 
 
-	all := r.Form.Get("all") == "1"
-	filter := r.Form.Get("filter")
-	only_ids := r.Form.Get("only_ids") == "1"
-
-	outs, err := srv.Images(all, only_ids, filter)
-	if err != nil {
-		return nil, err
-	}
-	b, err := json.Marshal(outs)
-	if err != nil {
-		return nil, err
+		outs, err := srv.Images(all, only_ids, filter)
+		if err != nil {
+			return nil, err
+		}
+		b, err := json.Marshal(outs)
+		if err != nil {
+			return nil, err
+		}
+		return b, nil
 	}
 	}
-	return b, nil
+	return nil, fmt.Errorf("No such format: %s", format)
 }
 }
 
 
 func getInfo(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 func getInfo(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
@@ -526,6 +523,7 @@ func ListenAndServe(addr string, srv *Server) error {
 			"/version":                      getVersion,
 			"/version":                      getVersion,
 			"/containers/{name:.*}/export":  getContainersExport,
 			"/containers/{name:.*}/export":  getContainersExport,
 			"/images":                       getImages,
 			"/images":                       getImages,
+			"/images/{format}":              getImages,
 			"/info":                         getInfo,
 			"/info":                         getInfo,
 			"/images/search":                getImagesSearch,
 			"/images/search":                getImagesSearch,
 			"/images/{name:.*}/history":     getImagesHistory,
 			"/images/{name:.*}/history":     getImagesHistory,

+ 3 - 1
commands.go

@@ -699,9 +699,11 @@ func CmdImages(args ...string) error {
 	}
 	}
 
 
 	if *flViz {
 	if *flViz {
-		if err := hijack("GET", "/images?viz=1", false); err != nil {
+		body, _, err := call("GET", "/images/viz", false)
+		if err != nil {
 			return err
 			return err
 		}
 		}
+		fmt.Printf("%s", body)
 	} else {
 	} else {
 		v := url.Values{}
 		v := url.Values{}
 		if cmd.NArg() == 1 {
 		if cmd.NArg() == 1 {

+ 36 - 3
docs/sources/remote-api/api.rst

@@ -460,15 +460,16 @@ List Images
 ***********
 ***********
 
 
 .. http:get:: /images
 .. http:get:: /images
+.. http:get:: /images/(format)
 
 
-	List images
+	List images ``format`` could be json or viz (json default)
 
 
 	**Example request**:
 	**Example request**:
 
 
 	.. sourcecode:: http
 	.. sourcecode:: http
 
 
-	   GET /images?all=0&only_ids=0 HTTP/1.1
-	   
+	   GET /images/json?all=0&only_ids=0 HTTP/1.1
+
 	**Example response**:
 	**Example response**:
 
 
 	.. sourcecode:: http
 	.. sourcecode:: http
@@ -490,6 +491,38 @@ List Images
 			"Created":1364102658
 			"Created":1364102658
 		}
 		}
 	   ]
 	   ]
+
+
+	**Example request**:
+
+	.. sourcecode:: http
+
+	   GET /images/viz HTTP/1.1
+
+	**Example response**:
+
+	.. sourcecode:: http
+
+	   HTTP/1.1 200 OK
+	   Content-Type: text/plain
+
+	   digraph docker {
+	   "d82cbacda43a" -> "074be284591f"
+	   "1496068ca813" -> "08306dc45919"
+	   "08306dc45919" -> "0e7893146ac2"
+	   "b750fe79269d" -> "1496068ca813"
+	   base -> "27cf78414709" [style=invis]
+	   "f71189fff3de" -> "9a33b36209ed"
+	   "27cf78414709" -> "b750fe79269d"
+	   "0e7893146ac2" -> "d6434d954665"
+	   "d6434d954665" -> "d82cbacda43a"
+	   base -> "e9aa60c60128" [style=invis]
+	   "074be284591f" -> "f71189fff3de"
+	   "b750fe79269d" [label="b750fe79269d\nbase",shape=box,fillcolor="paleturquoise",style="filled,rounded"];
+	   "e9aa60c60128" [label="e9aa60c60128\nbase2",shape=box,fillcolor="paleturquoise",style="filled,rounded"];
+	   "9a33b36209ed" [label="9a33b36209ed\ntest",shape=box,fillcolor="paleturquoise",style="filled,rounded"];
+	   base [style=invisible]
+	   }
  
  
 	:query only_ids: 1 or 0, Only display numeric IDs. Default 0
 	:query only_ids: 1 or 0, Only display numeric IDs. Default 0
 	:query all: 1 or 0, Show all containers. Only running containers are shown by default
 	:query all: 1 or 0, Show all containers. Only running containers are shown by default

+ 5 - 6
server.go

@@ -102,8 +102,7 @@ func (srv *Server) ImagesViz(out io.Writer) error {
 	if images == nil {
 	if images == nil {
 		return nil
 		return nil
 	}
 	}
-
-	fmt.Fprintf(out, "digraph docker {\n")
+	out.Write([]byte("digraph docker {\n"))
 
 
 	var (
 	var (
 		parentImage *Image
 		parentImage *Image
@@ -115,9 +114,9 @@ func (srv *Server) ImagesViz(out io.Writer) error {
 			return fmt.Errorf("Error while getting parent image: %v", err)
 			return fmt.Errorf("Error while getting parent image: %v", err)
 		}
 		}
 		if parentImage != nil {
 		if parentImage != nil {
-			fmt.Fprintf(out, "  \"%s\" -> \"%s\"\n", parentImage.ShortId(), image.ShortId())
+			out.Write([]byte(" \"" + parentImage.ShortId() + "\" -> \"" + image.ShortId() + "\"\n"))
 		} else {
 		} else {
-			fmt.Fprintf(out, "  base -> \"%s\" [style=invis]\n", image.ShortId())
+			out.Write([]byte(" base -> \"" + image.ShortId() + "\" [style=invis]\n"))
 		}
 		}
 	}
 	}
 
 
@@ -130,9 +129,9 @@ func (srv *Server) ImagesViz(out io.Writer) error {
 	}
 	}
 
 
 	for id, repos := range reporefs {
 	for id, repos := range reporefs {
-		fmt.Fprintf(out, "  \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n", id, id, strings.Join(repos, "\\n"))
+		out.Write([]byte(" \"" + id + "\" [label=\"" + id + "\\n" + strings.Join(repos, "\\n") + "\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n"))
 	}
 	}
-	fmt.Fprintf(out, "  base [style=invisible]\n}\n")
+	out.Write([]byte(" base [style=invisible]\n}\n"))
 	return nil
 	return nil
 }
 }