Monday, December 7, 2015

script to run puppet-lint, puppet validate, erb check and ending white space in yaml files under a git branch


#!/bin/bash
# sctipt need to be run from git repository
# cd ~/mygit && sh this_script.sh
# check you puppet code before adding it - git add .

(
# get all unstrack and staged files except one being deleted
all_files="$(git status -s|grep -v "^D "|awk '{print $NF}')"

# list all puppet files with .pp extention
all_pp_files="$(echo $all_files|tr ' ' '\n'|grep pp$)"

# TEST-1: runn puppet-lint on .pp file
if [ -z "$all_pp_files" ];then
  echo There is no pp file
else
echo -e "\nRUNNING PUPPET LINT .............................................................................................................."
for i in $all_pp_files
  do
    echo;echo "FILE : $i"
    /opt/puppet/bin/puppet-lint --with-context --with-filename --no-80chars-check $i
 done


# TEST-2: runn puppet validate on .pp files
echo -e "\nRUNNING PUPPET VALIall DATE ..........................................................................................................."
for i in $all_pp_files
  do
    echo;echo "FILE : $i"
    puppet parser --storeconfigs validate $i
 done
 fi


# TEST-3: check ruby erb template
 all_erb_files="$(echo $all_files|tr ' ' '\n'|grep erb$)"
 if [ -z "$all_erb_files" ]; then
   echo There is no erb file
 else
   echo -e "\nRUNNING ERB TEMPLATE CHECKS ......................................................................................................."
   for i in $all_erb_files
   do
      echo;echo "FILE : $i"
     /opt/puppet/bin/erb -P -x -T '-' $i | ruby -c
   done
 fi


# TEST-4: check yaml files for end of line white space
 all_yaml_files="$(echo $all_files|tr ' ' '\n'|grep yaml$)"
 if [ -z "$all_yaml_files" ]; then
   echo There is no yaml file
 else
 echo "RUNNING YAML CHECKS ......................................................................................................."
 for i in $all_yaml_files
 do
    echo;echo "FILE : $i"
    grep -n "[[:space:]]$" $i
 done
 fi

) 2>&1  | tee /tmp/validate.txt

echo Above output saved in /tmp/validate.txt
#end of script