Skip to content

Instantly share code, notes, and snippets.

@matiasfrndz
Created March 20, 2012 22:03
Show Gist options
  • Save matiasfrndz/2141740 to your computer and use it in GitHub Desktop.
Save matiasfrndz/2141740 to your computer and use it in GitHub Desktop.
Rakefile for Puppet syntax check, puppet-lint analysis and Puppet doc creation.
source "http://rubygems.org"
group :rake do
gem 'rake'
gem 'puppet', :require => "puppet/face"
gem 'puppet-lint'
end
GEM
remote: http://rubygems.org/
specs:
facter (1.6.5)
puppet (2.7.12)
facter (>= 1.5.1)
puppet-lint (0.1.12)
rake (0.9.2.2)
PLATFORMS
ruby
DEPENDENCIES
puppet
puppet-lint
rake
# Rakefile
require 'rubygems'
require 'bundler'
Bundler.require(:rake)
require 'rake/clean'
CLOBBER.include("target")
desc "Check syntax."
task :syntax, :environment do |t, args|
begin
require 'puppet/face'
rescue LoadError
fail 'Cannot load puppet/face, are you sure you have Puppet 2.7?'
end
def validate_manifest(file)
Puppet::Face[:parser, '0.0.1'].validate(file)
rescue Puppet::Error => error
puts error.message
end
environment = validate_environment(args[:environment])
files = File.join(environment, "**", "*.pp")
Dir.glob(files).each do|manifest|
puts "Evaluating syntax for #{manifest}"
validate_manifest manifest
end
end
desc "Run puppet-lint."
task :lint, :environment do |t, args|
begin
require 'puppet-lint'
rescue LoadError
fail "Cannot load puppet-lint, please install gem"
end
environment = validate_environment(args[:environment])
PuppetLint.configuration.log_format = "%{path}:%{linenumber}:%{check}:%{KIND}:%{message}"
linter = PuppetLint.new
files = File.join(environment, "**", "*.pp")
Dir.glob(files).each do |puppet_file|
puts "Evaluating #{puppet_file}"
linter.file = puppet_file
linter.run
end
end
desc "Generate documentation."
task :doc, [:environment] => [:clobber] do |t, args|
puts "Generating puppet documentation..."
environment = validate_environment(args[:environment])
work_dir = File.dirname(__FILE__)
sh %{puppet doc \
--outputdir #{work_dir}/doc \
--mode rdoc \
--manifestdir #{work_dir}/#{environment}/manifests \
--modulepath #{work_dir}/#{environment}/modules \
--manifest #{work_dir}/#{environment}/site.pp \
--environment #{environment}}
if File.exists? "#{work_dir}/doc/files/#{work_dir}/modules"
FileUtils.mv "#{work_dir}/doc/files/#{work_dir}", "#{work_dir}/doc/files"
end
Dir.glob('./**/*.*').each do |file|
if File.file? "#{file}"
sh %{sed -i "s@#{work_dir}/@/@g" "#{file}"}
end
end
end
def validate_environment(environment)
environment ||= 'production'
unless [ 'production', 'staging', 'development'].include?(environment)
raise "Invalid environment: '#{args[:environment]}'"
end
environment
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment