diff options
author | jomo <github@jomo.tv> | 2015-03-23 00:47:24 +0100 |
---|---|---|
committer | jomo <github@jomo.tv> | 2015-03-23 00:50:13 +0100 |
commit | 2237a5f510e57d027739eca844b06da38791562b (patch) | |
tree | 66952b8d6b1804cdadb2398426f3cd6f5147a355 | |
parent | b1e32fdedd72e79f921c16e885d8f1787ccb6d9a (diff) |
capistrano changes
-rw-r--r-- | Gemfile | 15 | ||||
-rw-r--r-- | Gemfile.lock | 35 | ||||
-rw-r--r-- | config/deploy.rb | 40 | ||||
-rw-r--r-- | config/deploy/production.rb | 4 | ||||
-rw-r--r-- | config/unicorn.rb | 39 | ||||
-rwxr-xr-x | config/unicorn_init.sh | 88 | ||||
-rw-r--r-- | lib/capistrano/tasks/unicorn.rake | 40 |
7 files changed, 110 insertions, 151 deletions
@@ -28,16 +28,11 @@ group :development do gem 'better_errors' gem 'binding_of_caller' gem 'rails-erd' + # deploy with capistrano + gem 'capistrano-rails', '~> 1.1.2' + gem 'capistrano-rbenv', '~> 2.0' + gem 'capistrano-bundler', '~> 1.1.3' end -# To use Jbuilder templates for JSON -# gem 'jbuilder' - # Use unicorn as the app server -gem 'unicorn' - -# Deploy with Capistrano -gem 'capistrano' - -# To use debugger -# gem 'debugger' +gem 'unicorn'
\ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 022c07d..44289d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,12 +66,19 @@ GEM binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) - capistrano (2.15.5) - highline - net-scp (>= 1.0.0) - net-sftp (>= 2.0.0) - net-ssh (>= 2.0.14) - net-ssh-gateway (>= 1.1.0) + capistrano (3.4.0) + i18n + rake (>= 10.0.0) + sshkit (~> 1.3) + capistrano-bundler (1.1.4) + capistrano (~> 3.1) + sshkit (~> 1.2) + capistrano-rails (1.1.2) + capistrano (~> 3.1) + capistrano-bundler (~> 1.1) + capistrano-rbenv (2.0.3) + capistrano (~> 3.1) + sshkit (~> 1.3) choice (0.1.6) coderay (1.1.0) coffee-rails (4.0.1) @@ -81,10 +88,10 @@ GEM coffee-script-source execjs coffee-script-source (1.7.0) + colorize (0.7.5) debug_inspector (0.0.2) erubis (2.7.0) execjs (2.0.2) - highline (1.6.21) hike (1.2.3) hirb (0.7.1) i18n (0.6.9) @@ -103,11 +110,7 @@ GEM mysql2 (0.3.15) net-scp (1.2.1) net-ssh (>= 2.6.5) - net-sftp (2.1.2) - net-ssh (>= 2.6.5) - net-ssh (2.9.1) - net-ssh-gateway (1.2.0) - net-ssh (>= 2.6.5) + net-ssh (2.9.2) nokogiri (1.6.1) mini_portile (~> 0.5.0) polyglot (0.3.4) @@ -158,6 +161,10 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (~> 2.8) + sshkit (1.7.1) + colorize (>= 0.7.0) + net-scp (>= 1.1.2) + net-ssh (>= 2.8.0) thor (0.19.1) thread_safe (0.3.3) tilt (1.4.1) @@ -183,7 +190,9 @@ DEPENDENCIES bcrypt-ruby better_errors binding_of_caller - capistrano + capistrano-bundler (~> 1.1.3) + capistrano-rails (~> 1.1.2) + capistrano-rbenv (~> 2.0) coffee-rails highlight_js-rails! hirb diff --git a/config/deploy.rb b/config/deploy.rb index f4aaa5f..28d9457 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -1,38 +1,24 @@ -require "bundler/capistrano" +# config valid only for current version of Capistrano +lock '3.4.0' -server "redstoner", :web, :app, :db, primary: true +set :repo_url, 'git@bitbucket.org:redstonesheep/redstoner.git' -set :application, "redstoner" -set :user, "www-data" -set :deploy_to, "/home/#{user}/apps/#{application}" -set :deploy_via, :remote_cache -set :use_sudo, false +set :scm, :git -set :scm, "git" -set :repository, "ssh://git@bitbucket.org/redstonesheep/redstoner.git" -set :branch, "master" +set :ssh_options, { forward_agent: true } +set :keep_releases, 5 -default_run_options[:pty] = true -ssh_options[:forward_agent] = true +set :deploy_to, -> { "/home/www-data/apps/#{fetch(:application)}" } -after "deploy", "deploy:cleanup" # keep only the last 5 releases +set :rbenv_ruby, '2.0.0-p247' -namespace :deploy do - %w[start stop restart].each do |command| - desc "#{command} unicorn server" - task command, roles: :app, except: {no_release: true} do - run "/etc/init.d/unicorn_#{application} #{command}" - end - end +set :bundle_without, %w{development test}.join(' ') - task :setup_config, roles: :app do - puts "Please run as root: 'ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}'" - puts "Please run as root: 'ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}'" - run "mkdir -p #{shared_path}/config" - end - after "deploy:setup", "deploy:setup_config" +# Default value for linked_dirs is [] +set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') +namespace :deploy do desc "Make sure local git is in sync with remote." task :check_revision, roles: :web do unless `git rev-parse HEAD` == `git rev-parse origin/master` @@ -42,4 +28,6 @@ namespace :deploy do end end before "deploy", "deploy:check_revision" + + after :publishing, :restart end
\ No newline at end of file diff --git a/config/deploy/production.rb b/config/deploy/production.rb new file mode 100644 index 0000000..627751c --- /dev/null +++ b/config/deploy/production.rb @@ -0,0 +1,4 @@ +set :application, 'redstoner' +set :branch, 'master' + +role :web, %w{www-data@redstoner}
\ No newline at end of file diff --git a/config/unicorn.rb b/config/unicorn.rb index 2067b63..cebbcde 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -1,15 +1,26 @@ -root = "/home/www-data/apps/redstoner/current" -working_directory root -pid "#{root}/tmp/pids/unicorn.pid" -stderr_path "#{root}/log/unicorn.log" -stdout_path "#{root}/log/unicorn.log" - -listen "/tmp/unicorn.redstoner.sock" -worker_processes 2 -timeout 30 - -# Force the bundler gemfile environment variable to -# reference the capistrano "current" symlink -before_exec do |_| - ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile') +worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) +timeout 15 +preload_app true + + +stderr_path "/home/www-data/apps/redstoner/shared/log/unicorn.stderr.log" +stdout_path "/home/www-data/apps/redstoner/shared/log/unicorn.stdout.log" + +before_fork do |server, worker| + Signal.trap 'TERM' do + puts 'Unicorn master intercepting TERM and sending myself QUIT instead' + Process.kill 'QUIT', Process.pid + end + + defined?(ActiveRecord::Base) and + ActiveRecord::Base.connection.disconnect! +end + +after_fork do |server, worker| + Signal.trap 'TERM' do + puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' + end + + defined?(ActiveRecord::Base) and + ActiveRecord::Base.establish_connection end
\ No newline at end of file diff --git a/config/unicorn_init.sh b/config/unicorn_init.sh deleted file mode 100755 index 25d4f13..0000000 --- a/config/unicorn_init.sh +++ /dev/null @@ -1,88 +0,0 @@ -#!/bin/sh -### BEGIN INIT INFO -# Provides: unicorn -# Required-Start: $remote_fs $syslog -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Manage unicorn server -# Description: Start, stop, restart unicorn server for a specific application. -### END INIT INFO -set -e - -# not sure why, but .bash_profile is not being executed -export PATH="$HOME/.rbenv/bin:$PATH" -eval "$(rbenv init -)" - -# Feel free to change any of the following variables for your app: -TIMEOUT="${TIMEOUT-60}" -APP_ROOT="/home/www-data/apps/redstoner/current" -PID="$APP_ROOT/tmp/pids/unicorn.pid" -CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production" -AS_USER="www-data" -set -u - -OLD_PIN="$PID.oldbin" - -sig () { - test -s "$PID" && kill -$1 `cat $PID` -} - -oldsig () { - test -s $OLD_PIN && kill -$1 `cat $OLD_PIN` -} - -run () { - if [ "$(id -un)" = "$AS_USER" ]; then - eval $1 - else - su -c "$1" - $AS_USER - fi -} - -case "$1" in -start) - sig 0 && echo >&2 "Already running" && exit 0 - run "$CMD" - ;; -stop) - sig QUIT && exit 0 - echo >&2 "Not running" - ;; -force-stop) - sig TERM && exit 0 - echo >&2 "Not running" - ;; -restart|reload) - sig HUP && echo reloaded OK && exit 0 - echo >&2 "Couldn't reload, starting '$CMD' instead" - run "$CMD" - ;; -upgrade) - if sig USR2 && sleep 2 && sig 0 && oldsig QUIT - then - n=$TIMEOUT - while test -s $OLD_PIN && test $n -ge 0 - do - printf '.' && sleep 1 && n=$(( $n - 1 )) - done - echo - - if test $n -lt 0 && test -s $OLD_PIN - then - echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds" - exit 1 - fi - exit 0 - fi - echo >&2 "Couldn't upgrade, starting '$CMD' instead" - run "$CMD" - ;; -reopen-logs) - sig USR1 - ;; -*) - echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>" - exit 1 - ;; -esac
\ No newline at end of file diff --git a/lib/capistrano/tasks/unicorn.rake b/lib/capistrano/tasks/unicorn.rake new file mode 100644 index 0000000..6d82d84 --- /dev/null +++ b/lib/capistrano/tasks/unicorn.rake @@ -0,0 +1,40 @@ +namespace :deploy do + after :start, :start_unicorn do + invoke 'unicorn:start' + end + + after :stop, :stop_unicorn do + invoke 'unicorn:stop' + end + + after :restart, :restart_unicorn do + begin + invoke "unicorn:restart" + rescue SSHKit::Runner::ExecuteError + invoke "unicorn:start" + end + end +end + +namespace :unicorn do + desc "Start Unicorn" + task :start do + on roles(:web) do + execute :sudo, :start, "unicorn" + end + end + + desc "Stop Unicorn" + task :stop do + on roles(:web) do + execute :sudo, :stop, "unicorn" + end + end + + desc "Restart Unicorn" + task :restart do + on roles(:web), in: :sequence, wait: 5 do + execute :sudo, :restart, "unicorn" + end + end +end
\ No newline at end of file |