2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2014-05-19 18:04:51 -04:00
|
|
|
"strings"
|
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/nat"
|
2014-07-24 16:37:44 -04:00
|
|
|
"github.com/docker/docker/pkg/log"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func Merge(userConf, imageConf *Config) error {
|
|
|
|
if userConf.User == "" {
|
|
|
|
userConf.User = imageConf.User
|
|
|
|
}
|
|
|
|
if userConf.Memory == 0 {
|
|
|
|
userConf.Memory = imageConf.Memory
|
|
|
|
}
|
|
|
|
if userConf.MemorySwap == 0 {
|
|
|
|
userConf.MemorySwap = imageConf.MemorySwap
|
|
|
|
}
|
|
|
|
if userConf.CpuShares == 0 {
|
|
|
|
userConf.CpuShares = imageConf.CpuShares
|
|
|
|
}
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.ExposedPorts) == 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
userConf.ExposedPorts = imageConf.ExposedPorts
|
|
|
|
} else if imageConf.ExposedPorts != nil {
|
|
|
|
if userConf.ExposedPorts == nil {
|
|
|
|
userConf.ExposedPorts = make(nat.PortSet)
|
|
|
|
}
|
|
|
|
for port := range imageConf.ExposedPorts {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.PortSpecs) > 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
if userConf.ExposedPorts == nil {
|
|
|
|
userConf.ExposedPorts = make(nat.PortSet)
|
|
|
|
}
|
|
|
|
ports, _, err := nat.ParsePortSpecs(userConf.PortSpecs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for port := range ports {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userConf.PortSpecs = nil
|
|
|
|
}
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(imageConf.PortSpecs) > 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
// FIXME: I think we can safely remove this. Leaving it for now for the sake of reverse-compat paranoia.
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
|
2014-02-11 23:04:39 -05:00
|
|
|
if userConf.ExposedPorts == nil {
|
|
|
|
userConf.ExposedPorts = make(nat.PortSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
ports, _, err := nat.ParsePortSpecs(imageConf.PortSpecs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for port := range ports {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-13 16:58:09 -04:00
|
|
|
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.Env) == 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
userConf.Env = imageConf.Env
|
|
|
|
} else {
|
|
|
|
for _, imageEnv := range imageConf.Env {
|
|
|
|
found := false
|
|
|
|
imageEnvKey := strings.Split(imageEnv, "=")[0]
|
|
|
|
for _, userEnv := range userConf.Env {
|
|
|
|
userEnvKey := strings.Split(userEnv, "=")[0]
|
|
|
|
if imageEnvKey == userEnvKey {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
userConf.Env = append(userConf.Env, imageEnv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.Entrypoint) == 0 {
|
2014-07-10 01:46:11 -04:00
|
|
|
if len(userConf.Cmd) == 0 {
|
|
|
|
userConf.Cmd = imageConf.Cmd
|
|
|
|
}
|
2014-10-23 20:23:25 -04:00
|
|
|
|
|
|
|
if userConf.Entrypoint == nil {
|
|
|
|
userConf.Entrypoint = imageConf.Entrypoint
|
|
|
|
}
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
|
|
|
if userConf.WorkingDir == "" {
|
|
|
|
userConf.WorkingDir = imageConf.WorkingDir
|
|
|
|
}
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.Volumes) == 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
userConf.Volumes = imageConf.Volumes
|
|
|
|
} else {
|
|
|
|
for k, v := range imageConf.Volumes {
|
|
|
|
userConf.Volumes[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|