Browse Source

Merge pull request #3180 from vieux/job_container_delete

Move container_delete to job
Victor Vieux 11 years ago
parent
commit
6dde20c0c5
3 changed files with 50 additions and 31 deletions
  1. 8 16
      api.go
  2. 9 3
      integration/server_test.go
  3. 33 12
      server.go

+ 8 - 16
api.go

@@ -71,13 +71,13 @@ func httpError(w http.ResponseWriter, err error) {
 	// create appropriate error types with clearly defined meaning.
 	// create appropriate error types with clearly defined meaning.
 	if strings.Contains(err.Error(), "No such") {
 	if strings.Contains(err.Error(), "No such") {
 		statusCode = http.StatusNotFound
 		statusCode = http.StatusNotFound
-	} else if strings.HasPrefix(err.Error(), "Bad parameter") {
+	} else if strings.Contains(err.Error(), "Bad parameter") {
 		statusCode = http.StatusBadRequest
 		statusCode = http.StatusBadRequest
-	} else if strings.HasPrefix(err.Error(), "Conflict") {
+	} else if strings.Contains(err.Error(), "Conflict") {
 		statusCode = http.StatusConflict
 		statusCode = http.StatusConflict
-	} else if strings.HasPrefix(err.Error(), "Impossible") {
+	} else if strings.Contains(err.Error(), "Impossible") {
 		statusCode = http.StatusNotAcceptable
 		statusCode = http.StatusNotAcceptable
-	} else if strings.HasPrefix(err.Error(), "Wrong login/password") {
+	} else if strings.Contains(err.Error(), "Wrong login/password") {
 		statusCode = http.StatusUnauthorized
 		statusCode = http.StatusUnauthorized
 	} else if strings.Contains(err.Error(), "hasn't been activated") {
 	} else if strings.Contains(err.Error(), "hasn't been activated") {
 		statusCode = http.StatusForbidden
 		statusCode = http.StatusForbidden
@@ -617,18 +617,10 @@ func deleteContainers(srv *Server, version float64, w http.ResponseWriter, r *ht
 	if vars == nil {
 	if vars == nil {
 		return fmt.Errorf("Missing parameter")
 		return fmt.Errorf("Missing parameter")
 	}
 	}
-	name := vars["name"]
-
-	removeVolume, err := getBoolParam(r.Form.Get("v"))
-	if err != nil {
-		return err
-	}
-	removeLink, err := getBoolParam(r.Form.Get("link"))
-	if err != nil {
-		return err
-	}
-
-	if err := srv.ContainerDestroy(name, removeVolume, removeLink); err != nil {
+	job := srv.Eng.Job("container_delete", vars["name"])
+	job.Setenv("removeVolume", r.Form.Get("v"))
+	job.Setenv("removeLink", r.Form.Get("link"))
+	if err := job.Run(); err != nil {
 		return err
 		return err
 	}
 	}
 	w.WriteHeader(http.StatusNoContent)
 	w.WriteHeader(http.StatusNoContent)

+ 9 - 3
integration/server_test.go

@@ -95,7 +95,9 @@ func TestCreateRm(t *testing.T) {
 		t.Errorf("Expected 1 container, %v found", len(c))
 		t.Errorf("Expected 1 container, %v found", len(c))
 	}
 	}
 
 
-	if err = srv.ContainerDestroy(id, true, false); err != nil {
+	job := eng.Job("container_delete", id)
+	job.SetenvBool("removeVolume", true)
+	if err := job.Run(); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 
 
@@ -135,7 +137,9 @@ func TestCreateRmVolumes(t *testing.T) {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 
 
-	if err = srv.ContainerDestroy(id, true, false); err != nil {
+	job = eng.Job("container_delete", id)
+	job.SetenvBool("removeVolume", true)
+	if err := job.Run(); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 
 
@@ -213,7 +217,9 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
 	}
 	}
 
 
 	// FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
 	// FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
-	if err := srv.ContainerDestroy(id, true, false); err != nil {
+	job = eng.Job("container_delete", id)
+	job.SetenvBool("removeVolume", true)
+	if err := job.Run(); err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 
 

+ 33 - 12
server.go

@@ -119,6 +119,10 @@ func jobInitApi(job *engine.Job) engine.Status {
 		job.Error(err)
 		job.Error(err)
 		return engine.StatusErr
 		return engine.StatusErr
 	}
 	}
+	if err := job.Eng.Register("container_delete", srv.ContainerDestroy); err != nil {
+		job.Error(err)
+		return engine.StatusErr
+	}
 	return engine.StatusOK
 	return engine.StatusOK
 }
 }
 
 
@@ -1446,24 +1450,36 @@ func (srv *Server) ContainerRestart(job *engine.Job) engine.Status {
 
 
 }
 }
 
 
-func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool) error {
+func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status {
+	if len(job.Args) != 1 {
+		job.Errorf("Not enough arguments. Usage: %s CONTAINER\n", job.Name)
+		return engine.StatusErr
+	}
+	name := job.Args[0]
+	removeVolume := job.GetenvBool("removeVolume")
+	removeLink := job.GetenvBool("removeLink")
+
 	container := srv.runtime.Get(name)
 	container := srv.runtime.Get(name)
 
 
 	if removeLink {
 	if removeLink {
 		if container == nil {
 		if container == nil {
-			return fmt.Errorf("No such link: %s", name)
+			job.Errorf("No such link: %s", name)
+			return engine.StatusErr
 		}
 		}
 		name, err := getFullName(name)
 		name, err := getFullName(name)
 		if err != nil {
 		if err != nil {
-			return err
+			job.Error(err)
+			return engine.StatusErr
 		}
 		}
 		parent, n := path.Split(name)
 		parent, n := path.Split(name)
 		if parent == "/" {
 		if parent == "/" {
-			return fmt.Errorf("Conflict, cannot remove the default name of the container")
+			job.Errorf("Conflict, cannot remove the default name of the container")
+			return engine.StatusErr
 		}
 		}
 		pe := srv.runtime.containerGraph.Get(parent)
 		pe := srv.runtime.containerGraph.Get(parent)
 		if pe == nil {
 		if pe == nil {
-			return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
+			job.Errorf("Cannot get parent %s for name %s", parent, name)
+			return engine.StatusErr
 		}
 		}
 		parentContainer := srv.runtime.Get(pe.ID())
 		parentContainer := srv.runtime.Get(pe.ID())
 
 
@@ -1476,14 +1492,16 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
 		}
 		}
 
 
 		if err := srv.runtime.containerGraph.Delete(name); err != nil {
 		if err := srv.runtime.containerGraph.Delete(name); err != nil {
-			return err
+			job.Error(err)
+			return engine.StatusErr
 		}
 		}
-		return nil
+		return engine.StatusOK
 	}
 	}
 
 
 	if container != nil {
 	if container != nil {
 		if container.State.IsRunning() {
 		if container.State.IsRunning() {
-			return fmt.Errorf("Impossible to remove a running container, please stop it first")
+			job.Errorf("Impossible to remove a running container, please stop it first")
+			return engine.StatusErr
 		}
 		}
 		volumes := make(map[string]struct{})
 		volumes := make(map[string]struct{})
 
 
@@ -1508,7 +1526,8 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
 			volumes[volumeId] = struct{}{}
 			volumes[volumeId] = struct{}{}
 		}
 		}
 		if err := srv.runtime.Destroy(container); err != nil {
 		if err := srv.runtime.Destroy(container); err != nil {
-			return fmt.Errorf("Cannot destroy container %s: %s", name, err)
+			job.Errorf("Cannot destroy container %s: %s", name, err)
+			return engine.StatusErr
 		}
 		}
 		srv.LogEvent("destroy", container.ID, srv.runtime.repositories.ImageName(container.Image))
 		srv.LogEvent("destroy", container.ID, srv.runtime.repositories.ImageName(container.Image))
 
 
@@ -1528,14 +1547,16 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
 					continue
 					continue
 				}
 				}
 				if err := srv.runtime.volumes.Delete(volumeId); err != nil {
 				if err := srv.runtime.volumes.Delete(volumeId); err != nil {
-					return err
+					job.Error(err)
+					return engine.StatusErr
 				}
 				}
 			}
 			}
 		}
 		}
 	} else {
 	} else {
-		return fmt.Errorf("No such container: %s", name)
+		job.Errorf("No such container: %s", name)
+		return engine.StatusErr
 	}
 	}
-	return nil
+	return engine.StatusOK
 }
 }
 
 
 var ErrImageReferenced = errors.New("Image referenced by a repository")
 var ErrImageReferenced = errors.New("Image referenced by a repository")