2014-10-04 22:47:54 -04:00
|
|
|
package daemon
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
import (
|
2015-09-18 13:48:16 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
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-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-09-29 13:51:40 -04:00
|
|
|
container, err := daemon.Get(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-03-11 12:17:23 -04:00
|
|
|
undo := func() {
|
|
|
|
container.Name = oldName
|
2015-09-29 13:51:40 -04:00
|
|
|
daemon.reserveName(container.ID, oldName)
|
2015-07-30 17:01:53 -04:00
|
|
|
daemon.containerGraphDB.Delete(newName)
|
2015-03-11 12:17:23 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
if err := daemon.containerGraphDB.Delete(oldName); err != nil {
|
2015-03-11 12:17:23 -04:00
|
|
|
undo()
|
2015-09-18 13:48:16 -04:00
|
|
|
return derr.ErrorCodeRenameDelete.WithArgs(oldName, err)
|
2015-01-13 20:30:49 -05:00
|
|
|
}
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-03-11 12:17:23 -04:00
|
|
|
if err := container.toDisk(); err != nil {
|
|
|
|
undo()
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2015-03-11 12:17:23 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
container.logEvent("rename")
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|