diff options
| author | Zen3515 <7106408+Zen3515@users.noreply.github.com> | 2023-01-31 14:23:06 +0700 | 
|---|---|---|
| committer | Zen3515 <7106408+Zen3515@users.noreply.github.com> | 2023-01-31 14:23:06 +0700 | 
| commit | 53aebddb4ca54b0cc4a296d6cc4c4d960c5f1d73 (patch) | |
| tree | 25a3980e580fd1e125ff5a450c45131342157043 | |
| parent | 44bca52113479fcbe12c55a4740dd4669d2b7856 (diff) | |
| download | vyos-1x-53aebddb4ca54b0cc4a296d6cc4c4d960c5f1d73.tar.gz vyos-1x-53aebddb4ca54b0cc4a296d6cc4c4d960c5f1d73.zip | |
container: T4014: Add `command`, `arg` and `entrypoint` configuration options for containers
| -rw-r--r-- | interface-definitions/container.xml.in | 27 | ||||
| -rwxr-xr-x | src/conf_mode/container.py | 21 | 
2 files changed, 45 insertions, 3 deletions
| diff --git a/interface-definitions/container.xml.in b/interface-definitions/container.xml.in index b61664125..2ea1e6ab2 100644 --- a/interface-definitions/container.xml.in +++ b/interface-definitions/container.xml.in @@ -104,11 +104,38 @@                </leafNode>              </children>            </tagNode> +          <leafNode name="entrypoint"> +            <properties> +              <help>Override the default ENTRYPOINT from the image</help> +              <constraint> +                <regex>[ !#-%&(-~]+</regex> +              </constraint> +              <constraintErrorMessage>Entrypoint must be ascii characters, use &quot; and &apos for double and single quotes respectively</constraintErrorMessage> +            </properties> +          </leafNode>            <leafNode name="image">              <properties>                <help>Image name in the hub-registry</help>              </properties>            </leafNode> +          <leafNode name="command"> +            <properties> +              <help>Override the default CMD from the image</help> +              <constraint> +                <regex>[ !#-%&(-~]+</regex> +              </constraint> +              <constraintErrorMessage>Command must be ascii characters, use &quot; and &apos for double and single quotes respectively</constraintErrorMessage> +            </properties> +          </leafNode> +          <leafNode name="arguments"> +            <properties> +              <help>The command's arguments for this container</help> +              <constraint> +                <regex>[ !#-%&(-~]+</regex> +              </constraint> +              <constraintErrorMessage>The command's arguments must be ascii characters, use &quot; and &apos for double and single quotes respectively</constraintErrorMessage> +            </properties> +          </leafNode>            <leafNode name="memory">              <properties>                <help>Memory (RAM) available to this container</help> diff --git a/src/conf_mode/container.py b/src/conf_mode/container.py index 08861053d..30016b865 100755 --- a/src/conf_mode/container.py +++ b/src/conf_mode/container.py @@ -279,8 +279,22 @@ def generate_run_arguments(name, container_config):                           f'--memory {memory}m --shm-size {shared_memory}m --memory-swap 0 --restart {restart} ' \                           f'--name {name} {device} {port} {volume} {env_opt}' +    entrypoint = '' +    if 'entrypoint' in container_config: +        # it needs to be json-formatted with single quote on the outside +        entrypoint = json_write(container_config['entrypoint'].split()).replace('"', """) +        entrypoint = f'--entrypoint '{entrypoint}'' + +    command = '' +    if 'command' in container_config: +        command = container_config['command'].strip() + +    command_arguments = '' +    if 'arguments' in container_config: +        command_arguments = container_config['arguments'].strip() +      if 'allow_host_networks' in container_config: -        return f'{container_base_cmd} --net host {image}' +        return f'{container_base_cmd} --net host {entrypoint} {image} {command} {command_arguments}'.strip()      ip_param = ''      networks = ",".join(container_config['network']) @@ -289,7 +303,7 @@ def generate_run_arguments(name, container_config):              address = container_config['network'][network]['address']              ip_param = f'--ip {address}' -    return f'{container_base_cmd} --net {networks} {ip_param} {image}' +    return f'{container_base_cmd} --net {networks} {ip_param} {entrypoint} {image} {command} {command_arguments}'.strip()  def generate(container):      # bail out early - looks like removal from running config @@ -341,7 +355,8 @@ def generate(container):              file_path = os.path.join(systemd_unit_path, f'vyos-container-{name}.service')              run_args = generate_run_arguments(name, container_config) -            render(file_path, 'container/systemd-unit.j2', {'name': name, 'run_args': run_args}) +            render(file_path, 'container/systemd-unit.j2', {'name': name, 'run_args': run_args,}, +                   formater=lambda _: _.replace(""", '"').replace("'", "'"))      return None | 
