From 1fe96425767610c2a0f90bef0942a4d1e58380d3 Mon Sep 17 00:00:00 2001 From: Sergey <6213510+sshnaidm@users.noreply.github.com> Date: Thu, 11 Jan 2024 15:30:24 +0200 Subject: [PATCH] Add option to parse CreateCommand easily for diff calc (#698) We have a few calculations of given options and arguments from CreateCommand, so let's do it in a more convinient way. Define a function _createcommand which receives an argument and returns all values for it in a command line of Podman. Signed-off-by: Sagi Shnaidman --- plugins/module_utils/podman/common.py | 19 +++++++ .../podman/podman_container_lib.py | 55 +++++++++++++------ 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/plugins/module_utils/podman/common.py b/plugins/module_utils/podman/common.py index d890043e..cbb6b080 100644 --- a/plugins/module_utils/podman/common.py +++ b/plugins/module_utils/podman/common.py @@ -19,6 +19,25 @@ ' < 2.11, you need to use Python < 3.12 with ' 'distutils.version present'), exc) +ARGUMENTS_OPTS_DICT = { + '--attach': ['--attach', '-a'], + '--cpu-shares': ['--cpu-shares', '-c'], + '--detach': ['--detach', '-d'], + '--env': ['--env', '-e'], + '--hostname': ['--hostname', '-h'], + '--interactive': ['--interactive', '-i'], + '--label': ['--label', '-l'], + '--memory': ['--memory', '-m'], + '--network': ['--network', '--net'], + '--publish': ['--publish', '-p'], + '--publish-all': ['--publish-all', '-P'], + '--quiet': ['--quiet', '-q'], + '--tty': ['--tty', '-t'], + '--user': ['--user', '-u'], + '--volume': ['--volume', '-v'], + '--workdir': ['--workdir', '-w'], +} + def run_podman_command(module, executable='podman', args=None, expected_rc=0, ignore_errors=False): if not isinstance(executable, list): diff --git a/plugins/module_utils/podman/podman_container_lib.py b/plugins/module_utils/podman/podman_container_lib.py index 33530fd3..d0acfcda 100644 --- a/plugins/module_utils/podman/podman_container_lib.py +++ b/plugins/module_utils/podman/podman_container_lib.py @@ -9,6 +9,7 @@ from ansible_collections.containers.podman.plugins.module_utils.podman.common import generate_systemd from ansible_collections.containers.podman.plugins.module_utils.podman.common import delete_systemd from ansible_collections.containers.podman.plugins.module_utils.podman.common import normalize_signal +from ansible_collections.containers.podman.plugins.module_utils.podman.common import ARGUMENTS_OPTS_DICT __metaclass__ = type @@ -732,6 +733,35 @@ def defaultize(self): params_with_defaults[p] = self.module_params[p] return params_with_defaults + def _createcommand(self, argument): + """Returns list of values for given argument from CreateCommand + from Podman container inspect output. + + Args: + argument (str): argument name + + Returns: + + all_values: list of values for given argument from createcommand + """ + if "createcommand" not in self.info["config"]: + return [] + cr_com = self.info["config"]["createcommand"] + argument_values = ARGUMENTS_OPTS_DICT.get(argument, [argument]) + all_values = [] + for arg in argument_values: + for ind, cr_opt in enumerate(cr_com): + if arg == cr_opt: + # This is a key=value argument + if not cr_com[ind + 1].startswith("-"): + all_values.append(cr_com[ind + 1]) + else: + # This is a false/true switching argument + return [True] + if cr_opt.startswith("%s=" % arg): + all_values.append(cr_opt.split("=", 1)[1]) + return all_values + def _diff_update_and_compare(self, param_name, before, after): if before != after: self.diff['before'].update({param_name: before}) @@ -881,10 +911,7 @@ def diffparam_device(self): before = [":".join([i['pathonhost'], i['pathincontainer']]) for i in self.info['hostconfig']['devices']] if not before and 'createcommand' in self.info['config']: - cr_com = self.info['config']['createcommand'] - if '--device' in cr_com: - before = [cr_com[k + 1].lower() - for k, i in enumerate(cr_com) if i == '--device'] + before = [i.lower() for i in self._createcommand('--device')] before = [":".join((i, i)) if len(i.split(":")) == 1 else i for i in before] after = [":".join(i.split(":")[:2]) for i in self.params['device']] @@ -1092,9 +1119,8 @@ def diffparam_mac_address(self): if macs: before = macs[0] if not before and 'createcommand' in self.info['config']: - cr_com = self.info['config']['createcommand'] - if '--mac-address' in cr_com: - before = cr_com[cr_com.index('--mac-address') + 1].lower() + before = [i.lower() for i in self._createcommand('--mac-address')] + before = before[0] if before else '' if self.module_params['mac_address'] is not None: after = self.params['mac_address'] else: @@ -1110,11 +1136,10 @@ def diffparam_network(self): before = [] # Special case for options for slirp4netns rootless networking from v2 if net_mode_before == 'slirp4netns' and 'createcommand' in self.info['config']: - cr_com = self.info['config']['createcommand'] - if '--network' in cr_com: - cr_net = cr_com[cr_com.index('--network') + 1].lower() - if 'slirp4netns:' in cr_net: - before = [cr_net] + cr_net = [i.lower() for i in self._createcommand('--network')] + for cr_net_opt in cr_net: + if 'slirp4netns:' in cr_net_opt: + before = [cr_net_opt] after = self.params['network'] or [] # If container is in pod and no networks are provided if not self.module_params['network'] and self.params['pod']: @@ -1256,11 +1281,7 @@ def diffparam_ulimit(self): after = self.params['ulimit'] or [] # In case of latest podman if 'createcommand' in self.info['config']: - ulimits = [] - for k, c in enumerate(self.info['config']['createcommand']): - if c == '--ulimit': - ulimits.append(self.info['config']['createcommand'][k + 1]) - before = ulimits + before = self._createcommand('--ulimit') before, after = sorted(before), sorted(after) return self._diff_update_and_compare('ulimit', before, after) if after: