2013-01-19 04:13:39 +04:00
|
|
|
package docker
|
|
|
|
|
2013-07-15 17:12:33 +04:00
|
|
|
import (
|
2013-08-16 03:35:03 +04:00
|
|
|
"fmt"
|
2013-11-08 04:35:26 +04:00
|
|
|
"github.com/dotcloud/docker/archive"
|
2013-12-24 03:45:18 +04:00
|
|
|
"github.com/dotcloud/docker/pkg/namesgenerator"
|
2013-10-05 06:25:15 +04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-12-06 01:18:02 +04:00
|
|
|
"io"
|
2013-11-08 03:58:03 +04:00
|
|
|
"strconv"
|
2013-07-15 17:12:33 +04:00
|
|
|
"strings"
|
2013-12-06 01:18:02 +04:00
|
|
|
"sync/atomic"
|
2013-07-15 17:12:33 +04:00
|
|
|
)
|
|
|
|
|
2013-11-08 04:35:26 +04:00
|
|
|
type Change struct {
|
|
|
|
archive.Change
|
|
|
|
}
|
|
|
|
|
2013-05-02 11: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 22:16:30 +04:00
|
|
|
a.CpuShares != b.CpuShares ||
|
2013-05-02 11:49:23 +04:00
|
|
|
a.OpenStdin != b.OpenStdin ||
|
2013-07-18 00:51:25 +04:00
|
|
|
a.Tty != b.Tty ||
|
|
|
|
a.VolumesFrom != b.VolumesFrom {
|
2013-05-02 11: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-12 04:31:07 +04:00
|
|
|
len(a.PortSpecs) != len(b.PortSpecs) ||
|
2013-10-05 06:25:15 +04:00
|
|
|
len(a.ExposedPorts) != len(b.ExposedPorts) ||
|
2013-07-18 00:51:25 +04:00
|
|
|
len(a.Entrypoint) != len(b.Entrypoint) ||
|
|
|
|
len(a.Volumes) != len(b.Volumes) {
|
2013-05-02 11: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-10-05 06:25:15 +04:00
|
|
|
for k := range a.ExposedPorts {
|
|
|
|
if _, exists := b.ExposedPorts[k]; !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 06:20:05 +04:00
|
|
|
for i := 0; i < len(a.Entrypoint); i++ {
|
|
|
|
if a.Entrypoint[i] != b.Entrypoint[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 00:51:25 +04:00
|
|
|
for key := range a.Volumes {
|
|
|
|
if _, exists := b.Volumes[key]; !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 11:49:23 +04:00
|
|
|
return true
|
|
|
|
}
|
2013-05-19 21:46:24 +04:00
|
|
|
|
2013-09-20 16:46:24 +04:00
|
|
|
func MergeConfig(userConf, imageConf *Config) error {
|
2013-05-24 00:48:50 +04:00
|
|
|
if userConf.User == "" {
|
2013-05-19 21: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
|
|
|
|
}
|
2013-10-05 06:25:15 +04:00
|
|
|
if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
|
|
|
|
userConf.ExposedPorts = imageConf.ExposedPorts
|
2013-11-08 02:31:25 +04:00
|
|
|
} else if imageConf.ExposedPorts != nil {
|
|
|
|
if userConf.ExposedPorts == nil {
|
|
|
|
userConf.ExposedPorts = make(map[Port]struct{})
|
|
|
|
}
|
|
|
|
for port := range imageConf.ExposedPorts {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2013-10-05 06:25:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for port := range ports {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
2013-07-15 17:12:33 +04:00
|
|
|
}
|
2013-10-05 06:25:15 +04:00
|
|
|
}
|
|
|
|
userConf.PortSpecs = nil
|
|
|
|
}
|
|
|
|
if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
|
|
|
|
utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
|
|
|
|
if userConf.ExposedPorts == nil {
|
|
|
|
userConf.ExposedPorts = make(map[Port]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
ports, _, err := parsePortSpecs(imageConf.PortSpecs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for port := range ports {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
2013-07-15 17:12:33 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 21: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 17: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 21: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 17:12:33 +04:00
|
|
|
} else {
|
|
|
|
//duplicates aren't an issue here
|
|
|
|
userConf.Dns = append(userConf.Dns, imageConf.Dns...)
|
2013-05-19 21:46:24 +04:00
|
|
|
}
|
2013-06-25 06:20:05 +04:00
|
|
|
if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
|
|
|
|
userConf.Entrypoint = imageConf.Entrypoint
|
|
|
|
}
|
2013-08-18 22:30:19 +04:00
|
|
|
if userConf.WorkingDir == "" {
|
|
|
|
userConf.WorkingDir = imageConf.WorkingDir
|
|
|
|
}
|
2013-07-18 01:06:46 +04:00
|
|
|
if userConf.VolumesFrom == "" {
|
|
|
|
userConf.VolumesFrom = imageConf.VolumesFrom
|
|
|
|
}
|
2013-07-04 06:33:30 +04:00
|
|
|
if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
|
|
|
|
userConf.Volumes = imageConf.Volumes
|
2013-07-15 17:12:33 +04:00
|
|
|
} else {
|
|
|
|
for k, v := range imageConf.Volumes {
|
|
|
|
userConf.Volumes[k] = v
|
|
|
|
}
|
2013-07-04 06:33:30 +04:00
|
|
|
}
|
2013-09-20 16:46:24 +04:00
|
|
|
return nil
|
2013-05-19 21:46:24 +04:00
|
|
|
}
|
2013-08-16 03:35:03 +04:00
|
|
|
|
2013-11-26 21:46:06 +04:00
|
|
|
func parseLxcConfOpts(opts ListOpts) ([]KeyValuePair, error) {
|
|
|
|
out := make([]KeyValuePair, opts.Len())
|
|
|
|
for i, o := range opts.GetAll() {
|
2013-08-16 03:35:03 +04:00
|
|
|
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
|
|
|
|
}
|
2013-10-05 06:25:15 +04:00
|
|
|
|
2013-11-22 22:36:49 +04:00
|
|
|
// FIXME: network related stuff (including parsing) should be grouped in network file
|
|
|
|
const (
|
|
|
|
PortSpecTemplate = "ip:hostPort:containerPort"
|
|
|
|
PortSpecTemplateFormat = "ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort"
|
|
|
|
)
|
|
|
|
|
2013-10-05 06:25:15 +04:00
|
|
|
// 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) {
|
2013-11-22 22:12:27 +04:00
|
|
|
var (
|
|
|
|
exposedPorts = make(map[Port]struct{}, len(ports))
|
|
|
|
bindings = make(map[Port][]PortBinding)
|
|
|
|
)
|
2013-10-05 06:25:15 +04:00
|
|
|
|
|
|
|
for _, rawPort := range ports {
|
|
|
|
proto := "tcp"
|
2013-11-22 22:12:27 +04:00
|
|
|
|
2013-10-05 06:25:15 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-11-22 22:36:49 +04:00
|
|
|
parts, err := utils.PartParser(PortSpecTemplate, rawPort)
|
2013-10-05 06:25:15 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2013-11-22 22:12:27 +04:00
|
|
|
|
|
|
|
var (
|
|
|
|
containerPort = parts["containerPort"]
|
|
|
|
rawIp = parts["ip"]
|
|
|
|
hostPort = parts["hostPort"]
|
|
|
|
)
|
2013-10-05 06:25:15 +04:00
|
|
|
|
|
|
|
if containerPort == "" {
|
|
|
|
return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
|
|
|
|
}
|
2013-11-01 05:33:42 +04:00
|
|
|
if _, err := strconv.ParseUint(containerPort, 10, 16); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
|
|
|
|
}
|
|
|
|
if _, err := strconv.ParseUint(hostPort, 10, 16); hostPort != "" && err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
|
|
|
|
}
|
2013-10-05 06:25:15 +04:00
|
|
|
|
|
|
|
port := NewPort(proto, containerPort)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splits a port in the format of port/proto
|
|
|
|
func splitProtoPort(rawPort string) (string, string) {
|
|
|
|
parts := strings.Split(rawPort, "/")
|
|
|
|
l := len(parts)
|
|
|
|
if l == 0 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
if l == 1 {
|
|
|
|
return "tcp", rawPort
|
|
|
|
}
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePort(rawPort string) (int, error) {
|
|
|
|
port, err := strconv.ParseUint(rawPort, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(port), nil
|
|
|
|
}
|
|
|
|
|
2013-10-30 19:15:12 +04:00
|
|
|
func migratePortMappings(config *Config, hostConfig *HostConfig) error {
|
2013-10-05 06:25:15 +04:00
|
|
|
if config.PortSpecs != nil {
|
2013-10-30 19:15:12 +04:00
|
|
|
ports, bindings, err := parsePortSpecs(config.PortSpecs)
|
2013-10-05 06:25:15 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.PortSpecs = nil
|
2013-10-30 19:15:12 +04:00
|
|
|
if len(bindings) > 0 {
|
|
|
|
if hostConfig == nil {
|
|
|
|
hostConfig = &HostConfig{}
|
|
|
|
}
|
|
|
|
hostConfig.PortBindings = bindings
|
|
|
|
}
|
2013-10-05 06:25:15 +04:00
|
|
|
|
|
|
|
if config.ExposedPorts == nil {
|
|
|
|
config.ExposedPorts = make(map[Port]struct{}, len(ports))
|
|
|
|
}
|
|
|
|
for k, v := range ports {
|
|
|
|
config.ExposedPorts[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Links come in the format of
|
|
|
|
// name:alias
|
|
|
|
func parseLink(rawLink string) (map[string]string, error) {
|
|
|
|
return utils.PartParser("name:alias", rawLink)
|
|
|
|
}
|
2013-10-29 03:58:59 +04:00
|
|
|
|
|
|
|
type checker struct {
|
|
|
|
runtime *Runtime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) Exists(name string) bool {
|
|
|
|
return c.runtime.containerGraph.Exists("/" + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a random and unique name
|
2013-10-31 05:26:01 +04:00
|
|
|
func generateRandomName(runtime *Runtime) (string, error) {
|
|
|
|
return namesgenerator.GenerateRandomName(&checker{runtime})
|
2013-10-29 03:58:59 +04:00
|
|
|
}
|
2013-12-06 01:18:02 +04:00
|
|
|
|
|
|
|
// Read an io.Reader and call a function when it returns EOF
|
|
|
|
func EofReader(r io.Reader, callback func()) *eofReader {
|
|
|
|
return &eofReader{
|
|
|
|
Reader: r,
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type eofReader struct {
|
|
|
|
io.Reader
|
|
|
|
gotEOF int32
|
|
|
|
callback func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *eofReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.Reader.Read(p)
|
|
|
|
if err == io.EOF {
|
|
|
|
// Use atomics to make the gotEOF check threadsafe
|
|
|
|
if atomic.CompareAndSwapInt32(&r.gotEOF, 0, 1) {
|
|
|
|
r.callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|