capistrano changes

This commit is contained in:
jomo
2015-03-23 00:47:24 +01:00
parent b1e32fdedd
commit 2237a5f510
7 changed files with 109 additions and 150 deletions

15
Gemfile
View File

@@ -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'

View File

@@ -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

View File

@@ -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'
set :bundle_without, %w{development test}.join(' ')
# 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
%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
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"
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

View File

@@ -0,0 +1,4 @@
set :application, 'redstoner'
set :branch, 'master'
role :web, %w{www-data@redstoner}

View File

@@ -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"
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
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')
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

View File

@@ -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

View File

@@ -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