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 ?

- 1,240
- 1
- 15
- 27
2 Answers
Try with after_party gem. This way run many rake task (you say which ones) like migrations, means don't execute twice. Then you must keep in mind that if you want change something executed previously by one of this rake task you must create a new one (like migrations)
For seeds purposes you can try with seed_fu. Using Constraints technique. This is, I believe, the correct for seeds, but have the seed_fu syntax.
Some people puts seeds or data in migration (I don't like). Look at this question/answer. This is, I believe, the worst merge seeds with migration is not good idea. Seed changing in time means new migrations and Seeds changing by environment is almost impossible.
-
Thanks for these tips ! I would prefer not to touch the app itself though and move the logic over to the deployment recipe. – Emil Apr 03 '14 at 06:36
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.

- 1,240
- 1
- 15
- 27
-
How do you do when need a new record for your seed. ex. new admin_user, country, etc?. Go to remove #{node[:deploy_path]}/db.seeded file and chef cook againg? – Montells Apr 03 '14 at 14:32
-
Then it's no more a one-time setup activity. This could be tied to migrations in some way, IMO. My concern was more of steps that need to take place once and only once. – Emil Apr 03 '14 at 14:43