summaryrefslogtreecommitdiffstats
path: root/ansible_collections/cisco/nxos/plugins/action/rollback.py
blob: 3602c4074ae9c04afbefc641a2377d258e302118 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#
# (c) 2016 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import absolute_import, division, print_function


__metaclass__ = type

from ansible import constants as C
from ansible.module_utils.connection import Connection
from ansible.utils.display import Display
from ansible_collections.ansible.netcommon.plugins.action.network import (
    ActionModule as ActionNetworkModule,
)


display = Display()


class ActionModule(ActionNetworkModule):
    def run(self, tmp=None, task_vars=None):
        del tmp  # tmp no longer has any effect

        module_name = self._task.action.split(".")[-1]
        self._config_module = True if module_name in ["nxos_config", "config"] else False
        persistent_connection = self._play_context.connection.split(".")[-1]

        warnings = []

        if (self._play_context.connection == "httpapi") and module_name in (
            "nxos_file_copy",
            "nxos_nxapi",
        ):
            return {
                "failed": True,
                "msg": "Connection httpapi is not valid for '%s' module." % (module_name),
            }

        if module_name == "nxos_file_copy":
            # when file_pull is enabled, the file_pull_timeout and connect_ssh_port options
            # will override persistent_command_timeout and port
            # this has been kept for backwards compatibility till these options are removed
            if persistent_connection != "network_cli":
                return {
                    "failed": True,
                    "msg": "Connection type must be fully qualified name for network_cli connection type, got %s"
                    % self._play_context.connection,
                }

            conn = Connection(self._connection.socket_path)
            file_pull = self._task.args.get("file_pull", False)
            file_pull_timeout = self._task.args.get("file_pull_timeout", 300)
            connect_ssh_port = self._task.args.get("connect_ssh_port", 22)

            if file_pull:
                conn.set_option("persistent_command_timeout", file_pull_timeout)
                conn.set_option("port", connect_ssh_port)

        if module_name == "nxos_install_os":
            connection = self._connection
            if connection.transport == "local":
                persistent_command_timeout = C.PERSISTENT_COMMAND_TIMEOUT
                persistent_connect_timeout = C.PERSISTENT_CONNECT_TIMEOUT
            else:
                persistent_command_timeout = connection.get_option("persistent_command_timeout")
                persistent_connect_timeout = connection.get_option("persistent_connect_timeout")

            display.vvvv(
                "PERSISTENT_COMMAND_TIMEOUT is %s" % str(persistent_command_timeout),
                self._play_context.remote_addr,
            )
            display.vvvv(
                "PERSISTENT_CONNECT_TIMEOUT is %s" % str(persistent_connect_timeout),
                self._play_context.remote_addr,
            )
            if persistent_command_timeout < 600 or persistent_connect_timeout < 600:
                msg = "PERSISTENT_COMMAND_TIMEOUT and PERSISTENT_CONNECT_TIMEOUT"
                msg += " must be set to 600 seconds or higher when using nxos_install_os module."
                msg += " Current persistent_command_timeout setting:" + str(
                    persistent_command_timeout,
                )
                msg += " Current persistent_connect_timeout setting:" + str(
                    persistent_connect_timeout,
                )
                return {"failed": True, "msg": msg}

        if persistent_connection in ("network_cli", "httpapi"):
            if module_name == "nxos_gir":
                conn = Connection(self._connection.socket_path)
                persistent_command_timeout = conn.get_option("persistent_command_timeout")
                gir_timeout = 200
                if persistent_command_timeout < gir_timeout:
                    conn.set_option("persistent_command_timeout", gir_timeout)
                    msg = "timeout value extended to %ss for nxos_gir" % gir_timeout
                    display.warning(msg)

        else:
            return {
                "failed": True,
                "msg": "Connection type %s is not valid for this module"
                % self._play_context.connection,
            }

        result = super(ActionModule, self).run(task_vars=task_vars)
        if warnings:
            if "warnings" in result:
                result["warnings"].extend(warnings)
            else:
                result["warnings"] = warnings
        return result