4747import subprocess
4848import sys
4949import tempfile
50+ import time
5051import uuid
5152import zipfile
5253
@@ -157,18 +158,18 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
157158 {'key' : 'secret' , 'value' : secret , 'return_text' : True },
158159 {'key' : 'size' , 'value' : size , 'return_text' : True },
159160 {'key' : 'branch' , 'value' : branch , 'return_text' : True },
160- {'key' : 'container_id' , 'value' : None , 'return_text' : True }]
161-
162- # Default spec file is Singularity
163- if spec_file == None :
164- spec_file = "Singularity"
165-
166- if bucket_name == None :
167- bucket_name = "singularity-hub"
161+ {'key' : 'spec_file' , 'value' : spec_file , 'return_text' : True }]
168162
169163 # Obtain values from build
170164 params = get_build_params (metadata )
171165
166+ # Default spec file is Singularity
167+ if params ['spec_file' ] == None :
168+ params ['spec_file' ] = "Singularity"
169+
170+ if params ['bucket_name' ] == None :
171+ params ['bucket_name' ] = "singularity-hub"
172+
172173 # Download the repo and image
173174 repo = download_repo (repo_url = params ['repo_url' ],
174175 destination = build_dir )
@@ -188,16 +189,16 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
188189 bot .logger .warning ("commit not specified, setting to current %s" , params ['commit' ])
189190
190191
191- if os .path .exists (spec_file ):
192- bot .logger .info ("Found spec file %s in repository" ,spec_file )
192+ if os .path .exists (params [ ' spec_file' ] ):
193+ bot .logger .info ("Found spec file %s in repository" ,params [ ' spec_file' ] )
193194
194- # If size is None, get from image + 200 padding
195+ # If size is None, get from image + 50 padding
195196 if params ['size' ] == None :
196197 bot .logger .info ("Size not detected for build. Will estimate with 200MB padding." )
197- params ['size' ] = estimate_image_size (spec = spec_file ,
198+ params ['size' ] = estimate_image_size (spec_file = os . path . abspath ( params [ ' spec_file' ]) ,
198199 sudopw = '' )
199200
200- image = build_from_spec (spec = spec_file , # default will package the image
201+ image = build_from_spec (spec_file = params [ ' spec_file' ] , # default will package the image
201202 size = params ['size' ],
202203 sudopw = '' , # with root should not need sudo
203204 output_folder = build_dir ,
@@ -209,7 +210,7 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
209210
210211 # Package the image metadata (files, folders, etc)
211212 image_package = package (image_path = image ,
212- spec_path = spec_file ,
213+ spec_path = params [ ' spec_file' ] ,
213214 output_folder = build_dir ,
214215 sudopw = '' ,
215216 remove_image = True ,
@@ -254,8 +255,7 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
254255 "repo_url" : params ['repo_url' ],
255256 "commit" : params ['commit' ],
256257 "repo_id" : params ['repo_id' ],
257- "secret" : params ['secret' ],
258- "container_id" : params ['container_id' ]}
258+ "secret" : params ['secret' ]}
259259
260260 # Did the user specify a specific log file?
261261 logfile = get_build_metadata (key = 'logfile' )
@@ -276,16 +276,16 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
276276 else :
277277 # Tell the user what is actually there
278278 present_files = glob ("*" )
279- bot .logger .error ("Build file %s not found in repository" ,spec_file )
279+ bot .logger .error ("Build file %s not found in repository" ,params [ ' spec_file' ] )
280280 bot .logger .info ("Found files are %s" ,"\n " .join (present_files ))
281281
282282
283283 # Clean up
284284 shutil .rmtree (build_dir )
285285
286286
287- def finish_build (logfile ,singularity_version = None ,repo_url = None ,bucket_name = None ,commit = None , verbose = True , repo_id = None ,
288- logging_response_url = None ,secret = None ,token = None ):
287+ def finish_build (logfile = None ,singularity_version = None ,repo_url = None ,bucket_name = None ,commit = None ,
288+ logging_url = None ,secret = None ,token = None , verbose = True , repo_id = None ):
289289 '''finish_build will finish the build by way of sending the log to the same bucket.
290290 :param build_dir: directory to do the build in. If not specified,
291291 will use temporary.
@@ -297,7 +297,7 @@ def finish_build(logfile,singularity_version=None,repo_url=None,bucket_name=None
297297 :param singularity_version: the version of singularity installed
298298 :param verbose: print out extra details as we go (default True)
299299 :param secret: a secret to match to the correct container
300- :param logging_response_url : the logging response url to send the response back to.
300+ :param logging_url : the logging response url to send the response back to.
301301 :: note: this function is currently configured to work with Google Compute
302302 Engine metadata api, and should (will) be customized if needed to work elsewhere
303303 '''
@@ -310,13 +310,14 @@ def finish_build(logfile,singularity_version=None,repo_url=None,bucket_name=None
310310 singularity_version = get_singularity_version (singularity_version )
311311
312312 # Get variables from the instance metadata API
313- metadata = [{'key' : 'logging_url' , 'value' : logging_response_url , 'return_text' : True },
313+ metadata = [{'key' : 'logging_url' , 'value' : logging_url , 'return_text' : True },
314314 {'key' : 'repo_url' , 'value' : repo_url , 'return_text' : False },
315315 {'key' : 'repo_id' , 'value' : repo_id , 'return_text' : True },
316316 {'key' : 'token' , 'value' : token , 'return_text' : False },
317317 {'key' : 'commit' , 'value' : commit , 'return_text' : True },
318318 {'key' : 'bucket_name' , 'value' : bucket_name , 'return_text' : True },
319- {'key' : 'secret' , 'value' : secret , 'return_text' : True }]
319+ {'key' : 'secret' , 'value' : secret , 'return_text' : True },
320+ {'key' : 'logfile' , 'value' : logfile , 'return_text' : True }]
320321
321322 if bucket_name == None :
322323 bucket_name = "singularity-hub"
@@ -333,24 +334,28 @@ def finish_build(logfile,singularity_version=None,repo_url=None,bucket_name=None
333334 log_file = upload_file (storage_service ,
334335 bucket = bucket ,
335336 bucket_path = image_path ,
336- file_name = logfile )
337+ file_name = params [ ' logfile' ] )
337338
338339 # Finally, package everything to send back to shub
339340 response = {"log" : json .dumps (log_file ),
340341 "repo_url" : params ['repo_url' ],
341342 "commit" : params ['commit' ],
343+ "logfile" : params ['logfile' ],
342344 "repo_id" : params ['repo_id' ],
343345 "secret" : params ['secret' ]}
344346
345-
346347 if params ['token' ] != None :
347348 response ['token' ] = params ['token' ]
348349
349350 # Send it back!
350351 if params ['logging_url' ] != None :
351352 finish = requests .post (params ['logging_url' ],data = response )
353+
354+ # Delay a minute, to give buffer between bringing instance down
355+ time .sleep (60 )
352356
353357
358+
354359#####################################################################################
355360# METADATA
356361#####################################################################################
@@ -393,7 +398,7 @@ def get_build_params(metadata):
393398 bot .logger .warning ('%s not found in function call.' ,item ['key' ])
394399 response = get_build_metadata (key = item ['key' ])
395400 item ['value' ] = response
396- params [item ['key' ]] = item ['value' ]
401+ params [item ['key' ]] = item ['value' ]
397402 if item ['key' ] != 'credential' :
398403 bot .logger .info ('%s is set to %s' ,item ['key' ],item ['value' ])
399404 return params
0 commit comments