Generate new style piwik configuration
[chef.git] / cookbooks / web / definitions / rails_port.rb
1 #
2 # Cookbook Name:: web
3 # Definition:: rails_port
4 #
5 # Copyright 2012, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 require "yaml"
21
22 define :rails_port, :action => [ :create, :enable ] do
23   name = params[:name]
24   ruby_version = params[:ruby] || "1.9.1"
25   rails_directory = params[:directory] || "/srv/#{name}"
26   rails_user = params[:user]
27   rails_group = params[:group]
28   rails_repository = params[:repository] || "git://git.openstreetmap.org/rails.git"
29   rails_revision = params[:revision] || "live"
30   run_migrations = params[:run_migrations] || false
31   status = params[:status] || "online"
32
33   database_params = {
34     :host => params[:database_host],
35     :port => params[:database_port],
36     :name => params[:database_name],
37     :username => params[:database_username],
38     :password => params[:database_password]
39   }
40
41   package "ruby#{ruby_version}"
42   package "ruby#{ruby_version}-dev"
43   package "rubygems#{ruby_version}"
44   package "irb#{ruby_version}"
45   package "imagemagick"
46
47   package "g++"
48   package "libpq-dev"
49   package "libsasl2-dev"
50   package "libxml2-dev"
51   package "libxslt1-dev"
52   package "libmemcached-dev"
53
54   gem_package "bundler#{ruby_version}" do
55     package_name "bundler"
56     version "1.3.5"
57     gem_binary "gem#{ruby_version}"
58     options "--format-executable"
59   end
60
61   file "/usr/lib/ruby/1.8/rack.rb" do
62     action :delete
63   end
64
65   directory "/usr/lib/ruby/1.8/rack" do
66     action :delete
67     recursive true
68   end
69
70   file "#{rails_directory}/tmp/restart.txt" do
71     action :nothing
72   end
73
74   file "#{rails_directory}/public/export/embed.html" do
75     action :nothing
76   end
77
78   execute "#{rails_directory}/public/assets" do
79     action :nothing
80     command "rake#{ruby_version} assets:precompile"
81     environment "RAILS_ENV" => "production"
82     cwd rails_directory
83     user rails_user
84     group rails_group
85     notifies :delete, resources(:file => "#{rails_directory}/public/export/embed.html")
86     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
87   end
88
89   execute "#{rails_directory}/db/migrate" do
90     action :nothing
91     command "rake#{ruby_version} db:migrate"
92     cwd rails_directory
93     user rails_user
94     group rails_group
95     notifies :run, resources(:execute => "#{rails_directory}/public/assets")
96   end
97
98   execute "#{rails_directory}/Gemfile" do
99     action :nothing
100     command "bundle#{ruby_version} install"
101     cwd rails_directory
102     user "root"
103     group "root"
104     if run_migrations
105       notifies :run, resources(:execute => "#{rails_directory}/db/migrate")
106     else
107       notifies :run, resources(:execute => "#{rails_directory}/public/assets")
108     end
109     subscribes :run, resources(:gem_package => "bundler#{ruby_version}")
110   end
111
112   directory rails_directory do
113     owner rails_user
114     group rails_group
115     mode 02775
116   end
117
118   git rails_directory do
119     action :sync
120     repository rails_repository
121     revision rails_revision
122     user rails_user
123     group rails_group
124     notifies :run, resources(:execute => "#{rails_directory}/Gemfile")
125   end
126
127   directory "#{rails_directory}/tmp" do
128     owner rails_user
129     group rails_group
130   end
131
132   file "#{rails_directory}/config/environment.rb" do
133     owner rails_user
134     group rails_group
135   end
136
137   template "#{rails_directory}/config/database.yml" do
138     cookbook "web"
139     source "database.yml.erb"
140     owner rails_user
141     group rails_group
142     mode 0664
143     variables database_params
144     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
145   end
146
147   file "#{rails_directory}/config/application.yml" do
148     owner rails_user
149     group rails_group
150     mode 0664
151     content_from_file "#{rails_directory}/config/example.application.yml" do |line|
152       line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
153
154       if params[:email_from]
155         line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
156       end
157
158       line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
159
160       if params[:messages_domain]
161         line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
162       end
163
164       line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
165
166       if params[:quova_username]
167         line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
168         line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
169       end
170
171       if params[:soft_memory_limit]
172         line.gsub!(/^( *)#soft_memory_limit:.*$/, "\\1soft_memory_limit: #{params[:soft_memory_limit]}")
173       end
174
175       if params[:hard_memory_limit]
176         line.gsub!(/^( *)#hard_memory_limit:.*$/, "\\1hard_memory_limit: #{params[:hard_memory_limit]}")
177       end
178
179       if params[:gpx_dir]
180         line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
181         line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
182       end
183
184       if params[:attachments_dir]
185         line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
186       end
187
188       if params[:log_path]
189         line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
190       end
191
192       if params[:memcache_servers]
193         line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
194       end
195
196       if params[:potlatch2_key]
197         line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
198       end
199
200       if params[:id_key]
201         line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
202       end
203
204       if params[:oauth_key]
205         line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
206       end
207
208       line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
209       line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
210
211       if params[:piwik_location]
212         line.gsub!(/^( *)#piwik_location:.*$/, "\\1piwik_location: \"#{params[:piwik_location]}\"")
213         line.gsub!(/^( *)#piwik_site:.*$/, "\\1piwik_site: #{params[:piwik_site]}")
214         line.gsub!(/^( *)#piwik_signup_goal:.*$/, "\\1piwik_signup_goal: #{params[:piwik_signup_goal]}")
215       end
216
217       line
218     end
219     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
220   end
221
222   if params[:piwik_configuration]
223     file "#{rails_directory}/config/piwik.yml" do
224       owner rails_user
225       group rails_group
226       mode 0664
227       content YAML.dump(params[:piwik_configuration])
228       notifies :run, resources(:execute => "#{rails_directory}/public/assets")
229     end
230   else
231     file "#{rails_directory}/config/piwik.yml" do
232       action :delete
233       notifies :run, resources(:execute => "#{rails_directory}/public/assets")
234     end
235   end
236
237   execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
238     command "ruby extconf.rb"
239     cwd "#{rails_directory}/lib/quad_tile"
240     user rails_user
241     group rails_group
242     not_if { File.exist?("#{rails_directory}/lib/quad_tile/Makefile") and File.mtime("#{rails_directory}/lib/quad_tile/Makefile") >= File.mtime("#{rails_directory}/lib/quad_tile/extconf.rb") }
243   end
244
245   execute "#{rails_directory}/lib/quad_tile/Makefile" do
246     command "make"
247     cwd "#{rails_directory}/lib/quad_tile"
248     user rails_user
249     group rails_group
250     not_if do
251       File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") and
252       File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") and
253       File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") and
254       File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
255     end
256     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
257   end
258
259   template "/etc/cron.daily/rails-#{name}" do
260     cookbook "web"
261     source "rails.cron.erb"
262     owner "root"
263     group "root"
264     mode 0755
265     variables :directory => rails_directory
266   end
267 end