github.com/lmars/docker@v1.6.0-rc2/runconfig/merge.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"strings"
     5  
     6  	log "github.com/Sirupsen/logrus"
     7  	"github.com/docker/docker/nat"
     8  )
     9  
    10  func Merge(userConf, imageConf *Config) error {
    11  	if userConf.User == "" {
    12  		userConf.User = imageConf.User
    13  	}
    14  	if userConf.Memory == 0 {
    15  		userConf.Memory = imageConf.Memory
    16  	}
    17  	if userConf.MemorySwap == 0 {
    18  		userConf.MemorySwap = imageConf.MemorySwap
    19  	}
    20  	if userConf.CpuShares == 0 {
    21  		userConf.CpuShares = imageConf.CpuShares
    22  	}
    23  	if len(userConf.ExposedPorts) == 0 {
    24  		userConf.ExposedPorts = imageConf.ExposedPorts
    25  	} else if imageConf.ExposedPorts != nil {
    26  		if userConf.ExposedPorts == nil {
    27  			userConf.ExposedPorts = make(nat.PortSet)
    28  		}
    29  		for port := range imageConf.ExposedPorts {
    30  			if _, exists := userConf.ExposedPorts[port]; !exists {
    31  				userConf.ExposedPorts[port] = struct{}{}
    32  			}
    33  		}
    34  	}
    35  
    36  	if len(userConf.PortSpecs) > 0 {
    37  		if userConf.ExposedPorts == nil {
    38  			userConf.ExposedPorts = make(nat.PortSet)
    39  		}
    40  		ports, _, err := nat.ParsePortSpecs(userConf.PortSpecs)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		for port := range ports {
    45  			if _, exists := userConf.ExposedPorts[port]; !exists {
    46  				userConf.ExposedPorts[port] = struct{}{}
    47  			}
    48  		}
    49  		userConf.PortSpecs = nil
    50  	}
    51  	if len(imageConf.PortSpecs) > 0 {
    52  		// FIXME: I think we can safely remove this. Leaving it for now for the sake of reverse-compat paranoia.
    53  		log.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
    54  		if userConf.ExposedPorts == nil {
    55  			userConf.ExposedPorts = make(nat.PortSet)
    56  		}
    57  
    58  		ports, _, err := nat.ParsePortSpecs(imageConf.PortSpecs)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		for port := range ports {
    63  			if _, exists := userConf.ExposedPorts[port]; !exists {
    64  				userConf.ExposedPorts[port] = struct{}{}
    65  			}
    66  		}
    67  	}
    68  
    69  	if len(userConf.Env) == 0 {
    70  		userConf.Env = imageConf.Env
    71  	} else {
    72  		for _, imageEnv := range imageConf.Env {
    73  			found := false
    74  			imageEnvKey := strings.Split(imageEnv, "=")[0]
    75  			for _, userEnv := range userConf.Env {
    76  				userEnvKey := strings.Split(userEnv, "=")[0]
    77  				if imageEnvKey == userEnvKey {
    78  					found = true
    79  				}
    80  			}
    81  			if !found {
    82  				userConf.Env = append(userConf.Env, imageEnv)
    83  			}
    84  		}
    85  	}
    86  
    87  	if userConf.Labels == nil {
    88  		userConf.Labels = map[string]string{}
    89  	}
    90  	if imageConf.Labels != nil {
    91  		for l := range userConf.Labels {
    92  			imageConf.Labels[l] = userConf.Labels[l]
    93  		}
    94  		userConf.Labels = imageConf.Labels
    95  	}
    96  
    97  	if len(userConf.Entrypoint) == 0 {
    98  		if len(userConf.Cmd) == 0 {
    99  			userConf.Cmd = imageConf.Cmd
   100  		}
   101  
   102  		if userConf.Entrypoint == nil {
   103  			userConf.Entrypoint = imageConf.Entrypoint
   104  		}
   105  	}
   106  	if userConf.WorkingDir == "" {
   107  		userConf.WorkingDir = imageConf.WorkingDir
   108  	}
   109  	if len(userConf.Volumes) == 0 {
   110  		userConf.Volumes = imageConf.Volumes
   111  	} else {
   112  		for k, v := range imageConf.Volumes {
   113  			userConf.Volumes[k] = v
   114  		}
   115  	}
   116  	return nil
   117  }