@@ -105,134 +105,6 @@ def _result(self, response, json=False, binary=False):
105
105
return response .content
106
106
return response .text
107
107
108
- def _container_config (self , image , command , hostname = None , user = None ,
109
- detach = False , stdin_open = False , tty = False ,
110
- mem_limit = 0 , ports = None , environment = None , dns = None ,
111
- volumes = None , volumes_from = None ,
112
- network_disabled = False , entrypoint = None ,
113
- cpu_shares = None , working_dir = None ,
114
- domainname = None , memswap_limit = 0 , cpuset = None ,
115
- host_config = None , mac_address = None ):
116
- if isinstance (command , six .string_types ):
117
- command = shlex .split (str (command ))
118
- if isinstance (environment , dict ):
119
- environment = [
120
- six .text_type ('{0}={1}' ).format (k , v )
121
- for k , v in six .iteritems (environment )
122
- ]
123
-
124
- if isinstance (mem_limit , six .string_types ):
125
- if len (mem_limit ) == 0 :
126
- mem_limit = 0
127
- else :
128
- units = {'b' : 1 ,
129
- 'k' : 1024 ,
130
- 'm' : 1024 * 1024 ,
131
- 'g' : 1024 * 1024 * 1024 }
132
- suffix = mem_limit [- 1 ].lower ()
133
-
134
- # Check if the variable is a string representation of an int
135
- # without a units part. Assuming that the units are bytes.
136
- if suffix .isdigit ():
137
- digits_part = mem_limit
138
- suffix = 'b'
139
- else :
140
- digits_part = mem_limit [:- 1 ]
141
-
142
- if suffix in units .keys () or suffix .isdigit ():
143
- try :
144
- digits = int (digits_part )
145
- except ValueError :
146
- message = ('Failed converting the string value for'
147
- ' mem_limit ({0}) to a number.' )
148
- formatted_message = message .format (digits_part )
149
- raise errors .DockerException (formatted_message )
150
-
151
- mem_limit = digits * units [suffix ]
152
- else :
153
- message = ('The specified value for mem_limit parameter'
154
- ' ({0}) should specify the units. The postfix'
155
- ' should be one of the `b` `k` `m` `g`'
156
- ' characters' )
157
- raise errors .DockerException (message .format (mem_limit ))
158
-
159
- if isinstance (ports , list ):
160
- exposed_ports = {}
161
- for port_definition in ports :
162
- port = port_definition
163
- proto = 'tcp'
164
- if isinstance (port_definition , tuple ):
165
- if len (port_definition ) == 2 :
166
- proto = port_definition [1 ]
167
- port = port_definition [0 ]
168
- exposed_ports ['{0}/{1}' .format (port , proto )] = {}
169
- ports = exposed_ports
170
-
171
- if isinstance (volumes , six .string_types ):
172
- volumes = [volumes , ]
173
-
174
- if isinstance (volumes , list ):
175
- volumes_dict = {}
176
- for vol in volumes :
177
- volumes_dict [vol ] = {}
178
- volumes = volumes_dict
179
-
180
- if volumes_from :
181
- if not isinstance (volumes_from , six .string_types ):
182
- volumes_from = ',' .join (volumes_from )
183
- else :
184
- # Force None, an empty list or dict causes client.start to fail
185
- volumes_from = None
186
-
187
- attach_stdin = False
188
- attach_stdout = False
189
- attach_stderr = False
190
- stdin_once = False
191
-
192
- if not detach :
193
- attach_stdout = True
194
- attach_stderr = True
195
-
196
- if stdin_open :
197
- attach_stdin = True
198
- stdin_once = True
199
-
200
- if utils .compare_version ('1.10' , self ._version ) >= 0 :
201
- message = ('{0!r} parameter has no effect on create_container().'
202
- ' It has been moved to start()' )
203
- if dns is not None :
204
- raise errors .DockerException (message .format ('dns' ))
205
- if volumes_from is not None :
206
- raise errors .DockerException (message .format ('volumes_from' ))
207
-
208
- return {
209
- 'Hostname' : hostname ,
210
- 'Domainname' : domainname ,
211
- 'ExposedPorts' : ports ,
212
- 'User' : user ,
213
- 'Tty' : tty ,
214
- 'OpenStdin' : stdin_open ,
215
- 'StdinOnce' : stdin_once ,
216
- 'Memory' : mem_limit ,
217
- 'AttachStdin' : attach_stdin ,
218
- 'AttachStdout' : attach_stdout ,
219
- 'AttachStderr' : attach_stderr ,
220
- 'Env' : environment ,
221
- 'Cmd' : command ,
222
- 'Dns' : dns ,
223
- 'Image' : image ,
224
- 'Volumes' : volumes ,
225
- 'VolumesFrom' : volumes_from ,
226
- 'NetworkDisabled' : network_disabled ,
227
- 'Entrypoint' : entrypoint ,
228
- 'CpuShares' : cpu_shares ,
229
- 'Cpuset' : cpuset ,
230
- 'WorkingDir' : working_dir ,
231
- 'MemorySwap' : memswap_limit ,
232
- 'HostConfig' : host_config ,
233
- 'MacAddress' : mac_address
234
- }
235
-
236
108
def _post_json (self , url , data , ** kwargs ):
237
109
# Go <1.1 can't unserialize null to a string
238
110
# so we do this disgusting thing here.
@@ -547,11 +419,11 @@ def create_container(self, image, command=None, hostname=None, user=None,
547
419
'host_config is not supported in API < 1.15'
548
420
)
549
421
550
- config = self . _container_config (
551
- image , command , hostname , user , detach , stdin_open , tty , mem_limit ,
552
- ports , environment , dns , volumes , volumes_from , network_disabled ,
553
- entrypoint , cpu_shares , working_dir , domainname , memswap_limit ,
554
- cpuset , host_config , mac_address
422
+ config = utils . create_container_config (
423
+ self . _version , image , command , hostname , user , detach , stdin_open ,
424
+ tty , mem_limit , ports , environment , dns , volumes , volumes_from ,
425
+ network_disabled , entrypoint , cpu_shares , working_dir , domainname ,
426
+ memswap_limit , cpuset , host_config , mac_address
555
427
)
556
428
return self .create_container_from_config (config , name )
557
429
0 commit comments