Permalink
Browse files

Use a runner

  • Loading branch information...
1 parent 56793a0 commit 9933c5993d6c4a92b1ba89a1ae1854012eb8feb6 dhubbard committed Jul 23, 2013
Showing with 30 additions and 36 deletions.
  1. +1 −1 sshmap/__init__.py
  2. +5 −6 sshmap/runner.py
  3. +15 −21 sshmap/sshmap.py
  4. +9 −8 tests/test.py
View
@@ -16,4 +16,4 @@
import callback
import utility
import runner
-from sshmap import run, run_command, rpc
+from sshmap import run, run_command
View
@@ -29,19 +29,18 @@
fh.write(\"\"\"{input}\"\"\".decode('base64').decode('{compressor}'))
"""
-def get_runner(command, input, password='', sudo=False, compressor='bz2'):
- if sudo:
- script = script_sudo
- else:
- script = script_stdin
+def get_runner(command, input, password='', runner_script=None,
+ compressor='bz2'):
+ if not runner_script:
+ runner_script = script_stdin
if compressor not in ['bz2', 'zlilb']:
compressor = 'bz2'
base64_command = base64.b64encode(command.encode('bz2'))
base64_input = base64.b64encode(input.encode('bz2'))
- return script.format(
+ return runner_script.format(
command=base64_command,
input=base64_input,
compressor=compressor,
View
@@ -390,7 +390,8 @@ def run_command(host, command="uname -a", username=None, password=None,
skip = False
for el in result.err:
if check_prompt:
- if password in el or 'assword:' in el or '[sudo] password' in el:
+ if password in el or 'assword:' in el or \
+ '[sudo] password' in el:
skip = True
else:
check_prompt = False
@@ -415,28 +416,21 @@ def init_worker():
signal.signal(signal.SIGINT, signal.SIG_IGN)
-def rpc(method, host_range, *args, **kwargs):
+def run_with_runner(*args, **kwargs):
"""
- Perform a remote procedure call on a range of hosts via ssh.
-
- This requires the python function/method be in an actual source file. It
- will not work from the python shell.
- :param method: Python function to execute
- :param host_range: List of hosts to execute on
- :param args: Arguments
- :param kwargs: Keyword arguments
+ Run a command with a python runner script
"""
- print('Required Arguments:')
- print(method)
- print(host_range)
- print('Method Arguments')
- print(args)
- print(kwargs)
- run_script = runner.get_runner(command='/usr/bin/python', input=inspect.getsource(method))
- print('Remote run script')
- print(run_script)
- os.popen('/usr/bin/python', 'w').write(run_script)
- run(host_range=host_range, command='/usr/bin/python', script=run_script)
+ if 'runner' in kwargs.keys() and isinstance(
+ kwargs['runner'], type.FunctionType):
+ kwargs['script'] = runner.get_runner(
+ command=args[1],
+ input="",
+ password=kwargs['password'],
+ runner_script=kwargs['runner'],
+ compressor='bz2'
+ )
+ del kwargs['runner']
+ return run(*args, **kwargs)
def run(host_range, command, username=None, password=None, sudo=False,
View
@@ -18,11 +18,7 @@
import sshmap
import os
import unittest
-
-
-def test_rpc_method():
- import os
- return os.popen('uname -n')
+import runner
class TestSSH(unittest.TestCase):
@@ -56,17 +52,22 @@ def test_shell_script_sudo(self):
"""Run a ssh command to localhost and verify it works """
open('testscript.test', 'w').write('#!/bin/bash\nid\n')
result = os.popen(
- 'sshmap/sshmap localhost --runscript testscript.test --sudo --timeout 15'
+ 'sshmap/sshmap localhost --runscript testscript.test --sudo '
+ '--timeout 15'
).read().strip()
self.assert_(
'localhost: uid=0(root) gid=0(root) groups=0(root)' in result)
os.remove('testscript.test')
- def test_rpc_call(self):
+ def run_with_runner(self):
"""
Execute an rpc call without arguments via ssh
"""
- result = sshmap.rpc(test_rpc_method, ['localhost'])
+ result = sshmap.run_with_runner(
+ 'localhost',
+ 'uname -n',
+ runner=runner.script_stdin
+ )
self.assertEqual(result, os.popen('uname -n').read())

0 comments on commit 9933c59

Please sign in to comment.