Skip to content

Commit e64a68b

Browse files
authored
Merge pull request #20 from singularityhub/add/resolv.conf
Adding creation of resolv.conf
2 parents 34b7e41 + a400e18 commit e64a68b

File tree

10 files changed

+102
-16
lines changed

10 files changed

+102
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pypi.
1515

1616
## [0.0.x](https://github.com/singularityhub/singularity-compose/tree/master) (0.0.x)
17+
- resolv.conf needs to bind by default (0.0.16)
1718
- adding run command (0.0.15)
1819
- ensuring that builds are streamed (0.0.14)
1920
- adding more build options to build as build-flags (0.0.13)

docs/commands.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,37 @@ If the build requires sudo (if you've defined sections in the config that warran
2121
setting up networking with sudo) the build will instead give you an instruction
2222
to run with sudo.
2323

24+
## Up
25+
26+
If you want to both build and bring them up, you can use "up." Note that for
27+
builds that require sudo, this will still stop and ask you to build with sudo.
28+
29+
```bash
30+
$ singularity-compose up
31+
```
32+
33+
### resolv.conf
34+
35+
By default, singularity-compose will generate a resolv.conf for you to bind
36+
to the container, instead of using the host. It's a simple template
37+
that uses Google nameservers:
38+
39+
```
40+
# This is resolv.conf generated by singularity-compose. It is provided
41+
# to provide Google nameservers. If you don't want to have it generated
42+
# and bound by default, use the up --no-resolv argument.
43+
44+
nameserver 8.8.8.8
45+
nameserver 8.8.4.4
46+
```
47+
48+
If you want to disable this:
49+
50+
```bash
51+
$ singularity-compose up --no-resolv
52+
Creating app
53+
```
54+
2455
## Create
2556

2657
Given that you have built your containers with `singularity-compose build`,
@@ -30,16 +61,25 @@ you can create your instances as follows:
3061
$ singularity-compose create
3162
```
3263

33-
## Up
64+
Akin to "up," you can also disable the generation of the resolv.conf.
65+
66+
```bash
67+
$ singularity-compose create --no-resolv
68+
Creating app
69+
```
3470

35-
If you want to both build and bring them up, you can use "up." Note that for
36-
builds that require sudo, this will still stop and ask you to build with sudo.
71+
72+
## restart
73+
74+
Restart is provided as a convenience to run "down" and then "up." You can
75+
specify most of the same arguments as create or up.
3776

3877
```bash
39-
$ singularity-compose up
78+
$ singularity-compose restart --no-resolv
79+
Stopping app
80+
Creating app
4081
```
4182

42-
Up is typically the command that you want to use to bring containers up and down.
4383

4484
## ps
4585

scompose/client/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def get_parser():
9797
help="disable the instance from writing to tmp",
9898
default=False, action='store_true')
9999

100+
sub.add_argument('--no-resolv', dest="no_resolv",
101+
help="do not generate and bind a resolv.conf",
102+
default=False, action='store_true')
103+
100104
sub.add_argument("--bridge", default="10.22.0.0/16",
101105
dest='bridge', type=str,
102106
help='the address of the bridge to derive others from.')

scompose/client/create.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ def main(args, parser, extra):
3333
# Create instances, and if none specified, create all
3434
project.create(args.names,
3535
writable_tmpfs=not args.read_only,
36-
bridge=args.bridge)
36+
bridge=args.bridge,
37+
no_resolv=args.no_resolv)

scompose/client/restart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ def main(args, parser, extra):
3636
# Create instances, and if none specified, create all
3737
project.up(args.names,
3838
writable_tmpfs=not args.read_only,
39-
bridge=args.bridge)
39+
bridge=args.bridge,
40+
no_resolv=args.no_resolv)

scompose/client/up.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ def main(args, parser, extra):
3333
# Create instances, and if none specified, create all
3434
project.up(args.names,
3535
writable_tmpfs=not args.read_only,
36-
bridge=args.bridge)
36+
bridge=args.bridge,
37+
no_resolv=args.no_resolv)

scompose/project/project.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,17 @@ def create_hosts(self, lookup):
254254
# Add the host file to be mounted
255255
write_file(hosts_file, template)
256256
return hosts_file
257-
257+
258+
259+
def generate_resolv_conf(self):
260+
'''generate a resolv.conf file to bind to the containers.
261+
We use the template provided by scompose.
262+
'''
263+
resolv_conf = os.path.join(self.working_dir, 'resolv.conf')
264+
if not os.path.exists(resolv_conf):
265+
template = read_file(get_template('resolv.conf'))
266+
write_file(resolv_conf, template)
267+
return resolv_conf
258268

259269
# Commands
260270

@@ -370,22 +380,36 @@ def down(self, names=None):
370380

371381
# Create
372382

373-
def create(self, names=None, writable_tmpfs=True, bridge="10.22.0.0/16"):
383+
def create(self, names=None,
384+
writable_tmpfs=True,
385+
bridge="10.22.0.0/16",
386+
no_resolv=False):
387+
374388
'''call the create function, which defaults to the command instance.create()
375389
'''
376-
return self._create(names, writable_tmpfs=writable_tmpfs)
390+
return self._create(names,
391+
writable_tmpfs=writable_tmpfs,
392+
no_resolv=no_resolv)
393+
394+
def up(self, names=None,
395+
writable_tmpfs=True,
396+
bridge="10.22.0.0/16",
397+
no_resolv=False):
377398

378-
def up(self, names=None, writable_tmpfs=True, bridge="10.22.0.0/16"):
379399
'''call the up function, instance.up(), which will build before if
380400
a container binary does not exist.
381401
'''
382-
return self._create(names, command="up", writable_tmpfs=writable_tmpfs)
402+
return self._create(names,
403+
command="up",
404+
writable_tmpfs=writable_tmpfs,
405+
no_resolv=no_resolv)
383406

384407
def _create(self,
385408
names,
386409
command="create",
387410
writable_tmpfs=True,
388-
bridge="10.22.0.0/16"):
411+
bridge="10.22.0.0/16",
412+
no_resolv=False):
389413

390414
'''create one or more instances. "Command" determines the sub function
391415
to call for the instance, which should be "create" or "up".
@@ -400,6 +424,8 @@ def _create(self,
400424
bridge: the bridge ip address to use for networking, and generating
401425
addresses for the individual containers.
402426
see /usr/local/etc/singularity/network/00_bridge.conflist
427+
no_resolv: if True, don't create and bind a resolv.conf with Google
428+
nameservers.
403429
'''
404430
# If no names provided, we create all
405431
names = names or self.get_instance_names()
@@ -431,9 +457,13 @@ def _create(self,
431457

432458
if do_create:
433459

434-
instance.volumes.append('%s:/etc/hosts' % hosts_file)
460+
# Generate a resolv.conf to bind to the container
461+
if not no_resolv:
462+
resolv = self.generate_resolv_conf()
463+
instance.volumes.append('%s:/etc/resolv.conf' % resolv)
435464

436465
# Create a hosts file for the instance based, add as volume
466+
instance.volumes.append('%s:/etc/hosts' % hosts_file)
437467

438468
# If we get here, execute command and add to list
439469
create_func = getattr(instance, command)

scompose/templates/resolv.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This is resolv.conf generated by singularity-compose. It is provided
2+
# to provide Google nameservers. If you don't want to have it generated
3+
# and bound by default, use the up --no-resolv argument.
4+
5+
nameserver 8.8.8.8
6+
nameserver 8.8.4.4
7+

scompose/tests/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_commands(tmp_path):
4242
print('Testing up')
4343
project.up()
4444
assert 'etc.hosts' in os.listdir()
45+
assert 'resolv.conf' in os.listdir()
4546

4647
print('Waiting for instance to start')
4748
sleep(10)

scompose/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
'''
1919

20-
__version__ = "0.0.15"
20+
__version__ = "0.0.16"
2121
AUTHOR = 'Vanessa Sochat'
2222
AUTHOR_EMAIL = '[email protected]'
2323
NAME = 'singularity-compose'

0 commit comments

Comments
 (0)