Skip to content

Commit 10ef73f

Browse files
committed
fixing issues with etc.hosts files
Signed-off-by: Vanessa Sochat <[email protected]>
1 parent a75ed00 commit 10ef73f

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

MANIFEST.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include README.md LICENSE
2+
recursive-include scompose *
3+
global-exclude *.py[co]
4+
recursive-exclude .git *
5+
global-exclude *.img
6+
global-exclude *.simg
7+
global-exclude *.sif
8+
global-exclude __pycache__

scompose/project/instance.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
'''
1919

2020
from scompose.logger import bot
21-
from scompose.utils import get_userhome
21+
from scompose.utils import (
22+
get_userhome,
23+
read_file,
24+
write_file
25+
)
2226
from spython.main import get_client
27+
from scompose.templates import get_template
28+
2329
import shlex as _shlex
2430
import os
2531
import platform
32+
import re
2633
import sys
2734

2835

@@ -280,6 +287,42 @@ def stop(self):
280287
self.instance.stop(sudo=self.sudo)
281288
self.instance = None
282289

290+
# Networking
291+
292+
def get_address(self):
293+
'''get an ip address of an image. If it's busybox, we can't use
294+
hostname -I.
295+
'''
296+
ip_address = None
297+
298+
if self.sudo:
299+
if self.exists():
300+
result = self.client.execute(image=self.instance.get_uri(),
301+
command=['hostname', '-I'],
302+
return_result=True,
303+
quiet=True,
304+
sudo=self.sudo)
305+
306+
# Busybox won't have hostname -I
307+
if result['return_code'] != 0:
308+
cmd = "ip -4 --oneline address show up eth0"
309+
result = self.client.execute(image=self.instance.get_uri(),
310+
command=cmd,
311+
return_result=True,
312+
quiet=True,
313+
sudo=self.sudo)
314+
315+
ip_address = result['message'].strip('\n').strip()
316+
317+
# Clean up busybox output
318+
if "inet" in ip_address:
319+
ip_address = re.match('.+ inet (?P<address>.+)/', ip_address).groups()[0]
320+
else:
321+
ip_address = '127.0.1.1'
322+
323+
return ip_address
324+
325+
283326
# Logs
284327

285328
def logs(self, tail=0):

scompose/project/project.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ def parse(self):
163163
for _, instance in self.instances.items():
164164
instance.set_volumes_from(self.instances)
165165

166+
166167
# Networking
167168

168169
def create_hosts(self, name, depends_on):
169170
'''create a hosts file to bind to all containers, where we define the
170171
correct hostnames to correspond with the ip addresses created.
171-
172172
Note: This function is terrible. Singularity should easily expose
173173
these addresses. See issue here:
174174
https://github.com/sylabs/singularity/issues/3751
@@ -179,34 +179,16 @@ def create_hosts(self, name, depends_on):
179179

180180
for _, instance in self.instances.items():
181181
if instance.name in depends_on:
182-
if self.sudo:
183-
if instance.exists():
184-
result = self.client.execute(image=instance.instance.get_uri(),
185-
command=['hostname', '-I'],
186-
return_result=True,
187-
quiet=True,
188-
sudo=self.sudo)
189-
190-
# Busybox won't have hostname -I
191-
if result['return_code'] != 0:
192-
cmd = "ip -4 --oneline address show up eth0"
193-
result = self.client.execute(image=instance.instance.get_uri(),
194-
command=cmd,
195-
return_result=True,
196-
quiet=True,
197-
sudo=self.sudo)
198-
199-
ip_address = result['message'].strip('\n').strip()
200-
201-
# Clean up busybox output
202-
if "inet" in ip_address:
203-
ip_address = re.match('.+ inet (?P<address>.+)/', ip_address).groups()[0]
204-
else:
205-
ip_address = '127.0.1.1'
206-
207-
template = ['%s\t%s\n' % (ip_address, instance.name)] + template
208-
instance.volumes.append('%s:/etc/hosts' % hosts_basename)
209-
write_file(hosts_file, template)
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)
210192

211193

212194
# Commands
@@ -218,7 +200,7 @@ def shell(self, name):
218200
if name in self.instances:
219201
instance = self.instances[name]
220202
if instance.exists():
221-
self.client.shell(instance.instance.get_uri())
203+
self.client.shell(instance.instance.get_uri(), sudo=self.sudo)
222204

223205

224206
def logs(self, names, tail=0):

0 commit comments

Comments
 (0)