1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/rename.go
Brian Goff ebcb7d6b40 Remove string checking in API error handling
Use strongly typed errors to set HTTP status codes.
Error interfaces are defined in the api/errors package and errors
returned from controllers are checked against these interfaces.

Errors can be wraeped in a pkg/errors.Causer, as long as somewhere in the
line of causes one of the interfaces is implemented. The special error
interfaces take precedence over Causer, meaning if both Causer and one
of the new error interfaces are implemented, the Causer is not
traversed.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2017-08-15 16:01:11 -04:00

122 lines
3.1 KiB
Go

package daemon
import (
"strings"
dockercontainer "github.com/docker/docker/container"
"github.com/docker/libnetwork"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// 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 validationError{errors.New("Neither old nor new names may be empty")}
}
if newName[0] != '/' {
newName = "/" + newName
}
container, err := daemon.GetContainer(oldName)
if err != nil {
return err
}
container.Lock()
defer container.Unlock()
oldName = container.Name
oldIsAnonymousEndpoint := container.NetworkSettings.IsAnonymousEndpoint
if oldName == newName {
return validationError{errors.New("Renaming a container with the same name as its current name")}
}
links := map[string]*dockercontainer.Container{}
for k, v := range daemon.linkIndex.children(container) {
if !strings.HasPrefix(k, oldName) {
return validationError{errors.Errorf("Linked container %s does not match parent %s", k, oldName)}
}
links[strings.TrimPrefix(k, oldName)] = v
}
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
return errors.Wrap(err, "Error when allocating new name")
}
for k, v := range links {
daemon.containersReplica.ReserveName(newName+k, v.ID)
daemon.linkIndex.link(container, v, newName+k)
}
container.Name = newName
container.NetworkSettings.IsAnonymousEndpoint = false
defer func() {
if err != nil {
container.Name = oldName
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
daemon.reserveName(container.ID, oldName)
for k, v := range links {
daemon.containersReplica.ReserveName(oldName+k, v.ID)
daemon.linkIndex.link(container, v, oldName+k)
daemon.linkIndex.unlink(newName+k, v, container)
daemon.containersReplica.ReleaseName(newName + k)
}
daemon.releaseName(newName)
}
}()
for k, v := range links {
daemon.linkIndex.unlink(oldName+k, v, container)
daemon.containersReplica.ReleaseName(oldName + k)
}
daemon.releaseName(oldName)
if err = container.CheckpointTo(daemon.containersReplica); 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.CheckpointTo(daemon.containersReplica); e != nil {
logrus.Errorf("%s: Failed in writing to Disk on rename failure: %v", container.ID, e)
}
}
}()
sid = container.NetworkSettings.SandboxID
if sid != "" && 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
}