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