Hailwood
Hailwood

Reputation: 92661

Recipe to run on deployment and standalone

I have a recipe below (migrate.rb) which is run as part of our deployment and works perfectly.

However one thing that I can't workout is how to set it up so it can also be run as a standalone recipe in the execute_recipe command.

As it stands if we execute this recipe as a stand alone then nothing happens since the node[:deploy].each has nothing to loop over (the deploy key doesn't exist)..

The only part that actually relies on the deploy node is this line cwd "#{deploy[:deploy_to]}/current" since I need to know where the code was deployed to.

node[:deploy].each do |application, deploy|

  execute 'DB migrate then seed' do
    cwd "#{deploy[:deploy_to]}/current"
    command 'php artisan migrate; while read -r line || [ -n "$line" ]; do php artisan db:seed --class="$line"; done < "app/database/run.list"'
  end

end

Upvotes: 0

Views: 85

Answers (1)

Florian Holzhauer
Florian Holzhauer

Reputation: 703

I would relocate that part to a definition (or a provider). So basically split your recipe in two parts:

recipes/deploy.rb:

node[:deploy].each do |application, deploy|
  php_artisan_setup do
    dir "#{deploy[:deploy_to]}/current"
  end
end

definitions/php_artisan_setup.rb:

define :php_artisan_setup do
  execute 'DB migrate then seed' do
    cwd params[:dir]
    command 'php artisan migrate; while read -r line || [ -n "$line" ]; do php artisan db:seed --class="$line"; done < "app/database/run.list"'
  end
end

This way, you can call php_artisan_setup from your "standalone" recipe, too. You still need two recipes, but you dont have to duplicate the relevant part.

Upvotes: 2

Related Questions