mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Fix snap module, and module helper behavior on rc != 0 in output (#2912)
* Try some snap fixes.
* Fix logic.
* Try to run tests privileged.
* Prevent failure on rc != 0.
* Fix formatting.
* Revert "Try to run tests privileged."
This reverts commit 77ca91f502
.
* Try to run tests on RHEL instead.
* Make sure that snapd is running.
* Add changelog fragment.
* str -> to_native.
* Make sure that installed binary is actually found.
* Add check mode tests.
* Mention #2835 in changelog fragment.
This commit is contained in:
parent
a97d82be88
commit
c63dc624b7
13 changed files with 111 additions and 45 deletions
|
@ -59,4 +59,7 @@ class ModuleHelperBase(object):
|
|||
self.__init_module__()
|
||||
self.__run__()
|
||||
self.__quit_module__()
|
||||
self.module.exit_json(changed=self.has_changed(), **self.output)
|
||||
output = self.output
|
||||
if 'failed' not in output:
|
||||
output['failed'] = False
|
||||
self.module.exit_json(changed=self.has_changed(), **output)
|
||||
|
|
|
@ -107,6 +107,8 @@ snaps_removed:
|
|||
|
||||
import re
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.module_helper import (
|
||||
CmdStateModuleHelper, ArgFormat, ModuleHelperException
|
||||
)
|
||||
|
@ -123,7 +125,7 @@ __state_map = dict(
|
|||
|
||||
|
||||
def _state_map(value):
|
||||
return __state_map[value]
|
||||
return [__state_map[value]]
|
||||
|
||||
|
||||
class Snap(CmdStateModuleHelper):
|
||||
|
@ -163,20 +165,20 @@ class Snap(CmdStateModuleHelper):
|
|||
results[i].append(output[i])
|
||||
|
||||
return [
|
||||
'; '.join(results[0]),
|
||||
'; '.join([to_native(x) for x in results[0]]),
|
||||
self._first_non_zero(results[1]),
|
||||
'\n'.join(results[2]),
|
||||
'\n'.join(results[3]),
|
||||
]
|
||||
|
||||
def snap_exists(self, snap_name):
|
||||
return 0 == self.run_command(params=[{'state': 'info'}, {'name': [snap_name]}])[0]
|
||||
return 0 == self.run_command(params=[{'state': 'info'}, {'name': snap_name}])[0]
|
||||
|
||||
def is_snap_installed(self, snap_name):
|
||||
return 0 == self.run_command(params=[{'state': 'list'}, {'name': [snap_name]}])[0]
|
||||
return 0 == self.run_command(params=[{'state': 'list'}, {'name': snap_name}])[0]
|
||||
|
||||
def is_snap_enabled(self, snap_name):
|
||||
rc, out, err = self.run_command(params=[{'state': 'list'}, {'name': [snap_name]}])
|
||||
rc, out, err = self.run_command(params=[{'state': 'list'}, {'name': snap_name}])
|
||||
if rc != 0:
|
||||
return None
|
||||
result = out.splitlines()[1]
|
||||
|
@ -196,7 +198,7 @@ class Snap(CmdStateModuleHelper):
|
|||
self.validate_input_snaps() # if snap doesnt exist, it will explode when trying to install
|
||||
self.vars.meta('classic').set(output=True)
|
||||
self.vars.meta('channel').set(output=True)
|
||||
actionable_snaps = [s for s in self.vars.name if self.is_snap_installed(s)]
|
||||
actionable_snaps = [s for s in self.vars.name if not self.is_snap_installed(s)]
|
||||
if not actionable_snaps:
|
||||
return
|
||||
self.changed = True
|
||||
|
@ -207,9 +209,9 @@ class Snap(CmdStateModuleHelper):
|
|||
has_one_pkg_params = bool(self.vars.classic) or self.vars.channel != 'stable'
|
||||
has_multiple_snaps = len(actionable_snaps) > 1
|
||||
if has_one_pkg_params and has_multiple_snaps:
|
||||
commands = [params + [s] for s in actionable_snaps]
|
||||
commands = [params + [{'actionable_snaps': [s]}] for s in actionable_snaps]
|
||||
else:
|
||||
commands = [params + actionable_snaps]
|
||||
commands = [params + [{'actionable_snaps': actionable_snaps}]]
|
||||
self.vars.cmd, rc, out, err = self._run_multiple_commands(commands)
|
||||
if rc == 0:
|
||||
return
|
||||
|
@ -227,7 +229,7 @@ class Snap(CmdStateModuleHelper):
|
|||
|
||||
def state_absent(self):
|
||||
self.validate_input_snaps() # if snap doesnt exist, it will be absent by definition
|
||||
actionable_snaps = [s for s in self.vars.name if not self.is_snap_installed(s)]
|
||||
actionable_snaps = [s for s in self.vars.name if self.is_snap_installed(s)]
|
||||
if not actionable_snaps:
|
||||
return
|
||||
self.changed = True
|
||||
|
@ -235,7 +237,7 @@ class Snap(CmdStateModuleHelper):
|
|||
if self.module.check_mode:
|
||||
return
|
||||
params = ['classic', 'channel', 'state'] # get base cmd parts
|
||||
commands = [params + actionable_snaps]
|
||||
commands = [params + [{'actionable_snaps': actionable_snaps}]]
|
||||
self.vars.cmd, rc, out, err = self._run_multiple_commands(commands)
|
||||
if rc == 0:
|
||||
return
|
||||
|
@ -253,7 +255,7 @@ class Snap(CmdStateModuleHelper):
|
|||
if self.module.check_mode:
|
||||
return
|
||||
params = ['classic', 'channel', 'state'] # get base cmd parts
|
||||
commands = [params + actionable_snaps]
|
||||
commands = [params + [{'actionable_snaps': actionable_snaps}]]
|
||||
self.vars.cmd, rc, out, err = self._run_multiple_commands(commands)
|
||||
if rc == 0:
|
||||
return
|
||||
|
@ -271,7 +273,7 @@ class Snap(CmdStateModuleHelper):
|
|||
if self.module.check_mode:
|
||||
return
|
||||
params = ['classic', 'channel', 'state'] # get base cmd parts
|
||||
commands = [params + actionable_snaps]
|
||||
commands = [params + [{'actionable_snaps': actionable_snaps}]]
|
||||
self.vars.cmd, rc, out, err = self._run_multiple_commands(commands)
|
||||
if rc == 0:
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue