Browse code

move container_delete to job

handle error

remove useless errStr

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor@docker.com> (github: vieux)

Victor Vieux authored on 2013/12/12 08:23:38
Showing 3 changed files
... ...
@@ -71,13 +71,13 @@ func httpError(w http.ResponseWriter, err error) {
71 71
 	// create appropriate error types with clearly defined meaning.
72 72
 	if strings.Contains(err.Error(), "No such") {
73 73
 		statusCode = http.StatusNotFound
74
-	} else if strings.HasPrefix(err.Error(), "Bad parameter") {
74
+	} else if strings.Contains(err.Error(), "Bad parameter") {
75 75
 		statusCode = http.StatusBadRequest
76
-	} else if strings.HasPrefix(err.Error(), "Conflict") {
76
+	} else if strings.Contains(err.Error(), "Conflict") {
77 77
 		statusCode = http.StatusConflict
78
-	} else if strings.HasPrefix(err.Error(), "Impossible") {
78
+	} else if strings.Contains(err.Error(), "Impossible") {
79 79
 		statusCode = http.StatusNotAcceptable
80
-	} else if strings.HasPrefix(err.Error(), "Wrong login/password") {
80
+	} else if strings.Contains(err.Error(), "Wrong login/password") {
81 81
 		statusCode = http.StatusUnauthorized
82 82
 	} else if strings.Contains(err.Error(), "hasn't been activated") {
83 83
 		statusCode = http.StatusForbidden
... ...
@@ -618,18 +618,10 @@ func deleteContainers(srv *Server, version float64, w http.ResponseWriter, r *ht
618 618
 	if vars == nil {
619 619
 		return fmt.Errorf("Missing parameter")
620 620
 	}
621
-	name := vars["name"]
622
-
623
-	removeVolume, err := getBoolParam(r.Form.Get("v"))
624
-	if err != nil {
625
-		return err
626
-	}
627
-	removeLink, err := getBoolParam(r.Form.Get("link"))
628
-	if err != nil {
629
-		return err
630
-	}
631
-
632
-	if err := srv.ContainerDestroy(name, removeVolume, removeLink); err != nil {
621
+	job := srv.Eng.Job("container_delete", vars["name"])
622
+	job.Setenv("removeVolume", r.Form.Get("v"))
623
+	job.Setenv("removeLink", r.Form.Get("link"))
624
+	if err := job.Run(); err != nil {
633 625
 		return err
634 626
 	}
635 627
 	w.WriteHeader(http.StatusNoContent)
... ...
@@ -95,7 +95,9 @@ func TestCreateRm(t *testing.T) {
95 95
 		t.Errorf("Expected 1 container, %v found", len(c))
96 96
 	}
97 97
 
98
-	if err = srv.ContainerDestroy(id, true, false); err != nil {
98
+	job := eng.Job("container_delete", id)
99
+	job.SetenvBool("removeVolume", true)
100
+	if err := job.Run(); err != nil {
99 101
 		t.Fatal(err)
100 102
 	}
101 103
 
... ...
@@ -135,7 +137,9 @@ func TestCreateRmVolumes(t *testing.T) {
135 135
 		t.Fatal(err)
136 136
 	}
137 137
 
138
-	if err = srv.ContainerDestroy(id, true, false); err != nil {
138
+	job = eng.Job("container_delete", id)
139
+	job.SetenvBool("removeVolume", true)
140
+	if err := job.Run(); err != nil {
139 141
 		t.Fatal(err)
140 142
 	}
141 143
 
... ...
@@ -211,7 +215,9 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
211 211
 	}
212 212
 
213 213
 	// FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
214
-	if err := srv.ContainerDestroy(id, true, false); err != nil {
214
+	job = eng.Job("container_delete", id)
215
+	job.SetenvBool("removeVolume", true)
216
+	if err := job.Run(); err != nil {
215 217
 		t.Fatal(err)
216 218
 	}
217 219
 
... ...
@@ -116,6 +116,10 @@ func jobInitApi(job *engine.Job) engine.Status {
116 116
 		job.Error(err)
117 117
 		return engine.StatusErr
118 118
 	}
119
+	if err := job.Eng.Register("container_delete", srv.ContainerDestroy); err != nil {
120
+		job.Error(err)
121
+		return engine.StatusErr
122
+	}
119 123
 	return engine.StatusOK
120 124
 }
121 125
 
... ...
@@ -1431,24 +1435,36 @@ func (srv *Server) ContainerRestart(name string, t int) error {
1431 1431
 	return nil
1432 1432
 }
1433 1433
 
1434
-func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool) error {
1434
+func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status {
1435
+	if len(job.Args) != 1 {
1436
+		job.Errorf("Not enough arguments. Usage: %s CONTAINER\n", job.Name)
1437
+		return engine.StatusErr
1438
+	}
1439
+	name := job.Args[0]
1440
+	removeVolume := job.GetenvBool("removeVolume")
1441
+	removeLink := job.GetenvBool("removeLink")
1442
+
1435 1443
 	container := srv.runtime.Get(name)
1436 1444
 
1437 1445
 	if removeLink {
1438 1446
 		if container == nil {
1439
-			return fmt.Errorf("No such link: %s", name)
1447
+			job.Errorf("No such link: %s", name)
1448
+			return engine.StatusErr
1440 1449
 		}
1441 1450
 		name, err := srv.runtime.getFullName(name)
1442 1451
 		if err != nil {
1443
-			return err
1452
+			job.Error(err)
1453
+			return engine.StatusErr
1444 1454
 		}
1445 1455
 		parent, n := path.Split(name)
1446 1456
 		if parent == "/" {
1447
-			return fmt.Errorf("Conflict, cannot remove the default name of the container")
1457
+			job.Errorf("Conflict, cannot remove the default name of the container")
1458
+			return engine.StatusErr
1448 1459
 		}
1449 1460
 		pe := srv.runtime.containerGraph.Get(parent)
1450 1461
 		if pe == nil {
1451
-			return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
1462
+			job.Errorf("Cannot get parent %s for name %s", parent, name)
1463
+			return engine.StatusErr
1452 1464
 		}
1453 1465
 		parentContainer := srv.runtime.Get(pe.ID())
1454 1466
 
... ...
@@ -1461,14 +1477,16 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
1461 1461
 		}
1462 1462
 
1463 1463
 		if err := srv.runtime.containerGraph.Delete(name); err != nil {
1464
-			return err
1464
+			job.Error(err)
1465
+			return engine.StatusErr
1465 1466
 		}
1466
-		return nil
1467
+		return engine.StatusOK
1467 1468
 	}
1468 1469
 
1469 1470
 	if container != nil {
1470 1471
 		if container.State.IsRunning() {
1471
-			return fmt.Errorf("Impossible to remove a running container, please stop it first")
1472
+			job.Errorf("Impossible to remove a running container, please stop it first")
1473
+			return engine.StatusErr
1472 1474
 		}
1473 1475
 		volumes := make(map[string]struct{})
1474 1476
 
... ...
@@ -1493,7 +1511,8 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
1493 1493
 			volumes[volumeId] = struct{}{}
1494 1494
 		}
1495 1495
 		if err := srv.runtime.Destroy(container); err != nil {
1496
-			return fmt.Errorf("Cannot destroy container %s: %s", name, err)
1496
+			job.Errorf("Cannot destroy container %s: %s", name, err)
1497
+			return engine.StatusErr
1497 1498
 		}
1498 1499
 		srv.LogEvent("destroy", container.ID, srv.runtime.repositories.ImageName(container.Image))
1499 1500
 
... ...
@@ -1513,14 +1532,16 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
1513 1513
 					continue
1514 1514
 				}
1515 1515
 				if err := srv.runtime.volumes.Delete(volumeId); err != nil {
1516
-					return err
1516
+					job.Error(err)
1517
+					return engine.StatusErr
1517 1518
 				}
1518 1519
 			}
1519 1520
 		}
1520 1521
 	} else {
1521
-		return fmt.Errorf("No such container: %s", name)
1522
+		job.Errorf("No such container: %s", name)
1523
+		return engine.StatusErr
1522 1524
 	}
1523
-	return nil
1525
+	return engine.StatusOK
1524 1526
 }
1525 1527
 
1526 1528
 var ErrImageReferenced = errors.New("Image referenced by a repository")