@@ -166,30 +166,46 @@ def parse(self):
166166
167167# Networking
168168
169+ def get_bridge_address (self , name = 'sbr0' ):
170+ '''get the (named) bridge address on the host. It should be automatically
171+ created by Singularity over 3.0.
172+ '''
173+ command = ["ip" , "-4" , "--oneline" , "address" , "show" , "up" , name ]
174+ result = self .client ._run_command (command ,
175+ return_result = True ,
176+ quiet = True ,
177+ sudo = self .sudo )['message' ]
178+ bridge_address = re .match ('.+ inet (?P<address>.+)/' , result ).groups ()[0 ]
179+ return bridge_address
180+
181+
169182 def create_hosts (self , name , depends_on ):
170183 '''create a hosts file to bind to all containers, where we define the
171184 correct hostnames to correspond with the ip addresses created.
172185 Note: This function is terrible. Singularity should easily expose
173186 these addresses. See issue here:
174187 https://github.com/sylabs/singularity/issues/3751
188+
189+ Parameters
190+ ==========
191+ name: the name of the instance to create
192+ depends_on: the other instances it depends on
175193 '''
176194 template = read_file (get_template ('hosts' ))
177195 hosts_file = os .path .join (self .working_dir , 'etc.hosts.%s' % name )
178196 hosts_basename = os .path .basename (hosts_file )
179197
198+ # Add an entry for each instance hostname to see the others
180199 for _ , instance in self .instances .items ():
181- if instance .name in depends_on :
182- ip_address = instance .get_address ()
183- if ip_address is not None :
184- template = ['%s\t %s\n ' % (ip_address , instance .name )] + template
185-
186- # Add to our instance
187- instance = self .instances .get (name )
188-
189- if instance :
190- instance .volumes .append ('%s:/etc/hosts' % hosts_basename )
191- write_file (hosts_file , template )
192-
200+ ip_address = instance .get_address ()
201+ if ip_address :
202+ print (ip_address )
203+ template = ['%s\t %s\n ' % (ip_address , instance .name )] + template
204+
205+ # Add the host file to be mounted
206+ write_file (hosts_file , template )
207+ return hosts_file
208+
193209
194210# Commands
195211
@@ -225,18 +241,20 @@ def execute(self, name, commands):
225241 sudo = self .sudo ):
226242 print (line , end = '' )
227243
244+ # Logs
245+
246+ def clear_logs (self , names ):
247+ '''clear_logs will remove all old error and output logs.
248+ '''
249+ for instance in self .iter_instances (names ):
250+ instance .clear_logs ()
251+
228252
229253 def logs (self , names , tail = 0 ):
230254 '''logs will print logs to the screen.
231255 '''
232- # If no names provided, show all logs
233- if not names :
234- names = self .get_instance_names ()
235-
236- # Print logs for each
237- for name in names :
238- if name in self .instances :
239- self .instances [name ].logs (tail = tail )
256+ for instance in self .iter_instances (names ):
257+ instance .logs (tail = tail )
240258
241259# Config
242260
@@ -273,7 +291,11 @@ def up(self, names, writable_tmpfs=False):
273291 '''
274292 return self ._create (names , command = "up" , writable_tmpfs = writable_tmpfs )
275293
276- def _create (self , names , command = "create" , writable_tmpfs = False ):
294+ def _create (self ,
295+ names ,
296+ command = "create" ,
297+ writable_tmpfs = True ):
298+
277299 '''create one or more instances. "Command" determines the sub function
278300 to call for the instance, which should be "create" or "up".
279301 If the user provide a list of names, use them, otherwise default
@@ -289,6 +311,8 @@ def _create(self, names, command="create", writable_tmpfs=False):
289311 if not names :
290312 names = self .get_instance_names ()
291313
314+ writable_tmpfs = True
315+
292316 # Keep a count to determine if we have circular dependency structure
293317 created = []
294318 count = 0
@@ -309,9 +333,11 @@ def _create(self, names, command="create", writable_tmpfs=False):
309333
310334 if do_create :
311335
312- # Create a hosts file for the instance based on depends
313- self .create_hosts (instance .name , created )
336+ # Create a hosts file for the instance based, add as volume
337+ hosts_file = self .create_hosts (instance .name , depends_on )
338+ instance .volumes .append ('%s:/etc/hosts' % hosts_file )
314339
340+ print (instance .volumes )
315341 # If we get here, execute command and add to list
316342 getattr (instance , command )(self .working_dir , writable_tmpfs )
317343 created .append (instance .name )
0 commit comments