I am pretty new to these new tools - unicorn and nginx - and I'd like to ask you, how to restart them after development. So far, I have this setup:
unicorn_ini.sh
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/project/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=deployer
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
unicorn.rb
root = "/home/deployer/apps/project/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.project.sock"
worker_processes 4
timeout 30
# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end
deploy.rb
lock '3.2.1'
set :application, 'project'
set :scm, :git
set :repo_url, 'git#bitbucket.org:me/project.git'
set :pty, true
set :deploy_to, "/home/deployer/apps/project"
set :keep_releases, 5
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
This is the current setup. Will be restarted after successful deployment nginx and unicorn, so I'd see the new code I deployed? Also, will the application be bundled?
Thank you, and sorry once again - I ma doing this the first time
You will consider use something like https://github.com/tablexi/capistrano3-unicorn, it allow you to have zero down time deployment, restart your unicorns, with almost 0 setup needed.
Related
I have set up a server for deploying my rails app. I am not able to start unicorn service. It is giving me a failed status. I am totally new to this. So as per this tutorial https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04. I created the shared folders for the socket, PIDs on the server. but I am using mina for deployment after every release my shared folder which I manually created, disappears and goes away with the old release with my unicorn files. So I created that shared folders on my local Desktop but they are not getting pushed to the server via git. So where and how should I place my unicorn pids and sockets to make it working below are my files
my unicorn.rb file
root = "/home/deployer/apps/rails_app/current"
working_directory root
pid "#{root}/shared/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/shared/unicorn.railsapp.sock"
worker_processes 2
timeout 30
preload_app true
My /etc/init.d/unicorn_rails_app file
set -e
USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
# app settings
USER="ggg"
APP_NAME="rails_app"
APP_ROOT="/home/$USER/$APP_NAME/current"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
# make sure the app exists
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting $APP_NAME"
su - $USER -c "$CMD"
;;
stop)
echo "Stopping $APP_NAME"
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
echo "Force stopping $APP_NAME"
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload|upgrade)
sig USR2 && echo "reloaded $APP_NAME" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
my mina deploy.rb file
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv' # for rbenv support. (http://rbenv.org)
set :domain, 'mydomain'
set :term_mode, :nil
set :deploy_to, 'path'
set :repository, 'myrepo'
set :branch, 'master'
set :user, 'gg' # Username in the server to SSH to.
set :keep_releases, 5
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log','tmp']
task :environment do
invoke :'rbenv:load'
end
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
end
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
queue "mkdir -p #{deploy_to}/current/tmp2/"
queue "touch #{deploy_to}/current/tmp2/restart.txt"
end
end
end
I am getting this errors
unicorn_rails_app.service - LSB: starts the unicorn app server
Loaded: loaded (/etc/init.d/unicorn_rails_app; generated)
Active: failed (Result: exit-code) since Tue 2019-06-04 07:35:52
UTC; 10s ago
Docs: man:systemd-sysv-generator(8)
Process: 30422 ExecStart=/etc/init.d/unicorn_rails_app start
(code=exited, status=1/FAILURE)
Keep them in shared folder, you can create symlink to tmp folder in shared.
I am using Capistrano to deploy my code to a DigitalOcean server. I added to the deploy.rb file the block for restarting Unicorn after every deploy, but since then I have noticed that when I got to the browser and start refreshing the website, sometimes I get refreshed the website and sometimes only a blank (white) page. It's a total random.
deploy.rb:
# config valid only for current version of Capistrano
lock "3.8.1"
set :application, "project"
set :repo_url, "git#bitbucket.org:username/project.git"
set :branch, "master"
set :tmp_dir, '/home/deployer/tmp'
set :deploy_to, "/home/deployer/apps/project"
set :keep_releases, 5
set(:executable_config_files, %w(
unicorn_init.sh
))
# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
set(:symlinks, [
{
source: "nginx.conf",
link: "/etc/nginx/sites-enabled/default"
},
{
source: "unicorn_init.sh",
link: "/etc/init.d/unicorn_#{fetch(:application)}"
},
{
source: "log_rotation",
link: "/etc/logrotate.d/#{fetch(:application)}"
},
{
source: "monit",
link: "/etc/monit/conf.d/#{fetch(:application)}.conf"
}
])
namespace :deploy do
desc 'Restart application'
task :restart_unicorn do
on roles(:app) do
execute '/home/deployer/apps/project/current/config/unicorn_init.sh restart'
end
end
after :publishing, :restart_unicorn
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:web) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
before "deploy", "deploy:check_revision"
end
unicorn_init.sh:
set -e
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/project/current
#PID=$APP_ROOT/tmp/pids/unicorn.pid
PID=/home/deployer/apps/project/shared/pids/unicorn.pid
#CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn/production.rb -E production"
AS_USER=deployer
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
sig USR2 && 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
What I have change in this file - this line:
sig HUP && echo reloaded OK && exit 0
for this one:
sig USR2 && echo reloaded OK && exit 0
I am trying to look to the unicorn log file, but there is nothing relevant to this issue. production.log - same, nothing relevant. When I am looking for an nginx log file, I found on here: /var/log/nginx/error.log, but the fiel is empty (size 0).
Any advice what to do with this issue/what is wrong or where even to start?
EDIT: in the /home/deployer/apps/rentalhistory/shared/pids directory, there are these two files:
unicorn.pid.oldbin and unicorn.pid - should not be one of the removed?
Thanks!
What was the problem in my case - when was reloaded Unicorn, the new unicorn PID was generated (unicorn.pid) - that's correct, but the old one (unicorn.pid.oldbin) was still staying in the system. And this was apparently causing the random downs of the whole website.
FIX: I checked my Unicorn config file (config/production.rb) and this line was wrong - the path:
old_pid = "#{root}/shared/pids/unicorn.pid.oldbin"
The shared directory is on the same level as current, not inside of it.
After this change, the new unicorn PID is created and the old one is properly removed.
I'm trying to deploy with Ruby 2.2.2, Rails 4, Capistrano 3, Unicorn and Nginx. When I run cap production deploy I get an error sating that Unicon can't find or access a directory for the "restart" command, stemming (I believe) from this line in my deploy.rb:
execute "/etc/init.d/unicorn_#{fetch(:application)}/unicorn_init.sh/#{command}"
The error:
SSHKit::Runner::ExecuteError: Exception while executing as deploy#0.0.0.0: /etc/init.d/unicorn_app/restart exit status: 126
/etc/init.d/unicorn_app/restart stdout: Nothing written
/etc/init.d/unicorn_app/restart stderr: /usr/bin/env: /etc/init.d/unicorn_app/restart: Permission denied
Deploy.rb
set :application, 'app'
set :repo_url, 'https://github.com/user/app.git'
set :deploy_to, '/opt/www/app'
set :user, 'deploy'
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets}
set :ssh_options, {
forward_agent: true,
port: 7822
}
namespace :deploy do
%w[start stop restart].each do |command|
desc 'Manage Unicorn'
task command do
on roles(:app), in: :sequence, wait: 1 do
execute "/etc/init.d/unicorn_#{fetch(:application)}/unicorn_init.sh/#{command}"
end
end
end
after :publishing, :restart
end
set :rvm_ruby_version, '2.2.2'
set :default_env, { rvm_bin_path: '/usr/local/rvm/bin' }
SSHKit.config.command_map[:rake] = "#{fetch(:default_env)[:rvm_bin_path]}/rvm ruby-#{fetch(:rvm_ruby_version)} do bundle exec rake"
Can anyone help me out here? I'll gladly post any missing info.
EDIT: unicorn_init.sh: I know 'testapp' is referenced in this file, but app is referenced elsewhere in this post. Please know that i'm consistent across all files with the name, I just changed them for the post.
#!/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:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/opt/www/testapp/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=deploy
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 "
exit 1
;;
esac
I am trying to setup Unicorn in Amazon EC2. I am following this blog.
But when I start Unicorn using:
sudo service unicorn_firstapp start
I am getting the following error:
/home/ec2-user/rails_projects/firstapp/config/unicorn.rb: line 2: syntax error near unexpected token `('
/home/ec2-user/rails_projects/firstapp/config/unicorn.rb: line 2: `app_dir = File.expand_path("../..", __FILE__)'
What is my mistake? If any one has any idea about this issue, please share it with me.
unicorn.rb:
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 2
preload_app true
timeout 30
# Set up socket location
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
# Logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"
/etc/init.d/unicorn_firstapp:
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn app server
# Description: starts unicorn using start-stop-daemon
### END INIT INFO
set -e
USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
# app settings
USER="ec2-user"
APP_NAME="firstapp"
APP_ROOT="/home/$USER/rails_projects/$APP_NAME"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
#setting for RVM
RAILS_ENV=production
PID=$APP_ROOT/shared/pids/unicorn.pid
DESC="Unicorn app - $RAILS_ENV"
UNICORN="$APP_ROOT/config/unicorn.rb"
UNICORN_OPTS="-c $APP_ROOT/config/unicorn.rb -E $RAILS_ENV -D"
CMD="RAILS_ENV=$RAILS_ENV $UNICORN $UNICORN_OPTS"
TIMEOUT=60
# make sure the app exists
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting $APP_NAME"
su - $USER -c "$CMD"
;;
stop)
echo "Stopping $APP_NAME"
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
echo "Force stopping $APP_NAME"
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload|upgrade)
sig USR2 && echo "reloaded $APP_NAME" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
Try including the correct hash-bang header, otherwise your code will be executed using your shell:
#!/usr/bin/env ruby
app_dir = File.expand_path("../..", __FILE__)
...
Hope that helps.
I have the following Chef routine to generate a script to start/stop a unicorn daemon.
However I always get a Permission denied when Capistrano execute this file. What I'm doing wrong with its permissions?
# recipes/default.rb
template "/etc/init.d/unicorn_#{node['pod']['app_name']}" do
owner node['pod']['user']
group node['pod']['user']
mode 777
source "unicorn_init.erb"
variables( :app_name => node['pod']['app_name'], :user => node['pod']['user'] )
end
# templates/default/unicorn_init.rb
#!/bin/sh
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/<%= #user %>/apps/<%= #app_name %>/current
PID=/var/run/unicorn/unicorn_<%= #app_name %>.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=<%= #user %>
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && su -c "kill -$1 `cat $PID`" - $AS_USER
}
oldsig () {
test -s $OLD_PIN && sudo -c "kill -$1 `cat $OLD_PIN`" - $AS_USER
}
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
As you can see, it doesn't set the correct mode for the file:
[2013-05-27T20:42:00+02:00] INFO: Processing template[/etc/init.d/unicorn_pod] action create (pod::default line 38)
[2013-05-27T20:42:00+02:00] INFO: template[/etc/init.d/unicorn_pod] owner changed to 1001
[2013-05-27T20:42:00+02:00] INFO: template[/etc/init.d/unicorn_pod] group changed to 110
[2013-05-27T20:42:00+02:00] INFO: template[/etc/init.d/unicorn_pod] mode changed to 1411
** [out :: localhost] -r----x--t 1 deployer deployer 1453 2013-05-27 17:48 unicorn_pod
See the docs. Mode should be octal or string:
template "/etc/init.d/unicorn_#{node['pod']['app_name']}" do
[...]
mode 0777 #or mode "777"
[...]
end