]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/recipes/replication.rb
Copy notes dumps to S3
[chef.git] / cookbooks / planet / recipes / replication.rb
1 #
2 # Cookbook:: planet
3 # Recipe:: dump
4 #
5 # Copyright:: 2013, 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 require "yaml"
21
22 include_recipe "accounts"
23 include_recipe "apt"
24 include_recipe "osmosis"
25 include_recipe "ruby"
26 include_recipe "tools"
27 include_recipe "awscli"
28
29 db_passwords = data_bag_item("db", "passwords")
30
31 ## Install required packages
32
33 package %w[
34   postgresql-client
35   ruby-libxml
36   make
37   gcc
38   libc6-dev
39   libpq-dev
40   osmdbt
41 ]
42
43 gem_package "pg" do
44   gem_binary node[:ruby][:gem]
45 end
46
47 ## Build preload library to flush files
48
49 remote_directory "/opt/flush" do
50   source "flush"
51   owner "root"
52   group "root"
53   mode "755"
54   files_owner "root"
55   files_group "root"
56   files_mode "755"
57 end
58
59 execute "/opt/flush/Makefile" do
60   action :nothing
61   command "make"
62   cwd "/opt/flush"
63   user "root"
64   group "root"
65   subscribes :run, "remote_directory[/opt/flush]"
66 end
67
68 ## Install scripts
69
70 remote_directory "/usr/local/bin" do
71   source "replication-bin"
72   owner "root"
73   group "root"
74   mode "755"
75   files_owner "root"
76   files_group "root"
77   files_mode "755"
78 end
79
80 template "/usr/local/bin/users-agreed" do
81   source "users-agreed.erb"
82   owner "root"
83   group "root"
84   mode "755"
85 end
86
87 template "/usr/local/bin/users-deleted" do
88   source "users-deleted.erb"
89   owner "root"
90   group "root"
91   mode "755"
92 end
93
94 ## Published deleted users directory
95
96 remote_directory "/store/planet/users_deleted" do
97   source "users_deleted"
98   owner "planet"
99   group "planet"
100   mode "755"
101   files_owner "root"
102   files_group "root"
103   files_mode "644"
104 end
105
106 ## Published replication directory
107
108 remote_directory "/store/planet/replication" do
109   source "replication-cgi"
110   owner "root"
111   group "root"
112   mode "755"
113   files_owner "root"
114   files_group "root"
115   files_mode "755"
116 end
117
118 ## Configuration directory
119
120 directory "/etc/replication" do
121   owner "root"
122   group "root"
123   mode "755"
124 end
125
126 ## Transient state directory
127
128 systemd_tmpfile "/run/replication" do
129   type "d"
130   owner "planet"
131   group "planet"
132   mode "755"
133 end
134
135 ## Persistent state directory
136
137 directory "/var/lib/replication" do
138   owner "planet"
139   group "planet"
140   mode "755"
141 end
142
143 ## Temporary directory
144
145 directory "/store/replication" do
146   owner "planet"
147   group "planet"
148   mode "755"
149 end
150
151 ## Users replication
152
153 template "/etc/replication/users-agreed.conf" do
154   source "users-agreed.conf.erb"
155   user "planet"
156   group "planet"
157   mode "600"
158   variables :password => db_passwords["planetdiff"]
159 end
160
161 systemd_service "users-agreed" do
162   description "Update list of users accepting CTs"
163   user "planet"
164   exec_start "/usr/local/bin/users-agreed"
165   nice 10
166   sandbox :enable_network => true
167   read_write_paths "/store/planet/users_agreed"
168 end
169
170 systemd_timer "users-agreed" do
171   description "Update list of users accepting CTs"
172   on_calendar "7:00"
173 end
174
175 systemd_service "users-deleted" do
176   description "Update list of deleted users"
177   user "planet"
178   exec_start "/usr/local/bin/users-deleted"
179   nice 10
180   sandbox :enable_network => true
181   read_write_paths "/store/planet/users_deleted"
182 end
183
184 systemd_timer "users-deleted" do
185   description "Update list of deleted users"
186   on_calendar "17:00"
187 end
188
189 ## Changeset replication
190
191 directory "/store/planet/replication/changesets" do
192   owner "planet"
193   group "planet"
194   mode "755"
195 end
196
197 template "/etc/replication/changesets.conf" do
198   source "changesets.conf.erb"
199   user "root"
200   group "planet"
201   mode "640"
202   variables :password => db_passwords["planetdiff"]
203 end
204
205 systemd_service "replication-changesets" do
206   description "Changesets replication"
207   user "planet"
208   exec_start "/usr/local/bin/replicate-changesets /etc/replication/changesets.conf"
209   sandbox :enable_network => true
210   read_write_paths [
211     "/run/replication",
212     "/store/planet/replication/changesets"
213   ]
214 end
215
216 systemd_timer "replication-changesets" do
217   description "Changesets replication"
218   on_boot_sec 60
219   on_unit_active_sec 60
220   accuracy_sec 5
221 end
222
223 ## Minutely replication
224
225 directory "/store/planet/replication/minute" do
226   owner "planet"
227   group "planet"
228   mode "755"
229 end
230
231 directory "/var/lib/replication/minute" do
232   owner "planet"
233   group "planet"
234   mode "755"
235 end
236
237 directory "/store/replication/minute" do
238   owner "planet"
239   group "planet"
240   mode "755"
241 end
242
243 osmdbt_config = {
244   "database" => {
245     "host" => node[:web][:database_host],
246     "dbname" => "openstreetmap",
247     "user" => "planetdiff",
248     "password" => db_passwords["planetdiff"],
249     "replication_slot" => "osmdbt"
250   },
251   "log_dir" => "/var/lib/replication/minute",
252   "changes_dir" => "/store/planet/replication/minute",
253   "tmp_dir" => "/store/replication/minute",
254   "run_dir" => "/run/replication"
255 }
256
257 file "/etc/replication/osmdbt-config.yaml" do
258   user "root"
259   group "planet"
260   mode "640"
261   content YAML.dump(osmdbt_config)
262 end
263
264 systemd_service "replication-minutely" do
265   description "Minutely replication"
266   user "planet"
267   working_directory "/etc/replication"
268   exec_start "/usr/local/bin/replicate-minute"
269   sandbox :enable_network => true
270   read_write_paths [
271     "/run/replication",
272     "/store",
273     "/var/lib/replication/minute"
274   ]
275 end
276
277 systemd_timer "replication-minutely" do
278   description "Minutely replication"
279   on_boot_sec 60
280   on_unit_active_sec 60
281   accuracy_sec 5
282 end
283
284 ## Hourly replication
285
286 directory "/store/planet/replication/hour" do
287   owner "planet"
288   group "planet"
289   mode "755"
290 end
291
292 directory "/var/lib/replication/hour" do
293   owner "planet"
294   group "planet"
295   mode "755"
296 end
297
298 link "/var/lib/replication/hour/data" do
299   to "/store/planet/replication/hour"
300 end
301
302 template "/var/lib/replication/hour/configuration.txt" do
303   source "replication.config.erb"
304   owner "planet"
305   group "planet"
306   mode "644"
307   variables :base => "minute", :interval => 3600
308 end
309
310 systemd_service "replication-hourly" do
311   description "Hourly replication"
312   user "planet"
313   exec_start "/usr/local/bin/osmosis -q --merge-replication-files workingDirectory=/var/lib/replication/hour"
314   environment "LD_PRELOAD" => "/opt/flush/flush.so"
315   sandbox :enable_network => true
316   memory_deny_write_execute false
317   read_write_paths [
318     "/store/planet/replication/hour",
319     "/var/lib/replication/hour"
320   ]
321 end
322
323 systemd_timer "replication-hourly" do
324   description "Hourly replication"
325   on_calendar "*-*-* *:02/15:00"
326 end
327
328 ## Daily replication
329
330 directory "/store/planet/replication/day" do
331   owner "planet"
332   group "planet"
333   mode "755"
334 end
335
336 directory "/var/lib/replication/day" do
337   owner "planet"
338   group "planet"
339   mode "755"
340 end
341
342 link "/var/lib/replication/day/data" do
343   to "/store/planet/replication/day"
344 end
345
346 template "/var/lib/replication/day/configuration.txt" do
347   source "replication.config.erb"
348   owner "planet"
349   group "planet"
350   mode "644"
351   variables :base => "hour", :interval => 86400
352 end
353
354 systemd_service "replication-daily" do
355   description "Daily replication"
356   user "planet"
357   exec_start "/usr/local/bin/osmosis -q --merge-replication-files workingDirectory=/var/lib/replication/day"
358   environment "LD_PRELOAD" => "/opt/flush/flush.so"
359   sandbox :enable_network => true
360   memory_deny_write_execute false
361   read_write_paths [
362     "/store/planet/replication/day",
363     "/var/lib/replication/day"
364   ]
365 end
366
367 systemd_timer "replication-daily" do
368   description "Daily replication"
369   on_calendar "*-*-* *:02/15:00"
370 end
371
372 ## Replication cleanup
373
374 systemd_service "replication-cleanup" do
375   description "Cleanup replication"
376   user "planet"
377   exec_start "/usr/local/bin/replicate-cleanup"
378   sandbox true
379   read_write_paths "/var/lib/replication"
380 end
381
382 systemd_timer "replication-cleanup" do
383   description "Cleanup replication"
384   on_boot_sec 60
385   on_unit_active_sec 86400
386   accuracy_sec 1800
387 end
388
389 ## Enable/disable feeds
390
391 if node[:planet][:replication] == "enabled"
392   service "users-agreed.timer" do
393     action [:enable, :start]
394   end
395
396   service "users-deleted.timer" do
397     action [:enable, :start]
398   end
399
400   service "replication-changesets.timer" do
401     action [:enable, :start]
402   end
403
404   service "replication-minutely.timer" do
405     action [:enable, :start]
406   end
407
408   service "replication-hourly.timer" do
409     action [:enable, :start]
410   end
411
412   service "replication-daily.timer" do
413     action [:enable, :start]
414   end
415
416   service "replication-cleanup.timer" do
417     action [:enable, :start]
418   end
419 else
420   service "users-agreed.timer" do
421     action [:stop, :disable]
422   end
423
424   service "users-deleted.timer" do
425     action [:stop, :disable]
426   end
427
428   service "replication-changesets.timer" do
429     action [:stop, :disable]
430   end
431
432   service "replication-minutely.timer" do
433     action [:stop, :disable]
434   end
435
436   service "replication-hourly.timer" do
437     action [:stop, :disable]
438   end
439
440   service "replication-daily.timer" do
441     action [:stop, :disable]
442   end
443
444   service "replication-cleanup.timer" do
445     action [:stop, :disable]
446   end
447 end