Forráskód Böngészése

Change API route for containers/ and images/ in order to avoid conflict

Guillaume J. Charmes 12 éve
szülő
commit
152ebeea43
4 módosított fájl, 54 hozzáadás és 62 törlés
  1. 25 22
      api.go
  2. 7 17
      api_test.go
  3. 20 21
      server.go
  4. 2 2
      server_test.go

+ 25 - 22
api.go

@@ -154,7 +154,6 @@ func getImagesHistory(srv *Server, w http.ResponseWriter, r *http.Request, vars
 		return nil, fmt.Errorf("Missing parameter")
 	}
 	name := vars["name"]
-	log.Printf("----------> %s\n", name)
 	outs, err := srv.ImageHistory(name)
 	if err != nil {
 		return nil, err
@@ -182,7 +181,7 @@ func getContainersChanges(srv *Server, w http.ResponseWriter, r *http.Request, v
 	return b, nil
 }
 
-func getContainers(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
+func getContainersPs(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
 	if err := parseForm(r); err != nil {
 		return nil, err
 	}
@@ -248,7 +247,8 @@ func postCommit(srv *Server, w http.ResponseWriter, r *http.Request, vars map[st
 	return b, nil
 }
 
-func postImages(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
+// Creates an image from Pull or from Import
+func postImagesCreate(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
 	if err := parseForm(r); err != nil {
 		return nil, err
 	}
@@ -355,21 +355,25 @@ func postBuild(srv *Server, w http.ResponseWriter, r *http.Request, vars map[str
 	return nil, nil
 }
 
-func postContainers(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
-	var config Config
-	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
+func postContainersCreate(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
+	config := &Config{}
+	if err := json.NewDecoder(r.Body).Decode(config); err != nil {
 		return nil, err
 	}
-	id, memoryW, swapW, err := srv.ContainerCreate(config)
+	id, err := srv.ContainerCreate(config)
 	if err != nil {
 		return nil, err
 	}
-	var out ApiRun
-	out.Id = id
-	if memoryW {
+
+	out := &ApiRun{
+		Id: id,
+	}
+	if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
+		log.Println("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.")
 		out.Warnings = append(out.Warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
 	}
-	if swapW {
+	if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
+		log.Println("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.")
 		out.Warnings = append(out.Warnings, "Your kernel does not support memory swap capabilities. Limitation discarded.")
 	}
 	b, err := json.Marshal(out)
@@ -545,28 +549,27 @@ func ListenAndServe(addr string, srv *Server, logging bool) error {
 		"GET": {
 			"/auth":                         getAuth,
 			"/version":                      getVersion,
-			"/containers/{name:.*}/export":  getContainersExport,
-			"/images":                       getImages,
+			"/info":                         getInfo,
 			"/images/json":                  getImages,
 			"/images/viz":                   getImagesViz,
-			"/info":                         getInfo,
 			"/images/search":                getImagesSearch,
 			"/images/{name:.*}/history":     getImagesHistory,
-			"/containers/{name:.*}/changes": getContainersChanges,
-			"/containers":                   getContainers,
 			"/images/{name:.*}/json":        getImagesByName,
+			"/containers/ps":                getContainersPs,
+			"/containers/{name:.*}/export":  getContainersExport,
+			"/containers/{name:.*}/changes": getContainersChanges,
 			"/containers/{name:.*}/json":    getContainersByName,
 		},
 		"POST": {
-			"/auth": postAuth,
-			"/containers/{name:.*}/kill":    postContainersKill,
-			"/images/{name:.*}/tag":         postImagesTag,
+			"/auth":                         postAuth,
 			"/commit":                       postCommit,
-			"/images":                       postImages,
+			"/build":                        postBuild,
+			"/images/create":                postImagesCreate,
 			"/images/{name:*.}/insert":      postImagesInsert,
 			"/images/{name:*.}/push":        postImagesPush,
-			"/build":                        postBuild,
-			"/containers":                   postContainers,
+			"/images/{name:.*}/tag":         postImagesTag,
+			"/containers/create":            postContainersCreate,
+			"/containers/{name:.*}/kill":    postContainersKill,
 			"/containers/{name:.*}/restart": postContainersRestart,
 			"/containers/{name:.*}/start":   postContainersStart,
 			"/containers/{name:.*}/stop":    postContainersStop,

+ 7 - 17
api_test.go

@@ -9,20 +9,6 @@ import (
 	"testing"
 )
 
-// func init() {
-// 	// Make it our Store root
-// 	runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
-// 	if err != nil {
-// 		panic(err)
-// 	}
-
-// 	// Create the "Server"
-// 	srv := &Server{
-// 		runtime: runtime,
-// 	}
-// 	go ListenAndServe("0.0.0.0:4243", srv, false)
-// }
-
 func TestAuth(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
@@ -108,7 +94,11 @@ func TestVersion(t *testing.T) {
 	}
 }
 
-func TestImages(t *testing.T) {
+func TestContainersExport(t *testing.T) {
+	//FIXME: Implement this test
+}
+
+func TestGetImages(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -274,7 +264,7 @@ func testCreateContainer(t *testing.T, srv *Server) {
 		t.Fatal(err)
 	}
 
-	body, err := postContainers(srv, r, req, nil)
+	body, err := postContainersCreate(srv, r, req, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -297,7 +287,7 @@ func testListContainers(t *testing.T, srv *Server, expected int) []ApiContainers
 		t.Fatal(err)
 	}
 
-	body, err := getContainers(srv, r, req, nil)
+	body, err := getContainersPs(srv, r, req, nil)
 	if err != nil {
 		t.Fatal(err)
 	}

+ 20 - 21
server.go

@@ -236,9 +236,10 @@ func (srv *Server) ContainerChanges(name string) ([]Change, error) {
 }
 
 func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, before string) []ApiContainers {
-	var outs []ApiContainers = []ApiContainers{} //produce [] when empty instead of 'null'
 	var foundBefore bool
 	var displayed int
+	retContainers := []ApiContainers{}
+
 	for _, container := range srv.runtime.List() {
 		if !container.State.Running && !all && n == -1 && since == "" && before == "" {
 			continue
@@ -258,23 +259,26 @@ func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, befor
 		if container.ShortId() == since {
 			break
 		}
-		displayed += 1
-		var out ApiContainers
-		out.Id = container.ShortId()
+		displayed++
+
+		c := ApiContainers{
+			Id: container.ShortId(),
+		}
+
 		if !only_ids {
 			command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
 			if trunc_cmd {
 				command = Trunc(command, 20)
 			}
-			out.Image = srv.runtime.repositories.ImageName(container.Image)
-			out.Command = command
-			out.Created = container.Created.Unix()
-			out.Status = container.State.String()
-			out.Ports = container.NetworkSettings.PortMappingHuman()
+			c.Image = srv.runtime.repositories.ImageName(container.Image)
+			c.Command = command
+			c.Created = container.Created.Unix()
+			c.Status = container.State.String()
+			c.Ports = container.NetworkSettings.PortMappingHuman()
 		}
-		outs = append(outs, out)
+		retContainers = append(retContainers, c)
 	}
-	return outs
+	return retContainers
 }
 
 func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
@@ -369,29 +373,24 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write
 	return nil
 }
 
-func (srv *Server) ContainerCreate(config Config) (string, bool, bool, error) {
-	var memoryW, swapW bool
+func (srv *Server) ContainerCreate(config *Config) (string, error) {
 
 	if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
-		memoryW = true
-		log.Println("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.")
 		config.Memory = 0
 	}
 
 	if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
-		swapW = true
-		log.Println("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.")
 		config.MemorySwap = -1
 	}
 	b := NewBuilder(srv.runtime)
-	container, err := b.Create(&config)
+	container, err := b.Create(config)
 	if err != nil {
 		if srv.runtime.graph.IsNotExist(err) {
-			return "", false, false, fmt.Errorf("No such image: %s", config.Image)
+			return "", fmt.Errorf("No such image: %s", config.Image)
 		}
-		return "", false, false, err
+		return "", err
 	}
-	return container.ShortId(), memoryW, swapW, nil
+	return container.ShortId(), nil
 }
 
 func (srv *Server) ImageCreateFromFile(dockerfile io.Reader, out io.Writer) error {

+ 2 - 2
server_test.go

@@ -18,7 +18,7 @@ func TestCreateRm(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	id, _, _, err := srv.ContainerCreate(*config)
+	id, err := srv.ContainerCreate(config)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -51,7 +51,7 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	id, _, _, err := srv.ContainerCreate(*config)
+	id, err := srv.ContainerCreate(config)
 	if err != nil {
 		t.Fatal(err)
 	}