Skip to content

Commit c62745b

Browse files
authored
Merge pull request #40 from vsoch/master
tweaks for builder
2 parents 8f4f02d + a2f1955 commit c62745b

File tree

5 files changed

+52
-42
lines changed

5 files changed

+52
-42
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
name="singularity",
88

99
# Version number:
10-
version="0.39",
10+
version="0.44",
1111

1212
# Application author details:
1313
author="Vanessa Sochat",

singularity/build/google.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import subprocess
4848
import sys
4949
import tempfile
50+
import time
5051
import uuid
5152
import 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

singularity/build/scripts/singularity-build-latest.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ sudo pip3 install --upgrade google
1212
sudo pip3 install oauth2client==3.0.0
1313
sudo pip3 install gitpython
1414
cd /tmp && git clone https://github.com/singularityware/singularity-python
15-
cd singularity-python && python3 setup.py sdist && sudo python3 setup.py install
15+
cd singularity-python && python3 setup.py sdist && sudo python3 setup.py install && cd ..
1616
python3 -c "from singularity.build.google import run_build; run_build()" > /tmp/.shub-log 2>&1
17-
command=$(echo "from singularity.build.google import finish_build; finish_build(logfile='/tmp/.shub-log')")
17+
command=$(echo "from singularity.build.google import finish_build; finish_build()")
1818
python3 -c "$command"

singularity/package.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
import os
2525

2626

27-
def estimate_image_size(spec=None,sudopw=None,padding=200):
27+
def estimate_image_size(spec_file,sudopw=None,padding=50):
2828
'''estimate_image_size will generate an image in a directory, and add
2929
some padding to it to estimate the size of the image file to generate
3030
:param sudopw: the sudopw for Singularity, root should provide ''
31-
:param spec: the spec file, called "Singuarity"
31+
:param spec_file: the spec file, called "Singuarity"
3232
:param padding: the padding (MB) to add to the image
3333
'''
3434
size_dir = tempfile.mkdtemp()
3535
tmp_dir = tempfile.mkdtemp()
36-
image_folder = build_from_spec(spec=spec_file, # default will package the image
36+
image_folder = build_from_spec(spec_file=spec_file, # default will package the image
3737
sudopw=sudopw, # with root should not need sudo
3838
output_folder=size_dir,
3939
build_dir=tmp_dir,
@@ -47,26 +47,26 @@ def estimate_image_size(spec=None,sudopw=None,padding=200):
4747
return padded_size
4848

4949

50-
def build_from_spec(spec=None,build_dir=None,size=None,sudopw=None,
50+
def build_from_spec(spec_file=None,build_dir=None,size=None,sudopw=None,
5151
output_folder=None,build_folder=False):
5252
'''build_from_spec will build a "spec" file in a "build_dir" and return the directory
53-
:param spec: the spec file, called "Singuarity"
53+
:param spec_file: the spec file, called "Singuarity"
5454
:param sudopw: the sudopw for Singularity, root should provide ''
5555
:param build_dir: the directory to build in. If not defined, will use tmpdir.
5656
:param size: the size of the image
5757
:param output_folder: where to output the image package
5858
:param build_folder: "build" the image into a folder instead. Default False
5959
'''
60-
if spec == None:
61-
spec = "Singularity"
60+
if spec_file == None:
61+
spec_file = "Singularity"
6262
if build_dir == None:
6363
build_dir = tempfile.mkdtemp()
6464
bot.logger.debug("Building in directory %s",build_dir)
6565

6666
# Copy the spec to a temporary directory
67-
spec_path = "%s/%s" %(build_dir,spec)
67+
spec_path = "%s/%s" %(build_dir,spec_file)
6868
if not os.path.exists(spec_path):
69-
shutil.copyfile(spec,spec_path)
69+
shutil.copyfile(spec_file,spec_path)
7070
# If name isn't provided, call it Singularity
7171
image_path = "%s/image" %(build_dir)
7272
# Run create image and bootstrap with Singularity command line tool.

singularity/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,22 @@ def read_file(filename,mode="r"):
234234
############################################################################
235235

236236

237-
def calculate_folder_size(folder_path):
237+
def calculate_folder_size(folder_path,truncate=True):
238238
'''calculate_folder size recursively walks a directory to calculate
239-
a total size (in)
239+
a total size (in MB)
240240
:param folder_path: the path to calculate size for
241+
:param truncate: if True, converts size to an int
241242
'''
242243
total_size = 0
243244
for dirpath, dirnames, filenames in os.walk(folder_path):
244245
for filey in filenames:
245-
fp = os.path.join(dirpath, filey)
246-
total_size += os.path.getsize(fp)
247-
return total_size
246+
file_path = os.path.join(dirpath, filey)
247+
if os.path.isfile(file_path) and not os.path.islink(file_path):
248+
total_size += os.path.getsize(file_path) # this is bytes
249+
size_mb = total_size / 1000000
250+
if truncate == True:
251+
size_mb = int(size_mb)
252+
return size_mb
248253

249254

250255
def remove_unicode_dict(input_dict):

0 commit comments

Comments
 (0)