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