Permalink
Browse files

Add 'forward_agent' argument to 'run' function.

  • Loading branch information...
svetlyak40wt committed Jan 21, 2012
1 parent 328af8d commit 9db86f389d0e859c1ecf7be55dd98352522c61ab
Showing with 45 additions and 1 deletion.
  1. +45 −1 fabricant/__init__.py
View
@@ -2,6 +2,7 @@
from fabric.api import *
from cuisine import *
+from cuisine import run as _cuisine_run
def make_symlinks(configs_dir='configs'):
@@ -28,7 +29,6 @@ def make_symlinks(configs_dir='configs'):
def use_ssh_config(env):
import ssh
- import pdb;pdb.set_trace()
def hostinfo(host, config):
hive = config.lookup(host)
if 'hostname' in hive:
@@ -51,3 +51,47 @@ def hostinfo(host, config):
env.key_filename = [os.path.expanduser(key) for key in keys if key is not None]
env.hosts = [hostinfo(host, config) for host in env.hosts]
+
+def _run_with_agent(command, shell=True, pty=True, ssh_options='-A'):
+ """
+ Helper function.
+ Runs a command with SSH agent forwarding enabled.
+
+ Note:: Fabric (and paramiko) can't forward your SSH agent.~
+ This helper uses your system's ssh to do so.
+ """
+ env = fabric.api.env
+ real_command = command
+ if shell:
+ cwd = env.get('cwd', '')
+ if cwd:
+ cwd = 'cd %s && ' % _shell_escape(cwd)
+ real_command = '%s "%s"' % (env.shell,
+ _shell_escape(cwd + real_command))
+
+ if fabric.api.output.debug:
+ print("[%s] run: %s" % (env.host_string, real_command))
+ elif fabric.api.output.running:
+ print("[%s] run: %s" % (env.host_string, command))
+
+ return fabric.api.local("ssh {options} -p {env.port} {env.host} '{command}'".format(
+ env=env,
+ command=real_command,
+ options=ssh_options,
+ )
+ )
+
+
+def run(*args, **kwargs):
+ if kwargs.get('forward_agent', False):
+ new_kwargs = dict(item for item in kwargs.items() if item[0] in ('shell', 'pty'))
+ return _run_with_agent(*args, **new_kwargs)
+ return _cuisine_run(*args, **kwargs)
+
+run.__doc__ = _cuisine_run.__doc__ + """
+
+If there is 'forward_agent=True in the kwargs, then this command
+will run true 'ssh' client locally. This is because pythonic 'ssh'
+library still lack this feature.
+"""
+

0 comments on commit 9db86f3

Please sign in to comment.