Skip to content

Instantly share code, notes, and snippets.

@welingtonsampaio
Created December 1, 2014 11:57
Show Gist options
  • Save welingtonsampaio/cf8cdf1e2a49dd4b7e15 to your computer and use it in GitHub Desktop.
Save welingtonsampaio/cf8cdf1e2a49dd4b7e15 to your computer and use it in GitHub Desktop.
puma
#! /bin/sh
### BEGIN INIT INFO
# Provides: puma
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Author: Welington Sampaio <welington.sampaio@zaez.net>
#
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/local/bin:/usr/local/sbin/:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Puma rack web server"
NAME=puma
DAEMON=$NAME
SCRIPTNAME=/etc/init.d/$NAME
CONFIG=/etc/puma.conf
JUNGLE=`cat $CONFIG`
# Load the VERBOSE setting and other rcS variables
#. /lib/init/vars.sh
# Define CentOS functions.
. /etc/init.d/functions
do_stop_one() {
prefix="RBENV_ROOT=~/.rbenv RBENV_VERSION=$3 RACK_ENV=$4 PATH=/usr/local/bin:$PATH"
echo -n $"Stoping APP $2($4): "
if [ -f $1/shared/tmp/pids/puma.pid ]; then
kill -9 $( cat $1/shared/tmp/pids/puma.pid )
fi
echo_success
echo ''
if [ "$5" != "0" ]; then
echo -n $"Stoping Sidekiq $2($4): "
runuser $2 -g $2 -s /bin/bash -c "cd $1/current && ( $prefix ~/.rbenv/bin/rbenv exec bundle exec sidekiqctl stop $1/shared/tmp/pids/sidekiq.pid 10 &> /dev/null )"
if [ -f $1/shared/tmp/pids/sidekiq.pid ]; then
kill -9 $( cat $1/shared/tmp/pids/sidekiq.pid )
fi
echo_success
echo ''
fi
}
do_stop_line() {
deploydir=`echo $1 | cut -d , -f 1`
user=`echo $1 | cut -d , -f 2`
rbenv_version=`echo $1 | cut -d , -f 3`
rack_env=`echo $1 | cut -d , -f 4`
has_sidekiq=`echo $1 | cut -d , -f 5`
do_stop_one $deploydir $user $rbenv_version $rack_env $has_sidekiq
}
do_stop() {
for i in $JUNGLE; do
do_stop_line $i
done
}
do_start_one() {
prefix="RBENV_ROOT=~/.rbenv RBENV_VERSION=$3 RACK_ENV=$4 PATH=/usr/local/bin:$PATH"
echo -n $"Starting APP $2($4): "
# if [ -f $1/shared/tmp/pids/puma.state ]; then
# echo "cd $1/current && ( $prefix ~/.rbenv/bin/rbenv exec bundle exec pumactl -S $1/shared/tmp/pids/puma.state restart &> /dev/null )"
# runuser $2 -g $2 -s /bin/bash -c "cd $1/current && ( $prefix ~/.rbenv/bin/rbenv exec bundle exec pumactl -S $1/shared/tmp/pids/puma.state restart &> /dev/null )"
# else
# echo -n " puma "
if [ -f $1/shared/tmp/pids/puma.pid ]; then
kill -9 $( cat $1/shared/tmp/pids/puma.pid ) &> /dev/null
fi
runuser $2 -g $2 -s /bin/bash -c "cd $1/current && ( $prefix ~/.rbenv/bin/rbenv exec bundle exec puma -C $1/shared/puma.rb --daemon &> /dev/null )"
# fi
echo_success
echo ''
if [ "$5" != "0" ]; then
echo -n $"Starting Sidekiq $2($4): "
runuser $2 -g $2 -s /bin/bash -c "cd $1/current && ( $prefix ~/.rbenv/bin/rbenv exec bundle exec sidekiq --index 0 --pidfile $1/shared/tmp/pids/sidekiq.pid --environment $4 --logfile $1/shared/log/sidekiq.log --daemon &> /dev/null )"
echo_success
echo ''
fi
}
do_start_line() {
deploydir=`echo $1 | cut -d , -f 1`
user=`echo $1 | cut -d , -f 2`
rbenv_version=`echo $1 | cut -d , -f 3`
rack_env=`echo $1 | cut -d , -f 4`
has_sidekiq=`echo $1 | cut -d , -f 5`
do_start_one $deploydir $user $rbenv_version $rack_env $has_sidekiq
}
do_start() {
for i in $JUNGLE; do
do_start_line $i
done
}
do_status() {
if [ -d "$1" ]; then
if [ "`grep -c "^$1" $CONFIG`" -eq 0 ]; then
echo "O app ja eesta sendo administrado. Remova antes caso precise alterar a configuracao."
exit 1
else
status -p $1/shared/tmp/pids/puma.pid
fi
else
echo "A pasta de deploy $1 nao existe."
exit 1
fi
}
do_add() {
str=""
# App's directory
if [ -d "$1" ]; then
if [ "`grep -c "^$1" $CONFIG`" -eq 0 ]; then
str=$1
else
echo "O app ja eesta sendo administrado. Remova antes caso precise alterar a configuracao."
exit 1
fi
else
echo "A pasta $1 nao existe."
exit 1
fi
# User to run it as
if [ "`grep -c "^$2:" /etc/passwd`" -eq 0 ]; then
echo "O usuario $2 nao existe."
exit 1
else
str="$str,$2"
fi
# Versao do ruby no rbenv: 2.1.1 | 1.9.3
if [ "$3" != "" ]; then
str="$str,$3"
else
echo "Deve ser informado a versao do ruby."
exit 1
fi
# Ambiente de execucao: production | staging
if [ "$4" != "" ]; then
str="$str,$4"
else
echo "Deve ser informado o env do ambiente."
exit 1
fi
# Contem Sidekiq
if [ "$4" != "true" ]; then
str="$str,1"
else
str="$str,0"
fi
# Add it to the jungle
echo $str >> $CONFIG
echo "Configuracao adicionada: $str. Voce deve executar o start."
}
do_remove() {
if [ "`grep -c "^$1" $CONFIG`" -eq 0 ]; then
echo "Esta pasta $1 nao esta disponivel para remover."
else
# Stop it first.
do_stop_line $1
# Remove it from the config.
sed -i "\\:^$1:d" $CONFIG
echo "App removido da configuracao: $1."
fi
}
case "$1" in
stop)
if [ "$#" -eq 1 ]; then
do_stop
else
i=`grep $2 $CONFIG`
do_stop_line $i
fi
;;
start)
if [ "$#" -eq 1 ]; then
do_start
else
i=`grep $2 $CONFIG`
do_start_line $i
fi
;;
status)
if [ "$#" -lt 2 ]; then
echo "Por favor verificar o status de um app deve ser informado a pasta de deploy."
echo " Usage: service puma status /path/to/deploy"
exit 1
else
do_status $2
fi
;;
add)
if [ "$#" -lt 3 ]; then
echo "Por favor verifica se foram informados os paramentros corretos."
echo " Usage: $SCRIPTNAME add /path/to/deploy user rbenv_version rack_env"
echo " config and log are optionals."
exit 1
else
do_add $2 $3 $4 $5
fi
;;
remove)
if [ "$#" -lt 2 ]; then
echo "Por favor, informe o diretorio do deploy."
exit 1
else
do_remove $2
fi
;;
*)
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment