|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +XMLSERVICE ssh call (ssh job, often in QUSRWRK) |
| 4 | +
|
| 5 | +License: |
| 6 | + BSD (LICENSE) |
| 7 | + -- or -- |
| 8 | + http://yips.idevcloud.com/wiki/index.php/XMLService/LicenseXMLService |
| 9 | +
|
| 10 | +Example: |
| 11 | + from itoolkit.transport import SshTransport |
| 12 | + import paramiko |
| 13 | + ssh = paramiko.SSHClient() |
| 14 | + # configure paramiko. Using only WarningPolicy() is not secure |
| 15 | + ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) |
| 16 | + ssh.connect(host, username="linux", password="linux1") |
| 17 | + itransport = SshTransport(ssh) |
| 18 | +
|
| 19 | +Notes: |
| 20 | + 1) Always uses XMLSERVICE shipped in the QXMLSERV library |
| 21 | + 2) does not disconnect the given SSHClient object when finished |
| 22 | +
|
| 23 | +""" |
| 24 | +from .base import XmlServiceTransport |
| 25 | + |
| 26 | +__all__ = [ |
| 27 | + 'SshTransport' |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +class SshTransport(XmlServiceTransport): |
| 32 | + """ |
| 33 | + Transport XMLSERVICE calls over SSH connection. |
| 34 | +
|
| 35 | + Args: |
| 36 | + sshclient (paramiko.SSHClient): connected and authenticated connection |
| 37 | +
|
| 38 | + Example: |
| 39 | + from itoolkit.transport import SshTransport |
| 40 | + import paramiko |
| 41 | + ssh = paramiko.SSHClient() |
| 42 | + # configure paramiko. Using only WarningPolicy() is not secure |
| 43 | + ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) |
| 44 | + ssh.connect(host, username="linux", password="linux1") |
| 45 | + itransport = SshTransport(ssh) |
| 46 | +
|
| 47 | + Returns: |
| 48 | + (obj) |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, sshclient=None): |
| 52 | + # TODO: allow connection to be materialized from IBM Cloud deployments |
| 53 | + if not hasattr(sshclient, "exec_command"): |
| 54 | + raise Exception("An instance of paramiko.SSHClient is required") |
| 55 | + self.conn = sshclient |
| 56 | + |
| 57 | + def call(self, tk): |
| 58 | + """Call xmlservice with accumulated input XML. |
| 59 | +
|
| 60 | + Args: |
| 61 | + tk - iToolkit object |
| 62 | +
|
| 63 | + Returns: |
| 64 | + xml |
| 65 | + """ |
| 66 | + command = "/QOpenSys/pkgs/bin/xmlservice-cli" |
| 67 | + stdin, stdout, stderr = self.conn.exec_command(command) |
| 68 | + xml_in = tk.xml_in() |
| 69 | + stdin.write(xml_in.encode()) |
| 70 | + stdin.flush() |
| 71 | + stdin.channel.shutdown_write() |
| 72 | + |
| 73 | + # rather than doing all this loop-de-loop, we could instead just use |
| 74 | + # a single call to stdout.readlines(), but there is a remote |
| 75 | + # possibility that the process is hanging writing to a filled-up |
| 76 | + # stderr pipe. So, we read a little from both until we're all done |
| 77 | + err_out = b"" |
| 78 | + xml_out = b"" |
| 79 | + blockSize = 64 # arbitrary |
| 80 | + while not stderr.closed or not stdout.closed: |
| 81 | + if not stderr.closed: |
| 82 | + newData = stderr.read(blockSize) |
| 83 | + if not newData: |
| 84 | + stderr.close() # reaching EOF doesn't implicitly close |
| 85 | + else: |
| 86 | + err_out += newData |
| 87 | + if not stdout.closed: |
| 88 | + newData = stdout.read(blockSize) |
| 89 | + if not newData: |
| 90 | + stdout.close() # reaching EOF doesn't implicitly close |
| 91 | + else: |
| 92 | + xml_out += newData |
| 93 | + stdout.channel.close() |
| 94 | + stderr.channel.close() |
| 95 | + return xml_out |
0 commit comments