PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/config/deploy.rb

https://bitbucket.org/shinyay/rails_capistrano_sample
Ruby | 126 lines | 81 code | 23 blank | 22 comment | 5 complexity | 55b14e61a6580803b49328bdcfc261fb MD5 | raw file
  1. # capistranoの出力をカラーに
  2. require 'capistrano_colors'
  3. # cap deploy時に自動で bundle install が実行される
  4. require "bundler/capistrano"
  5. # sshでログインするユーザ
  6. set :user, "shinyay"
  7. # コマンド実行時にsudoをつけるか
  8. set :use_sudo, true
  9. # デプロイサーバ定義
  10. role :web, "sakura"
  11. # lsコマンド実行タスク
  12. task :list do
  13. run "ls"
  14. end
  15. # アプリケーション名
  16. set :application, "rails_capistrano_test"
  17. # subverion, git, mercurial, cvs, bzrなど
  18. set :scm ,:git
  19. #gitリポジトリ
  20. #set :repository, "git@bitbucket.org:shinyay/rails_capistrano_sample.git"
  21. set :repository, "https://shinyay@bitbucket.org/shinyay/rails_capistrano_sample.git"
  22. #gitブランチ名
  23. set :branch, "master"
  24. # どういう方式でデプロイするか
  25. # copy デプロイ元でソースを最新化してからデプロイ先にコピー
  26. # checkout デプロイ先に接続した後scmに応じたcheckoutコマンドを実行する
  27. set :deploy_via , :remote_cache
  28. # SSH
  29. set :use_sudo, true
  30. set :default_run_options, :pty => true
  31. ssh_options[:forward_agent] = true
  32. set :normalize_asset_timestamps, false
  33. # 過去のデプロイしたフォルダを履歴として保持する数
  34. set :keep_releases, 5
  35. set :rails_env, 'development'
  36. # deploy先ディレクトリ
  37. set :deploy_to, "/var/www/#{rails_env}/#{application}"
  38. # precompile
  39. load 'deploy/assets'
  40. # cap deploy:setup /var/www/sample の権限変更
  41. namespace :setup do
  42. task :fix_permissions do
  43. sudo "chown -R #{user}.#{user} #{deploy_to}"
  44. end
  45. end
  46. after "deploy:setup", "setup:fix_permissions"
  47. # 再起動後に古い releases cleanup
  48. after 'deploy:restart', 'deploy:cleanup'
  49. # Unicorn 用の設定
  50. set :unicorn_config, "#{current_path}/config/unicorn.rb"
  51. set :unicorn_bin, 'unicorn'
  52. set :unicorn_pid, '/tmp/unicorn.undersky.co.pid'
  53. # Unicorn 用のデーモン操作タスク
  54. def remote_file_exists?(full_path)
  55. capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip == 'true'
  56. end
  57. def process_exists?(pid_file)
  58. capture("ps -p `cat #{pid_file}`; true").strip.split("\n").size == 2
  59. end
  60. namespace :deploy do
  61. desc 'Start Unicorn'
  62. task :start, roles: :app, except: {no_release: true} do
  63. if remote_file_exists? unicorn_pid
  64. if process_exists? unicorn_pid
  65. logger.important 'Unicorn is already running!', 'Unicorn'
  66. next
  67. else
  68. run "rm #{unicorn_pid}"
  69. end
  70. end
  71. logger.important 'Starting Unicorn...', 'Unicorn'
  72. run "cd #{current_path} && bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
  73. end
  74. desc 'Stop Unicorn'
  75. task :stop, :roles => :app, :except => {:no_release => true} do
  76. if remote_file_exists? unicorn_pid
  77. if process_exists? unicorn_pid
  78. logger.important 'Stopping Unicorn...', 'Unicorn'
  79. run "kill -s QUIT `cat #{unicorn_pid}`"
  80. else
  81. run "rm #{unicorn_pid}"
  82. logger.important 'Unicorn is not running.', 'Unicorn'
  83. end
  84. else
  85. logger.important 'No PIDs found. Check if unicorn is running.', 'Unicorn'
  86. end
  87. end
  88. desc 'Reload Unicorn'
  89. task :reload, :roles => :app, :except => {:no_release => true} do
  90. if remote_file_exists? unicorn_pid
  91. logger.important 'Reloading Unicorn...', 'Unicorn'
  92. run "kill -s HUP `cat #{unicorn_pid}`"
  93. else
  94. logger.important 'No PIDs found. Starting Unicorn...', 'Unicorn'
  95. run "cd #{current_path} && bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
  96. end
  97. end
  98. desc 'Restart Unicorn'
  99. task :restart, :roles => :app, :except => {:no_release => true} do
  100. stop
  101. start
  102. end
  103. end