Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for custom network args #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and **Merged pull requests**. Critical items to know are:
The versions coincide with releases on pypi.

## [0.1.x](https://github.com/singularityhub/singularity-compose/tree/master) (0.1.x)
- support for network args (0.1.19)
- allow for background run commands (run->background:true) 0.1.18
- add support for instance replicas (0.1.17)
- fix check command validation (0.1.16)
- fix a bug triggered when using startoptions in conjunction with network=false (0.1.15)
Expand Down
37 changes: 36 additions & 1 deletion docs/spec/spec-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ INFO: Cleaning up image...

To allow fakeroot to bind ports without sudo you need to execute this:

```
```bash
echo "allow net networks = bridge, fakeroot" >> /etc/singularity/singularity.conf
```

Expand All @@ -149,6 +149,41 @@ The example below will disable the network features:
enable: true | false
```

### Custom Network

As of version 0.1.19 we have support for custom network args, and a full example of using a custom network
[here](https://github.com/singularityhub/singularity-compose-examples/tree/master/v2.0/custom-network).
To summarize, let's say you start with this configuration at `/usr/local/etc/singularity/network/50_static-redis.conflist`:

```yaml
version: "2.0"
instances:
cg-cache:
name: redis
run:
background: true
build:
context: .
recipe: redis.def
start:
background: true
options:
- "env-file=./env-file.sh"
command: redis-server --bind 0.0.0.0 --requirepass ${REDIS_PASSWORD}
volumes:
- ./env-file.sh:/.singularity.d/env/env-file.sh
network:
enable: true
allocate_ip: true

# If network args are provided, --network none is not used
args:
- '"portmap=6379:6379/tcp"'
ports:
- 6379:6379
```

The addition is support for ``--network-args`` and the custom network (the portmap) that you've defined above.

## Start Group

Expand Down
7 changes: 3 additions & 4 deletions scompose/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_parser():
)

# print version and exit
version = subparsers.add_parser("version", help="show software version")
subparsers.add_parser("version", help="show software version")

# Build

Expand All @@ -112,8 +112,7 @@ def get_parser():
)

# Config

config = subparsers.add_parser("config", help="Validate and view the compose file")
subparsers.add_parser("config", help="Validate and view the compose file")

# Create (assumes built already), Up (will also build, if able)

Expand Down Expand Up @@ -189,7 +188,7 @@ def get_parser():
action="store_true",
)

ps = subparsers.add_parser("ps", help="list instances")
subparsers.add_parser("ps", help="list instances")

# Add list of names
for sub in [build, create, down, logs, up, restart, stop]:
Expand Down
3 changes: 2 additions & 1 deletion scompose/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

"""

import os
import sys

from jsonschema.exceptions import ValidationError
Expand Down Expand Up @@ -57,6 +56,8 @@ def validate_config(filepath):
"properties": {
"allocate_ip": {"type": "boolean"},
"enable": {"type": "boolean"},
# --network-args
"args": string_list,
},
}

Expand Down
24 changes: 19 additions & 5 deletions scompose/project/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import os
import platform
import re
import time


class Instance:
Expand Down Expand Up @@ -174,13 +173,17 @@ def set_network(self, params):
self.network[key] = self.network.get(key, True)

def set_ports(self, params):
"""set ports from the recipe to be used"""
"""
Set ports from the recipe to be used
"""
self.ports = params.get("ports", [])

# Commands

def set_start(self, params):
"""set arguments to the startscript"""
"""
Set arguments to the startscript
"""
start = params.get("start", {})
self.args = start.get("args", "")
self.start_opts = [
Expand Down Expand Up @@ -211,6 +214,13 @@ def _get_command_opts(self, group):
"""
return ["--%s" % opt if len(opt) > 1 else "-%s" % opt for opt in group]

@property
def network_args(self):
"""
Return a list of network args.
"""
return self.params.get("network", {}).get("args", [])

def _get_network_commands(self, ip_address=None):
"""
Take a list of ports, return the list of --network-args to
Expand All @@ -221,8 +231,12 @@ def _get_network_commands(self, ip_address=None):
# Fakeroot means not needing sudo
fakeroot = "--fakeroot" in self.start_opts or "-f" in self.start_opts

# If not sudo or fakeroot, we need --network none
if not self.sudo and not fakeroot:
# Add all network args
network_args = self.network_args
for arg in network_args:
ports += ["--network-args", arg]

if not network_args and (not self.sudo and not fakeroot):
ports += ["--network", "none"]

for pair in self.ports:
Expand Down
9 changes: 5 additions & 4 deletions scompose/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def get_instance_names(self):
return names

def set_filename(self, filename):
"""Set the filename to read the recipe from.
"""
Set the filename to read the recipe from.

If not provided, defaults to singularity-compose.yml. The working directory
is set to be the directory name of the configuration file.
Expand Down Expand Up @@ -297,7 +298,8 @@ def get_bridge_address(self, name="sbr0"):
return bridge_address

def create_hosts(self, lookup):
"""create a hosts file to bind to all containers, where we define the
"""
Create a hosts file to bind to all containers, where we define the
correct hostnames to correspond with the ip addresses created.
Note: This function is terrible. Singularity should easily expose
these addresses. See issue here:
Expand Down Expand Up @@ -503,8 +505,7 @@ def _create(
bridge: the bridge ip address to use for networking, and generating
addresses for the individual containers.
see /usr/local/etc/singularity/network/00_bridge.conflist
no_resolv: if True, don't create and bind a resolv.conf with Google
nameservers.
no_resolv: if True, don't create and bind a resolv.conf.
"""
# If no names provided, we create all
names = names or self.get_instance_names()
Expand Down
1 change: 0 additions & 1 deletion scompose/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from scompose.utils import run_command
from time import sleep
import requests
import pytest
import os


Expand Down
2 changes: 0 additions & 2 deletions scompose/tests/test_command_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

from scompose.logger import bot
from scompose.project import Project
from scompose.utils import run_command
from time import sleep
import shutil
import pytest
import os

here = os.path.dirname(os.path.abspath(__file__))
Expand Down
2 changes: 0 additions & 2 deletions scompose/tests/test_depends_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

from scompose.logger import bot
from scompose.project import Project
from scompose.utils import run_command
from time import sleep
import shutil
import pytest
import os

here = os.path.dirname(os.path.abspath(__file__))
Expand Down
2 changes: 1 addition & 1 deletion scompose/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""

__version__ = "0.1.17"
__version__ = "0.1.19"
AUTHOR = "Vanessa Sochat"
AUTHOR_EMAIL = "vsoch@users.noreply.github.com"
NAME = "singularity-compose"
Expand Down