Mysql – Install `thesql` gem for use in `chef-client`

chefgemMySQL

I am trying to deploy a rails application using chef, for the mysql cookbook to create a database, it needs the mysql gem. The mysql gem is installed system-wide by using an Ubuntu package, but this is not usable by chef-client which runs from /opt/chef/embedded.

I have tried adding this:

chef_gem 'mysql' do
    action :nothing
end.run_action(:install)

But this requires the libmysqlclient-dev Ubuntu package to be installed. Therefore I have also added this before the previous:

package 'libmysqlclient-dev' do
    action :nothing
end.run_action(:install)

But this is done before the apt recipe updates the apt repositories, and therefore the install of libmysqlclient-dev fails.

These 'hacks' look ugly and I am unable to find a way to run the apt-get update at the right time.

Could someone please help me to find the right (the most chef-like) way to solve my problem (the actual problem is to create the database using the application cookbook)?

Update

I have been able to fix the problem with this recipe as an ugly hack… I'm still searching for a better solution:

execute "apt-get update" do
  ignore_failure true
  action :nothing
end.run_action(:run)

node.set['build_essential']['compiletime'] = true
include_recipe "build-essential"

%w{build-essential mysql-client libmysqlclient-dev}.each do |p|
  package p do
    action :nothing
  end.run_action(:install)
end

chef_gem 'mysql' do
  action :nothing
end.run_action(:install)

Best Answer

The mysql::ruby recipe allow to install packages via node["mysql"]["client"]["packages"]:

Therefore include it in your run list:

run_list(
  #...
  "recipe[mysql::ruby]",
  #...
)

and specify your dependencies in the attributes:

# this is needed for debian based systems
node["mysql"]["client"]["packages"] = ["build-essential","mysql-client","libmysqlclient-dev"]
Related Topic