Skip to content

Instantly share code, notes, and snippets.

@scottkf
Created December 18, 2012 17:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottkf/4330261 to your computer and use it in GitHub Desktop.
Save scottkf/4330261 to your computer and use it in GitHub Desktop.
Puma capistrano deployment script, w/init.d script and nginx conf
require "bundler/capistrano"
server "server", :web, :app, :db, primary: true
set :application, "<application>"
set :user, "<user>"
set :group, "wheel"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git@github.com:scottkf"
set :branch, 'puma'
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} puma server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/puma #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/puma_init.sh /etc/init.d/puma"
sudo "chmod +x /etc/init.d/puma"
run "mkdir -p #{shared_path}/config"
run "mkdir -p #{shared_path}/sockets"
run "mkdir -p #{shared_path}/pids"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/config/puma.rb #{release_path}/config/puma.rb"
end
after "deploy:finalize_update", "deploy:symlink_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 bitbucket/#{branch}`
puts "WARNING: HEAD is not the same as bitbucket/#{branch}"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
upstream puma {
server unix:/home/<user>/apps/<app>/shared/sockets/puma.sock;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/<user>/apps/<app>/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @puma;
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
#!/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
# Feel free to change any of the following variables for your app:
APP_ROOT=/home/<user>/apps/<app>/
CMD="cd $APP_ROOT/current; bundle exec puma -C $APP_ROOT/shared/config/puma.rb -b unix://$APP_ROOT/shared/sockets/puma.sock -e production --control unix://$APP_ROOT/shared/sockets/pumactl.sock --state $APP_ROOT/shared/sockets/puma.state --pidfile $APP_ROOT/shared/pids/puma.pid 2>&1 >> $APP_ROOT/shared/log/puma.log &"
CTL="cd $APP_ROOT/current; bundle exec pumactl -S $APP_ROOT/shared/sockets/puma.state"
AS_USER=<user>
set -u
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
if [ ! -e "$APP_ROOT/shared/sockets/puma.sock" ]
then
run "$CMD"
exit 1
else
echo >&2 "Already running"
fi
;;
stop)
[ -e "$APP_ROOT/shared/sockets/puma.sock" ] && run "$CTL stop" && exit 0
echo >&2 "Not running"
;;
force-stop)
[ -e "$APP_ROOT/shared/sockets/puma.sock" ] && run "$CTL halt" && exit 0
echo >&2 "Not running"
;;
restart|reload)
[ -e "$APP_ROOT/shared/sockets/puma.sock" ] && run "$CTL restart" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|force-stop>"
exit 1
;;
esac
@mfrederickson
Copy link

@scottkf How is this working sudo "ln -nfs #{current_path}/config/puma_init.sh /etc/init.d/puma" [config-deploy L30] when the code doesn't exist on the server yet because you are running this right after the deploy:setup?

@mfrederickson
Copy link

And on line 25, you can run that without sudo? And where do you ever run the update-rc.d on the service script?

@mfrederickson
Copy link

Weird. I'm kinda new to cap, but the symlink got created but it failed on the chmod with
chmod: cannot operate on dangling symlink /etc/init.d/servicename...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment