Browse code

fix convertion issues

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)

Victor Vieux authored on 2014/01/30 09:56:42
Showing 5 changed files
... ...
@@ -104,6 +104,44 @@ type Config struct {
104 104
 	NetworkDisabled bool
105 105
 }
106 106
 
107
+func ContainerConfigFromJob(job *engine.Job) *Config {
108
+	var config Config
109
+	config.Hostname = job.Getenv("Hostname")
110
+	config.Domainname = job.Getenv("Domainname")
111
+	config.User = job.Getenv("User")
112
+	config.Memory = job.GetenvInt64("Memory")
113
+	config.MemorySwap = job.GetenvInt64("MemorySwap")
114
+	config.CpuShares = job.GetenvInt64("CpuShares")
115
+	config.AttachStdin = job.GetenvBool("AttachStdin")
116
+	config.AttachStdout = job.GetenvBool("AttachStdout")
117
+	config.AttachStderr = job.GetenvBool("AttachStderr")
118
+	if PortSpecs := job.GetenvList("PortSpecs"); PortSpecs != nil {
119
+		config.PortSpecs = PortSpecs
120
+	}
121
+	job.GetenvJson("ExposedPorts", &config.ExposedPorts)
122
+	config.Tty = job.GetenvBool("Tty")
123
+	config.OpenStdin = job.GetenvBool("OpenStdin")
124
+	config.StdinOnce = job.GetenvBool("StdinOnce")
125
+	if Env := job.GetenvList("Env"); Env != nil {
126
+		config.Env = Env
127
+	}
128
+	if Cmd := job.GetenvList("Cmd"); Cmd != nil {
129
+		config.Cmd = Cmd
130
+	}
131
+	if Dns := job.GetenvList("Dns"); Dns != nil {
132
+		config.Dns = Dns
133
+	}
134
+	config.Image = job.Getenv("Image")
135
+	job.GetenvJson("Volumes", &config.Volumes)
136
+	config.VolumesFrom = job.Getenv("VolumesFrom")
137
+	config.WorkingDir = job.Getenv("WorkingDir")
138
+	if Entrypoint := job.GetenvList("Entrypoint"); Entrypoint != nil {
139
+		config.Entrypoint = Entrypoint
140
+	}
141
+	config.NetworkDisabled = job.GetenvBool("NetworkDisabled")
142
+	return &config
143
+}
144
+
107 145
 type HostConfig struct {
108 146
 	Binds           []string
109 147
 	ContainerIDFile string
... ...
@@ -114,6 +152,22 @@ type HostConfig struct {
114 114
 	PublishAllPorts bool
115 115
 }
116 116
 
117
+func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
118
+	var hostConfig HostConfig
119
+	if Binds := job.GetenvList("Binds"); Binds != nil {
120
+		hostConfig.Binds = Binds
121
+	}
122
+	hostConfig.ContainerIDFile = job.Getenv("ContainerIDFile")
123
+	job.GetenvJson("LxcConf", &hostConfig.LxcConf)
124
+	hostConfig.Privileged = job.GetenvBool("Privileged")
125
+	job.GetenvJson("PortBindings", &hostConfig.PortBindings)
126
+	if Links := job.GetenvList("Links"); Links != nil {
127
+		hostConfig.Links = Links
128
+	}
129
+	hostConfig.PublishAllPorts = job.GetenvBool("PublishAllPorts")
130
+	return &hostConfig
131
+}
132
+
117 133
 type BindMap struct {
118 134
 	SrcPath string
119 135
 	DstPath string
... ...
@@ -191,24 +191,6 @@ func (env *Env) WriteTo(dst io.Writer) (n int64, err error) {
191 191
 	return 0, env.Encode(dst)
192 192
 }
193 193
 
194
-func (env *Env) Export(dst interface{}) (err error) {
195
-	defer func() {
196
-		if err != nil {
197
-			err = fmt.Errorf("ExportEnv %s", err)
198
-		}
199
-	}()
200
-	var buf bytes.Buffer
201
-	// step 1: encode/marshal the env to an intermediary json representation
202
-	if err := env.Encode(&buf); err != nil {
203
-		return err
204
-	}
205
-	// step 2: decode/unmarshal the intermediary json into the destination object
206
-	if err := json.NewDecoder(&buf).Decode(dst); err != nil {
207
-		return err
208
-	}
209
-	return nil
210
-}
211
-
212 194
 func (env *Env) Import(src interface{}) (err error) {
213 195
 	defer func() {
214 196
 		if err != nil {
... ...
@@ -84,32 +84,6 @@ func TestSetenvList(t *testing.T) {
84 84
 	}
85 85
 }
86 86
 
87
-func TestImportEnv(t *testing.T) {
88
-	type dummy struct {
89
-		DummyInt         int
90
-		DummyStringArray []string
91
-	}
92
-
93
-	job := mkJob(t, "dummy")
94
-	if err := job.ImportEnv(&dummy{42, []string{"foo", "bar"}}); err != nil {
95
-		t.Fatal(err)
96
-	}
97
-
98
-	dmy := dummy{}
99
-	if err := job.ExportEnv(&dmy); err != nil {
100
-		t.Fatal(err)
101
-	}
102
-
103
-	if dmy.DummyInt != 42 {
104
-		t.Fatalf("Expected 42, got %d", dmy.DummyInt)
105
-	}
106
-
107
-	if len(dmy.DummyStringArray) != 2 || dmy.DummyStringArray[0] != "foo" || dmy.DummyStringArray[1] != "bar" {
108
-		t.Fatalf("Expected {foo, bar}, got %v", dmy.DummyStringArray)
109
-	}
110
-
111
-}
112
-
113 87
 func TestEnviron(t *testing.T) {
114 88
 	job := mkJob(t, "dummy")
115 89
 	job.Setenv("foo", "bar")
... ...
@@ -164,10 +164,6 @@ func (job *Job) EncodeEnv(dst io.Writer) error {
164 164
 	return job.env.Encode(dst)
165 165
 }
166 166
 
167
-func (job *Job) ExportEnv(dst interface{}) (err error) {
168
-	return job.env.Export(dst)
169
-}
170
-
171 167
 func (job *Job) ImportEnv(src interface{}) (err error) {
172 168
 	return job.env.Import(src)
173 169
 }
... ...
@@ -1742,11 +1742,7 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status {
1742 1742
 		job.Printf("Usage: %s", job.Name)
1743 1743
 		return engine.StatusErr
1744 1744
 	}
1745
-	var config Config
1746
-	if err := job.ExportEnv(&config); err != nil {
1747
-		job.Error(err)
1748
-		return engine.StatusErr
1749
-	}
1745
+	config := ContainerConfigFromJob(job)
1750 1746
 	if config.Memory != 0 && config.Memory < 524288 {
1751 1747
 		job.Errorf("Minimum memory limit allowed is 512k")
1752 1748
 		return engine.StatusErr
... ...
@@ -1769,7 +1765,7 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status {
1769 1769
 		config.Dns = defaultDns
1770 1770
 	}
1771 1771
 
1772
-	container, buildWarnings, err := srv.runtime.Create(&config, name)
1772
+	container, buildWarnings, err := srv.runtime.Create(config, name)
1773 1773
 	if err != nil {
1774 1774
 		if srv.runtime.graph.IsNotExist(err) {
1775 1775
 			_, tag := utils.ParseRepositoryTag(config.Image)
... ...
@@ -2196,11 +2192,7 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status {
2196 2196
 	}
2197 2197
 	// If no environment was set, then no hostconfig was passed.
2198 2198
 	if len(job.Environ()) > 0 {
2199
-		var hostConfig HostConfig
2200
-		if err := job.ExportEnv(&hostConfig); err != nil {
2201
-			job.Error(err)
2202
-			return engine.StatusErr
2203
-		}
2199
+		hostConfig := ContainerHostConfigFromJob(job)
2204 2200
 		// Validate the HostConfig binds. Make sure that:
2205 2201
 		// 1) the source of a bind mount isn't /
2206 2202
 		//         The bind mount "/:/foo" isn't allowed.
... ...
@@ -2227,11 +2219,11 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status {
2227 2227
 			}
2228 2228
 		}
2229 2229
 		// Register any links from the host config before starting the container
2230
-		if err := srv.RegisterLinks(container, &hostConfig); err != nil {
2230
+		if err := srv.RegisterLinks(container, hostConfig); err != nil {
2231 2231
 			job.Error(err)
2232 2232
 			return engine.StatusErr
2233 2233
 		}
2234
-		container.hostConfig = &hostConfig
2234
+		container.hostConfig = hostConfig
2235 2235
 		container.ToDisk()
2236 2236
 	}
2237 2237
 	if err := container.Start(); err != nil {