2014-10-05 02:47:54 +00:00
|
|
|
package daemon
|
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
import (
|
2015-11-03 17:33:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2015-10-23 15:01:07 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-18 17:48:16 +00:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-10-23 15:01:07 +00:00
|
|
|
"github.com/docker/libnetwork"
|
2015-03-25 07:44:12 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerRename changes the name of a container, using the oldName
|
|
|
|
// to find the container. An error is returned if newName is already
|
|
|
|
// reserved.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
2015-10-23 15:01:07 +00:00
|
|
|
var (
|
2015-11-12 19:55:17 +00:00
|
|
|
sid string
|
|
|
|
sb libnetwork.Sandbox
|
2015-10-23 15:01:07 +00:00
|
|
|
)
|
|
|
|
|
2015-04-09 17:52:55 +00:00
|
|
|
if oldName == "" || newName == "" {
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeEmptyRename
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 17:39:28 +00:00
|
|
|
container, err := daemon.GetContainer(oldName)
|
2014-12-16 23:06:35 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 01:30:49 +00:00
|
|
|
oldName = container.Name
|
|
|
|
|
2014-10-05 02:47:54 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-09-29 17:51:40 +00:00
|
|
|
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeRenameTaken.WithArgs(err)
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
2015-01-14 01:30:49 +00:00
|
|
|
|
|
|
|
container.Name = newName
|
|
|
|
|
2015-10-23 15:01:07 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
container.Name = oldName
|
|
|
|
daemon.reserveName(container.ID, oldName)
|
|
|
|
daemon.containerGraphDB.Delete(newName)
|
|
|
|
}
|
|
|
|
}()
|
2015-03-11 16:17:23 +00:00
|
|
|
|
2015-10-23 15:01:07 +00:00
|
|
|
if err = daemon.containerGraphDB.Delete(oldName); err != nil {
|
2015-09-18 17:48:16 +00:00
|
|
|
return derr.ErrorCodeRenameDelete.WithArgs(oldName, err)
|
2015-01-14 01:30:49 +00:00
|
|
|
}
|
2014-10-05 02:47:54 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
if err = container.ToDisk(); err != nil {
|
2015-10-23 15:01:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.Running {
|
2015-11-03 17:33:13 +00:00
|
|
|
daemon.LogContainerEvent(container, "rename")
|
2015-10-23 15:01:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
container.Name = oldName
|
2015-11-12 19:55:17 +00:00
|
|
|
if e := container.ToDisk(); e != nil {
|
2015-10-23 15:01:07 +00:00
|
|
|
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 {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2015-03-11 16:17:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:33:13 +00:00
|
|
|
daemon.LogContainerEvent(container, "rename")
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|