]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tile/templates/default/replicate.erb
Add support for systemd timer units
[chef.git] / cookbooks / tile / templates / default / replicate.erb
1 #!/bin/bash
2
3 # DO NOT EDIT - This file is being maintained by Chef
4
5 # Before running updates, the replication needs to be set up with the timestamp
6 # set to the day of the latest planet dump. Setting to midnight ensures we get
7 # conistent data after first run. osmosis --read-replication-interval-init is
8 # used to initially create the state file
9
10 # Define exit handler
11 function onexit {
12     [ -f sequence-prev.txt ] && mv sequence-prev.txt sequence.txt
13 }
14
15 # Send output to the log
16 exec > /var/log/tile/replicate.log 2>&1
17
18 # Change to the replication state directory
19 cd /var/lib/replicate
20
21 # Install exit handler
22 trap onexit EXIT
23
24 # Loop indefinitely
25 while true
26 do
27     # Work out the name of the next file
28     file="changes-$(cat sequence.txt).osc.gz"
29
30     # Save sequence file so we can rollback if an error occurs
31     cp sequence.txt sequence-prev.txt
32
33     # Fetch the next set of changes
34     pyosmium-get-changes --sequence-file=sequence.txt --outfile=${file}
35
36     # Save exit status
37     status=$?
38
39     # Check for errors
40     if [ $status -eq 0 ]
41     then
42         # Enable exit on error
43         set -e
44
45         # Log the new data
46         echo "Fetched new data from $(cat sequence-prev.txt) to $(cat sequence.txt) into ${file}"
47
48         # Apply the changes to the database
49         osm2pgsql --database gis --slim --append --number-processes=1 \
50 <% if node[:tile][:node_file] -%>
51                   --flat-nodes=<%= node[:tile][:node_file] %> \
52 <% end -%>
53 <% if node[:tile][:styles][:default][:revision] >= "v4.0.0" -%>
54                   --multi-geometry --hstore \
55                   --style=/srv/tile.openstreetmap.org/styles/default/openstreetmap-carto.style \
56                   --tag-transform-script=/srv/tile.openstreetmap.org/styles/default/openstreetmap-carto.lua \
57 <% end -%>
58                   ${file}
59
60         # No need to rollback now
61         rm sequence-prev.txt
62
63         # Get buffer count
64         buffers=$(osmium fileinfo --extended --get=data.buffers.count ${file})
65
66         # If this diff has content mark it as the latest diff
67         if [ $buffers -gt 0 ]
68         then
69             ln -f ${file} changes-latest.osc.gz
70         fi
71
72         # Queue these changes for expiry processing
73         ln ${file} expire-queue/${file}
74
75         # Delete old downloads
76         find . -name 'changes-*.gz' -mmin +300 -exec rm -f {} \;
77
78         # Disable exit on error
79         set +e
80     elif [ $status -eq 3 ]
81     then
82         # Log the lack of data
83         echo "No new data available. Sleeping..."
84
85         # Remove file, it will just be an empty changeset
86         rm ${file}
87
88         # Sleep for a short while
89         sleep 30
90     else
91         # Log our failure to fetch changes
92         echo "Failed to fetch changes - waiting a few minutes before retry"
93
94         # Remove any output that was produced
95         rm -f ${file}
96
97         # Wait five minutes and have another go
98         sleep 300
99     fi
100 done