Commit 77730f31 authored by David Danier's avatar David Danier

Merge branch 'autocreate-external-networks' into 'master'

allow to auto-create external networks

See merge request build/b5!7
parents a229a3bc aaf8780a
......@@ -4,7 +4,7 @@ import pwd
import warnings
from b5.exceptions import B5ExecutionError
from b5.modules import BaseModule
from b5.modules import BaseModule, CONFIG_PREFIX_RE
class DockerModule(BaseModule):
......@@ -24,6 +24,7 @@ class DockerModule(BaseModule):
'docker_machine': None,
'commands': {},
'sync': {},
'setup': {},
}
def prepare_config(self):
......@@ -45,6 +46,12 @@ 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['setup'], dict):
raise B5ExecutionError('setup has to be a dictionary')
else:
if 'networks' in self.config['setup']:
if not isinstance(self.config['setup']['networks'], list):
raise B5ExecutionError('setup.networks has to be a list')
##### CONFIGURATION MANAGEMENT #####
......@@ -140,11 +147,13 @@ class DockerModule(BaseModule):
)))
script.append(self._script_function_source('run', '''
{name}:setup
(
{docker_env}
"$@"
)
'''.format(
name=self.name,
docker_env=self.get_script_env(),
base_path=shlex.quote(self.config['base_path']),
)))
......@@ -457,6 +466,65 @@ class DockerModule(BaseModule):
else shlex.quote(command_options.get('bin')),
)))
if self.config['setup']:
# Setup networks
if 'networks' in self.config['setup'] and self.config['setup']['networks']:
# Create a function for each network
for setup_network_name in self.config['setup']['networks']:
script.append(self._script_function_source('setup:network:{network}'.format(network=setup_network_name), '''
{name}:docker network inspect {network} &>/dev/null || (
{name}:docker network create {network} && echo \"Created network {network}.\"
);
'''.format(
name=self.name,
network=shlex.quote(setup_network_name),
)))
# Create general network setup function
script.append(self._script_function_source('setup:network', '''
{setup_all_networks}
'''.format(
setup_all_networks='\n '.join([
'{name}:setup:network:{network}'.format(
name=self.name,
network=setup_network_name,
)
for setup_network_name
in self.config['setup']['networks']
])
)))
# Create general setup function
setup_ran_var_name = '_{prefix}_SETUP_RAN'.format(
prefix=CONFIG_PREFIX_RE.sub('_', self.name.upper())
)
script.append('{setup_ran_var}=0'.format(setup_ran_var=setup_ran_var_name))
setup_running_var_name = '_{prefix}_SETUP_RUNNING'.format(
prefix=CONFIG_PREFIX_RE.sub('_', self.name.upper())
)
script.append('{setup_running_var_name}=0'.format(setup_running_var_name=setup_running_var_name))
script.append(self._script_function_source('setup', '''
if [ "${setup_ran_var_name}" -eq 1 ]
then
return 0
fi
if [ "${setup_running_var_name}" -eq 1 ]
then
return 0
fi
{setup_running_var_name}=1
{setup_networks}
{setup_ran_var_name}=1
'''.format(
setup_networks='{name}:setup:network'.format(name=self.name) if
'networks' in self.config['setup'] and self.config['setup']['networks'] else '',
setup_ran_var_name=setup_ran_var_name,
setup_running_var_name=setup_running_var_name,
)))
else:
# Create EMPTY general setup function, if no config exists
script.append(self._script_function_source('setup', '''
true
'''))
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,10 @@ 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.
* **setup**: Allows you to tell b5 to setup some docker specific things before running docker itself. This currently
only supports one subkey 'networks' which can be a list of networks to create (if they do not yet exist).
There is (currently) no logic implemented to clean up these networks after they have been created by b5.
Can be used to make sure networks marked as external exist before running docker.
* **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 +237,41 @@ task:sync:phpvendor() {
}
```
### Example 4 - auto-create 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
setup.networks and provide a list of 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:
setup:
# networks to auto-create
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