Skip to content

Commit

Permalink
Merge pull request #61 from kushaldas/hostcommand
Browse files Browse the repository at this point in the history
Fixes #58 adds Hostcommand directive
  • Loading branch information
kushaldas committed Jul 21, 2017
2 parents 6b74c5e + ebb59f8 commit 0f924ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
11 changes: 11 additions & 0 deletions docs/usage.rst
Expand Up @@ -157,6 +157,17 @@ successful ssh connection. It polls after every 10 seconds, and timeout is
currently set for 300 seconds. One should this one instead of *SLEEP* directive
after a reboot.

HOSTCOMMAND directive
----------------------

.. versionadded:: 0.18

Now we have *HOSTCOMMAND* directive, which can be used to run any command in the
host system itself. One major usecase for this directive wil be for generating
ansible inventory file using a simple script (local). The tests will continue
even if this command fails to execute properly.




For Multi-VM configurations
Expand Down
15 changes: 9 additions & 6 deletions tests.py
Expand Up @@ -10,10 +10,8 @@
except ImportError:
from io import StringIO

try:
from mock import patch, Mock
except ImportError:
from unittest.mock import patch, Mock

from unittest.mock import patch, Mock, call

import tunirlib
from tunirlib.tunirutils import Result, system
Expand Down Expand Up @@ -141,14 +139,17 @@ def test_multihost(self, p_br, p_sc, p_kill, p_exit, p_sleep, p_system, p_usyste
data = out.getvalue()
self.assertIn("Passed:1", data)
self.assertIn("Job status: True", data)
self.assertTrue(os.path.exists('./current_run_info.json'))
for filename in ['./current_run_info.json', ]:
self.assertTrue(os.path.exists(filename), filename)
last_call = p_system.call_args_list[-1]
self.assertEqual(last_call, call("touch /tmp/hostcommand.txt",))



class ExecuteTests(unittest.TestCase):
"""
Tests the execute function.
"""

@patch('tunirlib.tunirutils.run')
def test_execute(self, t_run):
config = {"host_string": "127.0.0.1",
Expand Down Expand Up @@ -224,3 +225,5 @@ def test_refresh_vol_pool(self, t_sys):

if __name__ == '__main__':
unittest.main()
os.system('rm ./current_run_info.json')
os.system('rm /tmp/hostcommand.txt')
1 change: 1 addition & 0 deletions testvalues/multihost.txt
@@ -1,3 +1,4 @@
vm1 sudo su -c"echo Hello > /abcd.txt"
HOSTCOMMAND: touch /tmp/hostcommand.txt
vm2 ls /
## cat /etc/os-release
9 changes: 7 additions & 2 deletions tunirlib/tunirutils.py
Expand Up @@ -164,15 +164,16 @@ def run(host='127.0.0.1', port='22', user='root',
out.return_code = status
return out


def clean_tmp_dirs(dirs):
# type: (List[str]) -> None
"Removes the temporary directories"
for path in dirs:
if os.path.exists(path) and path.startswith('/tmp'):
shutil.rmtree(path)

def system(cmd):
# type: (str) -> Tuple[str, str, int]

def system(cmd: str) -> Tuple[str, str, int]:
"""
Runs a shell command, and returns the output, err, returncode
Expand Down Expand Up @@ -339,6 +340,10 @@ def run_job(jobpath: str, job_name: str='', extra_config: Dict[str,str]={}, cont
break
else:
continue # We don't want to execute a POLL command in the remote system
if command.startswith("HOSTCOMMAND:"):
cmd = command[12:].strip()
os.system(cmd)
continue
elif command.startswith('PLAYBOOK'):
playbook_name = command.split(' ')[1]
playbook = os.path.join(ansible_path, playbook_name)
Expand Down

0 comments on commit 0f924ed

Please sign in to comment.