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