Capistrano tasks for Magento

Custom tasks for Capistrano that I am using to help manage a Magento website.

set :linked_files, %w{app/etc/local.xml .htaccess robots.txt}
set :linked_dirs, %w{sitemap var media}

namespace :mage do 

  task :restart do
    on roles(:app) do
      execute "cd #{current_path} && rm -f maintenance.flag"
    end
  end

  task :disable do 
    on roles(:app) do
      execute "cd #{current_path} && touch maintenance.flag"
    end
  end

  task :enable do 
    on roles(:app) do
      execute "cd #{current_path} && rm -f maintenance.flag"
    end
  end

  task :clear_cache do 
    on roles(:app) do
      execute "cd #{shared_path} && rm -rf var/cache/*"
    end
  end

  task :reindex do 
    on roles(:app) do
      execute "cd #{release_path}/shell && php -f indexer.php -- reindexall"
    end
  end

  task :create_config do  
    on roles(:app) do
      execute :touch, shared_path.join('app/etc/local.xml')
      execute :touch, shared_path.join('robots.txt')
    end
  end
end


before 'deploy:check:linked_files', 'mage:create_config'
after 'deploy:restart', 'mage:clear_cache'

Save this as mage.cap and put it under lib/capistrano/tasks/. Then in your Capfile add the following to load your custom tasks.

Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }

Now you can reindex the site, clear caches, put site into maintenance mode.

cap production mage:reindex
cap production mage:clear_cache