Skip to main content

Execution helpers for simplified usage of subprocess and ssh.

Project description

exec-helpers
============

.. image:: https://travis-ci.org/penguinolog/exec-helpers.svg?branch=master
:target: https://travis-ci.org/penguinolog/exec-helpers
.. image:: https://img.shields.io/appveyor/ci/penguinolog/exec-helpers.svg
:alt: AppVeyor
:target: https://ci.appveyor.com/project/penguinolog/exec-helpers
.. image:: https://coveralls.io/repos/github/penguinolog/exec-helpers/badge.svg?branch=master
:target: https://coveralls.io/github/penguinolog/exec-helpers?branch=master
.. image:: https://img.shields.io/pypi/v/exec-helpers.svg
:target: https://pypi.python.org/pypi/exec-helpers
.. image:: https://img.shields.io/pypi/pyversions/exec-helpers.svg
:target: https://pypi.python.org/pypi/exec-helpers
.. image:: https://img.shields.io/pypi/status/exec-helpers.svg
:target: https://pypi.python.org/pypi/exec-helpers
.. image:: https://img.shields.io/github/license/penguinolog/exec-helpers.svg
:target: https://raw.githubusercontent.com/penguinolog/exec-helpers/master/LICENSE

Execution helpers for simplified usage of subprocess and ssh.
Why another subprocess wrapper and why no clear `paramiko`?

Historically `paramiko` offers good ssh client, but with specific limitations:
you can call command with timeout, but without receiving return code,
or call command and wait for return code, but without timeout processing.

In the most cases, we are need just simple SSH client with comfortable API for calls, calls via SSH proxy and checking return code/stderr.
This library offers this functionality with connection memorizing, deadlock free polling and friendly result objects (with inline decoding of YAML, JSON, binary or just strings).
In addition this library offers the same API for subprocess calls, but with specific limitation: no parallel calls (for protection from race conditions).

Pros:

* STDOUT and STDERR polling during command execution - no deadlocks.
* The same API for subprocess and ssh.
* Connection memorize.
* Free software: Apache license
* Open Source: https://github.com/penguinolog/exec-helpers
* PyPI packaged: https://pypi.python.org/pypi/exec-helpers
* Self-documented code: docstrings with types in comments
* Tested: see bages on top
* Support multiple Python versions:

::

Python 2.7
Python 3.4
Python 3.5
Python 3.6

This package includes:

* `SSHClient` - historically the first one helper, which used for SSH connections and requires memorization
due to impossibility of connection close prediction.
Several API calls for sFTP also presents.

* `SSHAuth` - class for credentials storage. `SSHClient` does not store credentials as-is, but uses `SSHAuth` for it.
Objects of this class can be copied between ssh connection objects, also it used for `execute_through_host`.

* `Subprocess` - `subprocess.Popen` wrapper with timeouts, polling and almost the same API, as `SSHClient`
(except specific flags, like `cwd` for subprocess and `get_tty` for ssh).

* `ExecResult` - class for execution results storage.
Contains exit code, stdout, stderr and getters for decoding as JSON, YAML, string, bytearray and brief strings (up to 7 lines).

* `ExitCodes` - enumerator for standard Linux exit codes. BASH return codes (broduced from signal codes) also available.

Usage
=====

SSHClient
---------

Basic initialization of `SSHClient` can be done without construction of specific objects:

.. code-block:: python

client = exec_helpers.SSHClient(host, username="username", password="password")

If ssh agent is running - keys will be collected by paramiko automatically, but if keys are in specific location
- it should be loaded manually and providen as iterable object of `paramiko.RSAKey`.

For advanced cases or re-use of credentials, `SSHAuth` object should be used.
It can be collected from connection object via property `auth`.

Creation from scratch:

.. code-block:: python

auth = exec_helpers.SSHAuth(
username='username', # type: typing.Optional[str]
password='password', # type: typing.Optional[str]
key=None, # type: typing.Optional[paramiko.RSAKey]
keys=None,
)

Key is a main connection key (always tried first) and keys are alternate keys.
If main key now correct for username - alternate keys tried, if correct key found - it became main.
If no working key - password is used and None is set as main key.

Subprocess
==========

No initialization required.

Base methods
------------
Main methods are `execute`, `check_call` and `check_stderr` for simple executing, executing and checking return code
and executing, checking return code and checking for empty stderr output.
This methods are almost the same for `SSHCleint` and `Subprocess`, except specific flags.

.. code-block:: python

result = helper.execute(
command, # type: str
verbose=False, # type: bool
timeout=None, # type: typing.Optional[int]
**kwargs
)


.. code-block:: python

result = helper.check_call(
command, # type: str
verbose=False, # type: bool
timeout=None, # type: typing.Optional[int]
error_info=None, # type: typing.Optional[str]
expected=None, # type: typing.Optional[typing.Iterable[int]]
raise_on_err=True, # type: bool
**kwargs
)

.. code-block:: python

result = helper.check_stderr(
command, # type: str
verbose=False, # type: bool
timeout=None, # type: typing.Optional[int]
error_info=None, # type: typing.Optional[str]
raise_on_err=True, # type: bool
)

The next command level uses lower level and kwargs are forwarded, so expected exit codes are forwarded from `check_stderr`.
Implementation specific flags are always set via kwargs.

ExecResult
----------

Execution result object has a set of useful properties:

* `cmd` - Command
* `exit_code` - Command return code. If possible to decode using enumerators for Linux -> it used.
* `stdout` -> `typing.Tuple[bytes]`. Raw stdout output.
* `stderr` -> `typing.Tuple[bytes]`. Raw stderr output.
* `stdout_bin` -> `bytearray`. Binary stdout output.
* `stderr_bin` -> `bytearray`. Binary stderr output.
* `stdout_str` -> `six.text_types`. Text representation of output.
* `stderr_str` -> `six.text_types`. Text representation of output.
* `stdout_brief` -> `six.text_types`. Up to 7 lines from stdout (3 first and 3 last if >7 lines).
* `stderr_brief` -> `six.text_types`. Up to 7 lines from stderr (3 first and 3 last if >7 lines).

* `stdout_json` - STDOUT decoded as JSON.

* `stdout_yaml` - STDOUT decoded as YAML

* `timestamp` -> `typing.Optional(datetime.datetime)`. Timestamp for received exit code.

Testing
=======
The main test mechanism for the package `exec-helpers` is using `tox`.
Test environments available:

::

pep8
py27
py34
py35
py36
pylint
pep257

CI systems
==========
For code checking several CI systems is used in parallel:

1. `Travis CI: <https://travis-ci.org/penguinolog/exec-helpers>`_ is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it's publishes coverage on coveralls.

2. `AppVeyor<https://ci.appveyor.com/project/penguinolog/exec-helpers>`_ is used for checking windows compatibility.

3. `coveralls: <https://coveralls.io/github/penguinolog/exec-helpers>`_ is used for coverage display.


Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exec-helpers-0.8.0.zip (366.8 kB view hashes)

Uploaded Source

Built Distributions

exec_helpers-0.8.0-cp36-cp36m-manylinux1_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.6m

exec_helpers-0.8.0-cp36-cp36m-manylinux1_i686.whl (953.7 kB view hashes)

Uploaded CPython 3.6m

exec_helpers-0.8.0-cp35-cp35m-manylinux1_x86_64.whl (974.3 kB view hashes)

Uploaded CPython 3.5m

exec_helpers-0.8.0-cp35-cp35m-manylinux1_i686.whl (917.5 kB view hashes)

Uploaded CPython 3.5m

exec_helpers-0.8.0-cp34-cp34m-manylinux1_x86_64.whl (997.3 kB view hashes)

Uploaded CPython 3.4m

exec_helpers-0.8.0-cp34-cp34m-manylinux1_i686.whl (937.3 kB view hashes)

Uploaded CPython 3.4m

exec_helpers-0.8.0-cp27-cp27mu-manylinux1_x86_64.whl (905.4 kB view hashes)

Uploaded CPython 2.7mu

exec_helpers-0.8.0-cp27-cp27mu-manylinux1_i686.whl (824.2 kB view hashes)

Uploaded CPython 2.7mu

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page