]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/definitions/rails_port.rb
Don't try and restart rails when passenger isn't installed
[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}" if ruby_version.to_f < 1.9
44   package "irb#{ruby_version}" if ruby_version.to_f < 1.9
45   package "imagemagick"
46   package "nodejs"
47
48   package "g++"
49   package "pkg-config"
50   package "libpq-dev"
51   package "libsasl2-dev"
52   package "libxml2-dev"
53   package "libxslt1-dev"
54   package "libmemcached-dev"
55
56   gem_package "bundler#{ruby_version}" do
57     package_name "bundler"
58     version "1.3.5"
59     gem_binary "gem#{ruby_version}"
60     options "--format-executable"
61   end
62
63   file "/usr/lib/ruby/1.8/rack.rb" do
64     action :delete
65   end
66
67   directory "/usr/lib/ruby/1.8/rack" do
68     action :delete
69     recursive true
70   end
71
72   execute rails_directory do
73     action :nothing
74     command "passenger-config restart-app #{rails_directory}"
75     user "root"
76     group "root"
77     only_if { File.exist?("/usr/bin/passenger-config") }
78   end
79
80   file "#{rails_directory}/public/export/embed.html" do
81     action :nothing
82   end
83
84   execute "#{rails_directory}/public/assets" do
85     action :nothing
86     command "bundle#{ruby_version} exec rake#{ruby_version} assets:precompile"
87     environment "RAILS_ENV" => "production"
88     cwd rails_directory
89     user rails_user
90     group rails_group
91     notifies :delete, "file[#{rails_directory}/public/export/embed.html]"
92     notifies :run, "execute[#{rails_directory}]"
93   end
94
95   execute "#{rails_directory}/db/migrate" do
96     action :nothing
97     command "bundle#{ruby_version} exec rake#{ruby_version} db:migrate"
98     cwd rails_directory
99     user rails_user
100     group rails_group
101     notifies :run, "execute[#{rails_directory}/public/assets]"
102   end
103
104   execute "#{rails_directory}/Gemfile" do
105     action :nothing
106     command "bundle#{ruby_version} install"
107     cwd rails_directory
108     user "root"
109     group "root"
110     environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
111     if run_migrations
112       notifies :run, "execute[#{rails_directory}/db/migrate]"
113     else
114       notifies :run, "execute[#{rails_directory}/public/assets]"
115     end
116     subscribes :run, "gem_package[bundler#{ruby_version}]"
117   end
118
119   directory rails_directory do
120     owner rails_user
121     group rails_group
122     mode 02775
123   end
124
125   git rails_directory do
126     action :sync
127     repository rails_repository
128     revision rails_revision
129     user rails_user
130     group rails_group
131     notifies :run, "execute[#{rails_directory}/Gemfile]"
132   end
133
134   directory "#{rails_directory}/tmp" do
135     owner rails_user
136     group rails_group
137   end
138
139   file "#{rails_directory}/config/environment.rb" do
140     owner rails_user
141     group rails_group
142   end
143
144   template "#{rails_directory}/config/database.yml" do
145     cookbook "web"
146     source "database.yml.erb"
147     owner rails_user
148     group rails_group
149     mode 0664
150     variables database_params
151     notifies :run, "execute[#{rails_directory}]"
152   end
153
154   application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
155     line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
156
157     if params[:email_from]
158       line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
159     end
160
161     line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
162
163     if params[:messages_domain]
164       line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
165     end
166
167     line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
168
169     if params[:quova_username]
170       line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
171       line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
172     end
173
174     if params[:gpx_dir]
175       line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
176       line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
177     end
178
179     if params[:attachments_dir]
180       line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
181     end
182
183     if params[:log_path]
184       line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
185     end
186
187     if params[:memcache_servers]
188       line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
189     end
190
191     if params[:potlatch2_key]
192       line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
193     end
194
195     if params[:id_key]
196       line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
197     end
198
199     if params[:oauth_key]
200       line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
201     end
202
203     if params[:nominatim_url]
204       line.gsub!(/^( *)nominatim_url:.*$/, "\\1nominatim_url: \"#{params[:nominatim_url]}\"")
205     end
206
207     line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
208     line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
209
210     line
211   end
212
213   file "#{rails_directory}/config/application.yml" do
214     owner rails_user
215     group rails_group
216     mode 0664
217     content application_yml
218     notifies :run, "execute[#{rails_directory}]"
219   end
220
221   if params[:piwik_configuration]
222     file "#{rails_directory}/config/piwik.yml" do
223       owner rails_user
224       group rails_group
225       mode 0664
226       content YAML.dump(params[:piwik_configuration])
227       notifies :run, "execute[#{rails_directory}/public/assets]"
228     end
229   else
230     file "#{rails_directory}/config/piwik.yml" do
231       action :delete
232       notifies :run, "execute[#{rails_directory}/public/assets]"
233     end
234   end
235
236   execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
237     command "ruby extconf.rb"
238     cwd "#{rails_directory}/lib/quad_tile"
239     user rails_user
240     group rails_group
241     not_if { File.exist?("#{rails_directory}/lib/quad_tile/Makefile") && File.mtime("#{rails_directory}/lib/quad_tile/Makefile") >= File.mtime("#{rails_directory}/lib/quad_tile/extconf.rb") }
242   end
243
244   execute "#{rails_directory}/lib/quad_tile/Makefile" do
245     command "make"
246     cwd "#{rails_directory}/lib/quad_tile"
247     user rails_user
248     group rails_group
249     not_if do
250       File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") &&
251         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") &&
252         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") &&
253         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
254     end
255     notifies :run, "execute[#{rails_directory}]"
256   end
257
258   template "/etc/cron.daily/rails-#{name}" do
259     cookbook "web"
260     source "rails.cron.erb"
261     owner "root"
262     group "root"
263     mode 0755
264     variables :directory => rails_directory
265   end
266 end