Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Last active April 28, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebelweb/4701d91cf79ca78215f561069eeb9d7c to your computer and use it in GitHub Desktop.
Save rebelweb/4701d91cf79ca78215f561069eeb9d7c to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Copyright:: Copyright (c) 2015 Gitlab.com
# Copyright:: Copyright (c) 2016 Amusement SMART, LLC
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#This wrapper is meant to be invoked by omnibus-smart-mobile via Runit
# Let runit capture all script error messages
exec 2>&1
export JAVA_HOME="/opt/smart-mobile/embedded/jre"
export PATH="/opt/smart-mobile/embedded/jruby/bin:/opt/smart-mobile/embedded/bin:$PATH"
function assert_non_empty
{
if [ -z "$(eval echo \$$1)" ] ; then
echo "$0 error: \$$1 is empty"
exit 1
fi
}
function find_us_a_puma
{
adopt ${current_pidfile}
if [[ ${puma_pid} ]]; then
echo "adopted existing puma ${puma_pid}"
return
fi
adopt ${oldbin_pidfile}
if [[ ${puma_pid} ]]; then
echo "adopted existing oldbin puma ${puma_pid}"
return
fi
echo "starting new puma"
start_puma
sleep ${puma_wait_start}
adopt ${current_pidfile}
if [[ ${puma_pid} ]]; then
echo "adopted new puma ${puma_pid}"
return
fi
echo "failed to start a new puma"
exit
}
function wait_for_puma_to_exit
{
while sleep ${puma_poll_alive}; do
alive ${puma_pid} || break
done
}
function adopt
{
local pid=$(cat $1 2>/dev/null)
if alive ${pid} && is_puma ${pid}; then
readonly puma_pid=${pid}
fi
}
function is_puma
{
ps -p $1 -o args | grep -q puma
}
function alive
{
kill -0 $1 > /dev/null 2>&1
}
function start_puma
{
echo 'starting puma'
chpst -P -Usmart-mobile -u smart-mobile /opt/smart-mobile/embedded/jruby/bin/bundle exec puma /opt/smart-mobile/embedded/smart-mobile-rails/config.ru -e production -C /opt/smart-mobile/embedded/smart-mobile-rails/config/puma.rb
}
function forward_signal
{
echo "forwarding $1 to puma master ${unicorn_pid}"
kill -$1 ${unicorn_pid}
}
function trap_signals
{
# Forward all common runit signals except:
# - HUP which we handle below;
# - KILL which cannot be caught.
for sig in STOP CONT ALRM INT QUIT USR1 USR2 TERM; do
trap "forward_signal ${sig}" ${sig}
done
# Omnibus-ctl does not have a subcommand that sends USR2 but it can send HUP.
# To allow for reloading puma from the command line, translate HUP to
# USR2.
trap "echo 'wrapper received HUP'; forward_signal SIGKILL" HUP
}
function main
{
cd /opt/smart-mobile/embedded/smart-mobile-rails
find_us_a_puma
trap_signals
wait_for_puma_to_exit
}
assert_non_empty current_pidfile
assert_non_empty user
assert_non_empty environment
assert_non_empty puma_rb
readonly oldbin_pidfile=${current_pidfile}.oldbin
readonly puma_wait_start=1 # time in seconds
readonly puma_poll_alive=1 # time in seconds
main
echo "wrapper for puma ${puma_pid} exiting"
#!/bin/sh
# Let runit capture all script error messages
exec 2>&1
umask 077
exec chpst -P -u root \
/usr/bin/env \
current_pidfile=<%= node['smart-mobile']['puma']['pidfile'] %> \
rails_app=<%= @options[:rails_app] %> \
user=<%= @options[:user] %> \
environment='production' \
puma_rb=<%= @options[:puma_rb] %> \
/opt/smart-mobile/embedded/bin/puma-wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment