PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/config/deploy.rb

https://bitbucket.org/kvp/fusu.ma
Ruby | 140 lines | 112 code | 25 blank | 3 comment | 1 complexity | 947d58c69c9b472fa4ca88b46cd75729 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. require "bundler/capistrano"
  3. require "capistrano_colors"
  4. load "deploy/assets"
  5. set :application, "fusu.ma"
  6. set :repository, "fusuma.bitbucket.org:kvp/fusu.ma.git"
  7. set :user, "webmaster"
  8. set :scm, :git
  9. set :deploy_via, :copy
  10. set :use_sudo, true
  11. set :deploy_to, "/var/rails/#{application}"
  12. set :branch, "origin/master"
  13. set :rails_env, "production"
  14. set :normalize_asset_timestamps, false
  15. role :web, "54.248.117.75"
  16. role :app, "54.248.117.75"
  17. role :db, "54.248.117.75"
  18. set(:latest_release) { fetch(:current_path) }
  19. set(:release_path) { fetch(:current_path) }
  20. set(:current_release) { fetch(:current_path) }
  21. set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
  22. set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
  23. set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
  24. default_environment["RAILS_ENV"] = 'production'
  25. default_run_options[:shell] = 'bash'
  26. ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "fusuma_webmaster_rsa")]
  27. namespace :deploy do
  28. desc "Deploy your application"
  29. task :default do
  30. update
  31. run "#{try_sudo} cp /var/rails/fusu.ma/shared/application.yml /var/rails/fusu.ma/current/config/"
  32. run "#{try_sudo} cp /var/rails/fusu.ma/shared/mongoid.yml /var/rails/fusu.ma/current/config"
  33. restart
  34. end
  35. desc "Setup your git-based deployment app"
  36. task :setup, :except => { :no_release => true } do
  37. ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "fusuma_webmaster_rsa")]
  38. dirs = [deploy_to, shared_path]
  39. dirs += shared_children.map { |d| File.join(shared_path, d) }
  40. run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
  41. run "git clone #{repository} #{current_path}"
  42. end
  43. task :cold do
  44. update
  45. migrate
  46. end
  47. task :update do
  48. transaction do
  49. update_code
  50. end
  51. end
  52. desc "Update the deployed code."
  53. task :update_code, :except => { :no_release => true } do
  54. run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
  55. finalize_update
  56. end
  57. desc "Update the database (overwritten to avoid symlink)"
  58. task :migrations do
  59. transaction do
  60. update_code
  61. end
  62. migrate
  63. restart
  64. end
  65. task :finalize_update, :except => { :no_release => true } do
  66. run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
  67. # mkdir -p is making sure that the directories are there for some SCM's that don't
  68. # save empty folders
  69. run <<-CMD
  70. rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
  71. mkdir -p #{latest_release}/public &&
  72. mkdir -p #{latest_release}/tmp &&
  73. ln -s #{shared_path}/log #{latest_release}/log &&
  74. ln -s #{shared_path}/system #{latest_release}/public/system &&
  75. ln -s #{shared_path}/pids #{latest_release}/tmp/pids &&
  76. ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml
  77. CMD
  78. if fetch(:normalize_asset_timestamps, true)
  79. stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
  80. asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
  81. run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
  82. end
  83. end
  84. desc "Zero-downtime restart of Unicorn"
  85. task :restart, :except => { :no_release => true } do
  86. run "kill -s USR2 `cat /tmp/unicorn_fusu.ma.pid`"
  87. end
  88. desc "Start unicorn"
  89. task :start, :except => { :no_release => true } do
  90. run "cd #{current_path} ; #{try_sudo} bundle exec unicorn_rails -c config/unicorn.rb -D -E production -p 80"
  91. end
  92. desc "Stop unicorn"
  93. task :stop, :except => { :no_release => true } do
  94. sudo "kill -s QUIT `cat /tmp/unicorn_fusu.ma.pid`"
  95. end
  96. namespace :rollback do
  97. desc "Moves the repo back to the previous version of HEAD"
  98. task :repo, :except => { :no_release => true } do
  99. set :branch, "HEAD@{1}"
  100. deploy.default
  101. end
  102. desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
  103. task :cleanup, :except => { :no_release => true } do
  104. run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
  105. end
  106. desc "Rolls back to the previously deployed version."
  107. task :default do
  108. rollback.repo
  109. rollback.cleanup
  110. end
  111. end
  112. end
  113. def run_rake(cmd)
  114. run "cd #{current_path}; #{rake} #{cmd}"
  115. end