summaryrefslogtreecommitdiff
path: root/roles/openwrt/deploy/action_plugins/openwrt_sysupgrade.py
blob: 167729375e82eac4979c6fa4c27bc69329ce06c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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