10
10
import tempfile
11
11
import threading
12
12
from io import open # pylint: disable=redefined-builtin
13
- from typing import Dict , List , MutableMapping , Optional , Set
13
+ from typing import Dict , List , MutableMapping , Optional , Set , Tuple
14
14
15
15
import requests
16
16
from typing_extensions import Text # pylint: disable=unused-import
@@ -200,8 +200,8 @@ def append_volume(runtime, source, target, writable=False):
200
200
# type: (List[Text], Text, Text, bool) -> None
201
201
"""Add binding arguments to the runtime list."""
202
202
runtime .append (u"--volume={}:{}:{}" .format (
203
- docker_windows_path_adjust (source ),
204
- docker_windows_path_adjust ( target ), "rw" if writable else "ro" ))
203
+ docker_windows_path_adjust (source ), target ,
204
+ "rw" if writable else "ro" ))
205
205
206
206
def add_file_or_directory_volume (self ,
207
207
runtime , # type: List[Text]
@@ -215,9 +215,10 @@ def add_file_or_directory_volume(self,
215
215
self .append_volume (runtime , volume .resolved , volume .target )
216
216
217
217
def add_writable_file_volume (self ,
218
- runtime , # type: List[Text]
219
- volume , # type: MapperEnt
220
- host_outdir_tgt # type: Optional[Text]
218
+ runtime , # type: List[Text]
219
+ volume , # type: MapperEnt
220
+ host_outdir_tgt , # type: Optional[Text]
221
+ tmpdir_prefix # type: Text
221
222
): # type: (...) -> None
222
223
"""Append a writable file mapping to the runtime option list."""
223
224
if self .inplace_update :
@@ -229,7 +230,7 @@ def add_writable_file_volume(self,
229
230
# which is already going to be mounted
230
231
shutil .copy (volume .resolved , host_outdir_tgt )
231
232
else :
232
- tmpdir = tempfile .mkdtemp (dir = self . tmpdir )
233
+ tmpdir = tempfile .mkdtemp (dir = tmpdir_prefix )
233
234
file_copy = os .path .join (
234
235
tmpdir , os .path .basename (volume .resolved ))
235
236
shutil .copy (volume .resolved , file_copy )
@@ -238,16 +239,17 @@ def add_writable_file_volume(self,
238
239
ensure_writable (host_outdir_tgt or file_copy )
239
240
240
241
def add_writable_directory_volume (self ,
241
- runtime , # type: List[Text]
242
- volume , # type: MapperEnt
243
- host_outdir_tgt # type: Optional[Text]
242
+ runtime , # type: List[Text]
243
+ volume , # type: MapperEnt
244
+ host_outdir_tgt , # type: Optional[Text]
245
+ tmpdir_prefix # type: Text
244
246
): # type: (...) -> None
245
247
"""Append a writable directory mapping to the runtime option list."""
246
248
if volume .resolved .startswith ("_:" ):
247
249
# Synthetic directory that needs creating first
248
250
if not host_outdir_tgt :
249
251
new_dir = os .path .join (
250
- tempfile .mkdtemp (dir = self . tmpdir ),
252
+ tempfile .mkdtemp (dir = tmpdir_prefix ),
251
253
os .path .basename (volume .target ))
252
254
self .append_volume (runtime , new_dir , volume .target ,
253
255
writable = True )
@@ -259,7 +261,7 @@ def add_writable_directory_volume(self,
259
261
writable = True )
260
262
else :
261
263
if not host_outdir_tgt :
262
- tmpdir = tempfile .mkdtemp (dir = self . tmpdir )
264
+ tmpdir = tempfile .mkdtemp (dir = tmpdir_prefix )
263
265
new_dir = os .path .join (
264
266
tmpdir , os .path .basename (volume .resolved ))
265
267
shutil .copytree (volume .resolved , new_dir )
@@ -270,8 +272,10 @@ def add_writable_directory_volume(self,
270
272
shutil .copytree (volume .resolved , host_outdir_tgt )
271
273
ensure_writable (host_outdir_tgt or new_dir )
272
274
273
- def create_runtime (self , env , runtimeContext ):
274
- # type: (MutableMapping[Text, Text], RuntimeContext) -> List
275
+ def create_runtime (self ,
276
+ env , # type: MutableMapping[Text, Text]
277
+ runtimeContext # type: RuntimeContext
278
+ ): # type: (...) -> Tuple[List[Text], Optional[Text]]
275
279
any_path_okay = self .builder .get_requirement ("DockerRequirement" )[1 ] \
276
280
or False
277
281
user_space_docker_cmd = runtimeContext .user_space_docker_cmd
@@ -284,19 +288,18 @@ def create_runtime(self, env, runtimeContext):
284
288
runtime = [user_space_docker_cmd , u"run" ]
285
289
else :
286
290
runtime = [u"docker" , u"run" , u"-i" ]
287
-
288
- runtime .append (u"--volume=%s:%s:rw" % (
289
- docker_windows_path_adjust (os .path .realpath (self .outdir )),
290
- self .builder .outdir ))
291
- runtime .append (u"--volume=%s:%s:rw" % (
292
- docker_windows_path_adjust (os .path .realpath (self .tmpdir )), "/tmp" ))
293
-
291
+ self .append_volume (runtime , os .path .realpath (self .outdir ),
292
+ self .builder .outdir , writable = True )
293
+ self .append_volume (runtime , os .path .realpath (self .tmpdir ), "/tmp" ,
294
+ writable = True )
294
295
self .add_volumes (self .pathmapper , runtime , any_path_okay = True ,
295
- secret_store = runtimeContext .secret_store )
296
+ secret_store = runtimeContext .secret_store ,
297
+ tmpdir_prefix = runtimeContext .tmpdir_prefix )
296
298
if self .generatemapper is not None :
297
299
self .add_volumes (
298
300
self .generatemapper , runtime , any_path_okay = any_path_okay ,
299
- secret_store = runtimeContext .secret_store )
301
+ secret_store = runtimeContext .secret_store ,
302
+ tmpdir_prefix = runtimeContext .tmpdir_prefix )
300
303
301
304
if user_space_docker_cmd :
302
305
runtime = [x .replace (":ro" , "" ) for x in runtime ]
@@ -338,25 +341,26 @@ def create_runtime(self, env, runtimeContext):
338
341
runtime .append (u"--env=HOME=%s" % self .builder .outdir )
339
342
340
343
# add parameters to docker to write a container ID file
341
- if runtimeContext .record_container_id is True :
344
+
345
+ if runtimeContext .cidfile_dir :
342
346
cidfile_dir = runtimeContext .cidfile_dir
343
- if cidfile_dir is not None :
344
- if not os . path . isdir ( cidfile_dir ):
345
- _logger . error ( "--cidfile-dir %s error: \n %s" , cidfile_dir ,
346
- cidfile_dir + " is not a directory or "
347
- "directory doesn't exist, please check it first" )
348
- exit ( 2 )
349
- if not os . path . exists ( cidfile_dir ):
350
- _logger . error ( "--cidfile-dir %s error: \n %s" , cidfile_dir ,
351
- "directory doesn't exist, please create it first" )
352
- exit ( 2 )
353
- else :
354
- cidfile_dir = os . getcwd ()
355
- cidfile_name = datetime .datetime .now ().strftime ("%Y%m%d%H%M%S-%f" ) + ".cid"
356
- if runtimeContext .cidfile_prefix is not None :
357
- cidfile_name = str (runtimeContext .cidfile_prefix + "-" + cidfile_name )
358
- cidfile_path = os .path .join (cidfile_dir , cidfile_name )
359
- runtime .append (u"--cidfile=%s" % cidfile_path )
347
+ if not os . path . exists ( str ( cidfile_dir )) :
348
+ _logger . error ( "--cidfile-dir %s error: \n %s" , cidfile_dir ,
349
+ "directory doesn't exist, please create it first" )
350
+ exit ( 2 )
351
+ if not os . path . isdir ( cidfile_dir ):
352
+ _logger . error ( "--cidfile-dir %s error: \n %s" , cidfile_dir ,
353
+ cidfile_dir + " is not a directory, "
354
+ "please check it first" )
355
+ exit ( 2 )
356
+ else :
357
+ cidfile_dir = os . getcwd ()
358
+
359
+ cidfile_name = datetime .datetime .now ().strftime ("%Y%m%d%H%M%S-%f" ) + ".cid"
360
+ if runtimeContext .cidfile_prefix is not None :
361
+ cidfile_name = str (runtimeContext .cidfile_prefix + "-" + cidfile_name )
362
+ cidfile_path = os .path .join (cidfile_dir , cidfile_name )
363
+ runtime .append (u"--cidfile=%s" % cidfile_path )
360
364
361
365
for key , value in self .environment .items ():
362
366
runtime .append (u"--env=%s=%s" % (key , value ))
@@ -373,4 +377,4 @@ def create_runtime(self, env, runtimeContext):
373
377
"--strict-memory-limit for increased portability "
374
378
"assurance." , self .name )
375
379
376
- return runtime
380
+ return runtime , cidfile_path
0 commit comments