1111 api_post
1212)
1313
14+ from singularity .build .utils import get_singularity_version
15+
1416from singularity .package import (
1517 build_from_spec ,
18+ estimate_image_size ,
1619 package
1720)
1821
5962def get_storage_service ():
6063 credentials = GoogleCredentials .get_application_default ()
6164 return build ('storage' , 'v1' , credentials = credentials )
65+
66+
67+ def get_compute_service ():
68+ credentials = GoogleCredentials .get_application_default ()
69+ return build ('compute' , 'v1' , credentials = credentials )
6270
71+
6372def get_bucket (storage_service ,bucket_name ):
6473 req = storage_service .buckets ().get (bucket = bucket_name )
6574 return req .execute ()
@@ -106,7 +115,7 @@ def list_bucket(bucket,storage_service):
106115
107116
108117def run_build (build_dir = None ,spec_file = None ,repo_url = None ,token = None ,size = None ,bucket_name = None ,
109- repo_id = None ,commit = None ,verbose = True ,response_url = None ,secret = None ):
118+ repo_id = None ,commit = None ,verbose = True ,response_url = None ,secret = None , branch = None ):
110119 '''run_build will generate the Singularity build from a spec_file from a repo_url.
111120 If no arguments are required, the metadata api is queried for the values.
112121 :param build_dir: directory to do the build in. If not specified,
@@ -115,13 +124,14 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
115124 :param repo_url: the url to download the repo from
116125 :param repo_id: the repo_id to uniquely identify the repo (in case name changes)
117126 :param commit: the commit to checkout. If none provided, will use most recent.
118- :param size: the size of the image to build. If none set, builds default 1024.
127+ :param size: the size of the image to build. If none set, builds folder size + 200MB padding
119128 :param bucket_name: the name of the bucket to send files to
120129 :param verbose: print out extra details as we go (default True)
121130 :param token: a token to send back to the server to authenticate the collection
122131 :param secret: a secret to match to the correct container
123132 :param response_url: the build url to send the response back to. Should also come
124133 from metadata. If not specified, no response is sent
134+ :param branch: the branch to checkout for the build.
125135 :: note: this function is currently configured to work with Google Compute
126136 Engine metadata api, and should (will) be customized if needed to work elsewhere
127137 '''
@@ -145,7 +155,9 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
145155 {'key' : 'token' , 'value' : token , 'return_text' : False },
146156 {'key' : 'commit' , 'value' : commit , 'return_text' : True },
147157 {'key' : 'secret' , 'value' : secret , 'return_text' : True },
148- {'key' : 'size' , 'value' : size , 'return_text' : True }]
158+ {'key' : 'size' , 'value' : size , 'return_text' : True },
159+ {'key' : 'branch' , 'value' : branch , 'return_text' : True },
160+ {'key' : 'container_id' , 'value' : None , 'return_text' : True }]
149161
150162 # Default spec file is Singularity
151163 if spec_file == None :
@@ -162,6 +174,10 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
162174 destination = build_dir )
163175
164176 os .chdir (build_dir )
177+ if params ['branch' ] != None :
178+ bot .logger .info ('Checking out branch %s' ,params ['branch' ])
179+ os .system ('git checkout %s' % (params ['branch' ]))
180+
165181 if params ['commit' ] != None :
166182 bot .logger .info ('Checking out commit %s' ,params ['commit' ])
167183 os .system ('git checkout %s .' % (params ['commit' ]))
@@ -175,6 +191,12 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
175191 if os .path .exists (spec_file ):
176192 bot .logger .info ("Found spec file %s in repository" ,spec_file )
177193
194+ # If size is None, get from image + 200 padding
195+ if params ['size' ] == None :
196+ bot .logger .info ("Size not detected for build. Will estimate with 200MB padding." )
197+ params ['size' ] = estimate_image_size (spec = spec_file ,
198+ sudopw = '' )
199+
178200 image = build_from_spec (spec = spec_file , # default will package the image
179201 size = params ['size' ],
180202 sudopw = '' , # with root should not need sudo
@@ -232,7 +254,17 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
232254 "repo_url" : params ['repo_url' ],
233255 "commit" : params ['commit' ],
234256 "repo_id" : params ['repo_id' ],
235- "secret" : params ['secret' ]}
257+ "secret" : params ['secret' ],
258+ "container_id" : params ['container_id' ]}
259+
260+ # Did the user specify a specific log file?
261+ logfile = get_build_metadata (key = 'logfile' )
262+ if logfile != None :
263+ response ['logfile' ] = logfile
264+
265+ if params ['branch' ] != None :
266+ response ['branch' ] = params ['branch' ]
267+
236268
237269 if params ['token' ] != None :
238270 response ['token' ] = params ['token' ]
@@ -252,7 +284,7 @@ def run_build(build_dir=None,spec_file=None,repo_url=None,token=None,size=None,b
252284 shutil .rmtree (build_dir )
253285
254286
255- def finish_build (logfile ,repo_url = None ,bucket_name = None ,commit = None ,verbose = True ,repo_id = None ,
287+ def finish_build (logfile ,singularity_version = None , repo_url = None ,bucket_name = None ,commit = None ,verbose = True ,repo_id = None ,
256288 logging_response_url = None ,secret = None ,token = None ):
257289 '''finish_build will finish the build by way of sending the log to the same bucket.
258290 :param build_dir: directory to do the build in. If not specified,
@@ -262,6 +294,7 @@ def finish_build(logfile,repo_url=None,bucket_name=None,commit=None,verbose=True
262294 :param repo_id: the repo_id to uniquely identify the repo (in case name changes)
263295 :param commit: the commit to checkout. If none provided, will use most recent.
264296 :param bucket_name: the name of the bucket to send files to
297+ :param singularity_version: the version of singularity installed
265298 :param verbose: print out extra details as we go (default True)
266299 :param secret: a secret to match to the correct container
267300 :param logging_response_url: the logging response url to send the response back to.
@@ -273,6 +306,9 @@ def finish_build(logfile,repo_url=None,bucket_name=None,commit=None,verbose=True
273306 if go == None :
274307 sys .exit (0 )
275308
309+ if singularity_version == None :
310+ singularity_version = get_singularity_version (singularity_version )
311+
276312 # Get variables from the instance metadata API
277313 metadata = [{'key' : 'logging_url' , 'value' : logging_response_url , 'return_text' : True },
278314 {'key' : 'repo_url' , 'value' : repo_url , 'return_text' : False },
0 commit comments