Work around lack of support for lazy actions
[chef.git] / cookbooks / web / resources / rails_port.rb
1 #
2 # Cookbook Name:: web
3 # Resource:: 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 #     https://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 resource_name :rails_port
23
24 default_action :create
25
26 property :site, String, :name_attribute => true
27 property :ruby, String, :default => "2.3"
28 property :directory, String
29 property :user, String
30 property :group, String
31 property :repository, String, :default => "https://git.openstreetmap.org/public/rails.git"
32 property :revision, String, :default => "live"
33 property :run_migrations, [TrueClass, FalseClass], :default => false
34 property :email_from, String, :default => "OpenStreetMap <support@openstreetmap.org>"
35 property :status, String, :default => "online"
36 property :database_host, String
37 property :database_port, String
38 property :database_name, String
39 property :database_username, String
40 property :database_password, String
41 property :email_from, String
42 property :messages_domain, String
43 property :gpx_dir, String
44 property :attachments_dir, String
45 property :log_path, String
46 property :logstash_path, String
47 property :memcache_servers, Array
48 property :potlatch2_key, String
49 property :id_key, String
50 property :oauth_key, String
51 property :nominatim_url, String
52 property :osrm_url, String
53 property :google_auth_id, String
54 property :google_auth_secret, String
55 property :google_openid_realm, String
56 property :facebook_auth_id, String
57 property :facebook_auth_secret, String
58 property :windowslive_auth_id, String
59 property :windowslive_auth_secret, String
60 property :github_auth_id, String
61 property :github_auth_secret, String
62 property :wikipedia_auth_id, String
63 property :wikipedia_auth_secret, String
64 property :thunderforest_key, String
65 property :totp_key, String
66 property :csp_enforce, [TrueClass, FalseClass], :default => false
67 property :csp_report_url, String
68 property :piwik_configuration, Hash
69
70 action :create do
71   package %W[
72     ruby#{new_resource.ruby}
73     ruby#{new_resource.ruby}-dev
74     imagemagick
75     nodejs
76     geoip-database
77   ]
78
79   package %w[
80     g++
81     pkg-config
82     libpq-dev
83     libsasl2-dev
84     libxml2-dev
85     libxslt1-dev
86     libmemcached-dev
87     libffi-dev
88   ]
89
90   package %w[
91     pngcrush
92     advancecomp
93     optipng
94     pngquant
95     jhead
96     jpegoptim
97     gifsicle
98     libjpeg-turbo-progs
99   ]
100
101   gem_package "bundler#{new_resource.ruby}" do
102     package_name "bundler"
103     version "1.16.2"
104     gem_binary "gem#{new_resource.ruby}"
105     options "--format-executable"
106   end
107
108   gem_package "bundler#{new_resource.ruby}" do
109     package_name "pkg-config"
110     gem_binary "gem#{new_resource.ruby}"
111   end
112
113   declare_resource :directory, rails_directory do
114     owner new_resource.user
115     group new_resource.group
116     mode 0o2775
117   end
118
119   git rails_directory do
120     action :sync
121     repository new_resource.repository
122     revision new_resource.revision
123     user new_resource.user
124     group new_resource.group
125     notifies :run, "execute[#{rails_directory}/Gemfile]"
126     notifies :run, "execute[#{rails_directory}/app/assets/javascripts/i18n]"
127     notifies :run, "execute[#{rails_directory}/public/assets]"
128     notifies :delete, "file[#{rails_directory}/public/export/embed.html]"
129     notifies :restart, "passenger_application[#{rails_directory}]"
130   end
131
132   declare_resource :directory, "#{rails_directory}/tmp" do
133     owner new_resource.user
134     group new_resource.group
135   end
136
137   file "#{rails_directory}/config/environment.rb" do
138     owner new_resource.user
139     group new_resource.group
140   end
141
142   template "#{rails_directory}/config/database.yml" do
143     cookbook "web"
144     source "database.yml.erb"
145     owner new_resource.user
146     group new_resource.group
147     mode 0o664
148     variables :host => new_resource.database_host,
149               :port => new_resource.database_port,
150               :name => new_resource.database_name,
151               :username => new_resource.database_username,
152               :password => new_resource.database_password
153     notifies :restart, "passenger_application[#{rails_directory}]"
154   end
155
156   application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
157     line.gsub!(/^( *)server_protocol:.*$/, "\\1server_protocol: \"https\"")
158     line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{new_resource.site}\"")
159
160     line.gsub!(/^( *)#publisher_url:.*$/, "\\1publisher_url: \"https://plus.google.com/111953119785824514010\"")
161
162     line.gsub!(/^( *)support_email:.*$/, "\\1support_email: \"support@openstreetmap.org\"")
163
164     if new_resource.email_from
165       line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{new_resource.email_from}\"")
166     end
167
168     line.gsub!(/^( *)email_return_path:.*$/, "\\1email_return_path: \"bounces@openstreetmap.org\"")
169
170     line.gsub!(/^( *)status:.*$/, "\\1status: :#{new_resource.status}")
171
172     if new_resource.messages_domain
173       line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{new_resource.messages_domain}\"")
174     end
175
176     line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
177
178     line.gsub!(/^( *)#geoip_database:.*$/, "\\1geoip_database: \"/usr/share/GeoIP/GeoIPv6.dat\"")
179
180     if new_resource.gpx_dir
181       line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{new_resource.gpx_dir}/traces\"")
182       line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{new_resource.gpx_dir}/images\"")
183     end
184
185     if new_resource.attachments_dir
186       line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{new_resource.attachments_dir}\"")
187     end
188
189     if new_resource.log_path
190       line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{new_resource.log_path}\"")
191     end
192
193     if new_resource.logstash_path
194       line.gsub!(/^( *)#logstash_path:.*$/, "\\1logstash_path: \"#{new_resource.logstash_path}\"")
195     end
196
197     if new_resource.memcache_servers
198       line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{new_resource.memcache_servers.join('", "')}\" ]")
199     end
200
201     if new_resource.potlatch2_key
202       line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{new_resource.potlatch2_key}\"")
203     end
204
205     if new_resource.id_key
206       line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{new_resource.id_key}\"")
207     end
208
209     if new_resource.oauth_key
210       line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{new_resource.oauth_key}\"")
211     end
212
213     if new_resource.nominatim_url
214       line.gsub!(/^( *)nominatim_url:.*$/, "\\1nominatim_url: \"#{new_resource.nominatim_url}\"")
215     end
216
217     if new_resource.osrm_url
218       line.gsub!(/^( *)osrm_url:.*$/, "\\1osrm_url: \"#{new_resource.osrm_url}\"")
219     end
220
221     if new_resource.google_auth_id
222       line.gsub!(/^( *)#google_auth_id:.*$/, "\\1google_auth_id: \"#{new_resource.google_auth_id}\"")
223       line.gsub!(/^( *)#google_auth_secret:.*$/, "\\1google_auth_secret: \"#{new_resource.google_auth_secret}\"")
224       line.gsub!(/^( *)#google_openid_realm:.*$/, "\\1google_openid_realm: \"#{new_resource.google_openid_realm}\"")
225     end
226
227     if new_resource.facebook_auth_id
228       line.gsub!(/^( *)#facebook_auth_id:.*$/, "\\1facebook_auth_id: \"#{new_resource.facebook_auth_id}\"")
229       line.gsub!(/^( *)#facebook_auth_secret:.*$/, "\\1facebook_auth_secret: \"#{new_resource.facebook_auth_secret}\"")
230     end
231
232     if new_resource.windowslive_auth_id
233       line.gsub!(/^( *)#windowslive_auth_id:.*$/, "\\1windowslive_auth_id: \"#{new_resource.windowslive_auth_id}\"")
234       line.gsub!(/^( *)#windowslive_auth_secret:.*$/, "\\1windowslive_auth_secret: \"#{new_resource.windowslive_auth_secret}\"")
235     end
236
237     if new_resource.github_auth_id
238       line.gsub!(/^( *)#github_auth_id:.*$/, "\\1github_auth_id: \"#{new_resource.github_auth_id}\"")
239       line.gsub!(/^( *)#github_auth_secret:.*$/, "\\1github_auth_secret: \"#{new_resource.github_auth_secret}\"")
240     end
241
242     if new_resource.wikipedia_auth_id
243       line.gsub!(/^( *)#wikipedia_auth_id:.*$/, "\\1wikipedia_auth_id: \"#{new_resource.wikipedia_auth_id}\"")
244       line.gsub!(/^( *)#wikipedia_auth_secret:.*$/, "\\1wikipedia_auth_secret: \"#{new_resource.wikipedia_auth_secret}\"")
245     end
246
247     if new_resource.mapquest_key
248       line.gsub!(/^( *)#mapquest_key:.*$/, "\\1mapquest_key: \"#{new_resource.mapquest_key}\"")
249     end
250
251     if new_resource.mapzen_valhalla_key
252       line.gsub!(/^( *)#mapzen_valhalla_key:.*$/, "\\1mapzen_valhalla_key: \"#{new_resource.mapzen_valhalla_key}\"")
253     end
254
255     if new_resource.thunderforest_key
256       line.gsub!(/^( *)#thunderforest_key:.*$/, "\\1thunderforest_key: \"#{new_resource.thunderforest_key}\"")
257     end
258
259     if new_resource.totp_key
260       line.gsub!(/^( *)#totp_key:.*$/, "\\1totp_key: \"#{new_resource.totp_key}\"")
261     end
262
263     if new_resource.csp_enforce
264       line.gsub!(/^( *)csp_enforce:.*$/, "\\1csp_enforce: \"#{new_resource.csp_enforce}\"")
265     end
266
267     if new_resource.csp_report_url
268       line.gsub!(/^( *)#csp_report_url:.*$/, "\\1csp_report_url: \"#{new_resource.csp_report_url}\"")
269     end
270
271     line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
272     line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
273     line.gsub!(/^( *)trace_use_job_queue:.*$/, "\\1trace_use_job_queue: false")
274
275     line
276   end
277
278   file "create:#{rails_directory}/config/application.yml" do
279     path "#{rails_directory}/config/application.yml"
280     owner new_resource.user
281     group new_resource.group
282     mode 0o664
283     content application_yml
284     notifies :run, "execute[#{rails_directory}/public/assets]"
285     only_if { File.exist?("#{rails_directory}/config/example.application.yml") }
286   end
287
288   file "delete:#{rails_directory}/config/application.yml" do
289     path "#{rails_directory}/config/application.yml"
290     action :delete
291     not_if { File.exist?("#{rails_directory}/config/example.application.yml") }
292   end
293
294   settings = new_resource.slice(
295     "email_from",
296     "status",
297     "messages_domain",
298     "attachments_dir",
299     "log_path",
300     "logstash_path",
301     "memcache_servers",
302     "potlatch2_key",
303     "id_key",
304     "oauth_key",
305     "nominatim_url",
306     "osrm_url",
307     "google_auth_id",
308     "google_auth_secret",
309     "google_openid_realm",
310     "facebook_auth_id",
311     "facebook_auth_secret",
312     "windowslive_auth_id",
313     "windowslive_auth_secret",
314     "github_auth_id",
315     "gihub_auth_secret",
316     "wikipedia_auth_id",
317     "wikipedia_auth_secret",
318     "thunderforest_key",
319     "totp_key",
320     "csp_enforce",
321     "csp_report_url"
322   ).merge(
323     "server_protocol" => "https",
324     "server" => new_resource.site,
325     "publisher_url" => "https://plus.google.com/111953119785824514010",
326     "support_email" => "support@openstreetmap.org",
327     "email_return_path" => "bounces@openstreetmap.org",
328     "geonames_username" => "openstreetmap",
329     "geoip_database" => "/usr/share/GeoIP/GeoIPv6.dat",
330     "trace_use_job_queue" => false
331   )
332
333   if new_resource.gpx_dir
334     settings["gpx_trace_dir"] = "#{new_resource.gpx_dir}/traces"
335     settings["gpx_image_dir"] = "#{new_resource.gpx_dir}/images"
336   end
337
338   file "#{rails_directory}/config/settings.local.yml" do
339     owner new_resource.user
340     group new_resource.group
341     mode 0o664
342     content YAML.dump(settings)
343     notifies :run, "execute[#{rails_directory}/public/assets]"
344     only_if { File.exist?("#{rails_directory}/config/settings.yml") }
345   end
346
347   if new_resource.piwik_configuration
348     file "#{rails_directory}/config/piwik.yml" do
349       owner new_resource.user
350       group new_resource.group
351       mode 0o664
352       content YAML.dump(new_resource.piwik_configuration)
353       notifies :run, "execute[#{rails_directory}/public/assets]"
354     end
355   else
356     file "#{rails_directory}/config/piwik.yml" do
357       action :delete
358       notifies :run, "execute[#{rails_directory}/public/assets]"
359     end
360   end
361
362   execute "#{rails_directory}/Gemfile" do
363     action :nothing
364     command "bundle#{new_resource.ruby} install"
365     cwd rails_directory
366     user "root"
367     group "root"
368     environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
369     subscribes :run, "gem_package[bundler#{new_resource.ruby}]"
370     notifies :restart, "passenger_application[#{rails_directory}]"
371   end
372
373   execute "#{rails_directory}/db/migrate" do
374     action :nothing
375     command "bundle#{new_resource.ruby} exec rake db:migrate"
376     cwd rails_directory
377     user new_resource.user
378     group new_resource.group
379     subscribes :run, "git[#{rails_directory}]"
380     notifies :restart, "passenger_application[#{rails_directory}]"
381     only_if { new_resource.run_migrations }
382   end
383
384   execute "#{rails_directory}/app/assets/javascripts/i18n" do
385     action :nothing
386     command "bundle#{new_resource.ruby} exec rake i18n:js:export"
387     environment "RAILS_ENV" => "production"
388     cwd rails_directory
389     user new_resource.user
390     group new_resource.group
391     notifies :run, "execute[#{rails_directory}/public/assets]"
392   end
393
394   execute "#{rails_directory}/public/assets" do
395     action :nothing
396     command "bundle#{new_resource.ruby} exec rake assets:precompile"
397     environment "RAILS_ENV" => "production"
398     cwd rails_directory
399     user new_resource.user
400     group new_resource.group
401     notifies :restart, "passenger_application[#{rails_directory}]"
402   end
403
404   file "#{rails_directory}/public/export/embed.html" do
405     action :nothing
406   end
407
408   passenger_application rails_directory do
409     action :nothing
410     only_if { ::File.exist?("/usr/bin/passenger-config") }
411   end
412
413   template "/etc/cron.daily/rails-#{new_resource.site.tr('.', '-')}" do
414     cookbook "web"
415     source "rails.cron.erb"
416     owner "root"
417     group "root"
418     mode 0o755
419     variables :directory => rails_directory
420   end
421 end
422
423 action :restart do
424   passenger_application rails_directory do
425     action :restart
426   end
427 end
428
429 action_class do
430   include Chef::Mixin::EditFile
431
432   def rails_directory
433     new_resource.directory || "/srv/#{new_resource.site}"
434   end
435 end