Skip to content

Commit

Permalink
Salt backend: Implement glob or compound matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
philpep committed Nov 30, 2015
1 parent e174df3 commit 5e995a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions doc/source/backends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ salt
The salt backend use the `salt python client API
<http://docs.saltstack.com/en/latest/ref/clients/>`_ and can be used from the salt-master server::

$ testinfra --connection=salt # equivalent to --hosts='*'
$ testinfra --connection=salt --hosts=minion1,minion2
$ testinfra --connection=salt --hosts='web*'
$ testinfra --connection=salt --hosts=G@os:Debian

Testinfra will use the salt connection channel to run commands.

Host can be seleted using `glob` or `compound matchers
<https://docs.saltstack.com/en/latest/topics/targeting/compound.html>`_.


.. _ansible connection backend:

Expand Down
29 changes: 26 additions & 3 deletions testinfra/backend/salt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ def __init__(self, host, *args, **kwargs):
self._client = None
super(SaltBackend, self).__init__(self.host, *args, **kwargs)

@staticmethod
def _check_salt():
if not HAS_SALT:
raise RuntimeError(
"You must install salt package to use the salt backend")

@property
def client(self):
if self._client is None:
if not HAS_SALT:
raise RuntimeError(
"You must install salt package to use the salt backend")
self._check_salt()
self._client = salt.client.LocalClient()
return self._client

Expand All @@ -57,3 +61,22 @@ def run_salt(self, func, args=None):
"Error while running %s(%s): %s. Minion not connected ?" % (
func, args, out))
return out[self.host]

@classmethod
def get_hosts(cls, host, **kwargs):
if host is None:
host = "*"
if any([c in host for c in "@*[?"]):
cls._check_salt()
client = salt.client.LocalClient()
if "@" in host:
hosts = client.cmd(
host, "test.true", expr_form="compound").keys()
else:
hosts = client.cmd(host, "test.true").keys()
if not hosts:
raise RuntimeError("No host matching '%s'" % (host,))
else:
return hosts
else:
return super(SaltBackend, cls).get_hosts(host, **kwargs)

0 comments on commit 5e995a5

Please sign in to comment.