| 1 |
# Copyright: (c) 2018, Matt Davis <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from datetime import datetime, timezone |
| 7 |
|
| 8 |
from ansible.module_utils.common.text.converters import to_native |
| 9 |
from ansible.plugins.action import ActionBase |
| 10 |
from ansible.plugins.action.reboot import ActionModule as RebootActionModule |
| 11 |
from ansible.utils.display import Display |
| 12 |
|
| 13 |
display = Display() |
| 14 |
|
| 15 |
|
| 16 |
class TimedOutException(Exception): |
| 17 |
pass |
| 18 |
|
| 19 |
|
| 20 |
class ActionModule(RebootActionModule, ActionBase): |
| 21 |
TRANSFERS_FILES = False |
| 22 |
_VALID_ARGS = frozenset(( |
| 23 |
'connect_timeout', 'connect_timeout_sec', 'msg', 'post_reboot_delay', 'post_reboot_delay_sec', 'pre_reboot_delay', 'pre_reboot_delay_sec', |
| 24 |
'reboot_timeout', 'reboot_timeout_sec', 'shutdown_timeout', 'shutdown_timeout_sec', 'test_command', |
| 25 |
)) |
| 26 |
|
| 27 |
DEFAULT_BOOT_TIME_COMMAND = "(Get-WmiObject -ClassName Win32_OperatingSystem).LastBootUpTime" |
| 28 |
DEFAULT_CONNECT_TIMEOUT = 5 |
| 29 |
DEFAULT_PRE_REBOOT_DELAY = 2 |
| 30 |
DEFAULT_SUDOABLE = False |
| 31 |
DEFAULT_SHUTDOWN_COMMAND_ARGS = '/r /t {delay_sec} /c "{message}"' |
| 32 |
|
| 33 |
DEPRECATED_ARGS = { |
| 34 |
'shutdown_timeout': '2.5', |
| 35 |
'shutdown_timeout_sec': '2.5', |
| 36 |
} |
| 37 |
|
| 38 |
def __init__(self, *args, **kwargs): |
| 39 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 40 |
|
| 41 |
def get_distribution(self, task_vars): |
| 42 |
return {'name': 'windows', 'version': '', 'family': ''} |
| 43 |
|
| 44 |
def get_shutdown_command(self, task_vars, distribution): |
| 45 |
return self.DEFAULT_SHUTDOWN_COMMAND |
| 46 |
|
| 47 |
def run_test_command(self, distribution, **kwargs): |
| 48 |
# Need to wrap the test_command in our PowerShell encoded wrapper. This is done to align the command input to a |
| 49 |
# common shell and to allow the psrp connection plugin to report the correct exit code without manually setting |
| 50 |
# $LASTEXITCODE for just that plugin. |
| 51 |
test_command = self._task.args.get('test_command', self.DEFAULT_TEST_COMMAND) |
| 52 |
kwargs['test_command'] = self._connection._shell._encode_script(test_command) |
| 53 |
super(ActionModule, self).run_test_command(distribution, **kwargs) |
| 54 |
|
| 55 |
def perform_reboot(self, task_vars, distribution): |
| 56 |
shutdown_command = self.get_shutdown_command(task_vars, distribution) |
| 57 |
shutdown_command_args = self.get_shutdown_command_args(distribution) |
| 58 |
reboot_command = self._connection._shell._encode_script('{0} {1}'.format(shutdown_command, shutdown_command_args)) |
| 59 |
|
| 60 |
display.vvv("{action}: rebooting server...".format(action=self._task.action)) |
| 61 |
display.debug("{action}: distribution: {dist}".format(action=self._task.action, dist=distribution)) |
| 62 |
display.debug("{action}: rebooting server with command '{command}'".format(action=self._task.action, command=reboot_command)) |
| 63 |
|
| 64 |
result = {} |
| 65 |
reboot_result = self._low_level_execute_command(reboot_command, sudoable=self.DEFAULT_SUDOABLE) |
| 66 |
result['start'] = datetime.now(timezone.utc) |
| 67 |
|
| 68 |
# Test for "A system shutdown has already been scheduled. (1190)" and handle it gracefully |
| 69 |
stdout = reboot_result['stdout'] |
| 70 |
stderr = reboot_result['stderr'] |
| 71 |
if reboot_result['rc'] == 1190 or (reboot_result['rc'] != 0 and "(1190)" in reboot_result['stderr']): |
| 72 |
display.warning('A scheduled reboot was preempted by Ansible.') |
| 73 |
|
| 74 |
# Try to abort (this may fail if it was already aborted) |
| 75 |
result1 = self._low_level_execute_command(self._connection._shell._encode_script('shutdown /a'), |
| 76 |
sudoable=self.DEFAULT_SUDOABLE) |
| 77 |
|
| 78 |
# Initiate reboot again |
| 79 |
result2 = self._low_level_execute_command(reboot_command, sudoable=self.DEFAULT_SUDOABLE) |
| 80 |
|
| 81 |
reboot_result['rc'] = result2['rc'] |
| 82 |
stdout += result1['stdout'] + result2['stdout'] |
| 83 |
stderr += result1['stderr'] + result2['stderr'] |
| 84 |
|
| 85 |
if reboot_result['rc'] != 0: |
| 86 |
result['failed'] = True |
| 87 |
result['rebooted'] = False |
| 88 |
result['msg'] = "Reboot command failed, error was: {stdout} {stderr}".format( |
| 89 |
stdout=to_native(stdout.strip()), |
| 90 |
stderr=to_native(stderr.strip())) |
| 91 |
return result |
| 92 |
|
| 93 |
result['failed'] = False |
| 94 |
return result |