Source code for schneider

"""
A small example of schneider in combination with fabric to execute a command
on a remote host.  You need fabric for this.

.. code-block:: python

        import os
        import sys

        import schneider.agent
        import schneider.manager

        import fabric


        def main():
            # prepare an agent with a key; you will be asked for a passphrase
            # if the key is encrypted
            agent = schneider.agent.SSHAgent("my_agent")
            agent.create()
            agent.add_key("~/.ssh/keys/my_key")

            # create a manager instance and let is manage the agent
            manager = schneider.manager.Manager()
            manager.add_agent(agent)

            # activate the agent and connect to a server
            manager.activate_agent(agent.name)
            conn = fabric.Connection("my_server")
            result = conn.run("uname -a")
            conn.close()

            # cleanup
            manager.deactivate_agent(agent.name)
            manager.del_agent(agent.name)

            msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\\n{0.stdout}"
            print(msg.format(result))


        if __name__ == "__main__":
            main()

"""

__all__ = ["agent", "config", "manager"]


import functools

import schneider.agent
import schneider.config
import schneider.manager


[docs]class Agent: """Decorator class. The `Agent` decorator takes the name of an entry in your `~/.ssh/conf`. It looks for an assigned keyfile and creates a new agent and a manager that takes care of it. .. code-block:: python @Agent("prokyon") def ping(msg): print("pinging proykon with", msg) ping("hallo") ping("welt") """ def __init__(self, host): self.host = host self.manager = schneider.manager.Manager() agent = schneider.agent.SSHAgent(host) agent.create() # get config of host -> we need the keyfile path config = schneider.config.parse() if not self.host in config.get_hostnames(): raise schneider.config.HostNotFound(self.host) keyfile = schneider.config.get_keyfile(config, self.host) agent.add_key(keyfile) self.manager.add_agent(agent) def __call__(self, func): @functools.wraps(func) def wrapper(*args, **kwargs): #manager.activate(hostname) print("activating") ret = func(*args, **kwargs) #manager.deactivate(hostname) print("deactivating") return ret return wrapper