Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Teradata/stacki into fea…
Browse files Browse the repository at this point in the history
…ture/test_sles_patches
  • Loading branch information
mk180007 authored and mk180007 committed Aug 27, 2020
2 parents fafbb63 + 6d1c0bd commit 8d88199
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
10 changes: 10 additions & 0 deletions common/src/stack/ansible/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ install::
mkdir -p $(ROOT)/opt/stack/ansible/plugins/callback
$(INSTALL) -m0644 plugins/callback/failure_report.py $(ROOT)/opt/stack/ansible/plugins/callback/

# Install the module_utils plugin
mkdir -p $(ROOT)/opt/stack/ansible/plugins/module_utils
$(INSTALL) -m0644 plugins/module_utils/stacki.py $(ROOT)/opt/stack/ansible/plugins/module_utils/

# Install the modules
mkdir -p $(ROOT)/opt/stack/ansible/plugins/modules
ifneq ($(wildcard plugins/modules/*.py),)
$(INSTALL) -m0644 plugins/modules/*.py $(ROOT)/opt/stack/ansible/plugins/modules/
endif

clean::
2 changes: 2 additions & 0 deletions common/src/stack/ansible/etc/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
nocows = True
inventory_plugins = /opt/stack/ansible/plugins/inventory:~/.ansible/plugins/inventory:/usr/share/ansible/plugins/inventory
callback_plugins = /opt/stack/ansible/plugins/callback:~/.ansible/plugins/callback:/usr/share/ansible/plugins/callback
library = /opt/stack/ansible/plugins/modules:~/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
module_utils = /opt/stack/ansible/plugins/module_utils:~/.ansible/plugins/module_utils:/usr/share/ansible/plugins/module_utils
inventory = @stacki
callback_whitelist = failure_report

Expand Down
50 changes: 50 additions & 0 deletions common/src/stack/ansible/plugins/module_utils/stacki.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# @copyright@
# Copyright (c) 2006 - 2020 Teradata
# All rights reserved. Stacki(r) v5.x stacki.com
# https://github.com/Teradata/stacki/blob/master/LICENSE.txt
# @copyright@

import json
import subprocess


class StackCommandError(Exception):
"""Exception raised for errors running a stack command."""

def __init__(self, message):
self.message = message


def run_stack_command(command, args=None):
"""Runs a stack command, returning StackCommandError if there is an error."""

# Create our command to run
command_args = ["/opt/stack/bin/stack"]
command_args.extend(command.replace(".", " ").split())
if args:
command_args.extend(args)
command_args.append("output-format=json")

try:
# Run the command, throwing a CalledProcessError if something goes wrong
result = subprocess.run(
command_args, capture_output=True, text=True, check=True
)
except subprocess.CalledProcessError as e:
# Get the first line of the stderr
if e.stderr:
message = e.stderr.splitlines()[0]
else:
message = "error - unknown"

# Raise an exception for the caller
raise StackCommandError(message)

# Return the result as a dict
try:
data = json.loads(result.stdout)
except json.JSONDecodeError:
# Something invalid was returned, likely an empty string
data = []

return data
Empty file.
21 changes: 21 additions & 0 deletions test-framework/test-suites/integration/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
from pathlib import Path
import re
import random
import shutil
import subprocess
Expand Down Expand Up @@ -324,3 +325,23 @@ def _inner(path):
return os.path.join("/export/test-suites/integration/files", path)

return _inner

@pytest.fixture
def run_ansible_module(host):
def _inner(module, **kwargs):
command = f"ansible localhost -o -m {module}"
if kwargs:
command += ' -a "'
command += " ".join(f"{k}={v}" for k,v in kwargs.items())
command += '"'

result = host.run(command)

match = re.match(r"localhost \| (.*?) => (.*)$", result.stdout, flags=re.DOTALL)
if match:
result.status = match.group(1)
result.data = json.loads(match.group(2))

return result

return _inner

0 comments on commit 8d88199

Please sign in to comment.