bin/puma.sh
[code language=”ruby”]
#! /bin/sh
PUMA_CONFIG_FILE=/home/user/repofolder/current/config/puma.rb
PUMA_PID_FILE=/home/user/repofolder/current/shared/pids/puma.pid
PUMA_SOCKET=/home/user/repofolder/current/shared/sockets/puma.sock
# check if puma process is running
puma_is_running() {
if [ -S $PUMA_SOCKET ] ; then
if [ -e $PUMA_PID_FILE ] ; then
if cat $PUMA_PID_FILE | xargs pgrep -P > /dev/null ; then
return 0
else
echo "No puma process found"
fi
else
echo "No puma pid file found"
fi
else
echo "No puma socket found"
fi
return 1
}
case "$1" in
start)
echo "Starting puma…"
rm -f $PUMA_SOCKET
if [ -e $PUMA_CONFIG_FILE ] ; then
bundle exec puma -C $PUMA_CONFIG_FILE
else
bundle exec puma
fi
echo "done"
;;
stop)
echo "Stopping puma…"
kill -s SIGTERM `cat $PUMA_PID_FILE`
rm -f $PUMA_PID_FILE
rm -f $PUMA_SOCKET
echo "done"
;;
restart)
if puma_is_running ; then
echo "Hot-restarting puma…"
kill -s SIGUSR2 `cat $PUMA_PID_FILE`
echo "Doublechecking the process restart…"
sleep 5
if puma_is_running ; then
echo "done"
exit 0
else
echo "Puma restart failed :/"
fi
fi
echo "Trying cold reboot"
bin/puma.sh start
;;
*)
echo "Usage: script/puma.sh {start|stop|restart}" >&2
;;
esac
[/code]
config/puma.rb
[code language=”ruby”]
# #!/usr/bin/env puma
#
environment ENV[‘RAILS_ENV’] || ‘production’
#
daemonize true
#
pidfile "#{Dir.pwd}/shared/pids/puma.pid"
stdout_redirect "#{Dir.pwd}/shared/log/stdout", "#{Dir.pwd}/shared/log/stderr"
#
threads 0, 16
#
bind "unix://#{Dir.pwd}/shared/sockets/puma.sock"
[/code]
Gemfile
[code language=”ruby”]
gem ‘mina’
[/code]
config/deploy.rb
[code language=”ruby”]
require ‘mina/bundler’
require ‘mina/rails’
require ‘mina/git’
# require ‘mina/rbenv’ # for rbenv support. (http://rbenv.org)
require ‘mina/rvm’ # for rvm support. (http://rvm.io)
set :domain, ‘domain_here’
set :repository, ‘[email protected]:errakeshpd/myrepo.git’
set :branch, ‘master’
set :user, ‘user’
set :folder, ‘repofolder’
set :server, ‘IP ADDRESS HERE’
set :deploy_to, "/home/#{user}/#{folder}"
set :app_path, lambda { "#{deploy_to}/#{current_path}" }
set :env, ENV[‘RAILS_ENV’] || ‘production’
set :shared_paths, [‘config/database.yml’, ‘config/secrets.yml’, ‘log’]
set :forward_agent, true
set :keep_releases, ‘5’
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
queue ‘rvm install 2.3.0′
invoke :’rvm:use[ruby-2.3.0]’
queue ‘gem install bundler’
end
# Put any custom mkdir’s in here for when `mina setup` is ran.
# For Rails apps, we’ll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
queue %[echo "—–> Be sure to edit ‘#{deploy_to}/#{shared_path}/config/database.yml’ and ‘secrets.yml’."]
if repository
repo_host = repository.split(%r{@|://}).last.split(%r{:|\/}).first
repo_port = /:([0-9]+)/.match(repository) && /:([0-9]+)/.match(repository)[1] || ’22’
queue %[
if ! ssh-keygen -H -F #{repo_host} &>/dev/null; then
ssh-keyscan -t rsa -p #{repo_port} -H #{repo_host} >> ~/.ssh/known_hosts
fi
]
end
end
desc "Deploys the current version to the server."
task :deploy => :environment do
to :before_hook do
# Put things to run locally before ssh
end
deploy do
invoke :’git:clone’
invoke :’deploy:link_shared_paths’
invoke :’bundle:install’
invoke :’rails:db_create’
invoke :’rails:db_migrate’
invoke :’rails:assets_precompile’
#invoke :’deploy:cleanup’
#
to :launch do
invoke :’puma:restart’
invoke :’deploy:cleanup’
# queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
# queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt"
end
end
end
namespace :puma do
desc "Start the application"
task :start do
queue ‘echo "—–> Start Puma"’
queue "cd #{app_path} && RAILS_ENV=#{env} && bin/puma.sh start", :pty => false
end
desc "Stop the application"
task :stop do
queue ‘echo "—–> Stop Puma"’
queue "cd #{app_path} && RAILS_ENV=#{env} && bin/puma.sh stop"
end
desc "Restart the application"
task :restart do
queue ‘echo "—–> Restart Puma"’
queue "cd #{app_path} && RAILS_ENV=#{env} && bin/puma.sh restart"
end
end
[/code]