]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tile/templates/default/replicate.erb
Avoid cloning directory resources in the tile recipe
[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 state-prev.txt ] && mv state-prev.txt state.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 # Read in initial state
25 . state.txt
26
27 # Loop indefinitely
28 while true
29 do
30     # Work out the name of the next file
31     file="changes-${sequenceNumber}.osm.gz"
32
33     # Save state file so we can rollback if an error occurs
34     cp state.txt state-prev.txt
35
36     # Fetch the next set of changes
37     osmosis --read-replication-interval --simc --write-xml-change file="${file}" compressionMethod="gzip"
38
39     # Check for errors
40     if [ $? -eq 0 ]
41     then
42         # Enable exit on error
43         set -e
44
45         # Remember the previous sequence number
46         prevSequenceNumber=$sequenceNumber
47
48         # Read in new state
49         . state.txt
50
51         # Did we get any new data?
52         if [ "${sequenceNumber}" == "${prevSequenceNumber}" ]
53         then
54             # Log the lack of data
55             echo "No new data available. Sleeping..."
56
57             # Remove file, it will just be an empty changeset
58             rm ${file}
59
60             # No need to rollback now
61             rm state-prev.txt
62
63             # Sleep for a short while
64             sleep 30
65         else
66             # Log the new data
67             echo "Fetched new data from ${prevSequenceNumber} to ${sequenceNumber} into ${file}"
68
69             # Apply the changes to the database
70 <% if node[:tile][:node_file] -%>
71             osm2pgsql --slim --append --number-processes=1 --flat-nodes=<%= node[:tile][:node_file] %> ${file}
72 <% else -%>
73             osm2pgsql --slim --append --number-processes=1 ${file}
74 <% end -%>
75
76             # No need to rollback now
77             rm state-prev.txt
78
79             # Expire tiles which are touched by the changes
80             sudo -u www-data /usr/local/bin/expire-tiles ${file} > /dev/null 2>&1 &
81         fi
82
83         # Delete old downloads
84         find . -name 'changes-*.gz' -mmin +300 -exec rm -f {} \;
85
86         # Disable exit on error
87         set +e
88     else
89         # Log our failure to fetch changes
90         echo "Failed to fetch changes - waiting a few minutes before retry"
91
92         # Wait five minutes and have another go
93         sleep 300
94     fi
95 done