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