1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

snap: detect change by checking final state (#7340)

* snap: detect change by checking final state

* detect change in snap_status variable

* add changelog frag
This commit is contained in:
Alexei Znamensky 2023-10-08 23:03:31 +13:00 committed by GitHub
parent 324c22f612
commit 323a1dcff8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 19 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- snap - change the change detection mechanism from "parsing installation" to "comparing end state with initial state" (https://github.com/ansible-collections/community.general/pull/7340, https://github.com/ansible-collections/community.general/issues/7265).

View file

@ -183,7 +183,6 @@ class Snap(StateModuleHelper):
__disable_re = re.compile(r'(?:\S+\s+){5}(?P<notes>\S+)') __disable_re = re.compile(r'(?:\S+\s+){5}(?P<notes>\S+)')
__set_param_re = re.compile(r'(?P<snap_prefix>\S+:)?(?P<key>\S+)\s*=\s*(?P<value>.+)') __set_param_re = re.compile(r'(?P<snap_prefix>\S+:)?(?P<key>\S+)\s*=\s*(?P<value>.+)')
__list_re = re.compile(r'^(?P<name>\S+)\s+\S+\s+\S+\s+(?P<channel>\S+)') __list_re = re.compile(r'^(?P<name>\S+)\s+\S+\s+\S+\s+(?P<channel>\S+)')
__install_re = re.compile(r'(?P<name>\S+)\s.+\s(installed|refreshed)')
module = dict( module = dict(
argument_spec={ argument_spec={
'name': dict(type='list', elements='str', required=True), 'name': dict(type='list', elements='str', required=True),
@ -209,14 +208,24 @@ class Snap(StateModuleHelper):
# if state=present there might be file names passed in 'name', in # if state=present there might be file names passed in 'name', in
# which case they must be converted to their actual snap names, which # which case they must be converted to their actual snap names, which
# is done using the names_from_snaps() method calling 'snap info'. # is done using the names_from_snaps() method calling 'snap info'.
if self.vars.state == "present":
self.vars.set("snapinfo_run_info", [], output=(self.verbosity >= 4)) self.vars.set("snapinfo_run_info", [], output=(self.verbosity >= 4))
self.vars.set("status_run_info", [], output=(self.verbosity >= 4))
self.vars.set("status_out", None, output=(self.verbosity >= 4))
self.vars.set("run_info", [], output=(self.verbosity >= 4))
if self.vars.state == "present":
self.vars.set("snap_names", self.names_from_snaps(self.vars.name)) self.vars.set("snap_names", self.names_from_snaps(self.vars.name))
status_var = "snap_names" status_var = "snap_names"
else: else:
status_var = "name" status_var = "name"
self.vars.set("snap_status", self.snap_status(self.vars[status_var], self.vars.channel), output=False) self.vars.set("status_var", status_var, output=False)
self.vars.set("snap_status_map", dict(zip(self.vars.name, self.vars.snap_status)), output=False) self.vars.set("snap_status", self.snap_status(self.vars[self.vars.status_var], self.vars.channel), output=False, change=True)
self.vars.set("snap_status_map", dict(zip(self.vars.name, self.vars.snap_status)), output=False, change=True)
def __quit_module__(self):
self.vars.snap_status = self.snap_status(self.vars[self.vars.status_var], self.vars.channel)
if self.vars.channel is None:
self.vars.channel = "stable"
def _run_multiple_commands(self, commands, actionable_names, bundle=True, refresh=False): def _run_multiple_commands(self, commands, actionable_names, bundle=True, refresh=False):
results_cmd = [] results_cmd = []
@ -252,10 +261,6 @@ class Snap(StateModuleHelper):
results_run_info, results_run_info,
) )
def __quit_module__(self):
if self.vars.channel is None:
self.vars.channel = "stable"
def convert_json_subtree_to_map(self, json_subtree, prefix=None): def convert_json_subtree_to_map(self, json_subtree, prefix=None):
option_map = {} option_map = {}
@ -350,7 +355,6 @@ class Snap(StateModuleHelper):
list_out = out.split('\n')[1:] list_out = out.split('\n')[1:]
list_out = [self.__list_re.match(x) for x in list_out] list_out = [self.__list_re.match(x) for x in list_out]
list_out = [(m.group('name'), m.group('channel')) for m in list_out if m] list_out = [(m.group('name'), m.group('channel')) for m in list_out if m]
if self.verbosity >= 4:
self.vars.status_out = list_out self.vars.status_out = list_out
self.vars.status_run_info = ctx.run_info self.vars.status_run_info = ctx.run_info
@ -383,13 +387,9 @@ class Snap(StateModuleHelper):
self.vars.cmd, rc, out, err, run_info = self._run_multiple_commands(params, actionable_snaps, bundle=False, refresh=refresh) self.vars.cmd, rc, out, err, run_info = self._run_multiple_commands(params, actionable_snaps, bundle=False, refresh=refresh)
else: else:
self.vars.cmd, rc, out, err, run_info = self._run_multiple_commands(params, actionable_snaps, refresh=refresh) self.vars.cmd, rc, out, err, run_info = self._run_multiple_commands(params, actionable_snaps, refresh=refresh)
if self.verbosity >= 4:
self.vars.run_info = run_info self.vars.run_info = run_info
if rc == 0: if rc == 0:
match_install2 = [self.__install_re.match(line) for line in out.split('\n')]
match_install = [m.group('name') in actionable_snaps for m in match_install2 if m]
if len(match_install) == len(actionable_snaps):
return return
classic_snap_pattern = re.compile(r'^error: This revision of snap "(?P<package_name>\w+)"' classic_snap_pattern = re.compile(r'^error: This revision of snap "(?P<package_name>\w+)"'
@ -480,7 +480,6 @@ class Snap(StateModuleHelper):
if self.check_mode: if self.check_mode:
return return
self.vars.cmd, rc, out, err, run_info = self._run_multiple_commands(params, actionable_snaps) self.vars.cmd, rc, out, err, run_info = self._run_multiple_commands(params, actionable_snaps)
if self.verbosity >= 4:
self.vars.run_info = run_info self.vars.run_info = run_info
if rc == 0: if rc == 0:
return return

View file

@ -402,6 +402,19 @@ TEST_CASES = [
out="hello-world (12345/stable) v12345 from Canonical** installed\n", out="hello-world (12345/stable) v12345 from Canonical** installed\n",
err="", err="",
), ),
RunCmdCall(
command=['/testbin/snap', 'list'],
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
rc=0,
out=(
"Name Version Rev Tracking Publisher Notes"
"core20 20220826 1623 latest/stable canonical** base"
"lxd 5.6-794016a 23680 latest/stable/… canonical** -"
"hello-world 5.6-794016a 23680 latest/stable/… canonical** -"
"snapd 2.57.4 17336 latest/stable canonical** snapd"
""),
err="",
),
] ]
), ),
ModuleTestCase( ModuleTestCase(
@ -438,6 +451,20 @@ TEST_CASES = [
out=issue_6803_kubectl_out, out=issue_6803_kubectl_out,
err="", err="",
), ),
RunCmdCall(
command=['/testbin/snap', 'list'],
environ={'environ_update': {'LANGUAGE': 'C', 'LC_ALL': 'C'}, 'check_rc': False},
rc=0,
out=(
"Name Version Rev Tracking Publisher Notes"
"core20 20220826 1623 latest/stable canonical** base"
"lxd 5.6-794016a 23680 latest/stable/… canonical** -"
"microk8s 5.6-794016a 23680 latest/stable/… canonical** -"
"kubectl 5.6-794016a 23680 latest/stable/… canonical** -"
"snapd 2.57.4 17336 latest/stable canonical** snapd"
""),
err="",
),
] ]
), ),
] ]