diff options
author | Christian Poessinger <christian@poessinger.com> | 2021-08-23 11:48:47 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2021-08-23 11:49:46 +0200 |
commit | e71a98562aabea53cb9b5e1958398e1e674490bb (patch) | |
tree | 80290698e506e92a69710bb7cea70d37fa2fc2ee | |
parent | 1f6746c44c5349a35847abfb410d3826a4e7ca99 (diff) | |
download | vyos-1x-e71a98562aabea53cb9b5e1958398e1e674490bb.tar.gz vyos-1x-e71a98562aabea53cb9b5e1958398e1e674490bb.zip |
containers: T2216: add CLI commands to specify restart behavior and memory usage
A container is limited to 256MB memory by default and will always restart on
failure.
-rw-r--r-- | interface-definitions/containers.xml.in | 42 | ||||
-rwxr-xr-x | src/conf_mode/containers.py | 22 |
2 files changed, 59 insertions, 5 deletions
diff --git a/interface-definitions/containers.xml.in b/interface-definitions/containers.xml.in index 030980dba..419802866 100644 --- a/interface-definitions/containers.xml.in +++ b/interface-definitions/containers.xml.in @@ -47,6 +47,24 @@ <help>Image name in the hub-registry</help> </properties> </leafNode> + <leafNode name="memory"> + <properties> + <help>Constrain the memory available to a container (default: 256MB)</help> + <valueHelp> + <format>u32:0</format> + <description>Unlimited</description> + </valueHelp> + <valueHelp> + <format>u32:1-16384</format> + <description>Container memory in megabytes (MB)</description> + </valueHelp> + <constraint> + <validator name="numeric" argument="--range 0-16384"/> + </constraint> + <constraintErrorMessage>Container memory must be in range 0 to 16384 MB</constraintErrorMessage> + </properties> + <defaultValue>256</defaultValue> + </leafNode> <tagNode name="network"> <properties> <help>Attach user defined network to container</help> @@ -119,6 +137,30 @@ </leafNode> </children> </tagNode> + <leafNode name="restart"> + <properties> + <help>Mount a volume into the container</help> + <completionHelp> + <list>no on-failure always</list> + </completionHelp> + <valueHelp> + <format>no</format> + <description>Do not restart containers on exit</description> + </valueHelp> + <valueHelp> + <format>on-failure</format> + <description>Restart containers when they exit with a non-zero exit code, retrying indefinitely (default)</description> + </valueHelp> + <valueHelp> + <format>always</format> + <description>Restart containers when they exit, regardless of status, retrying indefinitely</description> + </valueHelp> + <constraint> + <regex>^(no|on-failure|always)$</regex> + </constraint> + </properties> + <defaultValue>on-failure</defaultValue> + </leafNode> <tagNode name="volume"> <properties> <help>Mount a volume into the container</help> diff --git a/src/conf_mode/containers.py b/src/conf_mode/containers.py index 23f17ab55..fd0c5e52d 100755 --- a/src/conf_mode/containers.py +++ b/src/conf_mode/containers.py @@ -79,8 +79,17 @@ def get_config(config=None): # We have gathered the dict representation of the CLI, but there are default # options which we need to update into the dictionary retrived. default_values = defaults(base) + # container base default values can not be merged here - remove and add them later + if 'name' in default_values: + del default_values['name'] container = dict_merge(default_values, container) + # Merge per-container default values + if 'name' in container: + default_values = defaults(base + ['name']) + for name in container['name']: + container['name'][name] = dict_merge(default_values, container['name'][name]) + # Delete container network, delete containers tmp = node_changed(conf, ['container', 'network']) if tmp: container.update({'net_remove' : tmp}) @@ -216,6 +225,8 @@ def apply(container): # Check if the container has already been created if not container_exists(name): image = container_config['image'] + memory = container_config['memory'] + restart = container_config['restart'] # Currently the best way to run a command and immediately print stdout print(os.system(f'podman pull {image}')) @@ -242,19 +253,20 @@ def apply(container): # Bind volume volume = '' if 'volume' in container_config: - for vol in container_config['volume']: - svol = container_config['volume'][vol]['source'] - dvol = container_config['volume'][vol]['destination'] + for vol, vol_config in container_config['volume']: + svol = vol_config['source'] + dvol = vol_config['destination'] volume += f' -v {svol}:{dvol}' + container_base_cmd = f'podman run -dit --name {name} --memory {memory} --restart {restart} {port} {volume} {env_opt} {image}' if 'allow_host_networks' in container_config: - _cmd(f'podman run -dit --name {name} --net host {port} {volume} {env_opt} {image}') + _cmd(f'{container_base_cmd} --net host') else: for network in container_config['network']: ipparam = '' if 'address' in container_config['network'][network]: ipparam = '--ip ' + container_config['network'][network]['address'] - _cmd(f'podman run --name {name} -dit --net {network} {ipparam} {port} {volume} {env_opt} {image}') + _cmd(f'{container_base_cmd} --net {network} {ipparam}') # Else container is already created. Just start it. # It's needed after reboot. |