This is a text-only version of the following page on https://raymii.org:
---
Title       :   Chef: chef_gem vs gem_package and ORDER
Author      :   Remy van Elst
Date        :   01-04-2014
URL         :   https://raymii.org/s/articles/Chef_-_chef_gem_vs_gem_package_and_ORDER.html
Format      :   Markdown/HTML
---



One of my clients use [Chef][1] and have a cookbook which builds a postgresql
database node. It should install the [pg][2] ruby gem. However, it uses the
chef_gem resource to do that, and it keeps failing.

<p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called  Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p>


They use a [wrapper-cookbook][4] around the regular [postgresql][5] cookbook.
The wrapper cookbook installs some extra packages required by their HA setup,
plus it places a few other templates than the regular templates.

First they pin on a specific postgresql version and remove any other version:



   package "postgresql-client-common" do
     action :purge
     not_if "psql --version | grep 9.1"
   end


The postgresql apt-repository is enabled:



   include_recipe 'postgresql::apt_pgdg_postgresql'


The `postgresql::server` recipe is included:



   include_recipe "postgresql::server"


Then a few templates from the postgresql cookbook are overwritten by templates
from the wrapper-cookbook:



   r = resources(:template => "#{node['postgresql']['dir']}/pg_hba.conf")
   r.source "pg_hba.conf.erb"
   r.cookbook "wrapper-postgresql"


This goes on for a few templates and files. Then the culprit comes along:



   chef_gem "pg"


Now what we want to happen is that first the postgresql apt-repository is
enabled. Then the correct packages are installed. This includes the `libpq-dev`
package. This is installed in the regular postgres cookbook.

This cookbook however, on a new node, fails with the following error messages:



   ================================================================================
   Error executing action `install` on resource 'chef_gem[pg]'
   ================================================================================

   Gem::Installer::ExtensionBuildError
   -----------------------------------
   ERROR: Failed to build gem native extension.

           /usr/bin/ruby extconf.rb
   checking for pg_config... no
   No pg_config... trying anyway. If building fails, please try again with
    --with-pg-config=/path/to/pg_config
   checking for libpq-fe.h... no
   [...]


This is before any of the other things from the run list (other roles etc) run.
Just boom, right away, it fails.

At first I thought it had something to do with `chef_gem` being [a method
instead of a resource][6] but that is not the case. On the [chef_gem
documentation][7] we read the following:



   The chef_gem and gem_package resources are both used to install Ruby gems. For any machine on which the chef-client is installed, there are two instances of Ruby. One is the standard, system-wide instance of Ruby and the other is a dedicated instance that is available only to the chef-client. Use the chef_gem resource to install gems into the instance of Ruby that is dedicated to the chef-client. Use the gem_package resource to install all other gems (i.e. install gems system-wide).


And, more important for this problem:



   The chef_gem resource works with all of the same attributes and options as the gem_package resource, but does not accept the gem_binary attribute because it always uses the CurrentGemEnvironment under which the chef-client is running. In addition to performing actions similar to the gem_package resource, the chef_gem resource does the following:

       Runs its actions immediately, before convergence, allowing a gem to be used in a recipe immediately after it is installed


So, this resource is executed before anything else. Exactly the problem we have.
It cannot install because the needed packages are not there yet and those will
never be installed because of this.

I asked around and it turns out the `pg` gem is not needed in the Ruby instance
dedicated to chef-client, but system wide. There is only one ruby and they
install `chef-client` via `gem install chef-client` with that ruby, so this has
always worked worked for them. Therefore, changing it to `gem_package` should
also have the same result.

It also turns out the other admins just did some things manually because they
did not get time from management to fix this issue...

In the end I changed the `chef_gem` to `gem_package`. The cookbook now works on
a new node without this issue.

  [1]: http://www.getchef.com/
  [2]: https://rubygems.org/gems/pg
  [3]: https://www.digitalocean.com/?refcode=7435ae6b8212
  [4]: http://support.rightscale.com/12-Guides/Chef_Cookbooks_Developer_Guide/04-Developer/06-Development_Resources/Chef_Cookbook_Design_Patterns/Wrapper_Cookbook_Pattern
  [5]: http://community.opscode.com/cookbooks/postgresql
  [6]: https://raymii.org/s/tutorials/Chef_include_recipe_only_if_not_if.html
  [7]: http://docs.opscode.com/resource_chef_gem.html

---

License:
All the text on this website is free as in freedom unless stated otherwise.
This means you can use it in any way you want, you can copy it, change it
the way you like and republish it, as long as you release the (modified)
content under the same license to give others the same freedoms you've got
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for
advertisements. You are tracked and Google knows everything about you.
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license
unless already licensed under a license which does not allows this form
of licensing or if another license is stated on that page / in that software:

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

Just to be clear, the information on this website is for meant for educational
purposes and you use it at your own risk. I do not take responsibility if you
screw something up. Use common sense, do not 'rm -rf /' as root for example.
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.