Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ansible syntax checks to tox #3938

Merged
merged 1 commit into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fnmatch
import re
import sys
import subprocess
import yaml

# Always prefer setuptools over distutils
Expand Down Expand Up @@ -199,6 +200,52 @@ def run(self):
print('\nAll generate scripts passed.\n')


class OpenShiftAnsibleSyntaxCheck(Command):
''' Command to run Ansible syntax check'''
description = "Run Ansible syntax check"
user_options = []

# Colors
FAIL = '\033[91m' # Red
ENDC = '\033[0m' # Reset

def initialize_options(self):
''' initialize_options '''
pass

def finalize_options(self):
''' finalize_options '''
pass

def run(self):
''' run command '''

has_errors = False

for yaml_file in find_files(
os.path.join(os.getcwd(), 'playbooks', 'byo'),
None, None, r'\.ya?ml$'):
with open(yaml_file, 'r') as contents:
for line in contents:
# initialize_groups.yml is used to identify entry point playbooks
if re.search(r'initialize_groups\.yml', line):
print('-' * 60)
print('Syntax checking playbook: %s' % yaml_file)
try:
subprocess.check_output(
['ansible-playbook', '-i localhost,',
'--syntax-check', yaml_file]
)
except subprocess.CalledProcessError as cpe:
print('{}Execution failed: {}{}'.format(
self.FAIL, cpe, self.ENDC))
has_errors = True
# Break for loop, no need to continue looping lines
break
if has_errors:
raise SystemExit(1)


class UnsupportedCommand(Command):
''' Basic Command to override unsupported commands '''
user_options = []
Expand Down Expand Up @@ -242,6 +289,7 @@ def run(self):
'lint': OpenShiftAnsiblePylint,
'yamllint': OpenShiftAnsibleYamlLint,
'generate_validation': OpenShiftAnsibleGenerateValidation,
'ansible_syntax': OpenShiftAnsibleSyntaxCheck,
},
packages=[],
)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ commands =
yamllint: python setup.py yamllint
generate_validation: python setup.py generate_validation
# TODO(rhcarvalho): check syntax of other important entrypoint playbooks
ansible_syntax: ansible-playbook --syntax-check playbooks/byo/config.yml
ansible_syntax: python setup.py ansible_syntax