2014-10-05 02:47:54 +00:00
|
|
|
package daemon
|
|
|
|
|
2015-03-25 07:44:12 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
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-04-09 17:52:55 +00:00
|
|
|
func (daemon *Daemon) ContainerRename(oldName, newName string) error {
|
|
|
|
if oldName == "" || newName == "" {
|
|
|
|
return fmt.Errorf("usage: docker rename OLD_NAME NEW_NAME")
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 23:06:35 +00:00
|
|
|
container, err := daemon.Get(oldName)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 01:30:49 +00:00
|
|
|
oldName = container.Name
|
|
|
|
|
2014-10-05 02:47:54 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-02-25 04:19:49 +00:00
|
|
|
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return fmt.Errorf("Error when allocating new name: %s", err)
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
2015-01-14 01:30:49 +00:00
|
|
|
|
|
|
|
container.Name = newName
|
|
|
|
|
2015-03-11 16:17:23 +00:00
|
|
|
undo := func() {
|
|
|
|
container.Name = oldName
|
|
|
|
daemon.reserveName(container.ID, oldName)
|
2015-07-30 21:01:53 +00:00
|
|
|
daemon.containerGraphDB.Delete(newName)
|
2015-03-11 16:17:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
if err := daemon.containerGraphDB.Delete(oldName); err != nil {
|
2015-03-11 16:17:23 +00:00
|
|
|
undo()
|
2015-03-25 07:44:12 +00:00
|
|
|
return fmt.Errorf("Failed to delete container %q: %v", oldName, err)
|
2015-01-14 01:30:49 +00:00
|
|
|
}
|
2014-10-05 02:47:54 +00:00
|
|
|
|
2015-03-11 16:17:23 +00:00
|
|
|
if err := container.toDisk(); err != nil {
|
|
|
|
undo()
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2015-03-11 16:17:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
container.logEvent("rename")
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|