]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tile/templates/default/replicate.erb
tile: send replicate logs to systemd journald
[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 # Change to the replication state directory
16 cd /var/lib/replicate
17
18 # Install exit handler
19 trap onexit EXIT
20
21 # Loop indefinitely
22 while true
23 do
24     # Work out the name of the next file
25     file="changes-$(cat sequence.txt).osc.gz"
26
27     # Save sequence file so we can rollback if an error occurs
28     cp sequence.txt sequence-prev.txt
29
30     # Fetch the next set of changes
31     pyosmium-get-changes --server=<%= node[:tile][:replication][:url] %> --sequence-file=sequence.txt --outfile=${file}
32
33     # Save exit status
34     status=$?
35
36     # Check for errors
37     if [ $status -eq 0 ]
38     then
39         # Enable exit on error
40         set -e
41
42         # Log the new data
43         echo "Fetched new data from $(cat sequence-prev.txt) to $(cat sequence.txt) into ${file}"
44
45         # Apply the changes to the database
46         osm2pgsql --database gis --slim --append --number-processes=1 \
47 <% if node[:tile][:database][:node_file] -%>
48                   --flat-nodes=<%= node[:tile][:database][:node_file] %> \
49 <% end -%>
50 <% if node[:tile][:database][:multi_geometry] -%>
51                   --multi-geometry \
52 <% end -%>
53 <% if node[:tile][:database][:hstore] -%>
54                   --hstore \
55 <% end -%>
56 <% if node[:tile][:database][:style_file] -%>
57                   --style=<%= node[:tile][:database][:style_file] %> \
58 <% end -%>
59 <% if node[:tile][:database][:tag_transform_script] -%>
60                   --tag-transform-script=<%= node[:tile][:database][:tag_transform_script] %> \
61 <% end -%>
62                   ${file}
63
64         # No need to rollback now
65         rm sequence-prev.txt
66
67         # Get buffer count
68         buffers=$(osmium fileinfo --extended --get=data.buffers.count ${file})
69
70         # If this diff has content mark it as the latest diff
71         if [ $buffers -gt 0 ]
72         then
73             ln -f ${file} changes-latest.osc.gz
74         fi
75
76         # Queue these changes for expiry processing
77         ln ${file} expire-queue/${file}
78
79         # Delete old downloads
80         find . -name 'changes-*.gz' -mmin +300 -exec rm -f {} \;
81
82         # Disable exit on error
83         set +e
84     elif [ $status -eq 3 ]
85     then
86         # Log the lack of data
87         echo "No new data available. Sleeping..."
88
89         # Remove file, it will just be an empty changeset
90         rm ${file}
91
92         # Sleep for a short while
93         sleep 30
94     else
95         # Log our failure to fetch changes
96         echo "Failed to fetch changes - waiting a few minutes before retry"
97
98         # Remove any output that was produced
99         rm -f ${file}
100
101         # Wait five minutes and have another go
102         sleep 300
103     fi
104 done