2014-10-04 22:47:54 -04:00
|
|
|
package daemon
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
import (
|
2015-11-03 12:33:13 -05:00
|
|
|
"strings"
|
|
|
|
|
2015-10-23 11:01:07 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-09-18 13:48:16 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-10-23 11:01:07 -04:00
|
|
|
"github.com/docker/libnetwork"
|
2015-03-25 03:44:12 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04: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 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
2015-10-23 11:01:07 -04:00
|
|
|
var (
|
2015-11-12 14:55:17 -05:00
|
|
|
sid string
|
|
|
|
sb libnetwork.Sandbox
|
2015-10-23 11:01:07 -04:00
|
|
|
)
|
|
|
|
|
2015-04-09 13:52:55 -04:00
|
|
|
if oldName == "" || newName == "" {
|
2015-09-18 13:48:16 -04:00
|
|
|
return derr.ErrorCodeEmptyRename
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(oldName)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-01-13 20:30:49 -05:00
|
|
|
oldName = container.Name
|
|
|
|
|
2014-10-04 22:47:54 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-09-29 13:51:40 -04:00
|
|
|
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
|
2015-09-18 13:48:16 -04:00
|
|
|
return derr.ErrorCodeRenameTaken.WithArgs(err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
2015-01-13 20:30:49 -05:00
|
|
|
|
|
|
|
container.Name = newName
|
|
|
|
|
2015-10-23 11:01:07 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
container.Name = oldName
|
|
|
|
daemon.reserveName(container.ID, oldName)
|
2015-09-03 20:51:04 -04:00
|
|
|
daemon.releaseName(newName)
|
2015-10-23 11:01:07 -04:00
|
|
|
}
|
|
|
|
}()
|
2015-03-11 12:17:23 -04:00
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
daemon.releaseName(oldName)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err = container.ToDisk(); err != nil {
|
2015-10-23 11:01:07 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:14:05 -05:00
|
|
|
attributes := map[string]string{
|
|
|
|
"oldName": oldName,
|
|
|
|
}
|
|
|
|
|
2015-10-23 11:01:07 -04:00
|
|
|
if !container.Running {
|
2016-01-07 17:14:05 -05:00
|
|
|
daemon.LogContainerEventWithAttributes(container, "rename", attributes)
|
2015-10-23 11:01:07 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
container.Name = oldName
|
2015-11-12 14:55:17 -05:00
|
|
|
if e := container.ToDisk(); e != nil {
|
2015-10-23 11:01:07 -04:00
|
|
|
logrus.Errorf("%s: Failed in writing to Disk on rename failure: %v", container.ID, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
sid = container.NetworkSettings.SandboxID
|
2016-01-23 00:02:39 -05:00
|
|
|
if daemon.netController != nil {
|
|
|
|
sb, err = daemon.netController.SandboxByID(sid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-23 11:01:07 -04:00
|
|
|
|
2016-01-23 00:02:39 -05:00
|
|
|
err = sb.Rename(strings.TrimPrefix(container.Name, "/"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-11 12:17:23 -04:00
|
|
|
}
|
2016-01-07 17:14:05 -05:00
|
|
|
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "rename", attributes)
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|