Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.29 KB | None | 0 0
  1. #!/usr/bin/python
  2. # (c) 2016, Matt Davis <mdavis@ansible.com>
  3. #
  4. # This file is part of Ansible
  5. #
  6. # Ansible is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Ansible is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. # CI-required python3 boilerplate
  20. from __future__ import (absolute_import, division, print_function)
  21. __metaclass__ = type
  22.  
  23. from ansible.plugins.action import ActionBase
  24. from ansible.utils.boolean import boolean
  25. from ansible.utils.unicode import to_unicode
  26. from ansible.errors import AnsibleUndefinedVariable
  27.  
  28. import socket
  29. import time
  30. import traceback
  31.  
  32. from datetime import datetime, timedelta
  33.  
  34. try:
  35.     from __main__ import display
  36. except ImportError:
  37.     from ansible.utils.display import Display
  38.     display = Display()
  39.  
  40. class TimedOutException(Exception):
  41.     pass
  42.  
  43. class ActionModule(ActionBase):
  44.     TRANSFERS_FILES = False
  45.  
  46.     DEFAULT_INSTALL_TIMEOUT_SEC = 60
  47.     DEFAULT_RECONNECT_TIMEOUT_SEC = 60
  48.     DEFAULT_CONNECT_TIMEOUT_SEC = 10
  49.     DEFAULT_TEST_COMMAND = 'Get-NetAdapter | Select-Object DriverVersionString'
  50.  
  51.     def do_until_success_or_timeout(self, what, timeout_sec, what_desc, fail_sleep_sec=1):
  52.         max_end_time = datetime.utcnow() + timedelta(seconds=timeout_sec)
  53.  
  54.         while datetime.utcnow() < max_end_time:
  55.             try:
  56.                 what()
  57.                 if what_desc:
  58.                     display.debug("win_reconnect: %s success" % what_desc)
  59.                 return
  60.             except:
  61.                 if what_desc:
  62.                     display.debug("win_reconnect: %s fail (expected), sleeping before retry..." % what_desc)
  63.                 time.sleep(fail_sleep_sec)
  64.  
  65.         raise TimedOutException("timed out waiting for %s" % what_desc)
  66.  
  67.     def run(self, tmp=None, task_vars=None):
  68.         if task_vars is None:
  69.             task_vars = dict()
  70.  
  71.         install_timeout_sec = int(self._task.args.get('install_timeout_sec', self.DEFAULT_INSTALL_TIMEOUT_SEC))
  72.         reconnect_timeout_sec = int(self._task.args.get('reconnect_timeout_sec', self.DEFAULT_RECONNECT_TIMEOUT_SEC))
  73.         connect_timeout_sec = int(self._task.args.get('connect_timeout_sec', self.DEFAULT_CONNECT_TIMEOUT_SEC))
  74.         driver_file_path = str(self._task.args.get('driver_file_path'))
  75.         test_command = self._task.args.get('test_command', self.DEFAULT_TEST_COMMAND)
  76.  
  77.         if self._play_context.check_mode:
  78.             display.vvv("win_reconnect: skipping for check_mode")
  79.             return dict(skipped=True)
  80.  
  81.         winrm_host = self._connection._winrm_host
  82.         winrm_port = self._connection._winrm_port
  83.  
  84.         result = super(ActionModule, self).run(tmp, task_vars)
  85.  
  86.         # initiate reconnect
  87.         (rc, stdout, stderr) = self._connection.exec_command('pnputil.exe -i -a {fname}'.format(fname=driver_file_path))
  88.  
  89.         if rc != 0:
  90.             result['failed'] = True
  91.             result['reconnected'] = False
  92.             result['msg'] = "Install command failed, error text was %s" % stderr
  93.             return result
  94.  
  95.         def raise_if_port_open():
  96.             try:
  97.                 sock = socket.create_connection((winrm_host, winrm_port), connect_timeout_sec)
  98.                 sock.close()
  99.             except:
  100.                 return False
  101.  
  102.             raise Exception("port is open")
  103.  
  104.         try:
  105.             self.do_until_success_or_timeout(raise_if_port_open, install_timeout_sec, what_desc="winrm port down")
  106.  
  107.             def connect_winrm_port():
  108.                 sock = socket.create_connection((winrm_host, winrm_port), connect_timeout_sec)
  109.                 sock.close()
  110.  
  111.             self.do_until_success_or_timeout(connect_winrm_port, reconnect_timeout_sec, what_desc="winrm port up")
  112.  
  113.             def run_test_command():
  114.                 display.vvv("attempting post-reconnect test command '%s'" % test_command)
  115.                 # call connection reset between runs if it's there
  116.                 try:
  117.                     self._connection._reset()
  118.                 except AttributeError:
  119.                     pass
  120.  
  121.                 (rc, stdout, stderr) = self._connection.exec_command(test_command)
  122.  
  123.                 if rc != 0:
  124.                     raise Exception('test command failed')
  125.  
  126.             # FUTURE: ensure that a reboot has actually occurred by watching for change in last boot time fact
  127.             # FUTURE: add a stability check (system must remain up for N seconds) to deal with self-multi-reboot updates
  128.  
  129.             self.do_until_success_or_timeout(run_test_command, reconnect_timeout_sec, what_desc="post-reconnect test command success")
  130.  
  131.             result['reconnected'] = True
  132.             result['changed'] = True
  133.  
  134.         except TimedOutException as toex:
  135.             result['failed'] = True
  136.             result['reconnected'] = True
  137.             result['msg'] = toex.message
  138.  
  139.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement