add scripts for performing YAML validation

this commit adds scripts for validating YAML documents, as well as a
wrapper for git pre-commit hooks.  The ci-scripts/validate-yaml
script will check individual files or entire directories, e.g:

    ./ci-scripts/validate-yaml playbooks

The ci-scripts/pre-commit-hook script is meant to be installed as a
git pre-commit hook:

    ln -s $PWD/ci-scripts/pre-commit-hook .git/hooks/pre-commit

This will run any executable scripts in `ci-scripts/pre-commit.d`.
There is one scripts there, `ci-scripts/pre-commit.d/validate-yaml-in-commit`, which will validate all of the .yml documents contained in a commit.  This can prevent CI failures do to simple syntax errors by ensuring that the yaml documents are correct before submitting a change to gerrit.

Change-Id: I088e7b56a066e9b8520af9d54489b8a61d8bc29b
This commit is contained in:
Lars Kellogg-Stedman 2016-04-12 12:08:56 -04:00
parent 7d9ee18e2d
commit 031b360f22
3 changed files with 152 additions and 0 deletions

40
ci-scripts/pre-commit-hook Executable file
View File

@ -0,0 +1,40 @@
#!/bin/sh
# Install this into .git/hooks/pre-commit
echo "running commit checks"
(
tmpfile=$(mktemp -t gitXXXXXX)
trap "rm -f $tmpfile" EXIT
for x in ci-scripts/pre-commit.d/*; do
[ -x "$x" ] || continue
tput setaf 3
printf "%-50s" $x
tput sgr0
if $x > $tmpfile 2>&1 ; then
tput setaf 2
printf "[OK]\n"
tput sgr0
else
tput setaf 1
printf "[OK]\n"
tput sgr0
cat $tmpfile >&2
exit 1
fi
done
)
if [ $? -ne 0 ]; then
tput setaf 1
echo "*** COMMIT CHECK FAILED ***" >&2
tput sgr0
exit 1
fi
exit 0

View File

@ -0,0 +1,18 @@
#!/bin/sh
SCRIPTS="$PWD/ci-scripts"
tmpdir=$(mktemp -d commitXXXXXX)
trap "rm -rf $tmpdir" EXIT
# This checks out the index into a temporary directory, which is
# necessary if you want to validate the files that are actually
# part of the commit (because when performing an interactive add,
# for example, files in a commit may not actually match the content
# of files in the working directory).
git checkout-index --prefix=$tmpdir/ -af
git diff --cached --name-only --diff-filter=ACM | grep '.yml' | (
cd $tmpdir
xargs --no-run-if-empty $SCRIPTS/validate-yaml
)

94
ci-scripts/validate-yaml Executable file
View File

@ -0,0 +1,94 @@
#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import argparse
import logging
import sys
import yaml
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('--verbose', '-v',
action='store_const',
const='INFO',
dest='loglevel')
p.add_argument('--fail-fast', '-x',
action='store_true')
p.add_argument('path', nargs='+')
p.set_defaults(loglevel='WARNING')
return p.parse_args()
def validate_file(path):
global args
valid = True
logging.info('checking: %s', path)
with open(path) as fd:
try:
yaml.safe_load(fd)
except yaml.error.YAMLError as error:
valid = False
logging.error('%s failed validation: %s',
path, error)
return valid
def validate_directory(path):
global args
all_valid = True
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if not (filename.endswith('.yml')
or filename.endswith('.yaml')):
continue
filename = os.path.join(dirpath, filename)
all_valid = validate_file(filename) and all_valid
if not all_valid and args.fail_fast:
break
else:
continue
break
return all_valid
def main():
global args
args = parse_args()
logging.basicConfig(level=args.loglevel)
for path in args.path:
if os.path.isdir(path):
all_valid = validate_directory(path)
elif os.path.isfile(path):
all_valid = validate_file(path)
else:
logging.error('%s is not a file or directory (skipping)',
path)
if not all_valid and args.fail_fast:
break
return 0 if all_valid else 1
if __name__ == '__main__':
sys.exit(main())