2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
2013-07-15 09:12:33 -04:00
|
|
|
import (
|
2013-08-15 19:35:03 -04:00
|
|
|
"fmt"
|
2013-07-15 09:12:33 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-05-02 03:49:23 -04:00
|
|
|
// Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
|
|
|
|
// If OpenStdin is set, then it differs
|
|
|
|
func CompareConfig(a, b *Config) bool {
|
|
|
|
if a == nil || b == nil ||
|
|
|
|
a.OpenStdin || b.OpenStdin {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a.AttachStdout != b.AttachStdout ||
|
|
|
|
a.AttachStderr != b.AttachStderr ||
|
|
|
|
a.User != b.User ||
|
|
|
|
a.Memory != b.Memory ||
|
|
|
|
a.MemorySwap != b.MemorySwap ||
|
2013-05-07 14:16:30 -04:00
|
|
|
a.CpuShares != b.CpuShares ||
|
2013-05-02 03:49:23 -04:00
|
|
|
a.OpenStdin != b.OpenStdin ||
|
2013-07-17 16:51:25 -04:00
|
|
|
a.Tty != b.Tty ||
|
|
|
|
a.VolumesFrom != b.VolumesFrom {
|
2013-05-02 03:49:23 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(a.Cmd) != len(b.Cmd) ||
|
|
|
|
len(a.Dns) != len(b.Dns) ||
|
|
|
|
len(a.Env) != len(b.Env) ||
|
2013-07-11 20:31:07 -04:00
|
|
|
len(a.PortSpecs) != len(b.PortSpecs) ||
|
2013-07-17 16:51:25 -04:00
|
|
|
len(a.Entrypoint) != len(b.Entrypoint) ||
|
|
|
|
len(a.Volumes) != len(b.Volumes) {
|
2013-05-02 03:49:23 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(a.Cmd); i++ {
|
|
|
|
if a.Cmd[i] != b.Cmd[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.Dns); i++ {
|
|
|
|
if a.Dns[i] != b.Dns[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.Env); i++ {
|
|
|
|
if a.Env[i] != b.Env[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a.PortSpecs); i++ {
|
|
|
|
if a.PortSpecs[i] != b.PortSpecs[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 22:20:05 -04:00
|
|
|
for i := 0; i < len(a.Entrypoint); i++ {
|
|
|
|
if a.Entrypoint[i] != b.Entrypoint[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 16:51:25 -04:00
|
|
|
for key := range a.Volumes {
|
|
|
|
if _, exists := b.Volumes[key]; !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 03:49:23 -04:00
|
|
|
return true
|
|
|
|
}
|
2013-05-19 13:46:24 -04:00
|
|
|
|
|
|
|
func MergeConfig(userConf, imageConf *Config) {
|
2013-05-23 16:48:50 -04:00
|
|
|
if userConf.User == "" {
|
2013-05-19 13:46:24 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
if userConf.PortSpecs == nil || len(userConf.PortSpecs) == 0 {
|
|
|
|
userConf.PortSpecs = imageConf.PortSpecs
|
2013-07-15 09:12:33 -04:00
|
|
|
} else {
|
|
|
|
for _, imagePortSpec := range imageConf.PortSpecs {
|
|
|
|
found := false
|
|
|
|
imageNat, _ := parseNat(imagePortSpec)
|
|
|
|
for _, userPortSpec := range userConf.PortSpecs {
|
|
|
|
userNat, _ := parseNat(userPortSpec)
|
2013-07-18 22:47:35 -04:00
|
|
|
if imageNat.Proto == userNat.Proto && imageNat.Backend == userNat.Backend {
|
2013-07-15 09:12:33 -04:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
userConf.PortSpecs = append(userConf.PortSpecs, imagePortSpec)
|
|
|
|
}
|
|
|
|
}
|
2013-05-19 13:46:24 -04:00
|
|
|
}
|
|
|
|
if !userConf.Tty {
|
|
|
|
userConf.Tty = imageConf.Tty
|
|
|
|
}
|
|
|
|
if !userConf.OpenStdin {
|
|
|
|
userConf.OpenStdin = imageConf.OpenStdin
|
|
|
|
}
|
|
|
|
if !userConf.StdinOnce {
|
|
|
|
userConf.StdinOnce = imageConf.StdinOnce
|
|
|
|
}
|
|
|
|
if userConf.Env == nil || len(userConf.Env) == 0 {
|
|
|
|
userConf.Env = imageConf.Env
|
2013-07-15 09:12:33 -04:00
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
2013-05-19 13:46:24 -04:00
|
|
|
}
|
|
|
|
if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
|
|
|
|
userConf.Cmd = imageConf.Cmd
|
|
|
|
}
|
|
|
|
if userConf.Dns == nil || len(userConf.Dns) == 0 {
|
|
|
|
userConf.Dns = imageConf.Dns
|
2013-07-15 09:12:33 -04:00
|
|
|
} else {
|
|
|
|
//duplicates aren't an issue here
|
|
|
|
userConf.Dns = append(userConf.Dns, imageConf.Dns...)
|
2013-05-19 13:46:24 -04:00
|
|
|
}
|
2013-06-24 22:20:05 -04:00
|
|
|
if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
|
|
|
|
userConf.Entrypoint = imageConf.Entrypoint
|
|
|
|
}
|
2013-08-18 14:30:19 -04:00
|
|
|
if userConf.WorkingDir == "" {
|
|
|
|
userConf.WorkingDir = imageConf.WorkingDir
|
|
|
|
}
|
2013-07-17 17:06:46 -04:00
|
|
|
if userConf.VolumesFrom == "" {
|
|
|
|
userConf.VolumesFrom = imageConf.VolumesFrom
|
|
|
|
}
|
2013-07-03 22:33:30 -04:00
|
|
|
if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
|
|
|
|
userConf.Volumes = imageConf.Volumes
|
2013-07-15 09:12:33 -04:00
|
|
|
} else {
|
|
|
|
for k, v := range imageConf.Volumes {
|
|
|
|
userConf.Volumes[k] = v
|
|
|
|
}
|
2013-07-03 22:33:30 -04:00
|
|
|
}
|
2013-05-19 13:46:24 -04:00
|
|
|
}
|
2013-08-15 19:35:03 -04:00
|
|
|
|
|
|
|
func parseLxcConfOpts(opts ListOpts) ([]KeyValuePair, error) {
|
|
|
|
out := make([]KeyValuePair, len(opts))
|
|
|
|
for i, o := range opts {
|
|
|
|
k, v, err := parseLxcOpt(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out[i] = KeyValuePair{Key: k, Value: v}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLxcOpt(opt string) (string, string, error) {
|
|
|
|
parts := strings.SplitN(opt, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", fmt.Errorf("Unable to parse lxc conf option: %s", opt)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
|
|
|
|
}
|