diff options
-rw-r--r-- | config/alert-tracks-debian.ugly | 17 | ||||
-rw-r--r-- | config/purge-varnish-debian.ugly | 17 | ||||
-rw-r--r-- | doc/INSTALL.md | 17 | ||||
-rw-r--r-- | lib/tasks/config_files.rake | 76 |
4 files changed, 108 insertions, 19 deletions
diff --git a/config/alert-tracks-debian.ugly b/config/alert-tracks-debian.ugly index 5bd146061..2b52ad840 100644 --- a/config/alert-tracks-debian.ugly +++ b/config/alert-tracks-debian.ugly @@ -4,7 +4,7 @@ # Provides: alert-tracks # Required-Start: $local_fs $syslog # Required-Stop: $local_fs $syslog -# Default-Start: 2 3 4 5 +# Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: alert-tracks is a daemon running the Alaveteli email alerts # Description: alert-tracks send Alaveteli email alerts as required @@ -13,11 +13,14 @@ # !!(*= $daemon_name *)!! Start the Alaveteli email alert daemon NAME=!!(*= $daemon_name *)!! -DAEMON=/data/vhost/!!(*= $vhost *)!!/alaveteli/script/runner +DAEMON=!!(*= $vhost_dir *)!!/alaveteli/script/runner DAEMON_ARGS="--daemon TrackMailer.alert_tracks_loop" -PIDFILE=/data/vhost/!!(*= $vhost *)!!/alert-tracks.pid -LOGFILE=/data/vhost/!!(*= $vhost *)!!/logs/alert-tracks.log +PIDFILE=!!(*= $vhost_dir *)!!/alert-tracks.pid +LOGFILE=!!(*= $vhost_dir *)!!/logs/alert-tracks.log DUSER=!!(*= $user *)!! +# Set RAILS_ENV - not needed if using config/rails_env.rb +# RAILS_ENV=your_rails_env +# export RAILS_ENV trap "" 1 @@ -48,17 +51,17 @@ case "$1" in exit 0 fi ;; - + start) echo -n "Starting Alaveteli alert daemon: $NAME" start_daemon ;; - + stop) echo -n "Stopping Alaveteli alert daemon: $NAME" stop_daemon ;; - + restart) echo -n "Restarting Alaveteli alert daemon: $NAME" stop_daemon diff --git a/config/purge-varnish-debian.ugly b/config/purge-varnish-debian.ugly index 1a9259da8..af32650a8 100644 --- a/config/purge-varnish-debian.ugly +++ b/config/purge-varnish-debian.ugly @@ -4,7 +4,7 @@ # Provides: purge-varnish # Required-Start: $local_fs $syslog # Required-Stop: $local_fs $syslog -# Default-Start: 2 3 4 5 +# Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: purge-varnish is a daemon purging the Alaveteli varnish cache # Description: purge-varnish purge the Alaveteli varnish cache @@ -13,11 +13,14 @@ # !!(*= $daemon_name *)!! Start the Alaveteli email purge-varnish daemon NAME=!!(*= $daemon_name *)!! -DAEMON=/data/vhost/!!(*= $vhost *)!!/alaveteli/script/runner +DAEMON=!!(*= $vhost_dir *)!!/alaveteli/script/runner DAEMON_ARGS="--daemon PurgeRequest.purge_all_loop" -PIDFILE=/data/vhost/!!(*= $vhost *)!!/purge-varnish.pid -LOGFILE=/data/vhost/!!(*= $vhost *)!!/logs/purge-varnish.log +PIDFILE=!!(*= $vhost_dir *)!!/purge-varnish.pid +LOGFILE=!!(*= $vhost_dir *)!!/logs/purge-varnish.log DUSER=!!(*= $user *)!! +# Set RAILS_ENV - not needed if using config/rails_env.rb +# RAILS_ENV=your_rails_env +# export RAILS_ENV trap "" 1 @@ -48,17 +51,17 @@ case "$1" in exit 0 fi ;; - + start) echo -n "Starting Alaveteli purge-varnish daemon: $NAME" start_daemon ;; - + stop) echo -n "Stopping Alaveteli purge-varnish daemon: $NAME" stop_daemon ;; - + restart) echo -n "Restarting Alaveteli purge-varnish daemon: $NAME" stop_daemon diff --git a/doc/INSTALL.md b/doc/INSTALL.md index aa9626afe..b805ee0c5 100644 --- a/doc/INSTALL.md +++ b/doc/INSTALL.md @@ -308,7 +308,7 @@ in the front end. It is possible completely to override the administrator authentication by setting `SKIP_ADMIN_AUTH` to `true` in `general.yml`. -# Cron jobs +# Cron jobs and init scripts `config/crontab.ugly` contains the cronjobs run on WhatDoTheyKnow. It's in a strange templating format they use in mySociety. mySociety @@ -323,6 +323,10 @@ like `!!(*= $this *)!!`. The variables are: `/data/vhost/!!(*= $vhost *)!!` -- you should replace that whole port with a path to the directory where your Alaveteli software installation lives, e.g. `/var/www/` +* `vhost_dir`: the entire path to the directory where the software is + served from. -- you should replace this with a path to the + directory where your Alaveteli software installation lives, + e.g. `/var/www/` * `vcspath`: the name of the alaveteli checkout, e.g. `alaveteli`. Thus, `/data/vhost/!!(*= $vhost *)!!/!!(*= $vcspath *)!!` might be replaced with `/var/www/alaveteli` in your cron tab @@ -336,11 +340,14 @@ One of the cron jobs refers to a script at `/etc/init.d/foi-alert-tracks`. This is an init script, a copy of which lives in `config/alert-tracks-debian.ugly`. As with the cron jobs above, replace the variables (and/or bits near the variables) -with paths to your software. `config/purge-varnish-debian.ugly` is a +with paths to your software. You can use the rake task `rake +config_files:convert_init_script` to do this. +`config/purge-varnish-debian.ugly` is a similar init script, which is optional and not required if you choose -not to run your site behind Varnish (see below). Either tweak the file permissions to make the script executable by your deploy user, or add the following line to your -sudoers file to allow these to be run by your deploy user (named `deploy` in -this case): +not to run your site behind Varnish (see below). Either tweak the file +permissions to make the scripts executable by your deploy user, or add the +following line to your sudoers file to allow these to be run by your deploy +user (named `deploy` in this case): deploy ALL = NOPASSWD: /etc/init.d/foi-alert-tracks, /etc/init.d/foi-purge-varnish diff --git a/lib/tasks/config_files.rake b/lib/tasks/config_files.rake new file mode 100644 index 000000000..1dcdde79a --- /dev/null +++ b/lib/tasks/config_files.rake @@ -0,0 +1,76 @@ +namespace :config_files do + + def usage_message message + puts '' + puts message + puts '' + exit 0 + end + + def check_for_env_vars(env_vars, example) + missing = [] + env_vars.each do |env_var| + unless ENV[env_var] + missing << env_var + end + end + if !missing.empty? + usage = "Usage: This task requires #{env_vars.to_sentence} - missing #{missing.to_sentence}" + if example + usage += "\nExample: #{example}" + end + usage_message usage + end + end + + def convert_ugly(file, replacements) + converted_lines = [] + ugly_var = /\!\!\(\*= \$([^ ]+) \*\)\!\!/ + File.open(file, 'r').each do |line| + line = line.gsub(ugly_var) do |match| + var = $1.to_sym + replacement = replacements[var] + if replacement == nil + if ! (skip[var] == true) + raise "Unhandled variable in .ugly file: $#{var}" + else + match + end + else + replacements[var] + end + end + converted_lines << line + end + converted_lines + end + + desc 'Convert Debian .ugly init script in config to a form suitable for installing in /etc/init.d' + task :convert_init_script => :environment do + example = 'rake config_files:convert_init_script DEPLOY_USER=deploy VHOST_DIR=/dir/above/alaveteli SCRIPT_FILE=config/alert-tracks-debian.ugly ' + check_for_env_vars(['DEPLOY_USER', 'VHOST_DIR', 'SCRIPT_FILE'], example) + + deploy_user = ENV['DEPLOY_USER'] + vhost_dir = ENV['VHOST_DIR'] + script_file = ENV['SCRIPT_FILE'] + + replacements = { :user => deploy_user, + :vhost_dir => vhost_dir } + + daemon_name = File.basename(script_file, '-debian.ugly') + replacements.update(:daemon_name => "foi-#{daemon_name}") + converted = convert_ugly(script_file, replacements) + rails_env_file = File.expand_path(File.join(Rails.root, 'config', 'rails_env.rb')) + if !File.exists?(rails_env_file) + 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") + end + end + converted.each do |line| + puts line + end + end + + +end
\ No newline at end of file |