From a9ae696fdb1e882a9be767574204bb845023f515 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 6 Jan 2022 03:57:19 +0100 Subject: basic openwrt deploy support --- .../deploy/action_plugins/openwrt_sysupgrade.py | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py (limited to 'roles/openwrt/deploy/action_plugins') diff --git a/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py b/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py new file mode 100644 index 00000000..16772937 --- /dev/null +++ b/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py @@ -0,0 +1,57 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os + +from ansible.errors import AnsibleError, AnsibleAction, AnsibleActionFail, AnsibleActionSkip, AnsibleConnectionFailure +from ansible.module_utils._text import to_native +from ansible.plugins.action import ActionBase + + +class ActionModule(ActionBase): + TRANSFERS_FILES = True + + def run(self, tmp=None, task_vars=None): + if task_vars is None: + task_vars = dict() + + if self._task.environment and any(self._task.environment): + self._display.warning('openwrt_sysupgrade module does not support the environment keyword') + + result = super(ActionModule, self).run(tmp, task_vars) + del tmp # tmp no longer has any effect + self._cleanup_remote_tmp = False + + try: + if self._play_context.check_mode: + raise AnsibleActionSkip('Check mode is not supported for this task.') + + result['changed'] = True + + try: + image = to_native(self._task.args.get('image', ''), errors='surrogate_or_strict') + image = self._loader.get_real_file(self._find_needle('files', image), decrypt=False) + except AnsibleError as e: + raise AnsibleActionFail(to_native(e)) + + tmp_img = self._connection._shell.join_path(self._connection._shell.tmpdir, os.path.basename(image)) + self._transfer_file(image, tmp_img) + self._fixup_perms2((self._connection._shell.tmpdir, tmp_img), execute=False) + + args = to_native(self._task.args.get('args', ''), errors='surrogate_or_strict') + + script_cmd = ' '.join(['sysupgrade', args, tmp_img]) + script_cmd = self._connection._shell.wrap_for_exec(script_cmd) + + try: + result.update(self._low_level_execute_command(cmd=script_cmd)) + except AnsibleConnectionFailure as e: + result['rc'] = 0 + + if 'rc' in result and result['rc'] != 0: + raise AnsibleActionFail('non-zero return code') + + except AnsibleAction as e: + result.update(e.result) + + return result -- cgit v1.2.3