Skip to content

Instantly share code, notes, and snippets.

@itsNikolay
Created February 5, 2012 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsNikolay/1746250 to your computer and use it in GitHub Desktop.
Save itsNikolay/1746250 to your computer and use it in GitHub Desktop.
# config/deploy.rb
require "bundler/capistrano"
set :scm, :git
set :repository, "git@github.com:itsNikolay/demoapp.git"
set :branch, "origin/master"
set :migrate_target, :current
set :ssh_options, { :forward_agent => true }
set :rails_env, "production"
set :deploy_to, "/vagrant/demoapp"
set :normalize_asset_timestamps, false
set :user, "vagrant"
set :use_sudo, true
set :password, "vagrant"
role :web, "33.33.33.10"
role :app, "33.33.33.10"
role :db, "33.33.33.10", :primary => true
set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
default_environment["RAILS_ENV"] = 'production'
# Use our ruby-1.9.2-p290@my_site gemset
#default_environment["PATH"] = "--"
#default_environment["GEM_HOME"] = "--"
#default_environment["GEM_PATH"] = "--"
#default_environment["RUBY_VERSION"] = "ruby-1.9.2-p290"
default_run_options[:shell] = 'bash'
namespace :deploy do
desc "Deploy your application"
task :default do
update
restart
end
desc "Setup your git-based deployment app"
task :setup, :except => { :no_release => true } do
dirs = [deploy_to, shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
run "git clone #{repository} #{current_path}"
end
task :cold do
update
migrate
end
task :update do
transaction do
update_code
end
end
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
finalize_update
end
desc "Update the database (overwritten to avoid symlink)"
task :migrations do
transaction do
update_code
end
migrate
restart
end
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
# mkdir -p is making sure that the directories are there for some SCM's that don't
# save empty folders
run <<-CMD
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
mkdir -p #{latest_release}/public &&
mkdir -p #{latest_release}/tmp &&
ln -s #{shared_path}/log #{latest_release}/log &&
ln -s #{shared_path}/system #{latest_release}/public/system &&
ln -s #{shared_path}/pids #{latest_release}/tmp/pids &&
ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml
CMD
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
end
end
desc "Zero-downtime restart of Unicorn"
task :restart, :except => { :no_release => true } do
run "kill -s USR2 `cat /tmp/unicorn.todo.pid`"
end
desc "Start unicorn"
task :start, :except => { :no_release => true } do
run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
end
desc "Stop unicorn"
task :stop, :except => { :no_release => true } do
run "kill -s QUIT `cat /tmp/unicorn.todo.pid`"
end
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => { :no_release => true } do
set :branch, "HEAD@{1}"
deploy.default
end
desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
task :cleanup, :except => { :no_release => true } do
run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
end
desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end
end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end
itsnikolay@itsnikolay-VirtualBox:~/vagrant/1vagrant/projects/demoapp$ cap deploy * executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
* executing "cd /vagrant/demoapp/current; git fetch origin; git reset --hard origin/master"
servers: ["33.33.33.10"]
[33.33.33.10] executing command
** [out :: 33.33.33.10] HEAD is now at 5347b5a first commit
command finished in 5800ms
* executing `deploy:finalize_update'
* executing "chmod -R g+w /vagrant/demoapp/current"
servers: ["33.33.33.10"]
[33.33.33.10] executing command
command finished in 738ms
* executing "rm -rf /vagrant/demoapp/current/log /vagrant/demoapp/current/public/system /vagrant/demoapp/current/tmp/pids &&\\\n mkdir -p /vagrant/demoapp/current/public &&\\\n mkdir -p /vagrant/demoapp/current/tmp &&\\\n ln -s /vagrant/demoapp/shared/log /vagrant/demoapp/current/log &&\\\n ln -s /vagrant/demoapp/shared/system /vagrant/demoapp/current/public/system &&\\\n ln -s /vagrant/demoapp/shared/pids /vagrant/demoapp/current/tmp/pids &&\\\n ln -sf /vagrant/demoapp/shared/database.yml /vagrant/demoapp/current/config/database.yml"
servers: ["33.33.33.10"]
[33.33.33.10] executing command
*** [err :: 33.33.33.10] ln:
*** [err :: 33.33.33.10] creating symbolic link `/vagrant/demoapp/current/log'
*** [err :: 33.33.33.10] : Read-only file system
*** [err :: 33.33.33.10]
command finished in 209ms
failed: "env RAILS_ENV=production bash -c 'rm -rf /vagrant/demoapp/current/log /vagrant/demoapp/current/public/system /vagrant/demoapp/current/tmp/pids &&\\\n mkdir -p /vagrant/demoapp/current/public &&\\\n mkdir -p /vagrant/demoapp/current/tmp &&\\\n ln -s /vagrant/demoapp/shared/log /vagrant/demoapp/current/log &&\\\n ln -s /vagrant/demoapp/shared/system /vagrant/demoapp/current/public/system &&\\\n ln -s /vagrant/demoapp/shared/pids /vagrant/demoapp/current/tmp/pids &&\\\n ln -sf /vagrant/demoapp/shared/database.yml /vagrant/demoapp/current/config/database.yml'" on 33.33.33.10
itsnikolay@itsnikolay-VirtualBox:~/vagrant/1vagrant/projects/demoapp$ cap deploy * executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
* executing "cd /vagrant/demoapp/current; git fetch origin; git reset --hard origin/master"
servers: ["33.33.33.10"]
[33.33.33.10] executing command
** [out :: 33.33.33.10] HEAD is now at 5347b5a first commit
command finished in 6339ms
* executing `deploy:finalize_update'
* executing "chmod -R g+w /vagrant/demoapp/current"
servers: ["33.33.33.10"]
[33.33.33.10] executing command
command finished in 916ms
* executing "rm -rf /vagrant/demoapp/current/log /vagrant/demoapp/current/public/system /vagrant/demoapp/current/tmp/pids &&\\\n mkdir -p /vagrant/demoapp/current/public &&\\\n mkdir -p /vagrant/demoapp/current/tmp &&\\\n ln -s /vagrant/demoapp/shared/log /vagrant/demoapp/current/log &&\\\n ln -s /vagrant/demoapp/shared/system /vagrant/demoapp/current/public/system &&\\\n ln -s /vagrant/demoapp/shared/pids /vagrant/demoapp/current/tmp/pids &&\\\n ln -sf /vagrant/demoapp/shared/database.yml /vagrant/demoapp/current/config/database.yml"
servers: ["33.33.33.10"]
[33.33.33.10] executing command
*** [err :: 33.33.33.10] ln:
*** [err :: 33.33.33.10] creating symbolic link `/vagrant/demoapp/current/log'
*** [err :: 33.33.33.10] : Read-only file system
*** [err :: 33.33.33.10]
command finished in 198ms
failed: "env RAILS_ENV=production bash -c 'rm -rf /vagrant/demoapp/current/log /vagrant/demoapp/current/public/system /vagrant/demoapp/current/tmp/pids &&\\\n mkdir -p /vagrant/demoapp/current/public &&\\\n mkdir -p /vagrant/demoapp/current/tmp &&\\\n ln -s /vagrant/demoapp/shared/log /vagrant/demoapp/current/log &&\\\n ln -s /vagrant/demoapp/shared/system /vagrant/demoapp/current/public/system &&\\\n ln -s /vagrant/demoapp/shared/pids /vagrant/demoapp/current/tmp/pids &&\\\n ln -sf /vagrant/demoapp/shared/database.yml /vagrant/demoapp/current/config/database.yml'" on 33.33.33.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment