Skip to content

Commit

Permalink
Add option to parse CreateCommand easily for diff calc (#698)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sshnaidm authored Jan 11, 2024
1 parent eab26e5 commit 1fe9642
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
19 changes: 19 additions & 0 deletions plugins/module_utils/podman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
55 changes: 38 additions & 17 deletions plugins/module_utils/podman/podman_container_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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']]
Expand Down Expand Up @@ -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:
Expand All @@ -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']:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 1fe9642

Please sign in to comment.