Saturday, August 15, 2015

Learning Chef and Ruby version pain ... rbenv issue 96



I was studiously working through the following chef-solo (knife) training document:




When I hit the following weird looking 'syntax error':


root@knife1:/vagrant# chef-solo -c solo.rb -j web.json
[2015-08-15T13:15:50+00:00] INFO: *** Chef 10.14.2 ***
[2015-08-15T13:15:50+00:00] INFO: Setting the run_list to ["recipe[apt]", "recipe[phpapp]"] from JSON
[2015-08-15T13:15:50+00:00] INFO: Run List is [recipe[apt], recipe[phpapp]]
[2015-08-15T13:15:50+00:00] INFO: Run List expands to [apt, phpapp]
[2015-08-15T13:15:50+00:00] INFO: Starting Chef Run for knife1
[2015-08-15T13:15:50+00:00] INFO: Running start handlers
[2015-08-15T13:15:50+00:00] INFO: Start handlers complete.


================================================================================
Recipe Compile Error in /vagrant/chef-repo/cookbooks/apt/providers/repository.rb
================================================================================


SyntaxError
-----------
compile error
/vagrant/chef-repo/cookbooks/apt/providers/repository.rb:45: syntax error, unexpected ':', expecting ')'
 so = Mixlib::ShellOut.new(cmd, env: { 'LANG' => 'en_US' })
                                    ^
/vagrant/chef-repo/cookbooks/apt/providers/repository.rb:45: syntax error, unexpected ')', expecting kEND


Cookbook Trace:
---------------
 /vagrant/chef-repo/cookbooks/apt/providers/repository.rb:203:in `class_from_file'


Relevant File Content:
----------------------
/vagrant/chef-repo/cookbooks/apt/providers/repository.rb:


 1:  #
 2:  # Cookbook Name:: apt
 3:  # Provider:: repository
 4:  #
 5:  # Copyright 2010-2011, Chef Software, Inc.
 6:  #
 7:  # Licensed under the Apache License, Version 2.0 (the "License");
 8:  # you may not use this file except in compliance with the License.
 9:  # You may obtain a copy of the License at


[2015-08-15T13:15:50+00:00] ERROR: Running exception handlers
[2015-08-15T13:15:50+00:00] ERROR: Exception handlers complete
[2015-08-15T13:15:50+00:00] FATAL: Stacktrace dumped to /vagrant/chef-solo/chef-stacktrace.out
[2015-08-15T13:15:50+00:00] FATAL: SyntaxError: compile error
/vagrant/chef-repo/cookbooks/apt/providers/repository.rb:45: syntax error, unexpected ':', expecting ')'
 so = Mixlib::ShellOut.new(cmd, env: { 'LANG' => 'en_US' })
                                    ^
/vagrant/chef-repo/cookbooks/apt/providers/repository.rb:45: syntax error, unexpected ')', expecting kEND


After double-checking all my scripts and commands (as I hadn't changed the repository.rb file), I started googling for this error and eventually stumbled across


todd commented on Apr 5, 2014
Narrowed this down - it looks like the recipe is using incompatible syntax with Ruby 1.8 (which I'm assuming is the Ruby version @CADBOT and @nazarhussain are using). Since Ruby 1.8 is not supported by this project, this isn't technically an issue, but I'd be more than happy to put some work in to make this compatible with 1.8.


nazarhussain commented on Apr 7, 2014
todd yes you are right I am using Ruby 1.8. Since its pre-built with Chef-Solo, so you must think to resolve this issue and make this recipe functional for old version of ruby.




Checking my ruby version, yes it was 1.8.x:


root@knife1:/vagrant# ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]


As this was a development/training effort and the build process was scripted via vagrant/provision.sh,  the easiest thing to was to upgrade the base image from Ubuntu 12.x (base ruby version is 1.8) to Ubuntu 14.x (base ruby version is 1.9).


For further details of the Vagrantfile, provision.sh and other setup files please see my github repo: