PageRenderTime 63ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/config/deploy.rb

https://bitbucket.org/ktadokoro/2ch_summary
Ruby | 99 lines | 72 code | 18 blank | 9 comment | 5 complexity | 6ea5493d1c7e1b24331fa30cbbcc215d MD5 | raw file
  1. # capistranoの出力がカラーになる
  2. require 'capistrano_colors'
  3. # deploy 時に自動で bundle install
  4. require 'bundler/capistrano'
  5. # deploy 時に自動で rake assets:precompile
  6. #load 'deploy/assets'
  7. set :default_environment, {
  8. 'RBENV_ROOT' => '$HOME/.rbenv',
  9. 'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
  10. }
  11. set :bundle_flags, "--quiet --binstubs --shebang ruby-local-exec"
  12. # SSHの設定
  13. set :user, "webapp"
  14. ssh_options[:port] = "22"
  15. ssh_options[:forward_agent] = true
  16. default_run_options[:pty] = true
  17. # アプリケーションの設定
  18. set :application, '2ch_summary'
  19. set :domain, 'www13399ui.sakura.ne.jp'
  20. set :rails_env, 'production'
  21. set :repository, 'https://ktadokoro@bitbucket.org/ktadokoro/2ch_summary.git'
  22. set :branch, 'master'
  23. set :deploy_via, :copy
  24. set :deploy_to, '/var/webapp/2ch_summary'
  25. set :use_sudo, false
  26. server domain, :app, :web
  27. # 再起動後に古い releases cleanup
  28. after 'deploy:restart', 'deploy:cleanup'
  29. # Unicorn 用の設定
  30. set :unicorn_config, "#{current_path}/config/unicorn.rb"
  31. set :unicorn_bin, 'unicorn'
  32. set :unicorn_pid, '/var/run/unicorn/unicorn_2ch_summary.pid'
  33. # Unicorn 用のデーモン操作タスク
  34. def remote_file_exists?(full_path)
  35. capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip == 'true'
  36. end
  37. def process_exists?(pid_file)
  38. capture("ps -p `cat #{pid_file}`; true").strip.split("\n").size == 2
  39. end
  40. namespace :deploy do
  41. desc 'Start Unicorn'
  42. task :start, roles: :app, except: {no_release: true} do
  43. if remote_file_exists? unicorn_pid
  44. if process_exists? unicorn_pid
  45. logger.important 'Unicorn is already running!', 'Unicorn'
  46. next
  47. end
  48. end
  49. logger.important 'Starting Unicorn...', 'Unicorn'
  50. run "cd #{current_path} && bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
  51. end
  52. desc 'Stop Unicorn'
  53. task :stop, :roles => :app, :except => {:no_release => true} do
  54. if remote_file_exists? unicorn_pid
  55. if process_exists? unicorn_pid
  56. logger.important 'Stopping Unicorn...', 'Unicorn'
  57. run "kill -s QUIT `cat #{unicorn_pid}`"
  58. else
  59. run "rm #{unicorn_pid}"
  60. logger.important 'Unicorn is not running.', 'Unicorn'
  61. end
  62. else
  63. logger.important 'No PIDs found. Check if unicorn is running.', 'Unicorn'
  64. end
  65. end
  66. desc 'Reload Unicorn'
  67. task :reload, :roles => :app, :except => {:no_release => true} do
  68. if remote_file_exists? unicorn_pid
  69. logger.important 'Reloading Unicorn...', 'Unicorn'
  70. run "kill -s HUP `cat #{unicorn_pid}`"
  71. else
  72. logger.important 'No PIDs found. Starting Unicorn...', 'Unicorn'
  73. run "cd #{current_path} && bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
  74. end
  75. end
  76. desc 'Restart Unicorn'
  77. task :restart, :roles => :app, :except => {:no_release => true} do
  78. stop
  79. start
  80. end
  81. end