2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-10-05 02:47:54 +00:00
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
import (
|
2015-11-03 17:33:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-06-28 01:48:11 +00:00
|
|
|
dockercontainer "github.com/docker/docker/container"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork"
|
2017-07-19 14:20:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
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 == "" {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(errors.New("Neither old nor new names may be empty"))
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 01:40:44 +00:00
|
|
|
if newName[0] != '/' {
|
|
|
|
newName = "/" + newName
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-04 15:35:58 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-01-14 01:30:49 +00:00
|
|
|
oldName = container.Name
|
2016-05-13 02:45:42 +00:00
|
|
|
oldIsAnonymousEndpoint := container.NetworkSettings.IsAnonymousEndpoint
|
2015-01-14 01:30:49 +00:00
|
|
|
|
2016-06-08 01:40:44 +00:00
|
|
|
if oldName == newName {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(errors.New("Renaming a container with the same name as its current name"))
|
2016-06-08 01:40:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 01:48:11 +00:00
|
|
|
links := map[string]*dockercontainer.Container{}
|
|
|
|
for k, v := range daemon.linkIndex.children(container) {
|
|
|
|
if !strings.HasPrefix(k, oldName) {
|
2017-11-29 04:09:37 +00:00
|
|
|
return errdefs.InvalidParameter(errors.Errorf("Linked container %s does not match parent %s", k, oldName))
|
2016-06-28 01:48:11 +00:00
|
|
|
}
|
|
|
|
links[strings.TrimPrefix(k, oldName)] = v
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
|
2017-07-19 14:20:13 +00:00
|
|
|
return errors.Wrap(err, "Error when allocating new name")
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
2015-01-14 01:30:49 +00:00
|
|
|
|
2016-06-28 01:48:11 +00:00
|
|
|
for k, v := range links {
|
2017-06-30 01:56:22 +00:00
|
|
|
daemon.containersReplica.ReserveName(newName+k, v.ID)
|
2016-06-28 01:48:11 +00:00
|
|
|
daemon.linkIndex.link(container, v, newName+k)
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:30:49 +00:00
|
|
|
container.Name = newName
|
2016-05-13 02:45:42 +00:00
|
|
|
container.NetworkSettings.IsAnonymousEndpoint = false
|
2015-01-14 01:30:49 +00:00
|
|
|
|
2015-10-23 15:01:07 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
container.Name = oldName
|
2016-05-13 02:45:42 +00:00
|
|
|
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
|
2015-10-23 15:01:07 +00:00
|
|
|
daemon.reserveName(container.ID, oldName)
|
2016-06-28 01:48:11 +00:00
|
|
|
for k, v := range links {
|
2017-06-30 01:56:22 +00:00
|
|
|
daemon.containersReplica.ReserveName(oldName+k, v.ID)
|
2016-06-28 01:48:11 +00:00
|
|
|
daemon.linkIndex.link(container, v, oldName+k)
|
|
|
|
daemon.linkIndex.unlink(newName+k, v, container)
|
2017-06-30 01:56:22 +00:00
|
|
|
daemon.containersReplica.ReleaseName(newName + k)
|
2016-06-28 01:48:11 +00:00
|
|
|
}
|
2015-09-04 00:51:04 +00:00
|
|
|
daemon.releaseName(newName)
|
2018-09-20 06:16:30 +00:00
|
|
|
} else {
|
|
|
|
daemon.releaseName(oldName)
|
2015-10-23 15:01:07 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-03-11 16:17:23 +00:00
|
|
|
|
2016-06-28 01:48:11 +00:00
|
|
|
for k, v := range links {
|
|
|
|
daemon.linkIndex.unlink(oldName+k, v, container)
|
2017-06-30 01:56:22 +00:00
|
|
|
daemon.containersReplica.ReleaseName(oldName + k)
|
2016-06-28 01:48:11 +00:00
|
|
|
}
|
2017-03-27 17:18:53 +00:00
|
|
|
if err = container.CheckpointTo(daemon.containersReplica); err != nil {
|
2015-10-23 15:01:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:14:05 +00:00
|
|
|
attributes := map[string]string{
|
|
|
|
"oldName": oldName,
|
|
|
|
}
|
|
|
|
|
2015-10-23 15:01:07 +00:00
|
|
|
if !container.Running {
|
2016-01-07 22:14:05 +00:00
|
|
|
daemon.LogContainerEventWithAttributes(container, "rename", attributes)
|
2015-10-23 15:01:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
container.Name = oldName
|
2016-05-13 02:45:42 +00:00
|
|
|
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
|
2017-03-27 17:18:53 +00:00
|
|
|
if e := container.CheckpointTo(daemon.containersReplica); 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
|
2017-01-11 13:31:45 +00:00
|
|
|
if sid != "" && daemon.netController != nil {
|
2016-01-23 05:02:39 +00:00
|
|
|
sb, err = daemon.netController.SandboxByID(sid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-23 15:01:07 +00:00
|
|
|
|
2016-01-23 05:02:39 +00:00
|
|
|
err = sb.Rename(strings.TrimPrefix(container.Name, "/"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-11 16:17:23 +00:00
|
|
|
}
|
2016-01-07 22:14:05 +00:00
|
|
|
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "rename", attributes)
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|