Search code examples
chef-infrachef-recipecookbook

How to run setup activities when using application cookbook?


I am trying to deploy my Rails app using application_ruby cookbook. I need to run a few one-time setup activities (like seeding the database using rake db:seed). These should not be done during subsequent chef runs. What's the right way to define these tasks ?


Solution

  • The way I solved this was using a conditional execute like this :

    execute "seed database" do
      cwd node[:release_path]
      user node[:owner]
      environment ({'RAILS_ENV' => node[:environment]})
      command "bundle exec rake db:seed && touch #{node[:deploy_path]}/db.seeded"
      not_if { ::File.exists?("#{node[:deploy_path]}/db.seeded") }
    end
    

    This makes sure a setup step only happens once.