Skip to content

Commit

Permalink
Merge pull request #560 from nvgoldin/sdk_ansible
Browse files Browse the repository at this point in the history
 sdk: added ansible inventory methods
  • Loading branch information
ovirt-infra authored Jun 1, 2017
2 parents ed2d079 + e06f502 commit aa5fd40
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions automation/check-patch.packages
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ansible
bats
dnf
dnf-command(builddep)
Expand Down
1 change: 1 addition & 0 deletions automation/check-patch.packages.el7
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ansible
bats
git
libffi-devel
Expand Down
2 changes: 1 addition & 1 deletion lago/plugins/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def extract_paths(self, paths, ignore_nopath):
:exc:`~lago.plugins.vm.ExtractPathError`: on all other failures.
"""
if self.vm.alive() and self.vm.ssh_reachable(
tries=1, propagate_fail=False
tries=5, propagate_fail=False
):
self._extract_paths_scp(paths=paths, ignore_nopath=ignore_nopath)
else:
Expand Down
60 changes: 60 additions & 0 deletions lago/sdk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import print_function
from lago import cmd
from lago.config import config as lago_config
from lago.lago_ansible import LagoAnsible
from lago import workdir as lago_workdir
from lago.log_utils import get_default_log_formatter
from sdk_utils import SDKWrapper, setup_sdk_logging
Expand Down Expand Up @@ -136,3 +137,62 @@ def destroy(self):
entirely the Lago working directory.
"""
self._workdir.destroy()

def ansible_inventory_temp_file(
self, keys=['vm-type', 'groups', 'vm-provider']
):
"""
Context manager which returns Ansible inventory written on a tempfile.
This is the same as :func:`~ansible_inventory`, only the inventory file
is written to a tempfile.
Args:
keys (list of str): Path to the keys that will be used to
create groups.
Yields:
tempfile.NamedTemporaryFile: Temp file containing the inventory
"""
lansible = LagoAnsible(self._prefix)
return lansible.get_inventory_temp_file(keys=keys)

def ansible_inventory(
self,
keys=['vm-type', 'groups', 'vm-provider'],
):
"""
Get an Ansible inventory as a string, ``keys`` should be list on which
to group the hosts by. You can use any key defined in LagoInitFile.
Examples of possible `keys`:
`keys=['disks/0/metadata/arch']`, would group the hosts by the
architecture.
`keys=['/disks/0/metadata/distro', 'disks/0/metadata/arch']`,
would create groups by architecture and also by distro.
`keys=['groups']` - would group hosts by the groups defined for
each VM in the LagoInitFile, i.e.:
domains:
vm-01:
...
groups: web-server
..
vm-02:
..
groups: db-server
Args:
keys (list of str): Path to the keys that will be used to
create groups.
Returns:
str: INI-like Ansible inventory
"""

lansible = LagoAnsible(self._prefix)
return lansible.get_inventory_str(keys=keys)
2 changes: 2 additions & 0 deletions lago/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ def get_ssh_client(
host_name,
err,
)
except EOFError as err:
LOGGER.debug('EOFError connecting to %s: %s', host_name, err)
ssh_tries -= 1
time.sleep(1)
else:
Expand Down
38 changes: 37 additions & 1 deletion tests/functional-sdk/test_sdk_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import uuid
from jinja2 import Environment, BaseLoader
from lago import sdk
from lago.utils import run_command


@pytest.fixture(scope='module')
def init_str(images):
init_template = textwrap.dedent(
"""
domains:
{% for vm_name, template in images.iteritems() %}
{% for vm_name, template in images.viewitems() %}
{{ vm_name }}:
memory: 1024
nics:
Expand All @@ -33,6 +34,7 @@ def init_str(images):
- /etc/resolv.conf
- /etc/sysconfig
- /etc/NetworkManager
groups: group{{ loop.index % 2 }}
{% endfor %}
nets:
Expand Down Expand Up @@ -205,3 +207,37 @@ def test_load_env_up(env, vms, tmp_workdir):
assert vm.state() == 'running'
for vm in loaded_env.get_vms().values():
assert vm.ssh_reachable(tries=200)


@pytest.mark.check_merged
def test_ansible_inventory(monkeypatch, env, test_results, vms):

# ansible returns the results in a bulk to stdout. Ideally we would test
# forthe hostname of each machine, but that is broken on debian.
# Instead, we let it compute something and count the unique occurences.

cmd = 'echo __abcd$(( 24 + 12 ))efgh___'
expected = '__abcd36efgh__'
results = []

with env.ansible_inventory_temp_file(keys=['groups']) as inv:
for group in ['group0', 'group1']:
logfile = os.path.join(
test_results, 'ansible-{0}.log'.format(group)
)
monkeypatch.setenv('ANSIBLE_LOG_PATH', logfile)
monkeypatch.setenv('ANSIBLE_HOST_KEY_CHECKING', 'False')
res = run_command(
[
'ansible', 'groups={0}'.format(group), '-v', '-u', 'root',
'-i', inv.name, '-m', 'raw', '-a', cmd
]
)

assert res.code == 0
assert res.out is not None
results.append(res)

occurences = sum([result.out.count(expected) for result in results])

assert occurences == len(vms.keys())

0 comments on commit aa5fd40

Please sign in to comment.