load 'deploy'
# ================================================================
# ROLES
# ================================================================
role :app, "ec2-23-23-24-42.compute-1.amazonaws.com"
# ================================================================
# VARIABLES
# ================================================================
# Webistrano defaults
set :webistrano_project, "sweet_high"
set :webistrano_stage, "staging"
set :application, ""
set :branch, "ec2"
set :bundle_disable_shared_gems, "\"1\""
set :bundle_path, "vendor/bundler_gems"
set :default_shell, "bash --login"
set :deploy_to, "/data/\#{application}"
set :deploy_via, :remote_cache
set :domain, ""
set :environment, "staging"
set :group, "ubuntu"
set :nginx_conf_path, "/etc/nginx/sites-enabled"
set :password, "deployment_user(SSH user) password"
set :rails_env, "staging"
set :repository, ""
set :runner, "ubuntu"
set :rvm_bin_path, "/home/#{user}/.rvm/bin/rvm"
set :rvm_type, :user
set :scm, :git
set :scm_password, "your_SVN_password"
set :scm_username, "your_SVN_user"
set :ssh_options, {:forward_agent => true,:paranoid => false,:keys => ["/home/#{user}/.ssh/rails_app_server.pem"]}
set :use_sudo, true
set :user, "ubuntu"
# ================================================================
# TEMPLATE TASKS
# ================================================================
# allocate a pty by default as some systems have problems without
default_run_options[:pty] = true
# set Net::SSH ssh options through normal variables
# at the moment only one SSH key is supported as arrays are not
# parsed correctly by Webistrano::Deployer.type_cast (they end up as strings)
[:ssh_port, :ssh_keys].each do |ssh_opt|
if exists? ssh_opt
logger.important("SSH options: setting #{ssh_opt} to: #{fetch(ssh_opt)}") ssh_options[ssh_opt.to_s.gsub(/ssh_/, '').to_sym] = fetch(ssh_opt)
end
end
# ================================================================
# CUSTOM RECIPES
# ================================================================
before "deploy:setup", :init_config
after "deploy:setup", :setup_cleanup
namespace :init_config do
desc <<-DESC
Created directory structure, changes ownership, etc...
DESC
task :default do
run "gem install bundler"
run "sudo mkdir -p #{deploy_to}" run "sudo chown -R #{user}:#{group} #{deploy_to}" run "mkdir -p #{shared_path}/config" run "mkdir -p #{shared_path}/config" File.open("/home/#{user}/.ssh/id_rsa", "rb").each_line{ |line| run "echo #{line} >> ~/.ssh/id_rsa" }
run "chown #{user}:#{group} ~/.ssh/id_rsa" run "chmod 600 ~/.ssh/id_rsa"
end
end
namespace :setup_cleanup do
task :default do
run "sudo chown -R #{user}:#{group} #{deploy_to}" end
end
namespace :deploy do
task :bundle, :roles => :app do
run "cd #{release_path} && mkdir -p .bundler" run "cd #{release_path}/.bundler && touch config" run "echo BUNDLE_PATH: #{bundle_path} >> #{release_path}/.bundler/config" run "echo BUNDLE_DISABLE_SHARED_GEMS: #{bundle_disable_shared_gems} >> #{release_path}/.bundler/config" run "cd #{release_path} && bundle install"
on_rollback do
if previous_release
run "cd #{previous_release} && bundle install" else
logger.important "no previous release to rollback to, rollback of bundler:install skipped"
end
end
end
task :bundle_new_release, :roles => :db do
deploy.bundle
end
desc <<-DESC
Restarts application
DESC
task :restart, :roles => :app do
run "cd #{current_path} && touch tmp/restart.txt" end
end
after "deploy:rollback:revision", "deploy:bundle"
after "deploy:update_code", "deploy:bundle_new_release"
after "deploy:setup", :nginx
after :nginx, "nginx:restart"
namespace :nginx do
desc "Create nginx.conf and generates nginx.conf symlink"
task :default, :roles => :app do
config_content = from_template("config/nginx.#{environment}.conf.erb") put config_content, "#{shared_path}/config/nginx.conf" run "sudo ln -nfs #{shared_path}/config/nginx.conf #{nginx_conf_path}/000-default" end
task :restart, :roles => :app do
run "sudo /etc/init.d/nginx restart"
end
end
def get_binding
binding # So that everything can be used in templates generated for the servers
end
def from_template(file)
require 'erb'
template = File.read(File.join(RAILS_ROOT, "/", file))
result = ERB.new(template).result(self.get_binding)
end