require 'yaml' require 'net/sftp' default_run_options[:pty] = true set :application, "RAILS_APP_NAME" set :domain, "DOMAIN" set :deploy_to, "/home/deploy/apps/#{application}" set :runner, "DEPLOY_USER" set :user, "DEPLOY_USER" set :use_sudo, false set :repository, "GIT_REPO" set :scm, :git set :scm_passphrase, "GIT_REPO_PASSPHRASE" set :scm_verbose, true set :ssh_options, { :forward_agent => true } set :branch , "master" set :deploy_via, :remote_cache # set :git_enable_submodules, 1 # set :git_shallow_clone, 1 role :app, "DOMAIN" role :web, "DOMAIN" role :db, "DOMAIN", :primary => true namespace :deploy do after "deploy:update_code", "deploy:copy_production_configuration" after "deploy:copy_production_configuration", "deploy:symlink_vendor_plugins" before "deploy:migrate", "deploy:create_production_database" after "deploy:restart", "deploy:backgroundrb_restart" configurations = { "database.yml" => " #{shared_path}/config/database.yml", "facebooker.yml" => " #{shared_path}/config/facebooker.yml", "memcached.yml" => " #{shared_path}/config/memcached.yml" } desc "Copy production configuration files stored on the same remote server" task :copy_production_configuration do configurations.each_pair do |file, src| run "cp #{src} #{release_path}/config/#{file}" end end desc "Create the production database on the remote server if it doesn't already exist" task :create_production_database do Net::SFTP.start('DEPLOY_HOST', 'DEPLOY_USER') do |sftp| database_yml = YAML::load(sftp.file.open("#{release_path}/config/database.yml", "r")) production_database = database_yml["production"]["database"] run "mysqladmin create #{production_database} --default-character-set=utf8" rescue Capistrano::CommandError end end desc "symlink vendor/plugins" task :symlink_vendor_plugins do run "rm -rf #{release_path}/vendor/plugins" run "ln -fs #{shared_path}/plugins #{release_path}/vendor/plugins" end desc "Restarts your application." task :restart, :roles => :app do run "touch #{current_path}/tmp/restart.txt" end desc "Stop the BackgrounDRB server" task :backgroundrb_stop, :roles => :app do run "cd #{current_path} && ./script/backgroundrb stop -e production" rescue Capistrano::CommandError end desc "Start the BackgrounDRB server" task :backgroundrb_start, :roles => :app do run "cd #{current_path} && nohup ./script/backgroundrb start -e production" rescue Capistrano::CommandError end desc "Restart the BackgrounDRB server" task :backgroundrb_restart, :roles => :app do backgroundrb_stop backgroundrb_start end namespace :web do desc "Serve up a custom maintenance page." task :disable, :roles => :web do require 'erb' on_rollback { run "rm #{shared_path}/system/maintenance.html" } reason = ENV['REASON'] deadline = ENV['UNTIL'] template = File.read("app/views/admin/maintenance.html.erb") page = ERB.new(template).result(binding) put page, "#{shared_path}/system/maintenance.html", :mode => 0644 end end end