Skip to content

Commit

Permalink
Update daemons and crontab to use rbenv
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed May 12, 2021
1 parent 9adc46e commit 4a9601c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 33 deletions.
13 changes: 9 additions & 4 deletions config/alert-tracks-debian.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
# <%= daemon_name %> Start the Alaveteli email alert daemon

PATH=/usr/local/bin:/usr/bin:/bin
NAME=<%= daemon_name %>
DAEMON=<%= vhost_dir %>/<%= vcspath %>/script/runner
DAEMON_ARGS="--daemon TrackMailer.alert_tracks_loop"
Expand All @@ -24,10 +25,14 @@ SITE_HOME=<%= vhost_dir %>/<%= vcspath %>
# Set RAILS_ENV - not needed if using config/rails_env.rb
# RAILS_ENV=your_rails_env
# export RAILS_ENV
PATH=/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:/usr/local/bin:/usr/bin:/bin
# Uncomment the following line if running under rbenv - we want this daemon to run
# in the context of the ruby version defined for the site
# PATH=/home/<%= user %>/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

<% if use_rbenv? %>
RBENV_ROOT="/home/<%= user %>/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
SET_PATH="cd <%= vhost_dir %>/<%= vcspath %>; rbenv rehash; rbenv local <%= ruby_version %>"
<% else %>
PATH="/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:$PATH"
<% end %>

trap "" 1

Expand Down
10 changes: 10 additions & 0 deletions config/crontab-example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ PATH=/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:/usr/local/bin:/usr/bin
# in the context of the ruby version defined for the site
# PATH=/home/<%= user %>/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

PATH=/usr/local/bin:/usr/bin:/bin

<% if use_rbenv? %>
RBENV_ROOT="/home/<%= user %>/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
SET_PATH="cd <%= vhost_dir %>/<%= vcspath %>; rbenv rehash; rbenv local <%= ruby_version %>"
<% else %>
PATH="/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:$PATH"
<% end %>

MAILTO=<%= mailto %>

# Every 5 minutes
Expand Down
13 changes: 9 additions & 4 deletions config/poll-for-incoming-debian.example
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
# <%= daemon_name %> Start the Alaveteli incoming mail poller daemon

PATH=/usr/local/bin:/usr/bin:/bin
NAME=<%= daemon_name %>
DAEMON=<%= vhost_dir %>/<%= vcspath %>/script/runner
DAEMON_ARGS="--daemon AlaveteliMailPoller.poll_for_incoming_loop"
Expand All @@ -24,10 +25,14 @@ SITE_HOME=<%= vhost_dir %>/<%= vcspath %>
# Set RAILS_ENV - not needed if using config/rails_env.rb
# RAILS_ENV=your_rails_env
# export RAILS_ENV
PATH=/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:/usr/local/bin:/usr/bin:/bin
# Uncomment the following line if running under rbenv - we want this daemon to run
# in the context of the ruby version defined for the site
# PATH=/home/<%= user %>/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

<% if use_rbenv? %>
RBENV_ROOT="/home/<%= user %>/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
SET_PATH="cd <%= vhost_dir %>/<%= vcspath %>; rbenv rehash; rbenv local <%= ruby_version %>"
<% else %>
PATH="/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:$PATH"
<% end %>

trap "" 1

Expand Down
13 changes: 9 additions & 4 deletions config/send-notifications-debian.example
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
# <%= daemon_name %> Start the Alaveteli notification daemon

PATH=/usr/local/bin:/usr/bin:/bin
NAME=<%= daemon_name %>
DAEMON=<%= vhost_dir %>/<%= vcspath %>/script/runner
DAEMON_ARGS="--daemon NotificationMailer.send_notifications_loop"
Expand All @@ -24,10 +25,14 @@ SITE_HOME=<%= vhost_dir %>/<%= vcspath %>
# Set RAILS_ENV - not needed if using config/rails_env.rb
# RAILS_ENV=your_rails_env
# export RAILS_ENV
PATH=/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:/usr/local/bin:/usr/bin:/bin
# Uncomment the following line if running under rbenv - we want this daemon to run
# in the context of the ruby version defined for the site
# PATH=/home/<%= user %>/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

<% if use_rbenv? %>
RBENV_ROOT="/home/<%= user %>/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
SET_PATH="cd <%= vhost_dir %>/<%= vcspath %>; rbenv rehash; rbenv local <%= ruby_version %>"
<% else %>
PATH="/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:$PATH"
<% end %>

trap "" 1

Expand Down
33 changes: 18 additions & 15 deletions config/sysvinit-thin.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#! /bin/sh
#
### BEGIN INIT INFO
# Provides: application-thin-<%= site %>
# Required-Start: $local_fs $network
Expand All @@ -12,48 +13,50 @@
# This example sysvinit script is based on the helpful example here:
# http://richard.wallman.org.uk/2010/02/howto-deploy-a-catalyst-application-using-fastcgi-and-nginx/

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/bin:/usr/bin:/bin
NAME=<%= site %>
SITE_HOME=<%= vhost_dir %>/<%= vcspath %>
DESC="Alaveteli app server"
USER=<%= user %>
CPUS=<%= cpus %>
RAILS_ENV=<%= rails_env %>
# Set RAILS_ENV - not needed if using config/rails_env.rb
# RAILS_ENV=your_rails_env
# export RAILS_ENV

set -e
CMD="bundle exec thin"

# Check that the Daemon can be run
CURRENT_USER=$(whoami)
if [ "$CURRENT_USER" = "$USER" ]
then
cd $SITE_HOME && bundle exec thin --version > /dev/null 2>&1 || exit 0
else
su -l -c "cd $SITE_HOME && bundle exec thin --version &> /dev/null || exit 0" $USER
fi
<% if use_rbenv? %>
RBENV_ROOT="/home/<%= user %>/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
CMD="cd <%= vhost_dir %>/<%= vcspath %>; rbenv rehash; rbenv local <%= ruby_version %>; $CMD"
<% else %>
PATH="/home/<%= user %>/.gem/ruby/<%= ruby_version %>/bin:$PATH"
CMD="cd <%= vhost_dir %>/<%= vcspath %>; $CMD"
<% end %>

start_daemon() {
echo -n "Starting $DESC: "
cd "$SITE_HOME" && bundle exec thin \
su - $USER -c "$CMD \
--environment=$RAILS_ENV \
--user="$USER" \
--group="$USER" \
--address=127.0.0.1 \
--daemonize \
--servers="$CPUS" \
--quiet \
start || true
start || true"
echo "$NAME."
}

stop_daemon() {
echo -n "Stopping $DESC: "
cd "$SITE_HOME" && bundle exec thin --quiet --servers="$CPUS" stop || true
su - $USER -c "$CMD --quiet --servers="$CPUS" stop || true"
echo "$NAME."
}

restart_daemon() {
echo -n "Restarting $DESC: "
cd "$SITE_HOME" && bundle exec thin --onebyone --quiet restart || true
su - $USER -c "$CMD --onebyone --quiet restart || true"
echo "$NAME."
}

Expand Down
10 changes: 7 additions & 3 deletions lib/tasks/config_files.rake
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ namespace :config_files do
'VCSPATH=alaveteli ' \
'SITE=alaveteli ' \
'SCRIPT_FILE=config/alert-tracks-debian.example ' \
'RUBY_VERSION=2.5.8 '
'RUBY_VERSION=2.5.8 ' \
'USE_RBENV=false '
check_for_env_vars(['DEPLOY_USER',
'VHOST_DIR',
'SCRIPT_FILE'], example)
Expand All @@ -93,7 +94,8 @@ namespace :config_files do
:site => ENV.fetch('SITE') { 'foi' },
:cpus => ENV.fetch('CPUS') { '1' },
:rails_env => ENV.fetch('RAILS_ENV') { 'development' },
:ruby_version => ENV.fetch('RUBY_VERSION') { '' }
:ruby_version => ENV.fetch('RUBY_VERSION') { '' },
:use_rbenv? => ENV['USE_RBENV'] == 'true'
}

# Use the filename for the $daemon_name ugly variable
Expand Down Expand Up @@ -125,6 +127,7 @@ namespace :config_files do
'SITE=alaveteli CRONTAB=config/crontab-example ' \
'[email protected] ' \
'RUBY_VERSION=2.5.8 '
'USE_RBENV=false '
check_for_env_vars(['DEPLOY_USER',
'VHOST_DIR',
'VCSPATH',
Expand All @@ -136,7 +139,8 @@ namespace :config_files do
:vcspath => ENV['VCSPATH'],
:site => ENV['SITE'],
:mailto => ENV.fetch('MAILTO') { "#{ ENV['DEPLOY_USER'] }@localhost" },
:ruby_version => ENV.fetch('RUBY_VERSION') { '' }
:ruby_version => ENV.fetch('RUBY_VERSION') { '' },
:use_rbenv? => ENV['USE_RBENV'] == 'true'
}

lines = []
Expand Down
1 change: 1 addition & 0 deletions script/install-as-user
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ if [ $USE_RBENV = true ]; then
# Compile and install Ruby and set as global default for this user
rbenv install -s $RUBY_VERSION
rbenv global $RUBY_VERSION
gem install bundler
fi

# Speed up the installation of gems:
Expand Down
6 changes: 3 additions & 3 deletions script/site-specific-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ clear_daemon() {

install_daemon() {
echo -n "Creating /etc/init.d/$SITE-$1... "
(su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_init_script DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' RUBY_VERSION='$RUBY_VERSION' SCRIPT_FILE=config/$1-debian.example" "$UNIX_USER") > /etc/init.d/"$SITE-$1"
(su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_init_script DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' RUBY_VERSION='$RUBY_VERSION' USE_RBENV=$USE_RBENV SCRIPT_FILE=config/$1-debian.example" "$UNIX_USER") > /etc/init.d/"$SITE-$1"
chgrp "$UNIX_USER" /etc/init.d/"$SITE-$1"
chmod 754 /etc/init.d/"$SITE-$1"

Expand Down Expand Up @@ -276,7 +276,7 @@ fi


echo -n "Creating /etc/cron.d/alaveteli... "
(su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_crontab DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' VCSPATH='$SITE' SITE='$SITE' RUBY_VERSION='$RUBY_VERSION' CRONTAB=config/crontab-example" "$UNIX_USER") > /etc/cron.d/alaveteli
(su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_crontab DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' VCSPATH='$SITE' SITE='$SITE' RUBY_VERSION='$RUBY_VERSION' USE_RBENV=$USE_RBENV CRONTAB=config/crontab-example" "$UNIX_USER") > /etc/cron.d/alaveteli
# There are some other parts to rewrite, so just do them with sed:
sed -r \
-e "s,^(MAILTO=).*,\1root@$HOST," \
Expand All @@ -285,7 +285,7 @@ echo $DONE_MSG

if [ ! "$DEVELOPMENT_INSTALL" = true ]; then
echo -n "Creating /etc/init.d/$SITE... "
(su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_init_script DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' VCSPATH='$SITE' SITE='$SITE' SCRIPT_FILE=config/sysvinit-thin.example" "$UNIX_USER") > /etc/init.d/"$SITE"
(su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_init_script DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' VCSPATH='$SITE' SITE='$SITE' USE_RBENV=$USE_RBENV SCRIPT_FILE=config/sysvinit-thin.example" "$UNIX_USER") > /etc/init.d/"$SITE"
chgrp "$UNIX_USER" /etc/init.d/"$SITE"
chmod 754 /etc/init.d/"$SITE"
echo $DONE_MSG
Expand Down

0 comments on commit 4a9601c

Please sign in to comment.