]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/recipes/rails.rb
web: use aws databag
[chef.git] / cookbooks / web / recipes / rails.rb
1 #
2 # Cookbook:: web
3 # Recipe:: rails
4 #
5 # Copyright:: 2011, 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 #     https://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 include_recipe "apache"
21 include_recipe "apt"
22 include_recipe "git"
23 include_recipe "geoipupdate"
24 include_recipe "nodejs"
25 include_recipe "passenger"
26 include_recipe "ruby"
27 include_recipe "tools"
28 include_recipe "web::base"
29
30 web_passwords = data_bag_item("web", "passwords")
31 db_passwords = data_bag_item("db", "passwords")
32 aws_credentials = data_bag_item("web", "aws")
33
34 ssl_certificate "www.openstreetmap.org" do
35   domains ["www.openstreetmap.org", "www.osm.org", "www.openstreetmap.com",
36            "api.openstreetmap.org", "api.osm.org",
37            "maps.openstreetmap.org", "maps.osm.org",
38            "mapz.openstreetmap.org", "mapz.osm.org",
39            "openstreetmap.org", "osm.org", "openstreetmap.com"]
40   notifies :reload, "service[apache2]"
41 end
42
43 nodejs_package "svgo"
44
45 rails_directory = "#{node[:web][:base_directory]}/rails"
46
47 matomo = data_bag_item("web", "matomo")
48
49 aws_access_key_id = aws_credentials["web_access_key_id"]
50 aws_secret_access_key = aws_credentials["web_secret_access_key"]
51
52 storage = {
53   "avatars" => {
54     "service" => "S3",
55     "access_key_id" => aws_access_key_id,
56     "secret_access_key" => aws_secret_access_key,
57     "region" => "eu-west-1",
58     "bucket" => "openstreetmap-user-avatars",
59     "public" => true,
60     "use_dualstack_endpoint" => true,
61     "upload" => {
62       "acl" => "public-read",
63       "cache_control" => "public, max-age=31536000, immutable"
64     }
65   },
66   "gps_traces" => {
67     "service" => "S3",
68     "access_key_id" => aws_access_key_id,
69     "secret_access_key" => aws_secret_access_key,
70     "region" => "eu-west-1",
71     "bucket" => "openstreetmap-gps-traces",
72     "use_dualstack_endpoint" => true,
73     "public" => true,
74     "upload" => {
75       "acl" => "public-read",
76       "cache_control" => "public, max-age=31536000, immutable"
77     }
78   },
79   "gps_images" => {
80     "service" => "S3",
81     "access_key_id" => aws_access_key_id,
82     "secret_access_key" => aws_secret_access_key,
83     "region" => "eu-west-1",
84     "bucket" => "openstreetmap-gps-images",
85     "use_dualstack_endpoint" => true,
86     "public" => true,
87     "upload" => {
88       "acl" => "public-read",
89       "cache_control" => "public, max-age=31536000, immutable"
90     }
91   }
92 }
93
94 db_host = if node[:web][:status] == "database_readonly"
95             node[:web][:readonly_database_host]
96           else
97             node[:web][:database_host]
98           end
99
100 rails_port "www.openstreetmap.org" do
101   directory rails_directory
102   user "rails"
103   group "rails"
104   repository "https://git.openstreetmap.org/public/rails.git"
105   revision "live"
106   database_host db_host
107   database_name "openstreetmap"
108   database_username "rails"
109   database_password db_passwords["rails"]
110   email_from "OpenStreetMap <web@noreply.openstreetmap.org>"
111   status node[:web][:status]
112   messages_domain "messages.openstreetmap.org"
113   log_path "#{node[:web][:log_directory]}/rails.log"
114   memcache_servers node[:web][:memcached_servers]
115   potlatch2_key web_passwords["potlatch2_key"]
116   id_key web_passwords["id_key"]
117   id_application web_passwords["id_application"]
118   oauth_key web_passwords["oauth_key"]
119   oauth_application web_passwords["oauth_application"]
120   matomo_configuration "location" => matomo[:location],
121                        "site" => matomo[:site],
122                        "visitor_cookie_timeout" => matomo[:visitor_cookie_timeout],
123                        "referral_cookie_timeout" => matomo[:referral_cookie_timeout],
124                        "session_cookie_timeout" => matomo[:session_cookie_timeout],
125                        "goals" => matomo[:goals].to_hash
126   google_auth_id "651529786092-6c5ahcu0tpp95emiec8uibg11asmk34t.apps.googleusercontent.com"
127   google_auth_secret web_passwords["google_auth_secret"]
128   google_openid_realm "https://www.openstreetmap.org"
129   apple_auth_id "org.openstreetmap.www"
130   apple_team_id "MF99FFFD4L"
131   apple_key_id "73A2H3M7J3"
132   apple_private_key web_passwords["apple_private_key"].join("\n")
133   facebook_auth_id "427915424036881"
134   facebook_auth_secret web_passwords["facebook_auth_secret"]
135   microsoft_auth_id "e34f14f1-f790-40f3-9fa4-3c5f1a027c38"
136   microsoft_auth_secret web_passwords["microsoft_auth_secret"]
137   github_auth_id "acf7da34edee99e35499"
138   github_auth_secret web_passwords["github_auth_secret"]
139   wikipedia_auth_id "e4fe0c2c5855d23ed7e1f1c0fa1f1c58"
140   wikipedia_auth_secret web_passwords["wikipedia_auth_secret"]
141   thunderforest_key web_passwords["thunderforest_key"]
142   tracestrack_key web_passwords["tracestrack_key"]
143   maptiler_key web_passwords["maptiler_key"]
144   totp_key web_passwords["totp_key"]
145   csp_enforce true
146   trace_use_job_queue true
147   diary_feed_delay 12
148   storage_configuration storage
149   avatar_storage "avatars"
150   trace_file_storage "gps_traces"
151   trace_image_storage "gps_images"
152   trace_icon_storage "gps_images"
153   avatar_storage_url "https://openstreetmap-user-avatars.s3.dualstack.eu-west-1.amazonaws.com"
154   trace_image_storage_url "https://openstreetmap-gps-images.s3.dualstack.eu-west-1.amazonaws.com"
155   overpass_url "https://query.openstreetmap.org/query-features"
156   overpass_credentials true
157   signup_ip_per_day 24
158   signup_ip_max_burst 48
159   signup_email_per_day 1
160   signup_email_max_burst 2
161   doorkeeper_signing_key web_passwords["openid_connect_key"].join("\n")
162   user_account_deletion_delay 7 * 24
163   # Requests to modify the imagery blacklist should come from the DWG only
164   imagery_blacklist [
165     # Current Google imagery URLs have google or googleapis in the domain
166     ".*\\.google(apis)?\\..*/.*",
167     # Blacklist VWorld
168     "https?://xdworld\\.vworld\\.kr[/:].*",
169     # Blacklist here
170     ".*\\.here\\.com[/:].*",
171     # Blacklist Mapy.cz
172     ".*\\.mapy\\.cz.*",
173     # Blacklist Yandex
174     ".*\\.api-maps\\.yandex\\.ru/.*",
175     ".*\\.maps\\.yandex\\.net/.*",
176     # Blacklist 2gis
177     ".*\\.maps\\.2gis\\.com/.*"
178   ]
179 end
180
181 systemd_service "rails-jobs@" do
182   description "Rails job queue runner"
183   type "simple"
184   environment "RAILS_ENV" => "production",
185               "QUEUE" => "%I",
186               "SLEEP_DELAY" => "60",
187               "SECRET_KEY_BASE" => web_passwords["secret_key_base"]
188   user "rails"
189   working_directory rails_directory
190   exec_start "#{node[:ruby][:bundle]} exec rails jobs:work"
191   restart "on-failure"
192   nice 10
193   sandbox :enable_network => true
194   memory_deny_write_execute false
195   read_write_paths "/var/log/web"
196 end
197
198 package "libjson-xs-perl"
199
200 template "/usr/local/bin/cleanup-rails-assets" do
201   source "cleanup-assets.erb"
202   owner "root"
203   group "root"
204   mode "755"
205 end
206
207 gem_package "apachelogregex" do
208   gem_binary node[:ruby][:gem]
209 end
210
211 gem_package "file-tail" do
212   gem_binary node[:ruby][:gem]
213 end
214
215 template "/usr/local/bin/api-statistics" do
216   source "api-statistics.erb"
217   owner "root"
218   group "root"
219   mode "755"
220 end
221
222 systemd_service "api-statistics" do
223   description "OpenStreetMap API Statistics Daemon"
224   user "rails"
225   group "adm"
226   exec_start "/usr/local/bin/api-statistics"
227   nice 10
228   sandbox true
229   read_write_paths [
230     "/srv/www.openstreetmap.org/rails/tmp",
231     "/var/lib/prometheus/node-exporter"
232   ]
233   restart "on-failure"
234 end
235
236 service "api-statistics" do
237   action [:enable, :start]
238   supports :restart => true
239   subscribes :restart, "template[/usr/local/bin/api-statistics]"
240   subscribes :restart, "systemd_service[api-statistics]"
241 end
242
243 gem_package "hpricot" do
244   gem_binary node[:ruby][:gem]
245 end