mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add libnetwork call on daemon rename
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
This commit is contained in:
parent
91de5ddf18
commit
8e0bbb2898
1 changed files with 45 additions and 10 deletions
|
@ -1,18 +1,28 @@
|
||||||
package daemon
|
package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
derr "github.com/docker/docker/errors"
|
derr "github.com/docker/docker/errors"
|
||||||
|
"github.com/docker/libnetwork"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContainerRename changes the name of a container, using the oldName
|
// ContainerRename changes the name of a container, using the oldName
|
||||||
// to find the container. An error is returned if newName is already
|
// to find the container. An error is returned if newName is already
|
||||||
// reserved.
|
// reserved.
|
||||||
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
sid string
|
||||||
|
sb libnetwork.Sandbox
|
||||||
|
container *Container
|
||||||
|
)
|
||||||
|
|
||||||
if oldName == "" || newName == "" {
|
if oldName == "" || newName == "" {
|
||||||
return derr.ErrorCodeEmptyRename
|
return derr.ErrorCodeEmptyRename
|
||||||
}
|
}
|
||||||
|
|
||||||
container, err := daemon.Get(oldName)
|
container, err = daemon.Get(oldName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -27,19 +37,44 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
||||||
|
|
||||||
container.Name = newName
|
container.Name = newName
|
||||||
|
|
||||||
undo := func() {
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
container.Name = oldName
|
container.Name = oldName
|
||||||
daemon.reserveName(container.ID, oldName)
|
daemon.reserveName(container.ID, oldName)
|
||||||
daemon.containerGraphDB.Delete(newName)
|
daemon.containerGraphDB.Delete(newName)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if err := daemon.containerGraphDB.Delete(oldName); err != nil {
|
if err = daemon.containerGraphDB.Delete(oldName); err != nil {
|
||||||
undo()
|
|
||||||
return derr.ErrorCodeRenameDelete.WithArgs(oldName, err)
|
return derr.ErrorCodeRenameDelete.WithArgs(oldName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := container.toDisk(); err != nil {
|
if err = container.toDisk(); err != nil {
|
||||||
undo()
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !container.Running {
|
||||||
|
container.logEvent("rename")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
container.Name = oldName
|
||||||
|
if e := container.toDisk(); e != nil {
|
||||||
|
logrus.Errorf("%s: Failed in writing to Disk on rename failure: %v", container.ID, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
sid = container.NetworkSettings.SandboxID
|
||||||
|
sb, err = daemon.netController.SandboxByID(sid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sb.Rename(strings.TrimPrefix(container.Name, "/"))
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue