Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

puppet pre-commit

By: a guest on Aug 24th, 2012  |  syntax: Bash  |  size: 1.68 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. ECHO=echo
  4. RUBY=$(which ruby)
  5. RUBY_OPTS="-c"
  6. ERB=$(which erb)
  7. ERB_OPTS="-x -T"
  8. PUPPET=$(which puppet)
  9. PUPPET_OPTS="parser validate"
  10. PUPPET_LINT=$(which puppet-lint)
  11.  
  12. function determine_type {
  13.   [ -d "$1" ] && return 0
  14.   ext=$( echo $1 |awk -F. '{ print $NF }' )
  15.   case $ext in
  16.     pp) # need a better way to check for a manifest
  17.       manifest_syntax $1
  18.       lint_check $1
  19.     ;;
  20.     erb) # assume it's an erb if it's not a manifest
  21.       erb_syntax $1
  22.     ;;
  23.     rb) # a bit of ruby
  24.       ruby_syntax $1
  25.     ;;
  26.     *)
  27.       ${ECHO} "$1 is something I don't recognize... skipping"
  28.     ;;
  29.   esac
  30.  
  31. }
  32.  
  33. function manifest_syntax {
  34.   ${ECHO} -en "Checking manifest\t$1: "
  35.   ${PUPPET} ${PUPPET_OPTS} $1 && ${ECHO} "Syntax OK"
  36. }
  37.  
  38. function lint_check {
  39.   ${ECHO} -en "Checking best practices\t$1: "
  40.   ${PUPPET_LINT} $1 && ${ECHO} "Syntax OK"
  41. }
  42.  
  43. function erb_syntax {
  44.   ${ECHO} -en "Checking erb\t\t$1: "
  45.   ${ERB} ${ERB_OPTS} '-' $1 | ${RUBY} ${RUBY_OPTS}
  46. }
  47.  
  48. function ruby_syntax {
  49.   ${ECHO} -en "Checking ruby\t\t$1: "
  50.   ${RUBY} -rpuppet $1 && ${ECHO} "Syntax OK"
  51. }
  52.  
  53. function notify {
  54.   if [ -z "$QUIET" ]; then
  55.     return
  56.   else
  57.     echo $1
  58.   fi
  59. }
  60.  
  61. if [ -z "$*" ]; then
  62.   START=.  #current directory
  63.   notify "No files or directories specified, checking $START"
  64. else
  65.   START=$*
  66. fi
  67.  
  68. # loop through objects given on commandline
  69. for object in $(git diff HEAD  --name-only); do
  70.   if [ -d "$object" ]; then
  71.     for file in $( find ${object} -not \( -name .git -prune \) -and -not \( -name .svn -prune \) -and -not \( -name CVS -prune \) ); do
  72.       determine_type $file
  73.     done
  74.   elif [ -f "$object" ]; then
  75.     determine_type $object
  76.   fi
  77. done