1
0
mirror of https://github.com/docker/docs.git synced 2026-10-31 10:34:26 +03:00

move check into cluster and rename func

Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
Xian Chaobo
2015-04-27 08:20:55 -04:00
parent b47e002e6c
commit 3d2537eb84
6 changed files with 37 additions and 26 deletions

View File

@@ -490,7 +490,7 @@ func postCommit(c *context, w http.ResponseWriter, r *http.Request) {
}
// POST /containers/{name:.*}/rename
func postRename(c *context, w http.ResponseWriter, r *http.Request) {
func postRenameContainer(c *context, w http.ResponseWriter, r *http.Request) {
container, err := getContainerFromVars(c, mux.Vars(r))
if err != nil {
httpError(w, err.Error(), http.StatusNotFound)
@@ -502,15 +502,10 @@ func postRename(c *context, w http.ResponseWriter, r *http.Request) {
return
}
// check new name whether avaliable
newName := r.Form.Get("name")
if conflictContainer := c.cluster.Container(newName); conflictContainer != nil {
httpError(w, fmt.Sprintf("Conflict, The new name %s is already assigned to %s. ", newName, conflictContainer.Id), http.StatusConflict)
return
}
// call cluster rename
err = c.cluster.Rename(container, newName)
// call cluster rename container
err = c.cluster.RenameContainer(container, newName)
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
}

View File

@@ -55,7 +55,7 @@ var routes = map[string]map[string]handler{
"/containers/{name:.*}/kill": proxyContainer,
"/containers/{name:.*}/pause": proxyContainer,
"/containers/{name:.*}/unpause": proxyContainer,
"/containers/{name:.*}/rename": postRename,
"/containers/{name:.*}/rename": postRenameContainer,
"/containers/{name:.*}/restart": proxyContainer,
"/containers/{name:.*}/start": proxyContainer,
"/containers/{name:.*}/stop": proxyContainer,

View File

@@ -44,6 +44,6 @@ type Cluster interface {
// Return a random engine
RANDOMENGINE() (*Engine, error)
// Rename a container
Rename(container *Container, newName string) error
// RenameContainer rename a container
RenameContainer(container *Container, newName string) error
}

View File

@@ -556,8 +556,8 @@ func (e *Engine) cleanupContainers() {
e.Unlock()
}
// Rename a container
func (e *Engine) Rename(container *Container, newName string) error {
// RenameContainer rename a container
func (e *Engine) RenameContainer(container *Container, newName string) error {
// send rename request
err := e.client.RenameContainer(container.Id, newName)
if err != nil {

View File

@@ -334,13 +334,18 @@ func (c *Cluster) RANDOMENGINE() (*cluster.Engine, error) {
return nil, nil
}
// Rename a container
func (c *Cluster) Rename(container *cluster.Container, newName string) error {
// RenameContainer rename a container
func (c *Cluster) RenameContainer(container *cluster.Container, newName string) error {
c.RLock()
defer c.RUnlock()
// check new name whether avaliable
if conflictContainer := c.Container(newName); conflictContainer != nil {
return fmt.Errorf("Error when allocating new name: Conflict. The name %q is already in use by container %s. You have to delete (or rename) that container to be able to reuse that name.", newName, conflictContainer.Id)
}
// call engine rename
err := container.Engine.Rename(container, newName)
err := container.Engine.RenameContainer(container, newName)
if err != nil {
return err
}

View File

@@ -359,20 +359,31 @@ function teardown() {
run docker_swarm run -d --name test_container busybox sleep 500
[ "$status" -eq 0 ]
# make sure container exist
run docker_swarm ps -l
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[*]}" == *"test_container"* ]]
[[ "${lines[*]}" != *"rename_container"* ]]
run docker_swarm run -d --name another_container busybox sleep 500
[ "$status" -eq 0 ]
# rename container
# make sure container exist
run docker_swarm ps -a
[ "${#lines[@]}" -eq 3 ]
[[ "${output}" == *"test_container"* ]]
[[ "${output}" == *"another_container"* ]]
[[ "${output}" != *"rename_container"* ]]
# rename container, conflict and fail
run docker_swarm rename test_container another_container
[ "$status" -ne 0 ]
[[ "${output}" == *"Error when allocating new name: Conflict."* ]]
# rename container, sucessful
run docker_swarm rename test_container rename_container
[ "$status" -eq 0 ]
# verify after rename
run docker_swarm ps -l
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[*]}" == *"rename_container"* ]]
# verify after, rename
run docker_swarm ps -a
[ "${#lines[@]}" -eq 3 ]
[[ "${output}" == *"rename_container"* ]]
[[ "${output}" == *"another_container"* ]]
[[ "${output}" != *"test_container"* ]]
}
@test "docker restart" {