Commit 7e6d3d62 authored by Manuel Munz's avatar Manuel Munz

allow to auto-create external networks

parent 0eadab6a
......@@ -24,6 +24,7 @@ class DockerModule(BaseModule):
'docker_machine': None,
'commands': {},
'sync': {},
'ensure_exists': {},
}
def prepare_config(self):
......@@ -45,6 +46,8 @@ class DockerModule(BaseModule):
raise B5ExecutionError('docker commands has to be a dictionary')
if not isinstance(self.config['sync'], dict):
raise B5ExecutionError('docker sync has to be a dictionary')
if not isinstance(self.config['ensure_exists'], dict):
raise B5ExecutionError('ensure_exists has to be a dictionary')
##### CONFIGURATION MANAGEMENT #####
......@@ -163,6 +166,7 @@ class DockerModule(BaseModule):
script.append(self._script_function_source('docker-compose', '''
(
cd {base_path} && \\
if (declare -f -F {name}:create_external_networks > /dev/null); then {name}:create_external_networks; fi && \\
{name}:run {docker_compose_bin} {docker_compose_configs} "$@"
)
'''.format(
......@@ -457,6 +461,43 @@ class DockerModule(BaseModule):
else shlex.quote(command_options.get('bin')),
)))
for ensure_exists, ensure_exists_options in self.config['ensure_exists'].items():
if ensure_exists == "external_networks":
network_script = ""
if not isinstance(ensure_exists_options, list):
raise B5ExecutionError('ensure_exists options have to be a list')
for network in ensure_exists_options:
network_script = network_script + """
{name}:run {docker_bin} network inspect {network} &>/dev/null || (
{name}:run {docker_bin} network create {network} && echo \"Created external network {network}.\"
);
""".format(
name=self.name,
docker_bin=shlex.quote(self.config['docker_bin']),
network=network
)
if network_script == "":
# no networks to create => dummy function
script.append(self._script_function_source('create_external_networks', '''
(
cd {base_path}
)
'''.format(
base_path=shlex.quote(self.config['base_path']),
)))
else:
# append script to add external networks
script.append(self._script_function_source('create_external_networks', '''
(
cd {base_path} && \\
{network_script}
)
'''.format(
base_path=shlex.quote(self.config['base_path']),
network_script=network_script
)))
for sync, sync_options in self.config['sync'].items():
if not isinstance(sync_options, dict):
raise B5ExecutionError('sync options have to contain some options (at least "from" and "to")')
......
......@@ -27,6 +27,9 @@ you probably want to put inside build/, too.
`--pipe-in` to handle pipes (see `container_run`). You may use `-T` or `--disable-tty` to disable pseudo-tty
allocation at all. All these parameters must be passed first, as other parameters will be passed to the executed
command inside docker.
* **ensure_exists**: This currently only supports one subkey 'external_networks' which can be a list of
external_networks to create (if they do not yet exist) before running docker-compose. There is (currently) no logic
implemented to clean up these external networks after they have been created by b5.
* **sync**: List of paths to sync to docker volumes. See example below. Syncing some paths into volumes may
increase the performance significantly. Note that these paths will only be updated when calling `docker:update`
or `docker:sync`. Internally
......@@ -233,3 +236,41 @@ task:sync:phpvendor() {
}
```
### Example 4 - auto-create external networks
If external networks are defined in docker-compose.yml they are by default not auto-created by docker-compose and
using docker-compose will fail with an error message about missing these networks.
To auto-create these external networks when using docker-compose commands use the option
ensure_exists['external_networks'] and provide a list of external networks to auto-create:
docker-compose.yml:
```yaml
version: "3"
services:
# use an external network 'text_external_net'
php:
image: php
networks:
default:
test_external_net:
networks:
default:
test_external_net:
external: true
```
config.yml:
```yaml
modules:
docker:
ensure_exists:
# networks to auto-create
external_networks:
- test_external_net
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment