utils.go
a27b4b8c
 package docker
 
193a7e1d
 import (
551092f9
 	"fmt"
c69549e0
 	"github.com/dotcloud/docker/utils"
 	"strconv"
193a7e1d
 	"strings"
 )
 
a46fc3a5
 // 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 ||
efd9becb
 		a.CpuShares != b.CpuShares ||
a46fc3a5
 		a.OpenStdin != b.OpenStdin ||
7c002012
 		a.Tty != b.Tty ||
 		a.VolumesFrom != b.VolumesFrom {
a46fc3a5
 		return false
 	}
 	if len(a.Cmd) != len(b.Cmd) ||
 		len(a.Dns) != len(b.Dns) ||
 		len(a.Env) != len(b.Env) ||
71d2ff49
 		len(a.PortSpecs) != len(b.PortSpecs) ||
1b3baab4
 		len(a.ExposedPorts) != len(b.ExposedPorts) ||
7c002012
 		len(a.Entrypoint) != len(b.Entrypoint) ||
 		len(a.Volumes) != len(b.Volumes) {
a46fc3a5
 		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
 		}
 	}
1b3baab4
 	for k := range a.ExposedPorts {
 		if _, exists := b.ExposedPorts[k]; !exists {
 			return false
 		}
 	}
b16ff9f8
 	for i := 0; i < len(a.Entrypoint); i++ {
 		if a.Entrypoint[i] != b.Entrypoint[i] {
 			return false
 		}
 	}
7c002012
 	for key := range a.Volumes {
 		if _, exists := b.Volumes[key]; !exists {
 			return false
 		}
 	}
a46fc3a5
 	return true
 }
0f312113
 
 func MergeConfig(userConf, imageConf *Config) {
83bc5b74
 	if userConf.User == "" {
0f312113
 		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
 	}
c69549e0
 	if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
 		userConf.ExposedPorts = imageConf.ExposedPorts
 	}
 
 	if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
 		if userConf.ExposedPorts == nil {
 			userConf.ExposedPorts = make(map[Port]struct{})
 		}
 		ports, _, err := parsePortSpecs(userConf.PortSpecs)
 		if err != nil {
 			panic(err)
 		}
 		for port := range ports {
 			if _, exists := userConf.ExposedPorts[port]; !exists {
 				userConf.ExposedPorts[port] = struct{}{}
193a7e1d
 			}
c69549e0
 		}
1b3baab4
 		userConf.PortSpecs = nil
c69549e0
 	}
 	if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
1b3baab4
 		utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
c69549e0
 		if userConf.ExposedPorts == nil {
 			userConf.ExposedPorts = make(map[Port]struct{})
 		}
 
 		ports, _, err := parsePortSpecs(imageConf.PortSpecs)
 		if err != nil {
 			panic(err)
 		}
 		for port := range ports {
 			if _, exists := userConf.ExposedPorts[port]; !exists {
 				userConf.ExposedPorts[port] = struct{}{}
193a7e1d
 			}
 		}
0f312113
 	}
 	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
193a7e1d
 	} 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)
 			}
 		}
0f312113
 	}
 	if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
 		userConf.Cmd = imageConf.Cmd
 	}
 	if userConf.Dns == nil || len(userConf.Dns) == 0 {
 		userConf.Dns = imageConf.Dns
193a7e1d
 	} else {
 		//duplicates aren't an issue here
 		userConf.Dns = append(userConf.Dns, imageConf.Dns...)
0f312113
 	}
b16ff9f8
 	if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
 		userConf.Entrypoint = imageConf.Entrypoint
 	}
31998833
 	if userConf.WorkingDir == "" {
 		userConf.WorkingDir = imageConf.WorkingDir
 	}
1a226f0e
 	if userConf.VolumesFrom == "" {
 		userConf.VolumesFrom = imageConf.VolumesFrom
 	}
eb9fef2c
 	if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
 		userConf.Volumes = imageConf.Volumes
193a7e1d
 	} else {
 		for k, v := range imageConf.Volumes {
 			userConf.Volumes[k] = v
 		}
eb9fef2c
 	}
0f312113
 }
551092f9
 
 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
 }
c69549e0
 
 // We will receive port specs in the format of ip:public:private/proto and these need to be
 // parsed in the internal types
 func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
 	exposedPorts := make(map[Port]struct{}, len(ports))
 	bindings := make(map[Port][]PortBinding)
 
 	for _, rawPort := range ports {
 		proto := "tcp"
 		if i := strings.LastIndex(rawPort, "/"); i != -1 {
 			proto = rawPort[i+1:]
 			rawPort = rawPort[:i]
 		}
 		if !strings.Contains(rawPort, ":") {
 			rawPort = fmt.Sprintf("::%s", rawPort)
 		} else if len(strings.Split(rawPort, ":")) == 2 {
 			rawPort = fmt.Sprintf(":%s", rawPort)
 		}
 
 		parts, err := utils.PartParser("ip:hostPort:containerPort", rawPort)
 		if err != nil {
 			return nil, nil, err
 		}
 		containerPort := parts["containerPort"]
 		rawIp := parts["ip"]
 		hostPort := parts["hostPort"]
 
 		if containerPort == "" {
 			return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
 		}
2e6329a9
 
 		port := NewPort(proto, containerPort)
c69549e0
 		if _, exists := exposedPorts[port]; !exists {
 			exposedPorts[port] = struct{}{}
 		}
 
 		binding := PortBinding{
 			HostIp:   rawIp,
 			HostPort: hostPort,
 		}
 		bslice, exists := bindings[port]
 		if !exists {
 			bslice = []PortBinding{}
 		}
 		bindings[port] = append(bslice, binding)
 	}
 	return exposedPorts, bindings, nil
 }
 
248e0c63
 // Splits a port in the format of port/proto
2e6329a9
 func splitProtoPort(rawPort string) (string, string) {
 	parts := strings.Split(rawPort, "/")
 	l := len(parts)
 	if l == 0 {
 		return "", ""
 	}
 	if l == 1 {
 		return "tcp", rawPort
 	}
248e0c63
 	return parts[0], parts[1]
2e6329a9
 }
 
c69549e0
 func parsePort(rawPort string) (int, error) {
 	port, err := strconv.ParseUint(rawPort, 10, 16)
 	if err != nil {
 		return 0, err
 	}
 	return int(port), nil
 }
 
 func migratePortMappings(config *Config) error {
 	if config.PortSpecs != nil {
 		// We don't have to worry about migrating the bindings to the host
 		// This is our breaking change
 		ports, _, err := parsePortSpecs(config.PortSpecs)
 		if err != nil {
 			return err
 		}
 		config.PortSpecs = nil
2e6329a9
 
 		if config.ExposedPorts == nil {
 			config.ExposedPorts = make(map[Port]struct{}, len(ports))
 		}
 		for k, v := range ports {
 			config.ExposedPorts[k] = v
 		}
c69549e0
 	}
 	return nil
 }
20c96269
 
 // Links come in the format of
457a9260
 // name:alias
c7d854af
 func parseLink(rawLink string) (map[string]string, error) {
457a9260
 	return utils.PartParser("name:alias", rawLink)
20c96269
 }