2

Have Vagrant + Chef executes the referral if the template files have been checke...

 2 years ago
source link: https://www.codesd.com/item/have-vagrant-chef-executes-the-referral-if-the-template-files-have-been-checked-with-the-crlf-end-of-line.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Have Vagrant + Chef executes the referral if the template files have been checked with the CRLF end of line

advertisements

I keep getting bit by an annoying gotcha that happens when Windows developers check out cookbooks from my Git repo with Git's autocrlf set to true. When they run vagrant up to bring up a Linux VM, the cookbook files are mapped into the VM with CRLF line endings, which causes no end of obscure errors when the shell and other POSIX utilities try to operate on the (now invalid) template files that have been copied into the VM.

The fix for that is simple enough: re-clone the repository after changing the autocrlf setting to input or false.

My problem is that, when you have the wrong line endings, the only symptoms are errors in strange places that in no way point to there being a problem with line endings.

How can I have Chef† check for the wrong line endings in, say, the cookbook template files and throw an error if it finds one? I think a simple Ruby snippet that does an assertion on the line endings in a given file that I can put at the top of a recipe would work.

Note: in the particular case of my repo, the sequence of steps is:

  1. Developer checks out repo
  2. Developer runs vagrant up
  3. Vagrant kicks off a Chef run in the VM
  4. Finally, the repo's build script runs in the VM

† Or really anything else included in the repo


Rubocop can be used to enforce unix style line endings (among many other things).

For example (from the command line, within the guest):

gem install rubocop
rubocop --only Style/EndOfLine  # only check line endings

Or it could be done from within the context of chef itself with something like the following recipe:

chef_gem 'rubocop'

ruby_block 'check line endings' do
  block do
    # It's probably better to call rubo cop code directly, rather than
    # shelling out, but that can be an exercise for the reader ;-)
    rubocop_cmd = Mixlib::ShellOut.new(
      'rubocop --only Style/EndOfLine',
      :cwd => 'dir_to_check'
    )
    rubocop_cmd.run_command

    # Raise an exception if it didn't exit with 0.
    rubocop_cmd.error!
  end
end

The uncaught exception will cause the chef run to bail out.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK