]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/definitions/rails_port.rb
Update chef client to 12.2.1
[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   end
78
79   file "#{rails_directory}/public/export/embed.html" do
80     action :nothing
81   end
82
83   execute "#{rails_directory}/public/assets" do
84     action :nothing
85     command "bundle#{ruby_version} exec rake#{ruby_version} assets:precompile"
86     environment "RAILS_ENV" => "production"
87     cwd rails_directory
88     user rails_user
89     group rails_group
90     notifies :delete, "file[#{rails_directory}/public/export/embed.html]"
91     notifies :run, "execute[#{rails_directory}]"
92   end
93
94   execute "#{rails_directory}/db/migrate" do
95     action :nothing
96     command "bundle#{ruby_version} exec rake#{ruby_version} db:migrate"
97     cwd rails_directory
98     user rails_user
99     group rails_group
100     notifies :run, "execute[#{rails_directory}/public/assets]"
101   end
102
103   execute "#{rails_directory}/Gemfile" do
104     action :nothing
105     command "bundle#{ruby_version} install"
106     cwd rails_directory
107     user "root"
108     group "root"
109     environment "NOKOGIRI_USE_SYSTEM_LIBRARIES" => "yes"
110     if run_migrations
111       notifies :run, "execute[#{rails_directory}/db/migrate]"
112     else
113       notifies :run, "execute[#{rails_directory}/public/assets]"
114     end
115     subscribes :run, "gem_package[bundler#{ruby_version}]"
116   end
117
118   directory rails_directory do
119     owner rails_user
120     group rails_group
121     mode 02775
122   end
123
124   git rails_directory do
125     action :sync
126     repository rails_repository
127     revision rails_revision
128     user rails_user
129     group rails_group
130     notifies :run, "execute[#{rails_directory}/Gemfile]"
131   end
132
133   directory "#{rails_directory}/tmp" do
134     owner rails_user
135     group rails_group
136   end
137
138   file "#{rails_directory}/config/environment.rb" do
139     owner rails_user
140     group rails_group
141   end
142
143   template "#{rails_directory}/config/database.yml" do
144     cookbook "web"
145     source "database.yml.erb"
146     owner rails_user
147     group rails_group
148     mode 0664
149     variables database_params
150     notifies :run, "execute[#{rails_directory}]"
151   end
152
153   application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
154     line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
155
156     if params[:email_from]
157       line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
158     end
159
160     line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
161
162     if params[:messages_domain]
163       line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
164     end
165
166     line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
167
168     if params[:quova_username]
169       line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
170       line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
171     end
172
173     if params[:soft_memory_limit]
174       line.gsub!(/^( *)#soft_memory_limit:.*$/, "\\1soft_memory_limit: #{params[:soft_memory_limit]}")
175     end
176
177     if params[:hard_memory_limit]
178       line.gsub!(/^( *)#hard_memory_limit:.*$/, "\\1hard_memory_limit: #{params[:hard_memory_limit]}")
179     end
180
181     if params[:gpx_dir]
182       line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
183       line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
184     end
185
186     if params[:attachments_dir]
187       line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
188     end
189
190     if params[:log_path]
191       line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
192     end
193
194     if params[:memcache_servers]
195       line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
196     end
197
198     if params[:potlatch2_key]
199       line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
200     end
201
202     if params[:id_key]
203       line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
204     end
205
206     if params[:oauth_key]
207       line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
208     end
209
210     if params[:nominatim_url]
211       line.gsub!(/^( *)nominatim_url:.*$/, "\\1nominatim_url: \"#{params[:nominatim_url]}\"")
212     end
213
214     line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
215     line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
216
217     line
218   end
219
220   file "#{rails_directory}/config/application.yml" do
221     owner rails_user
222     group rails_group
223     mode 0664
224     content application_yml
225     notifies :run, "execute[#{rails_directory}]"
226   end
227
228   if params[:piwik_configuration]
229     file "#{rails_directory}/config/piwik.yml" do
230       owner rails_user
231       group rails_group
232       mode 0664
233       content YAML.dump(params[:piwik_configuration])
234       notifies :run, "execute[#{rails_directory}/public/assets]"
235     end
236   else
237     file "#{rails_directory}/config/piwik.yml" do
238       action :delete
239       notifies :run, "execute[#{rails_directory}/public/assets]"
240     end
241   end
242
243   execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
244     command "ruby extconf.rb"
245     cwd "#{rails_directory}/lib/quad_tile"
246     user rails_user
247     group rails_group
248     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") }
249   end
250
251   execute "#{rails_directory}/lib/quad_tile/Makefile" do
252     command "make"
253     cwd "#{rails_directory}/lib/quad_tile"
254     user rails_user
255     group rails_group
256     not_if do
257       File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") &&
258         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") &&
259         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") &&
260         File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
261     end
262     notifies :run, "execute[#{rails_directory}]"
263   end
264
265   template "/etc/cron.daily/rails-#{name}" do
266     cookbook "web"
267     source "rails.cron.erb"
268     owner "root"
269     group "root"
270     mode 0755
271     variables :directory => rails_directory
272   end
273 end