github.com/ld86/docker@v1.7.1-rc3/daemon/rename.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  func (daemon *Daemon) ContainerRename(oldName, newName string) error {
     8  	if oldName == "" || newName == "" {
     9  		return fmt.Errorf("usage: docker rename OLD_NAME NEW_NAME")
    10  	}
    11  
    12  	container, err := daemon.Get(oldName)
    13  	if err != nil {
    14  		return err
    15  	}
    16  
    17  	oldName = container.Name
    18  
    19  	container.Lock()
    20  	defer container.Unlock()
    21  	if newName, err = daemon.reserveName(container.ID, newName); err != nil {
    22  		return fmt.Errorf("Error when allocating new name: %s", err)
    23  	}
    24  
    25  	container.Name = newName
    26  
    27  	undo := func() {
    28  		container.Name = oldName
    29  		daemon.reserveName(container.ID, oldName)
    30  		daemon.containerGraph.Delete(newName)
    31  	}
    32  
    33  	if err := daemon.containerGraph.Delete(oldName); err != nil {
    34  		undo()
    35  		return fmt.Errorf("Failed to delete container %q: %v", oldName, err)
    36  	}
    37  
    38  	if err := container.toDisk(); err != nil {
    39  		undo()
    40  		return err
    41  	}
    42  
    43  	return nil
    44  }