RNELSON0: Introducing generate-puppetfile, or Creating a ruby program to update your Puppet...

来源:互联网 时间:1970-01-01


About a month ago, I whipped up simple shell scriptthat would download some puppet modules and then generate a valid snippet of code you could insert into your Puppetfile. This was helpful when you wanted to add a single module but it had dependencies, and then those dependencies had dependencies, and then 30 minutes go by and you have no idea what it was you set out to do in the beginning. As I started using it, I realized this was only half the battle – I might already have some of those dependencies in my Puppetfile and adding the same modules again doesn’t work.

So I started adding to the script and quickly realized a shell script was not sufficient. About three weeks ago, I decided to convert it to a ruby program and add cli arguments to support all the new features I wanted and that some users were requesting. I had a few problems I knew I needed to solve, namely how to parse an existing Puppetfile and pull out the existing forge modules, how to combine that and any non-forge module data with the new module list and generate a new file, and how to generate a .fixtures.yml file. I also ended up with a boatload of problems that I didn’t know I needed to solve.

The first problem I had was how to get started writing a ruby program. I never did find a good guide for this. Instead, I looked at some programs that I used and how they structured things, like the program entry points and such. The two programs I drew the most from where puppet-lintand r10k. I’m still internalizing a lot of what I learned so I don’t feel confident in explaining it to others, but you might start with the lib/directory where the module’s namespaceis defined, then look at bin/where the CLI entry pointis (i.e. when you call r10kon the CLI versus require ‘r10k’in another ruby program). I stumbled a lot here, but after a day or two had something that worked, even if it wasn’t idiomatic ruby.

The next step was learning how to create and publish a ruby gem. Rubygem’s own Make Your Own Gemtutorial was pretty helpful. It does often tell you what to do, and expect you to just do it, rather than explaining why. Regardless, there’s enough information there to create a .gemspecfile, perform a gem build, and create an actual gem that you can upload with gem push.

This was a bit of a trial and I can’t say it was all that enjoyable (maybe ask me in a month or so!), but once I made it over those two humps, actually programming in ruby was pretty fun. If you’re interested in ruby, I hope this helps, and feel free to ask any questions in the comments or on twitter.

Today, I released v0.9.6 of generate-puppetfile. You can find the source on githuband gem at rubygems.org. Install it with gem install generate-puppetfileand call it with a list of modules. You can add the -foption to generate a .fixtures.ymlfile for use with rspec, or -p <filename>to read an existing Puppetfile (not shown):

[[email protected] ~]$ generate-puppetfile puppetlabs/apache rnelson0/certs -fInstalling modules. This may take a few minutes.Your Puppetfile has been generated. Copy and paste between the markers:=======================================================================forge 'http://forge.puppetlabs.com'# Modules discovered by generate-puppetfilemod 'puppetlabs/apache', '1.6.0'mod 'puppetlabs/concat', '1.2.4'mod 'puppetlabs/stdlib', '4.9.0'mod 'rnelson0/certs', '0.6.2'=======================================================================Generating .fixtures.yml using module name profile[[email protected] ~]$ cat .fixtures.ymlfixtures: symlinks: profile: "#{source_dir}" repositories: concat: https://github.com/puppetlabs/puppetlabs-concat apache: https://github.com/puppetlabs/puppetlabs-apache stdlib: https://github.com/puppetlabs/puppetlabs-stdlib certs: https://github.com/rnelson0/puppet-certs

If you’re using puppet and either librarian-puppet or r10k, you may find this useful. Report any bugs at the issue tracker, and PRs are gladly accepted!



相关阅读:
Top