prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  linux.wait_for_ssh action improvements
  Currently `linux.wait_for_ssh` action (https://github.com/StackStorm/st2/blob/master/contrib/linux/actions/wait_for_ssh.yaml) only supports authenticating with RSA SSH key.
  
  There are multiple ways we can improve this action and make it more generic.
  - Support for other key types
  - Support for password based authentication
  - Refactor the action to utilize the new Paramiko SSH client
  - Default to using system user (stanley) username and key file. Ideally key file location should be read from the config
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `contrib/linux/actions/wait_for_ssh.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 
  3 import time
  4 
  5 import paramiko
  6 
  7 from st2actions.runners.pythonrunner import Action
  8 
  9 
  10 class BaseAction(Action):
  11     def run(self, keyfile, username, hostname, ssh_timeout, retries):
  12         key = paramiko.RSAKey.from_private_key_file(keyfile)
  13         client = paramiko.SSHClient()
  14         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  15 
  16         for index in range(retries):
  17             attempt = index + 1
  18 
  19             try:
  20                 self.logger.debug('SSH connection attempt: %s' % (attempt))
  21                 client.connect(hostname=hostname, username=username, pkey=key)
  22                 return True
  23             except Exception as e:
  24                 self.logger.info('Attempt %s failed (%s), sleeping...' % (attempt, str(e)))
  25                 time.sleep(ssh_timeout)
  26 
  27         raise Exception('Exceeded max retries (%s)' % (retries))
  28
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/contrib/linux/actions/wait_for_ssh.py b/contrib/linux/actions/wait_for_ssh.py
  --- a/contrib/linux/actions/wait_for_ssh.py
  +++ b/contrib/linux/actions/wait_for_ssh.py
  @@ -2,26 +2,36 @@
   
   import time
   
  -import paramiko
  +from oslo_config import cfg
   
   from st2actions.runners.pythonrunner import Action
  +from st2actions.runners.ssh.paramiko_ssh import ParamikoSSHClient
   
   
   class BaseAction(Action):
  -    def run(self, keyfile, username, hostname, ssh_timeout, retries):
  -        key = paramiko.RSAKey.from_private_key_file(keyfile)
  -        client = paramiko.SSHClient()
  -        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  +    def run(self, hostname, port, username, password=None, keyfile=None, ssh_timeout=5,
  +            sleep_delay=20, retries=10):
  +        # Note: If neither password nor key file is provided, we try to use system user
  +        # key file
  +        if not password and not keyfile:
  +            keyfile = cfg.CONF.system_user.ssh_key_file
  +            self.logger.info('Neither "password" nor "keyfile" parameter provided, '
  +                             'defaulting to using "%s" key file' % (keyfile))
  +
  +        client = ParamikoSSHClient(hostname=hostname, port=port, username=username,
  +                                   password=password, key_files=keyfile,
  +                                   timeout=ssh_timeout)
   
           for index in range(retries):
               attempt = index + 1
   
               try:
                   self.logger.debug('SSH connection attempt: %s' % (attempt))
  -                client.connect(hostname=hostname, username=username, pkey=key)
  +                client.connect()
                   return True
               except Exception as e:
  -                self.logger.info('Attempt %s failed (%s), sleeping...' % (attempt, str(e)))
  -                time.sleep(ssh_timeout)
  +                self.logger.info('Attempt %s failed (%s), sleeping for %s seconds...' %
  +                                 (attempt, str(e), sleep_delay))
  +                time.sleep(sleep_delay)
   
           raise Exception('Exceeded max retries (%s)' % (retries))
  
verification_info: {"golden_diff": "diff --git a/contrib/linux/actions/wait_for_ssh.py b/contrib/linux/actions/wait_for_ssh.py\n--- a/contrib/linux/actions/wait_for_ssh.py\n+++ b/contrib/linux/actions/wait_for_ssh.py\n@@ -2,26 +2,36 @@\n \n import time\n \n-import paramiko\n+from oslo_config import cfg\n \n from st2actions.runners.pythonrunner import Action\n+from st2actions.runners.ssh.paramiko_ssh import ParamikoSSHClient\n \n \n class BaseAction(Action):\n-    def run(self, keyfile, username, hostname, ssh_timeout, retries):\n-        key = paramiko.RSAKey.from_private_key_file(keyfile)\n-        client = paramiko.SSHClient()\n-        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n+    def run(self, hostname, port, username, password=None, keyfile=None, ssh_timeout=5,\n+            sleep_delay=20, retries=10):\n+        # Note: If neither password nor key file is provided, we try to use system user\n+        # key file\n+        if not password and not keyfile:\n+            keyfile = cfg.CONF.system_user.ssh_key_file\n+            self.logger.info('Neither \"password\" nor \"keyfile\" parameter provided, '\n+                             'defaulting to using \"%s\" key file' % (keyfile))\n+\n+        client = ParamikoSSHClient(hostname=hostname, port=port, username=username,\n+                                   password=password, key_files=keyfile,\n+                                   timeout=ssh_timeout)\n \n         for index in range(retries):\n             attempt = index + 1\n \n             try:\n                 self.logger.debug('SSH connection attempt: %s' % (attempt))\n-                client.connect(hostname=hostname, username=username, pkey=key)\n+                client.connect()\n                 return True\n             except Exception as e:\n-                self.logger.info('Attempt %s failed (%s), sleeping...' % (attempt, str(e)))\n-                time.sleep(ssh_timeout)\n+                self.logger.info('Attempt %s failed (%s), sleeping for %s seconds...' %\n+                                 (attempt, str(e), sleep_delay))\n+                time.sleep(sleep_delay)\n \n         raise Exception('Exceeded max retries (%s)' % (retries))\n", "issue": "linux.wait_for_ssh action improvements\nCurrently `linux.wait_for_ssh` action (https://github.com/StackStorm/st2/blob/master/contrib/linux/actions/wait_for_ssh.yaml) only supports authenticating with RSA SSH key.\n\nThere are multiple ways we can improve this action and make it more generic.\n- Support for other key types\n- Support for password based authentication\n- Refactor the action to utilize the new Paramiko SSH client\n- Default to using system user (stanley) username and key file. Ideally key file location should be read from the config\n\n", "before_files": [{"content": "#!/usr/bin/env python\n\nimport time\n\nimport paramiko\n\nfrom st2actions.runners.pythonrunner import Action\n\n\nclass BaseAction(Action):\n    def run(self, keyfile, username, hostname, ssh_timeout, retries):\n        key = paramiko.RSAKey.from_private_key_file(keyfile)\n        client = paramiko.SSHClient()\n        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\n        for index in range(retries):\n            attempt = index + 1\n\n            try:\n                self.logger.debug('SSH connection attempt: %s' % (attempt))\n                client.connect(hostname=hostname, username=username, pkey=key)\n                return True\n            except Exception as e:\n                self.logger.info('Attempt %s failed (%s), sleeping...' % (attempt, str(e)))\n                time.sleep(ssh_timeout)\n\n        raise Exception('Exceeded max retries (%s)' % (retries))\n", "path": "contrib/linux/actions/wait_for_ssh.py"}], "after_files": [{"content": "#!/usr/bin/env python\n\nimport time\n\nfrom oslo_config import cfg\n\nfrom st2actions.runners.pythonrunner import Action\nfrom st2actions.runners.ssh.paramiko_ssh import ParamikoSSHClient\n\n\nclass BaseAction(Action):\n    def run(self, hostname, port, username, password=None, keyfile=None, ssh_timeout=5,\n            sleep_delay=20, retries=10):\n        # Note: If neither password nor key file is provided, we try to use system user\n        # key file\n        if not password and not keyfile:\n            keyfile = cfg.CONF.system_user.ssh_key_file\n            self.logger.info('Neither \"password\" nor \"keyfile\" parameter provided, '\n                             'defaulting to using \"%s\" key file' % (keyfile))\n\n        client = ParamikoSSHClient(hostname=hostname, port=port, username=username,\n                                   password=password, key_files=keyfile,\n                                   timeout=ssh_timeout)\n\n        for index in range(retries):\n            attempt = index + 1\n\n            try:\n                self.logger.debug('SSH connection attempt: %s' % (attempt))\n                client.connect()\n                return True\n            except Exception as e:\n                self.logger.info('Attempt %s failed (%s), sleeping for %s seconds...' %\n                                 (attempt, str(e), sleep_delay))\n                time.sleep(sleep_delay)\n\n        raise Exception('Exceeded max retries (%s)' % (retries))\n", "path": "contrib/linux/actions/wait_for_ssh.py"}]}