diff options
author | Louise Crow <louise.crow@gmail.com> | 2012-09-20 12:11:00 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2012-09-20 12:11:00 +0100 |
commit | 7b172122ed7c2d39d3b91f11e47e5ea34745db9c (patch) | |
tree | 3f8747e2a4d54b3e342a37eac128d2245e36a394 /lib/tasks | |
parent | 722d85479a2e3dee3ea7e3e852be2f6ebc550b32 (diff) |
Add task config_files:convert_init_script - it does the ugly variable replacement and uncomments the RAILS_ENV setting and exporting (substituting in the current RAILS_ENV) if there's no config/rails_env.rb
Diffstat (limited to 'lib/tasks')
-rw-r--r-- | lib/tasks/config_files.rake | 76 |
1 files changed, 76 insertions, 0 deletions
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 |