From 69adaa143cf9cb35918f8a18a6d9b3fe074c72b5 Mon Sep 17 00:00:00 2001 From: vsoch Date: Wed, 24 Aug 2022 17:27:41 -0600 Subject: [PATCH] adding support for custom network args Signed-off-by: vsoch --- CHANGELOG.md | 2 ++ docs/spec/spec-2.0.md | 37 ++++++++++++++++++++++++++++- scompose/client/__init__.py | 7 +++--- scompose/config/schema.py | 3 ++- scompose/project/instance.py | 24 +++++++++++++++---- scompose/project/project.py | 9 +++---- scompose/tests/test_client.py | 1 - scompose/tests/test_command_args.py | 2 -- scompose/tests/test_depends_on.py | 2 -- scompose/version.py | 2 +- 10 files changed, 68 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb99159..c03e137 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/docs/spec/spec-2.0.md b/docs/spec/spec-2.0.md index 3fbecdd..974f51e 100644 --- a/docs/spec/spec-2.0.md +++ b/docs/spec/spec-2.0.md @@ -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 ``` @@ -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 diff --git a/scompose/client/__init__.py b/scompose/client/__init__.py index 4599c16..70df764 100644 --- a/scompose/client/__init__.py +++ b/scompose/client/__init__.py @@ -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 @@ -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) @@ -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]: diff --git a/scompose/config/schema.py b/scompose/config/schema.py index 18e4832..560641a 100644 --- a/scompose/config/schema.py +++ b/scompose/config/schema.py @@ -8,7 +8,6 @@ """ -import os import sys from jsonschema.exceptions import ValidationError @@ -57,6 +56,8 @@ def validate_config(filepath): "properties": { "allocate_ip": {"type": "boolean"}, "enable": {"type": "boolean"}, + # --network-args + "args": string_list, }, } diff --git a/scompose/project/instance.py b/scompose/project/instance.py index d8c53a4..f79d3bb 100644 --- a/scompose/project/instance.py +++ b/scompose/project/instance.py @@ -16,7 +16,6 @@ import os import platform import re -import time class Instance: @@ -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 = [ @@ -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 @@ -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: diff --git a/scompose/project/project.py b/scompose/project/project.py index 420c2a4..2070480 100644 --- a/scompose/project/project.py +++ b/scompose/project/project.py @@ -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. @@ -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: @@ -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() diff --git a/scompose/tests/test_client.py b/scompose/tests/test_client.py index 15d0119..4e325c4 100644 --- a/scompose/tests/test_client.py +++ b/scompose/tests/test_client.py @@ -10,7 +10,6 @@ from scompose.utils import run_command from time import sleep import requests -import pytest import os diff --git a/scompose/tests/test_command_args.py b/scompose/tests/test_command_args.py index d8ac2e2..55e0b25 100644 --- a/scompose/tests/test_command_args.py +++ b/scompose/tests/test_command_args.py @@ -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__)) diff --git a/scompose/tests/test_depends_on.py b/scompose/tests/test_depends_on.py index 130d70c..31912ba 100644 --- a/scompose/tests/test_depends_on.py +++ b/scompose/tests/test_depends_on.py @@ -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__)) diff --git a/scompose/version.py b/scompose/version.py index 85b4d0a..2f95c74 100644 --- a/scompose/version.py +++ b/scompose/version.py @@ -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"