2014-10-05 06:47:54 +04:00
|
|
|
package daemon
|
|
|
|
|
2015-03-25 10:44:12 +03:00
|
|
|
import (
|
2015-09-11 01:01:18 +03:00
|
|
|
"github.com/docker/docker/context"
|
2015-09-18 20:48:16 +03:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-03-25 10:44:12 +03:00
|
|
|
)
|
|
|
|
|
2015-07-31 00:01:53 +03: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-11 01:01:18 +03:00
|
|
|
func (daemon *Daemon) ContainerRename(ctx context.Context, oldName, newName string) error {
|
2015-04-09 20:52:55 +03:00
|
|
|
if oldName == "" || newName == "" {
|
2015-09-18 20:48:16 +03:00
|
|
|
return derr.ErrorCodeEmptyRename
|
2014-10-05 06:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
container, err := daemon.Get(ctx, oldName)
|
2014-12-17 02:06:35 +03:00
|
|
|
if err != nil {
|
2015-03-25 10:44:12 +03:00
|
|
|
return err
|
2014-10-05 06:47:54 +04:00
|
|
|
}
|
|
|
|
|
2015-01-14 04:30:49 +03:00
|
|
|
oldName = container.Name
|
|
|
|
|
2014-10-05 06:47:54 +04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-09-11 01:01:18 +03:00
|
|
|
if newName, err = daemon.reserveName(ctx, container.ID, newName); err != nil {
|
2015-09-18 20:48:16 +03:00
|
|
|
return derr.ErrorCodeRenameTaken.WithArgs(err)
|
2014-10-05 06:47:54 +04:00
|
|
|
}
|
2015-01-14 04:30:49 +03:00
|
|
|
|
|
|
|
container.Name = newName
|
|
|
|
|
2015-03-11 19:17:23 +03:00
|
|
|
undo := func() {
|
|
|
|
container.Name = oldName
|
2015-09-11 01:01:18 +03:00
|
|
|
daemon.reserveName(ctx, container.ID, oldName)
|
2015-07-31 00:01:53 +03:00
|
|
|
daemon.containerGraphDB.Delete(newName)
|
2015-03-11 19:17:23 +03:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
if err := daemon.containerGraphDB.Delete(oldName); err != nil {
|
2015-03-11 19:17:23 +03:00
|
|
|
undo()
|
2015-09-18 20:48:16 +03:00
|
|
|
return derr.ErrorCodeRenameDelete.WithArgs(oldName, err)
|
2015-01-14 04:30:49 +03:00
|
|
|
}
|
2014-10-05 06:47:54 +04:00
|
|
|
|
2015-03-11 19:17:23 +03:00
|
|
|
if err := container.toDisk(); err != nil {
|
|
|
|
undo()
|
2015-03-25 10:44:12 +03:00
|
|
|
return err
|
2015-03-11 19:17:23 +03:00
|
|
|
}
|
|
|
|
|
2015-09-11 01:01:18 +03:00
|
|
|
container.logEvent(ctx, "rename")
|
2015-03-25 10:44:12 +03:00
|
|
|
return nil
|
2014-10-05 06:47:54 +04:00
|
|
|
}
|