require 'yaml' require 'net/sftp' default_run_options[:pty] = true set :application, "RAILS_APP_NAME" set :domain, "DOMAIN" set :deploy_to, "/mnt/app" 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 set :rails_env, "production" role :app, "EC2_HOSTNAME" role :web, "EC2_HOSTNAME" role :db, "EC2_HOSTNAME", :primary => true role :memcache, "EC2_HOSTNAME" # EC2 on Rails config. # NOTE: Some of these should be omitted if not needed. set :ec2onrails_config, { # S3 bucket and "subdir" used by the ec2onrails:db:restore task :restore_from_bucket => "S3_BUCKET", :restore_from_bucket_subdir => "database", # S3 bucket and "subdir" used by the ec2onrails:db:archive task # This does not affect the automatic backup of your MySQL db to S3, it's # just for manually archiving a db snapshot to a different bucket if # desired. :archive_to_bucket => "OTHER_S3_BUCKET", :archive_to_bucket_subdir => "db-archive/#{Time.new.strftime('%Y-%m-%d--%H-%M-%S')}", # Set a root password for MySQL. Run "cap ec2onrails:db:set_root_password" # to enable this. This is optional, and after doing this the # ec2onrails:db:drop task won't work, but be aware that MySQL accepts # connections on the public network interface (you should block the MySQL # port with the firewall anyway). # If you don't care about setting the mysql root password then remove this. # :mysql_root_password => "your-mysql-root-password", # Any extra Ubuntu packages to install if desired # If you don't want to install extra packages then remove this. # :packages => ["logwatch", "imagemagick"], # Any extra RubyGems to install if desired: can be "gemname" or if a # particular version is desired "gemname -v 1.0.1" # If you don't want to install extra rubygems then remove this # :rubygems => ["rmagick", "rfacebook -v 0.9.7"], # Set the server timezone. run "cap -e ec2onrails:server:set_timezone" for # details :timezone => "US/Eastern", # Files to deploy to the server (they'll be owned by root). It's intended # mainly for customized config files for new packages installed via the # ec2onrails:server:install_packages task. Subdirectories and files inside # here will be placed in the same structure relative to the root of the # server's filesystem. # If you don't need to deploy customized config files to the server then # remove this. # :server_config_files_root => "../server_config", # If config files are deployed, some services might need to be restarted. # If you don't need to deploy customized config files to the server then # remove this. # :services_to_restart => %w(apache2 postfix sysklogd), # Set an email address to forward admin mail messages to. If you don't # want to receive mail from the server (e.g. monit alert messages) then # remove this. :admin_mail_forward_address => "admin@DOMAIN", # Set this if you want SSL to be enabled on the web server. The SSL cert # and key files need to exist on the server, The cert file should be in # /etc/ssl/certs/default.pem and the key file should be in # /etc/ssl/private/default.key (see :server_config_files_root). # :enable_ssl => true } namespace :ec2onrails do namespace :db do after "ec2onrails:db:create", "ec2onrails:db:make_utf8" desc "Change DB to use UTF-8" task :make_utf8 , :roles => :db do run "mysql PRODUCTION_DATABASE -u root -e 'ALTER DATABASE PRODUCTION_DATABASE CHARACTER SET utf8'" end end end 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, :roles => :app 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, :roles => :db do Net::SFTP.start('butler', 'deploy') 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, :roles => :app 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