github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/daemon/update.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/engine-api/types/container"
     7  )
     8  
     9  // ContainerUpdate updates configuration of the container
    10  func (daemon *Daemon) ContainerUpdate(name string, hostConfig *container.HostConfig, validateHostname bool) ([]string, error) {
    11  	var warnings []string
    12  
    13  	warnings, err := daemon.verifyContainerSettings(hostConfig, nil, true, validateHostname)
    14  	if err != nil {
    15  		return warnings, err
    16  	}
    17  
    18  	if err := daemon.update(name, hostConfig); err != nil {
    19  		return warnings, err
    20  	}
    21  
    22  	return warnings, nil
    23  }
    24  
    25  // ContainerUpdateCmdOnBuild updates Path and Args for the container with ID cID.
    26  func (daemon *Daemon) ContainerUpdateCmdOnBuild(cID string, cmd []string) error {
    27  	if len(cmd) == 0 {
    28  		return nil
    29  	}
    30  	c, err := daemon.GetContainer(cID)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	c.Path = cmd[0]
    35  	c.Args = cmd[1:]
    36  	return nil
    37  }
    38  
    39  func (daemon *Daemon) update(name string, hostConfig *container.HostConfig) error {
    40  	if hostConfig == nil {
    41  		return nil
    42  	}
    43  
    44  	container, err := daemon.GetContainer(name)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	restoreConfig := false
    50  	backupHostConfig := *container.HostConfig
    51  	defer func() {
    52  		if restoreConfig {
    53  			container.Lock()
    54  			container.HostConfig = &backupHostConfig
    55  			container.ToDisk()
    56  			container.Unlock()
    57  		}
    58  	}()
    59  
    60  	if container.RemovalInProgress || container.Dead {
    61  		return errCannotUpdate(container.ID, fmt.Errorf("Container is marked for removal and cannot be \"update\"."))
    62  	}
    63  
    64  	if err := container.UpdateContainer(hostConfig); err != nil {
    65  		restoreConfig = true
    66  		return errCannotUpdate(container.ID, err)
    67  	}
    68  
    69  	// if Restart Policy changed, we need to update container monitor
    70  	container.UpdateMonitor(hostConfig.RestartPolicy)
    71  
    72  	// If container is not running, update hostConfig struct is enough,
    73  	// resources will be updated when the container is started again.
    74  	// If container is running (including paused), we need to update configs
    75  	// to the real world.
    76  	if container.IsRunning() && !container.IsRestarting() {
    77  		if err := daemon.containerd.UpdateResources(container.ID, toContainerdResources(hostConfig.Resources)); err != nil {
    78  			restoreConfig = true
    79  			return errCannotUpdate(container.ID, err)
    80  		}
    81  	}
    82  
    83  	daemon.LogContainerEvent(container, "update")
    84  
    85  	return nil
    86  }
    87  
    88  func errCannotUpdate(containerID string, err error) error {
    89  	return fmt.Errorf("Cannot update container %s: %v", containerID, err)
    90  }