Require version 1.3.5 of bundler for ruby 2.0 support
[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 define :rails_port, :action => [ :create, :enable ] do
21   name = params[:name]
22   ruby_version = params[:ruby] || "1.9.1"
23   rails_directory = params[:directory] || "/srv/#{name}"
24   rails_user = params[:user]
25   rails_group = params[:group]
26   rails_repository = params[:repository] || "git://git.openstreetmap.org/rails.git"
27   rails_revision = params[:revision] || "live"
28   run_migrations = params[:run_migrations] || false
29   status = params[:status] || "online"
30
31   database_params = {
32     :host => params[:database_host],
33     :port => params[:database_port],
34     :name => params[:database_name],
35     :username => params[:database_username],
36     :password => params[:database_password]
37   }
38
39   package "ruby#{ruby_version}"
40   package "ruby#{ruby_version}-dev"
41   package "rubygems#{ruby_version}"
42   package "irb#{ruby_version}"
43   package "imagemagick"
44
45   package "g++"
46   package "libpq-dev"
47   package "libsasl2-dev"
48   package "libxml2-dev"
49   package "libxslt1-dev"
50   package "libmemcached-dev"
51
52   gem_package "bundler#{ruby_version}" do
53     package_name "bundler"
54     version "1.3.5"
55     gem_binary "gem#{ruby_version}"
56     options "--format-executable"
57   end
58
59   file "/usr/lib/ruby/1.8/rack.rb" do
60     action :delete
61   end
62
63   directory "/usr/lib/ruby/1.8/rack" do
64     action :delete
65     recursive true
66   end
67
68   file "#{rails_directory}/tmp/restart.txt" do
69     action :nothing
70   end
71
72   file "#{rails_directory}/public/export/embed.html" do
73     action :nothing
74   end
75
76   execute "#{rails_directory}/public/assets" do
77     action :nothing
78     command "rake#{ruby_version} assets:precompile"
79     cwd rails_directory
80     user rails_user
81     group rails_group
82     notifies :delete, resources(:file => "#{rails_directory}/public/export/embed.html")
83     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
84   end
85
86   execute "#{rails_directory}/db/migrate" do
87     action :nothing
88     command "rake#{ruby_version} db:migrate"
89     cwd rails_directory
90     user rails_user
91     group rails_group
92     notifies :run, resources(:execute => "#{rails_directory}/public/assets")
93   end
94
95   execute "#{rails_directory}/Gemfile" do
96     action :nothing
97     command "bundle#{ruby_version} install"
98     cwd rails_directory
99     user "root"
100     group "root"
101     if run_migrations
102       notifies :run, resources(:execute => "#{rails_directory}/db/migrate")
103     else
104       notifies :run, resources(:execute => "#{rails_directory}/public/assets")
105     end
106     subscribes :run, resources(:gem_package => "bundler#{ruby_version}")
107   end
108
109   directory rails_directory do
110     owner rails_user
111     group rails_group
112     mode 02775
113   end
114
115   git rails_directory do
116     action :sync
117     repository rails_repository
118     revision rails_revision
119     user rails_user
120     group rails_group
121     notifies :run, resources(:execute => "#{rails_directory}/Gemfile")
122   end
123
124   directory "#{rails_directory}/tmp" do
125     owner rails_user
126     group rails_group
127   end
128
129   file "#{rails_directory}/config/environment.rb" do
130     owner rails_user
131     group rails_group
132   end
133
134   template "#{rails_directory}/config/database.yml" do
135     cookbook "web"
136     source "database.yml.erb"
137     owner rails_user
138     group rails_group
139     mode 0664
140     variables database_params
141     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
142   end
143
144   file "#{rails_directory}/config/application.yml" do
145     owner rails_user
146     group rails_group
147     mode 0664
148     content_from_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       if params[:piwik_location]
209         line.gsub!(/^( *)#piwik_location:.*$/, "\\1piwik_location: \"#{params[:piwik_location]}\"")
210         line.gsub!(/^( *)#piwik_site:.*$/, "\\1piwik_site: #{params[:piwik_site]}")
211         line.gsub!(/^( *)#piwik_signup_goal:.*$/, "\\1piwik_signup_goal: #{params[:piwik_signup_goal]}")
212       end
213
214       line
215     end
216     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
217   end
218
219   execute "#{rails_directory}/lib/quad_tile/extconf.rb" do
220     command "ruby extconf.rb"
221     cwd "#{rails_directory}/lib/quad_tile"
222     user rails_user
223     group rails_group
224     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") }
225   end
226
227   execute "#{rails_directory}/lib/quad_tile/Makefile" do
228     command "make"
229     cwd "#{rails_directory}/lib/quad_tile"
230     user rails_user
231     group rails_group
232     not_if do
233       File.exist?("#{rails_directory}/lib/quad_tile/quad_tile_so.so") and
234       File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/Makefile") and
235       File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.c") and
236       File.mtime("#{rails_directory}/lib/quad_tile/quad_tile_so.so") >= File.mtime("#{rails_directory}/lib/quad_tile/quad_tile.h")
237     end
238     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
239   end
240
241   template "/etc/cron.daily/rails-#{name}" do
242     cookbook "web"
243     source "rails.cron.erb"
244     owner "root"
245     group "root"
246     mode 0755
247     variables :directory => rails_directory
248   end
249 end