Quickly validate puppet manifests in a git repo.
Mon 17 September 2012 by tvdI have been bitten on more than one occasion with a forgotten curly brace or missing comma in a puppet manifest. So, I wrote a little git post-commit script that will validate all manifests in a repo. I have tested it on a repo with ~60 modules and ~400 manifests, and it only adds a little bit of latency to committing (less than 1s on my system with the email disabled).
I still like to use continuous integration via Travis or Jenkins to run smoke tests, spec tests, and lint. This post-commit commit script just provides some quick feedback for syntax errors.
#!/usr/bin/env ruby
#
# Parse all manifests. Send notification if errors exist.
require 'puppet/face'
# Email sending config
SEND_MAIL=false
SMTP_HOST='localhost'
RECIPIENTS=''
busted_manifests = []
errors = []
# Iterate through all manifests, recording which ones fail.
Dir.glob('**/{manifests,tests}/**/*.pp').each do |manifest|
pparse = Puppet::Face['parser', :current]
begin
pparse.validate(manifest)
rescue Puppet::Error => e
busted_manifests << manifest
errors << e.to_s
end
end
if not busted_manifests.empty?
subject = "#{busted_manifests.length} busted manifests..."
body = busted_manifests.join(', ') + "\n\n" + errors.join("\n\n")
puts subject + "\n\n" + body + "\n"
if SEND_MAIL
# Send some email.
require 'net/smtp'
require 'socket'
sender = "repo@#{Socket.gethostname}"
message = <<EOM
From: #{sender}
To: #{RECIPIENTS}
Subject: #{subject}
#{body}
EOM
Net::SMTP.start(SMTP_HOST) do |smtp|
smtp.send_message message, sender, RECIPIENTS
end
end
end