aboutsummaryrefslogtreecommitdiffstats
path: root/lib/tasks/config_files.rake
blob: 1528d732444cfb0486860987f05a96f31121af26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require File.join(File.dirname(__FILE__), 'usage')
namespace :config_files do

    include Usage

    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
                    raise "Unhandled variable in .ugly file: $#{var}"
                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 VCSPATH=alaveteli SITE=alaveteli SCRIPT_FILE=config/alert-tracks-debian.ugly'
        check_for_env_vars(['DEPLOY_USER',
                            'VHOST_DIR',
                            'SCRIPT_FILE'], example)

        replacements = {
            :user => ENV['DEPLOY_USER'],
            :vhost_dir => ENV['VHOST_DIR'],
            :vcspath => ENV.fetch('VCSPATH') { 'alaveteli' },
            :site => ENV.fetch('SITE') { 'foi' },
            :rails_env => ENV.fetch('RAILS_ENV') { 'development' }
        }

        # 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)

        # 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")
            end
        end

        converted.each do |line|
            puts line
        end
    end

    desc 'Convert Debian .ugly crontab file in config to a form suitable for installing in /etc/cron.d'
    task :convert_crontab => :environment do
        example = 'rake config_files:convert_crontab DEPLOY_USER=deploy VHOST_DIR=/dir/above/alaveteli VCSPATH=alaveteli SITE=alaveteli CRONTAB=config/crontab-example MAILTO=cron-alaveteli@example.org'
        check_for_env_vars(['DEPLOY_USER',
                            'VHOST_DIR',
                            'VCSPATH',
                            'SITE',
                            'CRONTAB'], example)
        replacements = {
            :user => ENV['DEPLOY_USER'],
            :vhost_dir => ENV['VHOST_DIR'],
            :vcspath => ENV['VCSPATH'],
            :site => ENV['SITE'],
            :mailto => ENV.fetch('MAILTO') { "cron-#{ ENV['SITE'] }@mysociety.org" }
        }
        convert_ugly(ENV['CRONTAB'], replacements).each do |line|
            puts line
        end
    end

end