Skip to content

Commit fa8c351

Browse files
author
Dan
committed
Added documentation pages. Updated readme
1 parent 1ca1357 commit fa8c351

File tree

6 files changed

+265
-10
lines changed

6 files changed

+265
-10
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Asynchronous parallel SSH client library.
66

77
Run SSH commands over many - hundreds/hundreds of thousands - number of servers asynchronously and with minimal system load on the client host.
88

9+
.. image:: https://img.shields.io/badge/License-LGPL%20v2-blue.svg
10+
:target: https://pypi.python.org/pypi/parallel-ssh
11+
:alt: License
912
.. image:: https://img.shields.io/pypi/v/parallel-ssh.svg
1013
:target: https://pypi.python.org/pypi/parallel-ssh
1114
:alt: Latest Version

doc/front_page.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Parallel-SSH Documentation
2+
===========================
3+
4+
.. image:: https://img.shields.io/badge/License-LGPL%20v2-blue.svg
5+
:target: https://pypi.python.org/pypi/parallel-ssh
6+
:alt: License
7+
.. image:: https://img.shields.io/pypi/v/parallel-ssh.svg
8+
:target: https://pypi.python.org/pypi/parallel-ssh
9+
:alt: Latest Version
10+
.. image:: https://travis-ci.org/ParallelSSH/parallel-ssh.svg?branch=master
11+
:target: https://travis-ci.org/ParallelSSH/parallel-ssh
12+
.. image:: https://coveralls.io/repos/ParallelSSH/parallel-ssh/badge.png?branch=master
13+
:target: https://coveralls.io/r/ParallelSSH/parallel-ssh?branch=master
14+
.. image:: https://readthedocs.org/projects/parallel-ssh/badge/?version=latest
15+
:target: http://parallel-ssh.readthedocs.org/en/latest/
16+
:alt: Latest documentation
17+
18+
19+
`Parallel-SSH` is a parallel SSH client library. It makes use of gevent to make asynchronous SSH connections for its client and is, to date, the only publicly available asynchronous SSH client library for Python, as well as the only asynchronous *parallel* SSH client library available for Python.
20+
21+
22+
***********
23+
User Guide
24+
***********
25+
26+
.. toctree::
27+
28+
introduction
29+
installation
30+
quickstart

doc/index.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@ Parallel-SSH's documentation
99
.. toctree::
1010
:hidden:
1111

12-
self
12+
front_page
1313

14-
.. toctree::
15-
16-
pssh_client
17-
ssh_client
18-
output
19-
agent
20-
exceptions
14+
In a nutshell::
2115

22-
Welcome to ParallelSSH's API documentation.
16+
client = ParallelSSHClient(['localhost'])
17+
output = client.run_command('whoami')
18+
for line in output['localhost'].stdout:
19+
print(line)
2320

24-
New users should start with :mod:`pssh.pssh_client.ParallelSSHClient` and in particular :mod:`pssh.pssh_client.ParallelSSHClient.run_command`.
21+
Output::
2522

23+
<your username here>
2624

2725
Indices and tables
2826
==================

doc/installation.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
*************
2+
Installation
3+
*************
4+
5+
Installation is handled by Python's standard `setuptools` library and `pip`.
6+
7+
Pip Install
8+
------------
9+
10+
::
11+
12+
pip install parallel-ssh
13+
14+
If `pip` is not available on your Python platform, `see this installation guide <http://docs.python-guide.org/en/latest/starting/installation/>`_.
15+
16+
Old Python Versions
17+
---------------------
18+
19+
`1.1.x` and above releases will not be guaranteed to be compatible with Python `2.6`.
20+
21+
If you are running a deprecated Python version such as `2.6` you may need to install an older version of `parallel-ssh` that is compatible with your Python platform.
22+
23+
For example, to install the `1.0.0` version, run the following.
24+
25+
::
26+
27+
pip install parallel-ssh==1.0.0
28+
29+
`1.0.0` is compatible with all Python versions over or equal to `2.6`, including all of the `3.x` series.
30+
31+
Older versions such as `0.70.x` are compatible with Python `2.5` but not the `3.x` series.
32+
33+
Source Code
34+
-------------
35+
36+
Parallel-SSH is hosted on GitHub and the repository can be cloned with the following::
37+
38+
git clone git@github.com:ParallelSSH/parallel-ssh.git
39+
40+
To install from source run::
41+
42+
python setup.py install
43+
44+
Or with `pip`'s development mode which will ensure local changes are made available::
45+
46+
pip install -e .
47+

doc/introduction.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*****************
2+
Design And Goals
3+
*****************
4+
5+
``Parallel-SSH``'s design goals and motivation are to provide a *library* for running *asynchronous* SSH commands in parallel with little to no load induced on the system by doing so with the intended usage being completely programmatic and non-interactive.
6+
7+
To meet these goals, API driven solutions are preferred first and foremost. This frees up the developer to drive the library via any method desired, be that environment variables, CI driven tasks, command line tools, existing OpenSSH or new configuration files, from within an application et al.
8+
9+
10+
Design Principles
11+
-------------------
12+
13+
Taking a cue from `PEP 20 <https://www.python.org/dev/peps/pep-0020/>`_, heavy emphasis is in the following areas.
14+
15+
* Readability
16+
* Explicit is better than implicit
17+
* Simple is better than complex
18+
* Beautiful is better than ugly
19+
20+
Contributions are asked to keep these in mind.

doc/quickstart.rst

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
***********
2+
Quickstart
3+
***********
4+
5+
First, make sure that `parallel-ssh` is `installed <installation>`_.
6+
7+
Run a command on hosts in parallel
8+
------------------------------------
9+
10+
The most basic usage of `parallel-ssh` is, unsurprisingly, to run a command on multiple hosts in parallel.
11+
12+
Examples here will be using `print` as a function, for which a future import is needed for Python `2.7`.
13+
14+
Make a list of the hosts to run on::
15+
16+
from __future__ import print_function
17+
18+
hosts = ['host1', 'host2', 'host3', 'host4']
19+
20+
Where `host1` to `host4` are valid host names. IP addresses may also be used.
21+
22+
Create a client for these hosts::
23+
24+
client = ParallelSSHClient(hosts)
25+
26+
The client object can, and should, be reused. Existing connections to hosts will remain alive as long as the client object is kept alive. Subsequent commands to the same host(s) will reuse their existing connection and benefit from much faster response times.
27+
28+
Now one or more commands can be run via the client::
29+
30+
output = client.run_command('whoami')
31+
32+
At this point the remote command has started executing in parallel.
33+
34+
Output is keyed by host and contains a host output object. From that, SSH output is available
35+
36+
Authentication
37+
----------------
38+
39+
By default `parallel-ssh` will use an available SSH agent's credentials to login to hosts via private key authentication.
40+
41+
User/Password authentication
42+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
44+
User/password authentication can be used by providing user name and password credentials::
45+
46+
client = ParallelSSHClient(hosts, user='my_user', password='my_pass')
47+
48+
Programmatic Private Key authentication
49+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
It is also possible to programmatically use a private key for authentication.
52+
53+
The helper function `pssh.utils.load_private_key` is provided to easily load all possible key types. It takes either a file path or a file-like object.
54+
55+
File path::
56+
57+
from pssh.utils import load_private_key
58+
pkey = load_private_key('my_pkey.pem')
59+
client = ParallelSSHClient(hosts, pkey=pkey)
60+
61+
File object::
62+
63+
from pssh.utils import load_private_key
64+
pkey = load_private_key(open('my_pkey.pem'))
65+
client = ParallelSSHClient(hosts, pkey=pkey)
66+
67+
68+
Standard Output
69+
----------------
70+
71+
Standard output, aka `stdout` for `host1`::
72+
73+
for line in output['host1']:
74+
print(line)
75+
76+
Output::
77+
78+
<your username here>
79+
80+
There is nothing special needed to ensure output is available.
81+
82+
Please note that retrieving all of a command's standard output by definition requires that the command has completed.
83+
84+
Iterating over the `stdout` attribute for any host will therefor *block* until that host's command has completed unless interrupted.
85+
86+
87+
All hosts iteration
88+
^^^^^^^^^^^^^^^^^^^^^
89+
90+
Of course, iterating over all hosts can also be done the same way::
91+
92+
for host, host_output in output.items():
93+
for line in host_output:
94+
print("Host %s: %s" % (host, line))
95+
96+
Exit codes
97+
-------------
98+
99+
Exit codes are available on the host output object.
100+
101+
First, ensure that all commands have finished and exit codes gathered by joining on the output object, then iterate over all hosts::
102+
103+
client.join(output)
104+
for host, host_output in output.items():
105+
print("Host %s exit code: %s" % (host, host_output.exit_code))
106+
107+
Host Logger
108+
------------
109+
110+
There is a built in host logger that can be enabled to log output from remote hosts. The helper function ``pssh.utils.enable_host_logger`` will enable host logging to standard output, for example ::
111+
112+
from pssh.utils import enable_host_logger
113+
enable_host_logger()
114+
client.join(client.run_command('uname'))
115+
116+
[localhost] Linux
117+
118+
Using standard input
119+
----------------------
120+
121+
Along with standard output and error, input is also available on the host output object. It can be used to send input to the remote host where required, for example password prompts or any other prompt requiring user input.
122+
123+
The `stdin` channel is a file-like object that can be written to::
124+
125+
output = client.run_command('read')
126+
stdin = output['localhost'].stdin
127+
stdin.write("writing to stdin\\n")
128+
stdin.flush()
129+
for line in output['localhost'].stdout:
130+
print(line)
131+
132+
Output::
133+
134+
writing to stdin
135+
136+
Errors and Exceptions
137+
-----------------------
138+
139+
By default, `parallel-ssh` will fail early on any errors connecting to hosts, whether that be connection errors such as DNS resolution failure or unreachable host, SSH authentication failures or any other errors.
140+
141+
Alternatively, the `stop_on_errors` flag is provided to tell the client to go ahead and attempt the command(s) anyway and return output for all hosts, including the exception on any hosts that failed::
142+
143+
output = client.run_command('whoami', stop_on_errors=False)
144+
145+
With this flag, the `exception` attribute will contain the exception on any failed hosts, or `None`::
146+
147+
client.join(output)
148+
for host, host_output in output.items():
149+
print("Host %s: exit code %s, exception %s" % (
150+
host, host_output.exit_code, host_output.exception))
151+
152+
Output::
153+
154+
host1: 0, None
155+
host2: None, AuthenticationException <..>
156+
157+
Possible exceptions can be found in :mod:`pssh.exceptions` module.

0 commit comments

Comments
 (0)