mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
8084f80259
During the renaming of a container, no need to call `container.Lock()` if `oldName == newName`. This is a follow-up from #23360 (commit88d1ee6c11
) Signed-off-by: Vincent Demeester <vincent@sbr.pm> (cherry picked from commit7e1ec8d2bd
)
98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package daemon
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/docker/libnetwork"
|
|
)
|
|
|
|
// ContainerRename changes the name of a container, using the oldName
|
|
// to find the container. An error is returned if newName is already
|
|
// reserved.
|
|
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
|
var (
|
|
sid string
|
|
sb libnetwork.Sandbox
|
|
)
|
|
|
|
if oldName == "" || newName == "" {
|
|
return fmt.Errorf("Neither old nor new names may be empty")
|
|
}
|
|
|
|
if newName[0] != '/' {
|
|
newName = "/" + newName
|
|
}
|
|
|
|
container, err := daemon.GetContainer(oldName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
oldName = container.Name
|
|
oldIsAnonymousEndpoint := container.NetworkSettings.IsAnonymousEndpoint
|
|
|
|
if oldName == newName {
|
|
return fmt.Errorf("Renaming a container with the same name as its current name")
|
|
}
|
|
|
|
container.Lock()
|
|
defer container.Unlock()
|
|
|
|
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
|
|
return fmt.Errorf("Error when allocating new name: %v", err)
|
|
}
|
|
|
|
container.Name = newName
|
|
container.NetworkSettings.IsAnonymousEndpoint = false
|
|
|
|
defer func() {
|
|
if err != nil {
|
|
container.Name = oldName
|
|
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
|
|
daemon.reserveName(container.ID, oldName)
|
|
daemon.releaseName(newName)
|
|
}
|
|
}()
|
|
|
|
daemon.releaseName(oldName)
|
|
if err = container.ToDisk(); err != nil {
|
|
return err
|
|
}
|
|
|
|
attributes := map[string]string{
|
|
"oldName": oldName,
|
|
}
|
|
|
|
if !container.Running {
|
|
daemon.LogContainerEventWithAttributes(container, "rename", attributes)
|
|
return nil
|
|
}
|
|
|
|
defer func() {
|
|
if err != nil {
|
|
container.Name = oldName
|
|
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
|
|
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
|
|
if daemon.netController != nil {
|
|
sb, err = daemon.netController.SandboxByID(sid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = sb.Rename(strings.TrimPrefix(container.Name, "/"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "rename", attributes)
|
|
return nil
|
|
}
|