Chef & Strainer and how my chef-repo was lacking.

Just to start I love Seths work, I know he gets pissed when people comment about his work this is not a comment on his work its a, I hope your google search comes up better then mine.

OK that said I spent a night learning how to get strainer running with travis-ci and read the instruction on the https://github.com/customink/strainer page and was like OK this should take no time at all. Turns out I was wrong. Looking back this should have taken me less then two minutes to figure out but let me be honest I blew more then a hour on this one.

For my testing I created a simple travis-chef-repo directory, dropped in a cookbook directory with the openssh cookbook and its dependency of apt and iptables. Followed the directions on https://github.com/customink/strainer to create a .travis.yml and a Strainer file. Commit it, and your off to the races. Not so quick.

First you need to make sure your Gemfile is correct in travis-chef-repo, here is my example:

jmiller11:travis-chef-repo jmiller$ cat Gemfile

source “https://rubygems.org”

gem ‘rake’
gem ‘chef’
gem ‘foodcritic’
gem ‘rspec’
gem ‘strainer’

Then create the Strainerfile in

jmiller11:travis-chef-repo jmiller$ cat Strainerfile

#
Strainerfile
knife test: bundle exec knife cookbook test $COOKBOOK
foodcritic: bundle exec foodcritic -f any $SANDBOX/$COOKBOOK

Then lets test it!

jmiller11:travistest-chef-repo jmiller$ bundle exec strainer test openssh
I could not detect if you were a chef-repo or a cookbook!
Strainer marked build OK

What the heck, why not? Maybe I have that command wrong lets try this one and maybe it will auto detect the cookbook.

jmiller11:travistest-chef-repo jmiller$ bundle exec strainer test
I could not detect if you were a chef-repo or a cookbook!
Strainer marked build OK

Or how about if we give it the path that must be it.

jmiller11:travistest-chef-repo jmiller$ bundle exec strainer test –cookbooks-path=./cookbooks/
I could not detect if you were a chef-repo or a cookbook!
Strainer marked build OK

Really, how do you tell if this is repo? Lets go look at the code:

https://github.com/customink/strainer/blob/master/lib/strainer/sandbox.rb#L54

else
Strainer.ui.warn “I could not detect if you were a chef-repo or a cookbook!”
@cookbooks = []
end

Umm yea that helps, let look at how we entered this if block:

https://github.com/customink/strainer/blob/master/lib/strainer/sandbox.rb#L54

if chef_repo?

OK so lets look at the chef_repo method:

https://github.com/customink/strainer/blob/master/lib/strainer/sandbox.rb#L244-L249

# Determines if the current project is a chef repo
#
# @return [Boolean]
# true if the current project is a chef repo, false otherwise
def chef_repo?
@_chef_repo ||= begin
chef_folders = %w(.chef certificates config cookbooks data_bags environments roles)
(root_folders & chef_folders).size > 2
end

What really, you need the following directories or are not a chef repo (.chef certificates config cookbooks data_bags environments roles)? At least its easy to fix.

mkdir .chef certificates config cookbooks data_bags environments roles

Lets test this sucker!

jmiller11:travistest-chef-repo jmiller$ bundle exec strainer test openssh
# Straining ‘openssh (v1.3.5)’
knife test | bundle exec knife cookbook test openssh
knife test | checking openssh
knife test | Running syntax check on openssh
knife test | Validating ruby files
knife test | Validating templates
knife test | SUCCESS!
foodcritic | bundle exec foodcritic -f any /Users/jmiller/Development/travistest-chef-repo/cookbooks/openssh
foodcritic | FC007: Ensure recipe dependencies are reflected in cookbook metadata: /Users/jmiller/Development/travistest-chef-repo/cookbooks/openssh/recipes/iptables.rb:20
foodcritic | Terminated with a non-zero exit status. Strainer assumes this is a failure.
foodcritic | FAILURE!
Strainer marked build as failure
jmiller11:travistest-chef-repo jmiller$

I win, lets add, commit, and push. Same “I could not detect if you were a chef-repo or a cookbook!” from travis … really whats going on here. Since I am not a git expert I realize git is not adding empty directories so lets add a file if one does not exist:

for i in certificates config .chef cookbooks data_bags environments roles; do touch $i/README.md;done

git add, commit, push and wait the 8 minutes for the gem install and success. I now see the same foodcritic errors I saw on the command line and have a failing build. Syntax check and lint tools running, time to move on.

Leave a Reply

Your email address will not be published. Required fields are marked *