Using Ruby & Thor For Easy Puppet Module Development

Tehmasp Chaudhri | @tehmaspc

What is Thor?

  • Thor is a Ruby toolkit for building powerful command-line interfaces.
  • The developer can easily integrate Thor into their Ruby code.
  • Classes such as Groups, Actions, and Shell are provided which make tasks such as file system interaction and command line user dialogue easier.
  • Thor can be leveraged to make custom project generators (e.g. 'bundle gem -b mygem')

Popular Projects Using Thor:

Why Did We Use Thor?

Technical Reasons:

  • Thor is written in Ruby, our Puppet code is in a Ruby DSL.
  • Thor's custom project generator use case was appealing and works well. It's easy to add and update template files.
  • We wanted to create a framework for Puppet module development which provided the Puppet tools we like, so that we could create standalone & well tested Puppet modules. Thor let us glue all this together.


Business Reasons:

  • We allow (many) dev teams to contribute to our Puppet module development; thus we needed a way to make creating Puppet modules quick & easy, consistent, manageable, and with 'best practices' baked in.

Tools We Like To Use With Puppet Module Development:


Shoutout to Phil Zimmerman's Puppet Testing For The Win

Thor Custom Generator Example:

...
def write_emptydirs
  empty_directory target.join('files')
  empty_directory target.join('manifests')
  empty_directory target.join('templates')
  empty_directory target.join('spec')
  empty_directory target.join('serverspec/spec')
  empty_directory target.join('.vagrant_puppet')
end

def write_modulefile
  template 'puppet/ModuleFile.erb', target.join('ModuleFile')
end
...

ModuleFile.erb Example:


name '<%= module_name %>'
version '0.1.0'
source 'UNKNOWN'
author '<%= maintainer_email %>'
license '<%= license_name %>'
summary 'UNKNOWN'
description 'UNKNOWN'
project_page 'UNKNOWN'

## Add dependencies, if any:
# dependency 'username/name', '>= 1.2.0'

Current List Of Template Files:

./git/gitignore.erb
./git/puppet-git-hooks-pre-commit.erb
./licenses/apachev2.erb
./licenses/mit.erb
./licenses/reserved.erb
./puppet/init.pp.erb
./puppet/ModuleFile.erb
./README.md.erb
./spec/fixtures.yml.erb
./spec/rspec/init_spec.rb.erb
./spec/rspec/spec_helper.rb.erb
./spec/serverspec/init_spec.rb.erb
./spec/serverspec/spec_helper.rb.erb
./util/Gemfile.erb
./util/Rakefile.erb
./vagrant/init.pp.erb
./vagrant/Vagrantfile.erb

Demo

Magnum (our custom Thor based tool)

Questions?

 

Using Ruby & Thor For Easier Puppet Module Development

By Tehmasp Chaudhri

Using Ruby & Thor For Easier Puppet Module Development

Making Puppet Module Development Consistent

  • 2,318