| 1 |
# (c) 2017, Dag Wieers <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
# CI-required python3 boilerplate |
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import time |
| 22 |
from datetime import datetime, timedelta, timezone |
| 23 |
|
| 24 |
from ansible.module_utils.common.text.converters import to_text |
| 25 |
from ansible.plugins.action import ActionBase |
| 26 |
from ansible.utils.display import Display |
| 27 |
|
| 28 |
display = Display() |
| 29 |
|
| 30 |
|
| 31 |
class TimedOutException(Exception): |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
class ActionModule(ActionBase): |
| 36 |
TRANSFERS_FILES = False |
| 37 |
_VALID_ARGS = frozenset(('connect_timeout', 'delay', 'sleep', 'timeout')) |
| 38 |
|
| 39 |
DEFAULT_CONNECT_TIMEOUT = 5 |
| 40 |
DEFAULT_DELAY = 0 |
| 41 |
DEFAULT_SLEEP = 1 |
| 42 |
DEFAULT_TIMEOUT = 600 |
| 43 |
|
| 44 |
def do_until_success_or_timeout(self, what, timeout, connect_timeout, what_desc, sleep=1): |
| 45 |
max_end_time = datetime.now(timezone.utc) + timedelta(seconds=timeout) |
| 46 |
|
| 47 |
e = None |
| 48 |
while datetime.now(timezone.utc) < max_end_time: |
| 49 |
try: |
| 50 |
what(connect_timeout) |
| 51 |
if what_desc: |
| 52 |
display.debug("wait_for_connection: %s success" % what_desc) |
| 53 |
return |
| 54 |
except Exception as e: |
| 55 |
error = e # PY3 compatibility to store exception for use outside of this block |
| 56 |
if what_desc: |
| 57 |
display.debug("wait_for_connection: %s fail (expected), retrying in %d seconds..." % (what_desc, sleep)) |
| 58 |
time.sleep(sleep) |
| 59 |
|
| 60 |
raise TimedOutException("timed out waiting for %s: %s" % (what_desc, error)) |
| 61 |
|
| 62 |
def run(self, tmp=None, task_vars=None): |
| 63 |
if task_vars is None: |
| 64 |
task_vars = dict() |
| 65 |
|
| 66 |
connect_timeout = int(self._task.args.get('connect_timeout', self.DEFAULT_CONNECT_TIMEOUT)) |
| 67 |
delay = int(self._task.args.get('delay', self.DEFAULT_DELAY)) |
| 68 |
sleep = int(self._task.args.get('sleep', self.DEFAULT_SLEEP)) |
| 69 |
timeout = int(self._task.args.get('timeout', self.DEFAULT_TIMEOUT)) |
| 70 |
|
| 71 |
if self._task.check_mode: |
| 72 |
display.vvv("wait_for_connection: not executing in check_mode") |
| 73 |
return dict(skipped=True, # deprecated: description='returning skipped from actions/modules' core_version='2.25' |
| 74 |
changed=False, |
| 75 |
msg='`wait_for_connection` did not execute due to check mode') |
| 76 |
|
| 77 |
result = super(ActionModule, self).run(tmp, task_vars) |
| 78 |
del tmp # tmp no longer has any effect |
| 79 |
|
| 80 |
def ping_module_test(connect_timeout): |
| 81 |
""" Test ping module, if available """ |
| 82 |
display.vvv("wait_for_connection: attempting ping module test") |
| 83 |
# re-run interpreter discovery if we ran it in the first iteration |
| 84 |
if self._discovered_interpreter_key: |
| 85 |
task_vars['ansible_facts'].pop(self._discovered_interpreter_key, None) |
| 86 |
# call connection reset between runs if it's there |
| 87 |
try: |
| 88 |
self._connection.reset() |
| 89 |
except AttributeError: |
| 90 |
pass |
| 91 |
|
| 92 |
ping_result = self._execute_module(module_name='ansible.legacy.ping', module_args=dict(), task_vars=task_vars) |
| 93 |
|
| 94 |
# Test module output |
| 95 |
if ping_result['ping'] != 'pong': |
| 96 |
raise Exception('ping test failed') |
| 97 |
|
| 98 |
start = datetime.now() |
| 99 |
|
| 100 |
if delay: |
| 101 |
time.sleep(delay) |
| 102 |
|
| 103 |
try: |
| 104 |
# If the connection has a transport_test method, use it first |
| 105 |
if hasattr(self._connection, 'transport_test'): |
| 106 |
self.do_until_success_or_timeout(self._connection.transport_test, timeout, connect_timeout, what_desc="connection port up", sleep=sleep) |
| 107 |
|
| 108 |
# Use the ping module test to determine end-to-end connectivity |
| 109 |
self.do_until_success_or_timeout(ping_module_test, timeout, connect_timeout, what_desc="ping module test", sleep=sleep) |
| 110 |
|
| 111 |
except TimedOutException as e: |
| 112 |
result['failed'] = True |
| 113 |
result['msg'] = to_text(e) |
| 114 |
|
| 115 |
elapsed = datetime.now() - start |
| 116 |
result['elapsed'] = elapsed.seconds |
| 117 |
|
| 118 |
# remove a temporary path we created |
| 119 |
self._remove_tmp_path(self._connection._shell.tmpdir) |
| 120 |
|
| 121 |
return result |