diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-08-07 12:25:14 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-08-07 12:25:14 +0100 |
commit | ca92eb8c15534ebdc30d01e09c6ca51ff4220e98 (patch) | |
tree | c17a5b7d658854b3025076179101364521d07412 | |
parent | fd4c0668f89eabc1974c8454f85ef90bb3dfe0ee (diff) | |
parent | efa7f24c08c0cffa373b44ec4745a698003f85f2 (diff) |
Merge branch 'sysvinit-example' into rails-3-develop
-rwxr-xr-x | config/sysvinit-thin.ugly | 68 | ||||
-rwxr-xr-x | config/sysvinit.example | 53 | ||||
-rw-r--r-- | lib/tasks/config_files.rake | 21 | ||||
-rwxr-xr-x | script/site-specific-install.sh | 11 |
4 files changed, 86 insertions, 67 deletions
diff --git a/config/sysvinit-thin.ugly b/config/sysvinit-thin.ugly new file mode 100755 index 000000000..cc604d994 --- /dev/null +++ b/config/sysvinit-thin.ugly @@ -0,0 +1,68 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: application-thin-!!(*= $site *)!! +# Required-Start: $local_fs $network +# Required-Stop: $local_fs $network +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Starts the Thin app server for the "!!(*= $site *)!!" site +# Description: The Thin app server for the "!!(*= $site *)!!" site +### END INIT INFO + +# 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 +NAME=!!(*= $site *)!! +SITE_HOME=!!(*= $vhost_dir *)!!/!!(*= $vcspath *)!! +DESC="Alaveteli app server" +USER=!!(*= $user *)!! + +set -e + +# Check that the Daemon can be run +su -l -c "cd $SITE_HOME && bundle exec thin --version &> /dev/null || exit 0" $USER + +start_daemon() { + echo -n "Starting $DESC: " + cd "$SITE_HOME" && bundle exec thin \ + --environment=production \ + --user="$USER" \ + --group="$USER" \ + --address=127.0.0.1 \ + --daemonize \ + --quiet \ + start || true + echo "$NAME." +} + +stop_daemon() { + echo -n "Stopping $DESC: " + cd "$SITE_HOME" && bundle exec thin --quiet stop || true + echo "$NAME." +} + +restart_daemon() { + echo -n "Restarting $DESC: " + cd "$SITE_HOME" && bundle exec thin --onebyone --quiet restart || true + echo "$NAME." +} + +case "$1" in + start) + start_daemon + ;; + stop) + stop_daemon + ;; + reload|restart|force-reload) + restart_daemon + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|reload|restart|force-reload}" >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/config/sysvinit.example b/config/sysvinit.example deleted file mode 100755 index 443e7c3fb..000000000 --- a/config/sysvinit.example +++ /dev/null @@ -1,53 +0,0 @@ -#! /bin/sh -### BEGIN INIT INFO -# Provides: application-thin-alaveteli -# Required-Start: $local_fs $network -# Required-Stop: $local_fs $network -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Starts the Thin web server for the "Alaveteli" site -# Description: The Thin web server for the "Alaveteli" site -### END INIT INFO - -# 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 -SITE_HOME=/var/www/alaveteli -NAME=alaveteli -DESC="Alaveteli app server" -USER=fms - -echo $DAEMON -test -f $DAEMON || exit 0 - -set -e - -start_daemon() { - su -l -c "cd $SITE_HOME/alaveteli && bundle exec thin -d -p 3300 -e development start" $USER -} - -stop_daemon() { - pkill -f thin -u $USER || true -} - -case "$1" in - start) - start_daemon - ;; - stop) - stop_daemon - ;; - reload|restart|force-reload) - stop_daemon - sleep 5 - start_daemon - ;; - *) - N=/etc/init.d/$NAME - echo "Usage: $N {start|stop|reload|restart|force-reload}" >&2 - exit 1 - ;; -esac - -exit 0 diff --git a/lib/tasks/config_files.rake b/lib/tasks/config_files.rake index 438b63fc0..8ebd3fc07 100644 --- a/lib/tasks/config_files.rake +++ b/lib/tasks/config_files.rake @@ -28,22 +28,23 @@ namespace :config_files do 'VHOST_DIR', 'SCRIPT_FILE'], example) - script_file = ENV['SCRIPT_FILE'] - site = ENV.fetch('SITE', 'foi') - replacements = { :user => ENV['DEPLOY_USER'], :vhost_dir => ENV['VHOST_DIR'], - :vcspath => ENV.fetch('VCSPATH', 'alaveteli'), - :site => site + :vcspath => ENV.fetch('VCSPATH'), { 'alaveteli' }, + :site => ENV.fetch('SITE') { 'foi' } } - daemon_name = File.basename(script_file, '-debian.ugly') - replacements.update(:daemon_name => "#{ site }-#{ daemon_name }") - converted = convert_ugly(script_file, replacements) - rails_env_file = File.expand_path(File.join(Rails.root, 'config', 'rails_env.rb')) + # Use the filename for the $daemon_name ugly variable + daemon_name = File.basename(ENV['SCRIPT_FILE'], '-debian.ugly') + replacements.update(:daemon_name => "#{ replacements[:site] }-#{ daemon_name }") + + # Generate the template for potential further processing + converted = convert_ugly(ENV['SCRIPT_FILE'], replacements) - unless File.exists?(rails_env_file) + # gsub the RAILS_ENV in to the generated template if its not set by the + # hard coded config file + unless File.exists?("#{ Rails.root }/config/rails_env.rb") converted.each do |line| line.gsub!(/^#\s*RAILS_ENV=your_rails_env/, "RAILS_ENV=#{Rails.env}") line.gsub!(/^#\s*export RAILS_ENV/, "export RAILS_ENV") diff --git a/script/site-specific-install.sh b/script/site-specific-install.sh index fce230822..4c8c99aa2 100755 --- a/script/site-specific-install.sh +++ b/script/site-specific-install.sh @@ -134,10 +134,6 @@ su -l -c "$BIN_DIRECTORY/install-as-user '$UNIX_USER' '$HOST' '$DIRECTORY'" "$UN # no longer need the PostgreSQL user to be a superuser: echo "ALTER USER \"$UNIX_USER\" WITH NOSUPERUSER;" | su -l -c 'psql' postgres -if [ ! "$DEVELOPMENT_INSTALL" = true ]; then - install_sysvinit_script -fi - # Set up root's crontab: cd "$REPOSITORY" @@ -151,6 +147,13 @@ sed -r \ -i /etc/cron.d/alaveteli 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.ugly" "$UNIX_USER") > /etc/init.d/"$SITE" + chmod a+rx /etc/init.d/"$SITE" + echo $DONE_MSG +fi + echo -n "Creating /etc/init.d/foi-alert-tracks... " (su -l -c "cd '$REPOSITORY' && bundle exec rake config_files:convert_init_script DEPLOY_USER='$UNIX_USER' VHOST_DIR='$DIRECTORY' SCRIPT_FILE=config/alert-tracks-debian.ugly" "$UNIX_USER") > /etc/init.d/foi-alert-tracks chmod a+rx /etc/init.d/foi-alert-tracks |