mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Move the canonical run configuration objects to a sub-package
* Config is now runconfig.Config
    * HostConfig is now runconfig.HostConfig
    * MergeConfig is now runconfig.Merge
    * CompareConfig is now runconfig.Compare
    * ParseRun is now runconfig.Parse
    * ContainerConfigFromJob is now runconfig.ContainerConfigFromJob
    * ContainerHostConfigFromJob is now runconfig.ContainerHostConfigFromJob
This facilitates refactoring commands.go and shrinks the core.
Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
			
			
This commit is contained in:
		
							parent
							
								
									9a9690360c
								
							
						
					
					
						commit
						6393c38339
					
				
					 23 changed files with 731 additions and 674 deletions
				
			
		
							
								
								
									
										67
									
								
								runconfig/compare.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								runconfig/compare.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,67 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
// Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
 | 
			
		||||
// If OpenStdin is set, then it differs
 | 
			
		||||
func Compare(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 ||
 | 
			
		||||
		a.CpuShares != b.CpuShares ||
 | 
			
		||||
		a.OpenStdin != b.OpenStdin ||
 | 
			
		||||
		a.Tty != b.Tty ||
 | 
			
		||||
		a.VolumesFrom != b.VolumesFrom {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	if len(a.Cmd) != len(b.Cmd) ||
 | 
			
		||||
		len(a.Dns) != len(b.Dns) ||
 | 
			
		||||
		len(a.Env) != len(b.Env) ||
 | 
			
		||||
		len(a.PortSpecs) != len(b.PortSpecs) ||
 | 
			
		||||
		len(a.ExposedPorts) != len(b.ExposedPorts) ||
 | 
			
		||||
		len(a.Entrypoint) != len(b.Entrypoint) ||
 | 
			
		||||
		len(a.Volumes) != len(b.Volumes) {
 | 
			
		||||
		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
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	for k := range a.ExposedPorts {
 | 
			
		||||
		if _, exists := b.ExposedPorts[k]; !exists {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	for i := 0; i < len(a.Entrypoint); i++ {
 | 
			
		||||
		if a.Entrypoint[i] != b.Entrypoint[i] {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	for key := range a.Volumes {
 | 
			
		||||
		if _, exists := b.Volumes[key]; !exists {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										76
									
								
								runconfig/config.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								runconfig/config.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,76 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/dotcloud/docker/engine"
 | 
			
		||||
	"github.com/dotcloud/docker/nat"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Note: the Config structure should hold only portable information about the container.
 | 
			
		||||
// Here, "portable" means "independent from the host we are running on".
 | 
			
		||||
// Non-portable information *should* appear in HostConfig.
 | 
			
		||||
type Config struct {
 | 
			
		||||
	Hostname        string
 | 
			
		||||
	Domainname      string
 | 
			
		||||
	User            string
 | 
			
		||||
	Memory          int64 // Memory limit (in bytes)
 | 
			
		||||
	MemorySwap      int64 // Total memory usage (memory + swap); set `-1' to disable swap
 | 
			
		||||
	CpuShares       int64 // CPU shares (relative weight vs. other containers)
 | 
			
		||||
	AttachStdin     bool
 | 
			
		||||
	AttachStdout    bool
 | 
			
		||||
	AttachStderr    bool
 | 
			
		||||
	PortSpecs       []string // Deprecated - Can be in the format of 8080/tcp
 | 
			
		||||
	ExposedPorts    map[nat.Port]struct{}
 | 
			
		||||
	Tty             bool // Attach standard streams to a tty, including stdin if it is not closed.
 | 
			
		||||
	OpenStdin       bool // Open stdin
 | 
			
		||||
	StdinOnce       bool // If true, close stdin after the 1 attached client disconnects.
 | 
			
		||||
	Env             []string
 | 
			
		||||
	Cmd             []string
 | 
			
		||||
	Dns             []string
 | 
			
		||||
	Image           string // Name of the image as it was passed by the operator (eg. could be symbolic)
 | 
			
		||||
	Volumes         map[string]struct{}
 | 
			
		||||
	VolumesFrom     string
 | 
			
		||||
	WorkingDir      string
 | 
			
		||||
	Entrypoint      []string
 | 
			
		||||
	NetworkDisabled bool
 | 
			
		||||
	OnBuild         []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ContainerConfigFromJob(job *engine.Job) *Config {
 | 
			
		||||
	config := &Config{
 | 
			
		||||
		Hostname:        job.Getenv("Hostname"),
 | 
			
		||||
		Domainname:      job.Getenv("Domainname"),
 | 
			
		||||
		User:            job.Getenv("User"),
 | 
			
		||||
		Memory:          job.GetenvInt64("Memory"),
 | 
			
		||||
		MemorySwap:      job.GetenvInt64("MemorySwap"),
 | 
			
		||||
		CpuShares:       job.GetenvInt64("CpuShares"),
 | 
			
		||||
		AttachStdin:     job.GetenvBool("AttachStdin"),
 | 
			
		||||
		AttachStdout:    job.GetenvBool("AttachStdout"),
 | 
			
		||||
		AttachStderr:    job.GetenvBool("AttachStderr"),
 | 
			
		||||
		Tty:             job.GetenvBool("Tty"),
 | 
			
		||||
		OpenStdin:       job.GetenvBool("OpenStdin"),
 | 
			
		||||
		StdinOnce:       job.GetenvBool("StdinOnce"),
 | 
			
		||||
		Image:           job.Getenv("Image"),
 | 
			
		||||
		VolumesFrom:     job.Getenv("VolumesFrom"),
 | 
			
		||||
		WorkingDir:      job.Getenv("WorkingDir"),
 | 
			
		||||
		NetworkDisabled: job.GetenvBool("NetworkDisabled"),
 | 
			
		||||
	}
 | 
			
		||||
	job.GetenvJson("ExposedPorts", &config.ExposedPorts)
 | 
			
		||||
	job.GetenvJson("Volumes", &config.Volumes)
 | 
			
		||||
	if PortSpecs := job.GetenvList("PortSpecs"); PortSpecs != nil {
 | 
			
		||||
		config.PortSpecs = PortSpecs
 | 
			
		||||
	}
 | 
			
		||||
	if Env := job.GetenvList("Env"); Env != nil {
 | 
			
		||||
		config.Env = Env
 | 
			
		||||
	}
 | 
			
		||||
	if Cmd := job.GetenvList("Cmd"); Cmd != nil {
 | 
			
		||||
		config.Cmd = Cmd
 | 
			
		||||
	}
 | 
			
		||||
	if Dns := job.GetenvList("Dns"); Dns != nil {
 | 
			
		||||
		config.Dns = Dns
 | 
			
		||||
	}
 | 
			
		||||
	if Entrypoint := job.GetenvList("Entrypoint"); Entrypoint != nil {
 | 
			
		||||
		config.Entrypoint = Entrypoint
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return config
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										150
									
								
								runconfig/config_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										150
									
								
								runconfig/config_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,150 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/dotcloud/docker/nat"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestCompare(t *testing.T) {
 | 
			
		||||
	volumes1 := make(map[string]struct{})
 | 
			
		||||
	volumes1["/test1"] = struct{}{}
 | 
			
		||||
	config1 := Config{
 | 
			
		||||
		Dns:         []string{"1.1.1.1", "2.2.2.2"},
 | 
			
		||||
		PortSpecs:   []string{"1111:1111", "2222:2222"},
 | 
			
		||||
		Env:         []string{"VAR1=1", "VAR2=2"},
 | 
			
		||||
		VolumesFrom: "11111111",
 | 
			
		||||
		Volumes:     volumes1,
 | 
			
		||||
	}
 | 
			
		||||
	config2 := Config{
 | 
			
		||||
		Dns:         []string{"0.0.0.0", "2.2.2.2"},
 | 
			
		||||
		PortSpecs:   []string{"1111:1111", "2222:2222"},
 | 
			
		||||
		Env:         []string{"VAR1=1", "VAR2=2"},
 | 
			
		||||
		VolumesFrom: "11111111",
 | 
			
		||||
		Volumes:     volumes1,
 | 
			
		||||
	}
 | 
			
		||||
	config3 := Config{
 | 
			
		||||
		Dns:         []string{"1.1.1.1", "2.2.2.2"},
 | 
			
		||||
		PortSpecs:   []string{"0000:0000", "2222:2222"},
 | 
			
		||||
		Env:         []string{"VAR1=1", "VAR2=2"},
 | 
			
		||||
		VolumesFrom: "11111111",
 | 
			
		||||
		Volumes:     volumes1,
 | 
			
		||||
	}
 | 
			
		||||
	config4 := Config{
 | 
			
		||||
		Dns:         []string{"1.1.1.1", "2.2.2.2"},
 | 
			
		||||
		PortSpecs:   []string{"0000:0000", "2222:2222"},
 | 
			
		||||
		Env:         []string{"VAR1=1", "VAR2=2"},
 | 
			
		||||
		VolumesFrom: "22222222",
 | 
			
		||||
		Volumes:     volumes1,
 | 
			
		||||
	}
 | 
			
		||||
	volumes2 := make(map[string]struct{})
 | 
			
		||||
	volumes2["/test2"] = struct{}{}
 | 
			
		||||
	config5 := Config{
 | 
			
		||||
		Dns:         []string{"1.1.1.1", "2.2.2.2"},
 | 
			
		||||
		PortSpecs:   []string{"0000:0000", "2222:2222"},
 | 
			
		||||
		Env:         []string{"VAR1=1", "VAR2=2"},
 | 
			
		||||
		VolumesFrom: "11111111",
 | 
			
		||||
		Volumes:     volumes2,
 | 
			
		||||
	}
 | 
			
		||||
	if Compare(&config1, &config2) {
 | 
			
		||||
		t.Fatalf("Compare should return false, Dns are different")
 | 
			
		||||
	}
 | 
			
		||||
	if Compare(&config1, &config3) {
 | 
			
		||||
		t.Fatalf("Compare should return false, PortSpecs are different")
 | 
			
		||||
	}
 | 
			
		||||
	if Compare(&config1, &config4) {
 | 
			
		||||
		t.Fatalf("Compare should return false, VolumesFrom are different")
 | 
			
		||||
	}
 | 
			
		||||
	if Compare(&config1, &config5) {
 | 
			
		||||
		t.Fatalf("Compare should return false, Volumes are different")
 | 
			
		||||
	}
 | 
			
		||||
	if !Compare(&config1, &config1) {
 | 
			
		||||
		t.Fatalf("Compare should return true")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMerge(t *testing.T) {
 | 
			
		||||
	volumesImage := make(map[string]struct{})
 | 
			
		||||
	volumesImage["/test1"] = struct{}{}
 | 
			
		||||
	volumesImage["/test2"] = struct{}{}
 | 
			
		||||
	configImage := &Config{
 | 
			
		||||
		Dns:         []string{"1.1.1.1", "2.2.2.2"},
 | 
			
		||||
		PortSpecs:   []string{"1111:1111", "2222:2222"},
 | 
			
		||||
		Env:         []string{"VAR1=1", "VAR2=2"},
 | 
			
		||||
		VolumesFrom: "1111",
 | 
			
		||||
		Volumes:     volumesImage,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	volumesUser := make(map[string]struct{})
 | 
			
		||||
	volumesUser["/test3"] = struct{}{}
 | 
			
		||||
	configUser := &Config{
 | 
			
		||||
		Dns:       []string{"3.3.3.3"},
 | 
			
		||||
		PortSpecs: []string{"3333:2222", "3333:3333"},
 | 
			
		||||
		Env:       []string{"VAR2=3", "VAR3=3"},
 | 
			
		||||
		Volumes:   volumesUser,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := Merge(configUser, configImage); err != nil {
 | 
			
		||||
		t.Error(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(configUser.Dns) != 3 {
 | 
			
		||||
		t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
 | 
			
		||||
	}
 | 
			
		||||
	for _, dns := range configUser.Dns {
 | 
			
		||||
		if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
 | 
			
		||||
			t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(configUser.ExposedPorts) != 3 {
 | 
			
		||||
		t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
 | 
			
		||||
	}
 | 
			
		||||
	for portSpecs := range configUser.ExposedPorts {
 | 
			
		||||
		if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
 | 
			
		||||
			t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if len(configUser.Env) != 3 {
 | 
			
		||||
		t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
 | 
			
		||||
	}
 | 
			
		||||
	for _, env := range configUser.Env {
 | 
			
		||||
		if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
 | 
			
		||||
			t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(configUser.Volumes) != 3 {
 | 
			
		||||
		t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
 | 
			
		||||
	}
 | 
			
		||||
	for v := range configUser.Volumes {
 | 
			
		||||
		if v != "/test1" && v != "/test2" && v != "/test3" {
 | 
			
		||||
			t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if configUser.VolumesFrom != "1111" {
 | 
			
		||||
		t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ports, _, err := nat.ParsePortSpecs([]string{"0000"})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Error(err)
 | 
			
		||||
	}
 | 
			
		||||
	configImage2 := &Config{
 | 
			
		||||
		ExposedPorts: ports,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := Merge(configUser, configImage2); err != nil {
 | 
			
		||||
		t.Error(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(configUser.ExposedPorts) != 4 {
 | 
			
		||||
		t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
 | 
			
		||||
	}
 | 
			
		||||
	for portSpecs := range configUser.ExposedPorts {
 | 
			
		||||
		if portSpecs.Port() != "0000" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
 | 
			
		||||
			t.Fatalf("Expected 0000 or 1111 or 2222 or 3333, found %s", portSpecs)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								runconfig/hostconfig.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								runconfig/hostconfig.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,39 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/dotcloud/docker/engine"
 | 
			
		||||
	"github.com/dotcloud/docker/nat"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type HostConfig struct {
 | 
			
		||||
	Binds           []string
 | 
			
		||||
	ContainerIDFile string
 | 
			
		||||
	LxcConf         []KeyValuePair
 | 
			
		||||
	Privileged      bool
 | 
			
		||||
	PortBindings    nat.PortMap
 | 
			
		||||
	Links           []string
 | 
			
		||||
	PublishAllPorts bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type KeyValuePair struct {
 | 
			
		||||
	Key   string
 | 
			
		||||
	Value string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
 | 
			
		||||
	hostConfig := &HostConfig{
 | 
			
		||||
		ContainerIDFile: job.Getenv("ContainerIDFile"),
 | 
			
		||||
		Privileged:      job.GetenvBool("Privileged"),
 | 
			
		||||
		PublishAllPorts: job.GetenvBool("PublishAllPorts"),
 | 
			
		||||
	}
 | 
			
		||||
	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
 | 
			
		||||
	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
 | 
			
		||||
	if Binds := job.GetenvList("Binds"); Binds != nil {
 | 
			
		||||
		hostConfig.Binds = Binds
 | 
			
		||||
	}
 | 
			
		||||
	if Links := job.GetenvList("Links"); Links != nil {
 | 
			
		||||
		hostConfig.Links = Links
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return hostConfig
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										119
									
								
								runconfig/merge.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								runconfig/merge.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,119 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/dotcloud/docker/nat"
 | 
			
		||||
	"github.com/dotcloud/docker/utils"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
 | 
			
		||||
		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{}{}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
 | 
			
		||||
		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
 | 
			
		||||
	}
 | 
			
		||||
	if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
 | 
			
		||||
		// FIXME: I think we can safely remove this. Leaving it for now for the sake of reverse-compat paranoia.
 | 
			
		||||
		utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
 | 
			
		||||
		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{}{}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	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
 | 
			
		||||
	} 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)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
 | 
			
		||||
		userConf.Cmd = imageConf.Cmd
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.Dns == nil || len(userConf.Dns) == 0 {
 | 
			
		||||
		userConf.Dns = imageConf.Dns
 | 
			
		||||
	} else {
 | 
			
		||||
		//duplicates aren't an issue here
 | 
			
		||||
		userConf.Dns = append(userConf.Dns, imageConf.Dns...)
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
 | 
			
		||||
		userConf.Entrypoint = imageConf.Entrypoint
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.WorkingDir == "" {
 | 
			
		||||
		userConf.WorkingDir = imageConf.WorkingDir
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.VolumesFrom == "" {
 | 
			
		||||
		userConf.VolumesFrom = imageConf.VolumesFrom
 | 
			
		||||
	}
 | 
			
		||||
	if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
 | 
			
		||||
		userConf.Volumes = imageConf.Volumes
 | 
			
		||||
	} else {
 | 
			
		||||
		for k, v := range imageConf.Volumes {
 | 
			
		||||
			userConf.Volumes[k] = v
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										246
									
								
								runconfig/parse.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										246
									
								
								runconfig/parse.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,246 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/dotcloud/docker/nat"
 | 
			
		||||
	flag "github.com/dotcloud/docker/pkg/mflag"
 | 
			
		||||
	"github.com/dotcloud/docker/pkg/opts"
 | 
			
		||||
	"github.com/dotcloud/docker/pkg/sysinfo"
 | 
			
		||||
	"github.com/dotcloud/docker/utils"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"path"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	ErrInvalidWorikingDirectory = fmt.Errorf("The working directory is invalid. It needs to be an absolute path.")
 | 
			
		||||
	ErrConflictAttachDetach     = fmt.Errorf("Conflicting options: -a and -d")
 | 
			
		||||
	ErrConflictDetachAutoRemove = fmt.Errorf("Conflicting options: -rm and -d")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
//FIXME Only used in tests
 | 
			
		||||
func Parse(args []string, sysInfo *sysinfo.SysInfo) (*Config, *HostConfig, *flag.FlagSet, error) {
 | 
			
		||||
	cmd := flag.NewFlagSet("run", flag.ContinueOnError)
 | 
			
		||||
	cmd.SetOutput(ioutil.Discard)
 | 
			
		||||
	cmd.Usage = nil
 | 
			
		||||
	return parseRun(cmd, args, sysInfo)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FIXME: this maps the legacy commands.go code. It should be merged with Parse to only expose a single parse function.
 | 
			
		||||
func ParseSubcommand(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Config, *HostConfig, *flag.FlagSet, error) {
 | 
			
		||||
	return parseRun(cmd, args, sysInfo)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Config, *HostConfig, *flag.FlagSet, error) {
 | 
			
		||||
	var (
 | 
			
		||||
		// FIXME: use utils.ListOpts for attach and volumes?
 | 
			
		||||
		flAttach  = opts.NewListOpts(opts.ValidateAttach)
 | 
			
		||||
		flVolumes = opts.NewListOpts(opts.ValidatePath)
 | 
			
		||||
		flLinks   = opts.NewListOpts(opts.ValidateLink)
 | 
			
		||||
		flEnv     = opts.NewListOpts(opts.ValidateEnv)
 | 
			
		||||
 | 
			
		||||
		flPublish     opts.ListOpts
 | 
			
		||||
		flExpose      opts.ListOpts
 | 
			
		||||
		flDns         opts.ListOpts
 | 
			
		||||
		flVolumesFrom opts.ListOpts
 | 
			
		||||
		flLxcOpts     opts.ListOpts
 | 
			
		||||
 | 
			
		||||
		flAutoRemove      = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
 | 
			
		||||
		flDetach          = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: Run container in the background, print new container id")
 | 
			
		||||
		flNetwork         = cmd.Bool([]string{"n", "-networking"}, true, "Enable networking for this container")
 | 
			
		||||
		flPrivileged      = cmd.Bool([]string{"#privileged", "-privileged"}, false, "Give extended privileges to this container")
 | 
			
		||||
		flPublishAll      = cmd.Bool([]string{"P", "-publish-all"}, false, "Publish all exposed ports to the host interfaces")
 | 
			
		||||
		flStdin           = cmd.Bool([]string{"i", "-interactive"}, false, "Keep stdin open even if not attached")
 | 
			
		||||
		flTty             = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-tty")
 | 
			
		||||
		flContainerIDFile = cmd.String([]string{"#cidfile", "-cidfile"}, "", "Write the container ID to the file")
 | 
			
		||||
		flEntrypoint      = cmd.String([]string{"#entrypoint", "-entrypoint"}, "", "Overwrite the default entrypoint of the image")
 | 
			
		||||
		flHostname        = cmd.String([]string{"h", "-hostname"}, "", "Container host name")
 | 
			
		||||
		flMemoryString    = cmd.String([]string{"m", "-memory"}, "", "Memory limit (format: <number><optional unit>, where unit = b, k, m or g)")
 | 
			
		||||
		flUser            = cmd.String([]string{"u", "-user"}, "", "Username or UID")
 | 
			
		||||
		flWorkingDir      = cmd.String([]string{"w", "-workdir"}, "", "Working directory inside the container")
 | 
			
		||||
		flCpuShares       = cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
 | 
			
		||||
 | 
			
		||||
		// For documentation purpose
 | 
			
		||||
		_ = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxify all received signal to the process (even in non-tty mode)")
 | 
			
		||||
		_ = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	cmd.Var(&flAttach, []string{"a", "-attach"}, "Attach to stdin, stdout or stderr.")
 | 
			
		||||
	cmd.Var(&flVolumes, []string{"v", "-volume"}, "Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)")
 | 
			
		||||
	cmd.Var(&flLinks, []string{"#link", "-link"}, "Add link to another container (name:alias)")
 | 
			
		||||
	cmd.Var(&flEnv, []string{"e", "-env"}, "Set environment variables")
 | 
			
		||||
 | 
			
		||||
	cmd.Var(&flPublish, []string{"p", "-publish"}, fmt.Sprintf("Publish a container's port to the host (format: %s) (use 'docker port' to see the actual mapping)", nat.PortSpecTemplateFormat))
 | 
			
		||||
	cmd.Var(&flExpose, []string{"#expose", "-expose"}, "Expose a port from the container without publishing it to your host")
 | 
			
		||||
	cmd.Var(&flDns, []string{"#dns", "-dns"}, "Set custom dns servers")
 | 
			
		||||
	cmd.Var(&flVolumesFrom, []string{"#volumes-from", "-volumes-from"}, "Mount volumes from the specified container(s)")
 | 
			
		||||
	cmd.Var(&flLxcOpts, []string{"#lxc-conf", "-lxc-conf"}, "Add custom lxc options -lxc-conf=\"lxc.cgroup.cpuset.cpus = 0,1\"")
 | 
			
		||||
 | 
			
		||||
	if err := cmd.Parse(args); err != nil {
 | 
			
		||||
		return nil, nil, cmd, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if the kernel supports memory limit cgroup.
 | 
			
		||||
	if sysInfo != nil && *flMemoryString != "" && !sysInfo.MemoryLimit {
 | 
			
		||||
		*flMemoryString = ""
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Validate input params
 | 
			
		||||
	if *flDetach && flAttach.Len() > 0 {
 | 
			
		||||
		return nil, nil, cmd, ErrConflictAttachDetach
 | 
			
		||||
	}
 | 
			
		||||
	if *flWorkingDir != "" && !path.IsAbs(*flWorkingDir) {
 | 
			
		||||
		return nil, nil, cmd, ErrInvalidWorikingDirectory
 | 
			
		||||
	}
 | 
			
		||||
	if *flDetach && *flAutoRemove {
 | 
			
		||||
		return nil, nil, cmd, ErrConflictDetachAutoRemove
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// If neither -d or -a are set, attach to everything by default
 | 
			
		||||
	if flAttach.Len() == 0 && !*flDetach {
 | 
			
		||||
		if !*flDetach {
 | 
			
		||||
			flAttach.Set("stdout")
 | 
			
		||||
			flAttach.Set("stderr")
 | 
			
		||||
			if *flStdin {
 | 
			
		||||
				flAttach.Set("stdin")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var flMemory int64
 | 
			
		||||
	if *flMemoryString != "" {
 | 
			
		||||
		parsedMemory, err := utils.RAMInBytes(*flMemoryString)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, nil, cmd, err
 | 
			
		||||
		}
 | 
			
		||||
		flMemory = parsedMemory
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var binds []string
 | 
			
		||||
	// add any bind targets to the list of container volumes
 | 
			
		||||
	for bind := range flVolumes.GetMap() {
 | 
			
		||||
		if arr := strings.Split(bind, ":"); len(arr) > 1 {
 | 
			
		||||
			if arr[0] == "/" {
 | 
			
		||||
				return nil, nil, cmd, fmt.Errorf("Invalid bind mount: source can't be '/'")
 | 
			
		||||
			}
 | 
			
		||||
			dstDir := arr[1]
 | 
			
		||||
			flVolumes.Set(dstDir)
 | 
			
		||||
			binds = append(binds, bind)
 | 
			
		||||
			flVolumes.Delete(bind)
 | 
			
		||||
		} else if bind == "/" {
 | 
			
		||||
			return nil, nil, cmd, fmt.Errorf("Invalid volume: path can't be '/'")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var (
 | 
			
		||||
		parsedArgs = cmd.Args()
 | 
			
		||||
		runCmd     []string
 | 
			
		||||
		entrypoint []string
 | 
			
		||||
		image      string
 | 
			
		||||
	)
 | 
			
		||||
	if len(parsedArgs) >= 1 {
 | 
			
		||||
		image = cmd.Arg(0)
 | 
			
		||||
	}
 | 
			
		||||
	if len(parsedArgs) > 1 {
 | 
			
		||||
		runCmd = parsedArgs[1:]
 | 
			
		||||
	}
 | 
			
		||||
	if *flEntrypoint != "" {
 | 
			
		||||
		entrypoint = []string{*flEntrypoint}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	lxcConf, err := parseLxcConfOpts(flLxcOpts)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, cmd, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var (
 | 
			
		||||
		domainname string
 | 
			
		||||
		hostname   = *flHostname
 | 
			
		||||
		parts      = strings.SplitN(hostname, ".", 2)
 | 
			
		||||
	)
 | 
			
		||||
	if len(parts) > 1 {
 | 
			
		||||
		hostname = parts[0]
 | 
			
		||||
		domainname = parts[1]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ports, portBindings, err := nat.ParsePortSpecs(flPublish.GetAll())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, cmd, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Merge in exposed ports to the map of published ports
 | 
			
		||||
	for _, e := range flExpose.GetAll() {
 | 
			
		||||
		if strings.Contains(e, ":") {
 | 
			
		||||
			return nil, nil, cmd, fmt.Errorf("Invalid port format for --expose: %s", e)
 | 
			
		||||
		}
 | 
			
		||||
		p := nat.NewPort(nat.SplitProtoPort(e))
 | 
			
		||||
		if _, exists := ports[p]; !exists {
 | 
			
		||||
			ports[p] = struct{}{}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config := &Config{
 | 
			
		||||
		Hostname:        hostname,
 | 
			
		||||
		Domainname:      domainname,
 | 
			
		||||
		PortSpecs:       nil, // Deprecated
 | 
			
		||||
		ExposedPorts:    ports,
 | 
			
		||||
		User:            *flUser,
 | 
			
		||||
		Tty:             *flTty,
 | 
			
		||||
		NetworkDisabled: !*flNetwork,
 | 
			
		||||
		OpenStdin:       *flStdin,
 | 
			
		||||
		Memory:          flMemory,
 | 
			
		||||
		CpuShares:       *flCpuShares,
 | 
			
		||||
		AttachStdin:     flAttach.Get("stdin"),
 | 
			
		||||
		AttachStdout:    flAttach.Get("stdout"),
 | 
			
		||||
		AttachStderr:    flAttach.Get("stderr"),
 | 
			
		||||
		Env:             flEnv.GetAll(),
 | 
			
		||||
		Cmd:             runCmd,
 | 
			
		||||
		Dns:             flDns.GetAll(),
 | 
			
		||||
		Image:           image,
 | 
			
		||||
		Volumes:         flVolumes.GetMap(),
 | 
			
		||||
		VolumesFrom:     strings.Join(flVolumesFrom.GetAll(), ","),
 | 
			
		||||
		Entrypoint:      entrypoint,
 | 
			
		||||
		WorkingDir:      *flWorkingDir,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	hostConfig := &HostConfig{
 | 
			
		||||
		Binds:           binds,
 | 
			
		||||
		ContainerIDFile: *flContainerIDFile,
 | 
			
		||||
		LxcConf:         lxcConf,
 | 
			
		||||
		Privileged:      *flPrivileged,
 | 
			
		||||
		PortBindings:    portBindings,
 | 
			
		||||
		Links:           flLinks.GetAll(),
 | 
			
		||||
		PublishAllPorts: *flPublishAll,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
 | 
			
		||||
		//fmt.Fprintf(stdout, "WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n")
 | 
			
		||||
		config.MemorySwap = -1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// When allocating stdin in attached mode, close stdin at client disconnect
 | 
			
		||||
	if config.OpenStdin && config.AttachStdin {
 | 
			
		||||
		config.StdinOnce = true
 | 
			
		||||
	}
 | 
			
		||||
	return config, hostConfig, cmd, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func parseLxcConfOpts(opts opts.ListOpts) ([]KeyValuePair, error) {
 | 
			
		||||
	out := make([]KeyValuePair, opts.Len())
 | 
			
		||||
	for i, o := range opts.GetAll() {
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								runconfig/parse_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								runconfig/parse_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
package runconfig
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestParseLxcConfOpt(t *testing.T) {
 | 
			
		||||
	opts := []string{"lxc.utsname=docker", "lxc.utsname = docker "}
 | 
			
		||||
 | 
			
		||||
	for _, o := range opts {
 | 
			
		||||
		k, v, err := parseLxcOpt(o)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.FailNow()
 | 
			
		||||
		}
 | 
			
		||||
		if k != "lxc.utsname" {
 | 
			
		||||
			t.Fail()
 | 
			
		||||
		}
 | 
			
		||||
		if v != "docker" {
 | 
			
		||||
			t.Fail()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue