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