github.com/ld86/docker@v1.7.1-rc3/runconfig/merge.go (about)

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