]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/recipes/replication.rb
Copy changeset replication diffs 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 "planet::aws"
26 include_recipe "ruby"
27 include_recipe "tools"
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     "/home/planet/.aws",
212     "/run/replication",
213     "/store/planet/replication/changesets"
214   ]
215 end
216
217 systemd_timer "replication-changesets" do
218   description "Changesets replication"
219   on_boot_sec 60
220   on_unit_active_sec 60
221   accuracy_sec 5
222 end
223
224 ## Minutely replication
225
226 directory "/store/planet/replication/minute" do
227   owner "planet"
228   group "planet"
229   mode "755"
230 end
231
232 directory "/var/lib/replication/minute" do
233   owner "planet"
234   group "planet"
235   mode "755"
236 end
237
238 directory "/store/replication/minute" do
239   owner "planet"
240   group "planet"
241   mode "755"
242 end
243
244 osmdbt_config = {
245   "database" => {
246     "host" => node[:web][:database_host],
247     "dbname" => "openstreetmap",
248     "user" => "planetdiff",
249     "password" => db_passwords["planetdiff"],
250     "replication_slot" => "osmdbt"
251   },
252   "log_dir" => "/var/lib/replication/minute",
253   "changes_dir" => "/store/planet/replication/minute",
254   "tmp_dir" => "/store/replication/minute",
255   "run_dir" => "/run/replication"
256 }
257
258 file "/etc/replication/osmdbt-config.yaml" do
259   user "root"
260   group "planet"
261   mode "640"
262   content YAML.dump(osmdbt_config)
263 end
264
265 systemd_service "replication-minutely" do
266   description "Minutely replication"
267   user "planet"
268   working_directory "/etc/replication"
269   exec_start "/usr/local/bin/replicate-minute"
270   sandbox :enable_network => true
271   read_write_paths [
272     "/run/replication",
273     "/store",
274     "/var/lib/replication/minute"
275   ]
276 end
277
278 systemd_timer "replication-minutely" do
279   description "Minutely replication"
280   on_boot_sec 60
281   on_unit_active_sec 60
282   accuracy_sec 5
283 end
284
285 ## Hourly replication
286
287 directory "/store/planet/replication/hour" do
288   owner "planet"
289   group "planet"
290   mode "755"
291 end
292
293 directory "/var/lib/replication/hour" do
294   owner "planet"
295   group "planet"
296   mode "755"
297 end
298
299 link "/var/lib/replication/hour/data" do
300   to "/store/planet/replication/hour"
301 end
302
303 template "/var/lib/replication/hour/configuration.txt" do
304   source "replication.config.erb"
305   owner "planet"
306   group "planet"
307   mode "644"
308   variables :base => "minute", :interval => 3600
309 end
310
311 systemd_service "replication-hourly" do
312   description "Hourly replication"
313   user "planet"
314   exec_start "/usr/local/bin/osmosis -q --merge-replication-files workingDirectory=/var/lib/replication/hour"
315   environment "LD_PRELOAD" => "/opt/flush/flush.so"
316   sandbox :enable_network => true
317   memory_deny_write_execute false
318   read_write_paths [
319     "/store/planet/replication/hour",
320     "/var/lib/replication/hour"
321   ]
322 end
323
324 systemd_timer "replication-hourly" do
325   description "Hourly replication"
326   on_calendar "*-*-* *:02/15:00"
327 end
328
329 ## Daily replication
330
331 directory "/store/planet/replication/day" do
332   owner "planet"
333   group "planet"
334   mode "755"
335 end
336
337 directory "/var/lib/replication/day" do
338   owner "planet"
339   group "planet"
340   mode "755"
341 end
342
343 link "/var/lib/replication/day/data" do
344   to "/store/planet/replication/day"
345 end
346
347 template "/var/lib/replication/day/configuration.txt" do
348   source "replication.config.erb"
349   owner "planet"
350   group "planet"
351   mode "644"
352   variables :base => "hour", :interval => 86400
353 end
354
355 systemd_service "replication-daily" do
356   description "Daily replication"
357   user "planet"
358   exec_start "/usr/local/bin/osmosis -q --merge-replication-files workingDirectory=/var/lib/replication/day"
359   environment "LD_PRELOAD" => "/opt/flush/flush.so"
360   sandbox :enable_network => true
361   memory_deny_write_execute false
362   read_write_paths [
363     "/store/planet/replication/day",
364     "/var/lib/replication/day"
365   ]
366 end
367
368 systemd_timer "replication-daily" do
369   description "Daily replication"
370   on_calendar "*-*-* *:02/15:00"
371 end
372
373 ## Replication cleanup
374
375 systemd_service "replication-cleanup" do
376   description "Cleanup replication"
377   user "planet"
378   exec_start "/usr/local/bin/replicate-cleanup"
379   sandbox true
380   read_write_paths "/var/lib/replication"
381 end
382
383 systemd_timer "replication-cleanup" do
384   description "Cleanup replication"
385   on_boot_sec 60
386   on_unit_active_sec 86400
387   accuracy_sec 1800
388 end
389
390 ## Enable/disable feeds
391
392 if node[:planet][:replication] == "enabled"
393   service "users-agreed.timer" do
394     action [:enable, :start]
395   end
396
397   service "users-deleted.timer" do
398     action [:enable, :start]
399   end
400
401   service "replication-changesets.timer" do
402     action [:enable, :start]
403   end
404
405   service "replication-minutely.timer" do
406     action [:enable, :start]
407   end
408
409   service "replication-hourly.timer" do
410     action [:enable, :start]
411   end
412
413   service "replication-daily.timer" do
414     action [:enable, :start]
415   end
416
417   service "replication-cleanup.timer" do
418     action [:enable, :start]
419   end
420 else
421   service "users-agreed.timer" do
422     action [:stop, :disable]
423   end
424
425   service "users-deleted.timer" do
426     action [:stop, :disable]
427   end
428
429   service "replication-changesets.timer" do
430     action [:stop, :disable]
431   end
432
433   service "replication-minutely.timer" do
434     action [:stop, :disable]
435   end
436
437   service "replication-hourly.timer" do
438     action [:stop, :disable]
439   end
440
441   service "replication-daily.timer" do
442     action [:stop, :disable]
443   end
444
445   service "replication-cleanup.timer" do
446     action [:stop, :disable]
447   end
448 end