summaryrefslogtreecommitdiff
path: root/test/support/windows-integration/collections/ansible_collections/ansible/windows/plugins/plugin_utils/_reboot.py
blob: 2399ee48ca32bb10294c3ce7d7703cb0c1905738 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# Copyright: (c) 2021, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

"""Reboot action for Windows hosts

This contains the code to reboot a Windows host for use by other action plugins
in this collection. Right now it should only be used in this collection as the
interface is not final and count be subject to change.
"""

# FOR INTERNAL COLLECTION USE ONLY
# The interfaces in this file are meant for use within the ansible.windows collection
# and may not remain stable to outside uses. Changes may be made in ANY release, even a bugfix release.
# See also: https://github.com/ansible/community/issues/539#issuecomment-780839686
# Please open an issue if you have questions about this.

import datetime
import json
import random
import time
import traceback
import uuid
import typing as t

from ansible.errors import AnsibleConnectionFailure, AnsibleError
from ansible.module_utils.common.text.converters import to_text
from ansible.plugins.connection import ConnectionBase
from ansible.utils.display import Display

from ansible_collections.ansible.windows.plugins.plugin_utils._quote import quote_pwsh


# This is not ideal but the psrp connection plugin doesn't catch all these exceptions as an AnsibleConnectionFailure.
# Until we can guarantee we are using a version of psrp that handles all this we try to handle those issues.
try:
    from requests.exceptions import (
        RequestException,
    )
except ImportError:
    RequestException = AnsibleConnectionFailure


_LOGON_UI_KEY = (
    r"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoLogonChecked"
)

_DEFAULT_BOOT_TIME_COMMAND = (
    "(Get-CimInstance -ClassName Win32_OperatingSystem -Property LastBootUpTime)"
    ".LastBootUpTime.ToFileTime()"
)

T = t.TypeVar("T")

display = Display()


class _ReturnResultException(Exception):
    """Used to sneak results back to the return dict from an exception"""

    def __init__(self, msg, **result):
        super().__init__(msg)
        self.result = result


class _TestCommandFailure(Exception):
    """Differentiates between a connection failure and just a command assertion failure during the reboot loop"""


def reboot_host(
    task_action: str,
    connection: ConnectionBase,
    boot_time_command: str = _DEFAULT_BOOT_TIME_COMMAND,
    connect_timeout: int = 5,
    msg: str = "Reboot initiated by Ansible",
    post_reboot_delay: int = 0,
    pre_reboot_delay: int = 2,
    reboot_timeout: int = 600,
    test_command: t.Optional[str] = None,
) -> t.Dict[str, t.Any]:
    """Reboot a Windows Host.

    Used by action plugins in ansible.windows to reboot a Windows host. It
    takes in the connection plugin so it can run the commands on the targeted
    host and monitor the reboot process. The return dict will have the
    following keys set:

        changed: Whether a change occurred (reboot was done)
        elapsed: Seconds elapsed between the reboot and it coming back online
        failed: Whether a failure occurred
        unreachable: Whether it failed to connect to the host on the first cmd
        rebooted: Whether the host was rebooted

    When failed=True there may be more keys to give some information around
    the failure like msg, exception. There are other keys that might be
    returned as well but they are dependent on the failure that occurred.

    Verbosity levels used:
        2: Message when each reboot step is completed
        4: Connection plugin operations and their results
        5: Raw commands run and the results of those commands
        Debug: Everything, very verbose

    Args:
        task_action: The name of the action plugin that is running for logging.
        connection: The connection plugin to run the reboot commands on.
        boot_time_command: The command to run when getting the boot timeout.
        connect_timeout: Override the connection timeout of the connection
            plugin when polling the rebooted host.
        msg: The message to display to interactive users when rebooting the
            host.
        post_reboot_delay: Seconds to wait after sending the reboot command
            before checking to see if it has returned.
        pre_reboot_delay: Seconds to wait when sending the reboot command.
        reboot_timeout: Seconds to wait while polling for the host to come
            back online.
        test_command: Command to run when the host is back online and
            determines the machine is ready for management. When not defined
            the default command should wait until the reboot is complete and
            all pre-login configuration has completed.

    Returns:
        (Dict[str, Any]): The return result as a dictionary. Use the 'failed'
            key to determine if there was a failure or not.
    """
    result: t.Dict[str, t.Any] = {
        "changed": False,
        "elapsed": 0,
        "failed": False,
        "unreachable": False,
        "rebooted": False,
    }
    host_context = {"do_close_on_reset": True}

    # Get current boot time. A lot of tasks that require a reboot leave the WSMan stack in a bad place. Will try to
    # get the initial boot time 3 times before giving up.
    try:
        previous_boot_time = _do_until_success_or_retry_limit(
            task_action,
            connection,
            host_context,
            "pre-reboot boot time check",
            3,
            _get_system_boot_time,
            task_action,
            connection,
            boot_time_command,
        )

    except Exception as e:
        # Report a the failure based on the last exception received.
        if isinstance(e, _ReturnResultException):
            result.update(e.result)

        if isinstance(e, AnsibleConnectionFailure):
            result["unreachable"] = True
        else:
            result["failed"] = True

        result["msg"] = str(e)
        result["exception"] = traceback.format_exc()
        return result

    # Get the original connection_timeout option var so it can be reset after
    original_connection_timeout: t.Optional[float] = None
    try:
        original_connection_timeout = connection.get_option("connection_timeout")
        display.vvvv(
            f"{task_action}: saving original connection_timeout of {original_connection_timeout}"
        )
    except KeyError:
        display.vvvv(
            f"{task_action}: connection_timeout connection option has not been set"
        )

    # Initiate reboot
    # This command may be wrapped in other shells or command making it hard to detect what shutdown.exe actually
    # returned. We use this hackery to return a json that contains the stdout/stderr/rc as a structured object for our
    # code to parse and detect if something went wrong.
    reboot_command = """$ErrorActionPreference = 'Continue'

if ($%s) {
    Remove-Item -LiteralPath '%s' -Force -ErrorAction SilentlyContinue
}

$stdout = $null
$stderr = . { shutdown.exe /r /t %s /c %s | Set-Variable stdout } 2>&1 | ForEach-Object ToString

ConvertTo-Json -Compress -InputObject @{
    stdout = (@($stdout) -join "`n")
    stderr = (@($stderr) -join "`n")
    rc = $LASTEXITCODE
}
""" % (
        str(not test_command),
        _LOGON_UI_KEY,
        int(pre_reboot_delay),
        quote_pwsh(msg),
    )

    expected_test_result = (
        None  # We cannot have an expected result if the command is user defined
    )
    if not test_command:
        # It turns out that LogonUI will create this registry key if it does not exist when it's about to show the
        # logon prompt. Normally this is a volatile key but if someone has explicitly created it that might no longer
        # be the case. We ensure it is not present on a reboot so we can wait until LogonUI creates it to determine
        # the host is actually online and ready, e.g. no configurations/updates still to be applied.
        # We echo a known successful statement to catch issues with powershell failing to start but the rc mysteriously
        # being 0 causing it to consider a successful reboot too early (seen on ssh connections).
        expected_test_result = f"success-{uuid.uuid4()}"
        test_command = f"Get-Item -LiteralPath '{_LOGON_UI_KEY}' -ErrorAction Stop; '{expected_test_result}'"

    start = None
    try:
        _perform_reboot(task_action, connection, reboot_command)

        start = datetime.datetime.utcnow()
        result["changed"] = True
        result["rebooted"] = True

        if post_reboot_delay != 0:
            display.vv(
                f"{task_action}: waiting an additional {post_reboot_delay} seconds"
            )
            time.sleep(post_reboot_delay)

        # Keep on trying to run the last boot time check until it is successful or the timeout is raised
        display.vv(f"{task_action} validating reboot")
        _do_until_success_or_timeout(
            task_action,
            connection,
            host_context,
            "last boot time check",
            reboot_timeout,
            _check_boot_time,
            task_action,
            connection,
            host_context,
            previous_boot_time,
            boot_time_command,
            connect_timeout,
        )

        # Reset the connection plugin connection timeout back to the original
        if original_connection_timeout is not None:
            _set_connection_timeout(
                task_action,
                connection,
                host_context,
                original_connection_timeout,
            )

        # Run test command until ti is successful or a timeout occurs
        display.vv(f"{task_action} running post reboot test command")
        _do_until_success_or_timeout(
            task_action,
            connection,
            host_context,
            "post-reboot test command",
            reboot_timeout,
            _run_test_command,
            task_action,
            connection,
            test_command,
            expected=expected_test_result,
        )

        display.vv(f"{task_action}: system successfully rebooted")

    except Exception as e:
        if isinstance(e, _ReturnResultException):
            result.update(e.result)

        result["failed"] = True
        result["msg"] = str(e)
        result["exception"] = traceback.format_exc()

    if start:
        elapsed = datetime.datetime.utcnow() - start
        result["elapsed"] = elapsed.seconds

    return result


def _check_boot_time(
    task_action: str,
    connection: ConnectionBase,
    host_context: t.Dict[str, t.Any],
    previous_boot_time: int,
    boot_time_command: str,
    timeout: int,
):
    """Checks the system boot time has been changed or not"""
    display.vvvv("%s: attempting to get system boot time" % task_action)

    # override connection timeout from defaults to custom value
    if timeout:
        _set_connection_timeout(task_action, connection, host_context, timeout)

    # try and get boot time
    current_boot_time = _get_system_boot_time(
        task_action, connection, boot_time_command
    )
    if current_boot_time == previous_boot_time:
        raise _TestCommandFailure("boot time has not changed")


def _do_until_success_or_retry_limit(
    task_action: str,
    connection: ConnectionBase,
    host_context: t.Dict[str, t.Any],
    action_desc: str,
    retries: int,
    func: t.Callable[..., T],
    *args: t.Any,
    **kwargs: t.Any,
) -> t.Optional[T]:
    """Runs the function multiple times ignoring errors until the retry limit is hit"""

    def wait_condition(idx):
        return idx < retries

    return _do_until_success_or_condition(
        task_action,
        connection,
        host_context,
        action_desc,
        wait_condition,
        func,
        *args,
        **kwargs,
    )


def _do_until_success_or_timeout(
    task_action: str,
    connection: ConnectionBase,
    host_context: t.Dict[str, t.Any],
    action_desc: str,
    timeout: float,
    func: t.Callable[..., T],
    *args: t.Any,
    **kwargs: t.Any,
) -> t.Optional[T]:
    """Runs the function multiple times ignoring errors until a timeout occurs"""
    max_end_time = datetime.datetime.utcnow() + datetime.timedelta(seconds=timeout)

    def wait_condition(idx):
        return datetime.datetime.utcnow() < max_end_time

    try:
        return _do_until_success_or_condition(
            task_action,
            connection,
            host_context,
            action_desc,
            wait_condition,
            func,
            *args,
            **kwargs,
        )
    except Exception:
        raise Exception(
            "Timed out waiting for %s (timeout=%s)" % (action_desc, timeout)
        )


def _do_until_success_or_condition(
    task_action: str,
    connection: ConnectionBase,
    host_context: t.Dict[str, t.Any],
    action_desc: str,
    condition: t.Callable[[int], bool],
    func: t.Callable[..., T],
    *args: t.Any,
    **kwargs: t.Any,
) -> t.Optional[T]:
    """Runs the function multiple times ignoring errors until the condition is false"""
    fail_count = 0
    max_fail_sleep = 12
    reset_required = False
    last_error = None

    while fail_count == 0 or condition(fail_count):
        try:
            if reset_required:
                # Keep on trying the reset until it succeeds.
                _reset_connection(task_action, connection, host_context)
                reset_required = False

            else:
                res = func(*args, **kwargs)
                display.vvvvv("%s: %s success" % (task_action, action_desc))

                return res

        except Exception as e:
            last_error = e

            if not isinstance(e, _TestCommandFailure):
                # The error may be due to a connection problem, just reset the connection just in case
                reset_required = True

            # Use exponential backoff with a max timeout, plus a little bit of randomness
            random_int = random.randint(0, 1000) / 1000
            fail_sleep = 2**fail_count + random_int
            if fail_sleep > max_fail_sleep:
                fail_sleep = max_fail_sleep + random_int

            try:
                error = str(e).splitlines()[-1]
            except IndexError:
                error = str(e)

            display.vvvvv(
                "{action}: {desc} fail {e_type} '{err}', retrying in {sleep:.4} seconds...\n{tcb}".format(
                    action=task_action,
                    desc=action_desc,
                    e_type=type(e).__name__,
                    err=error,
                    sleep=fail_sleep,
                    tcb=traceback.format_exc(),
                )
            )

            fail_count += 1
            time.sleep(fail_sleep)

    if last_error:
        raise last_error

    return None


def _execute_command(
    task_action: str,
    connection: ConnectionBase,
    command: str,
) -> t.Tuple[int, str, str]:
    """Runs a command on the Windows host and returned the result"""
    display.vvvvv(f"{task_action}: running command: {command}")

    # Need to wrap the command in our PowerShell encoded wrapper. This is done to align the command input to a
    # common shell and to allow the psrp connection plugin to report the correct exit code without manually setting
    # $LASTEXITCODE for just that plugin.
    command = connection._shell._encode_script(command)

    try:
        rc, stdout, stderr = connection.exec_command(
            command, in_data=None, sudoable=False
        )
    except RequestException as e:
        # The psrp connection plugin should be doing this but until we can guarantee it does we just convert it here
        # to ensure AnsibleConnectionFailure refers to actual connection errors.
        raise AnsibleConnectionFailure(f"Failed to connect to the host: {e}")

    rc = rc or 0
    stdout = to_text(stdout, errors="surrogate_or_strict").strip()
    stderr = to_text(stderr, errors="surrogate_or_strict").strip()

    display.vvvvv(
        f"{task_action}: command result - rc: {rc}, stdout: {stdout}, stderr: {stderr}"
    )

    return rc, stdout, stderr


def _get_system_boot_time(
    task_action: str,
    connection: ConnectionBase,
    boot_time_command: str,
) -> str:
    """Gets a unique identifier to represent the boot time of the Windows host"""
    display.vvvv(f"{task_action}: getting boot time")
    rc, stdout, stderr = _execute_command(task_action, connection, boot_time_command)

    if rc != 0:
        msg = f"{task_action}: failed to get host boot time info"
        raise _ReturnResultException(msg, rc=rc, stdout=stdout, stderr=stderr)

    display.vvvv(f"{task_action}: last boot time: {stdout}")
    return stdout


def _perform_reboot(
    task_action: str,
    connection: ConnectionBase,
    reboot_command: str,
    handle_abort: bool = True,
) -> None:
    """Runs the reboot command"""
    display.vv(f"{task_action}: rebooting server...")

    stdout = stderr = None
    try:
        rc, stdout, stderr = _execute_command(task_action, connection, reboot_command)

    except AnsibleConnectionFailure as e:
        # If the connection is closed too quickly due to the system being shutdown, carry on
        display.vvvv(f"{task_action}: AnsibleConnectionFailure caught and handled: {e}")
        rc = 0

    if stdout:
        try:
            reboot_result = json.loads(stdout)
        except getattr(json.decoder, "JSONDecodeError", ValueError):
            # While the reboot command should output json it may have failed for some other reason. We continue
            # reporting with that output instead
            pass
        else:
            stdout = reboot_result.get("stdout", stdout)
            stderr = reboot_result.get("stderr", stderr)
            rc = int(reboot_result.get("rc", rc))

    # Test for "A system shutdown has already been scheduled. (1190)" and handle it gracefully
    if handle_abort and (rc == 1190 or (rc != 0 and stderr and "(1190)" in stderr)):
        display.warning("A scheduled reboot was pre-empted by Ansible.")

        # Try to abort (this may fail if it was already aborted)
        rc, stdout, stderr = _execute_command(
            task_action, connection, "shutdown.exe /a"
        )
        display.vvvv(
            f"{task_action}: result from trying to abort existing shutdown - rc: {rc}, stdout: {stdout}, stderr: {stderr}"
        )

        return _perform_reboot(
            task_action, connection, reboot_command, handle_abort=False
        )

    if rc != 0:
        msg = f"{task_action}: Reboot command failed"
        raise _ReturnResultException(msg, rc=rc, stdout=stdout, stderr=stderr)


def _reset_connection(
    task_action: str,
    connection: ConnectionBase,
    host_context: t.Dict[str, t.Any],
    ignore_errors: bool = False,
) -> None:
    """Resets the connection handling any errors"""

    def _wrap_conn_err(func, *args, **kwargs):
        try:
            func(*args, **kwargs)

        except (AnsibleError, RequestException) as e:
            if ignore_errors:
                return False

            raise AnsibleError(e)

        return True

    # While reset() should probably better handle this some connection plugins don't clear the existing connection on
    # reset() leaving resources still in use on the target (WSMan shells). Instead we try to manually close the
    # connection then call reset. If it fails once we want to skip closing to avoid a perpetual loop and just hope
    # reset() brings us back into a good state. If it's successful we still want to try it again.
    if host_context["do_close_on_reset"]:
        display.vvvv(f"{task_action}: closing connection plugin")
        try:
            success = _wrap_conn_err(connection.close)

        except Exception:
            host_context["do_close_on_reset"] = False
            raise

        host_context["do_close_on_reset"] = success

    # For some connection plugins (ssh) reset actually does something more than close so we also class that
    display.vvvv(f"{task_action}: resetting connection plugin")
    try:
        _wrap_conn_err(connection.reset)

    except AttributeError:
        # Not all connection plugins have reset so we just ignore those, close should have done our job.
        pass


def _run_test_command(
    task_action: str,
    connection: ConnectionBase,
    command: str,
    expected: t.Optional[str] = None,
) -> None:
    """Runs the user specified test command until the host is able to run it properly"""
    display.vvvv(f"{task_action}: attempting post-reboot test command")

    rc, stdout, stderr = _execute_command(task_action, connection, command)

    if rc != 0:
        msg = f"{task_action}: Test command failed - rc: {rc}, stdout: {stdout}, stderr: {stderr}"
        raise _TestCommandFailure(msg)

    if expected and expected not in stdout:
        msg = f"{task_action}: Test command failed - '{expected}' was not in stdout: {stdout}"
        raise _TestCommandFailure(msg)


def _set_connection_timeout(
    task_action: str,
    connection: ConnectionBase,
    host_context: t.Dict[str, t.Any],
    timeout: float,
) -> None:
    """Sets the connection plugin connection_timeout option and resets the connection"""
    try:
        current_connection_timeout = connection.get_option("connection_timeout")
    except KeyError:
        # Not all connection plugins implement this, just ignore the setting if it doesn't work
        return

    if timeout == current_connection_timeout:
        return

    display.vvvv(f"{task_action}: setting connect_timeout {timeout}")
    connection.set_option("connection_timeout", timeout)

    _reset_connection(task_action, connection, host_context, ignore_errors=True)