Pergunta

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 ?

Foi útil?

Solução 2

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.

Outras dicas

  1. 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)

  2. 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.

  3. 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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top