]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/templates/default/planetdump.erb
create XML RSS Feed for .torrent files (using xmlstarlet)
[chef.git] / cookbooks / planet / templates / default / planetdump.erb
1 #!/bin/bash
2
3 # DO NOT EDIT - This file is being maintained by Chef
4
5 # Exit on error
6 set -e
7
8 # Get the name of the file and the expected pattern
9 file="$1"
10 pattern="^osm-([0-9]{4})-([0-9]{2})-([0-9]{2})\.dmp$"
11
12 # Give up now if the file isn't a database dump
13 [[ $file =~ $pattern ]] || exit 0
14
15 # Save the year and date from the file name
16 year="${BASH_REMATCH[1]}"
17 date="${year:2:2}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
18
19 # Check the lock
20 if [ -f /tmp/planetdump.lock ]; then
21     if [ "$(ps -p `cat /tmp/planetdump.lock` | wc -l)" -gt 1 ]; then
22         echo "Error: Another planetdump is running"
23         exit 1
24     else
25         rm /tmp/planetdump.lock
26     fi
27 fi
28
29 # Redirect this shell's output to a file. This is so that it
30 # can be emailed later, since this script is run from incron
31 # and incron doesn't yet support MAILTO like cron does. The
32 # command below appears to work in bash as well as dash.
33 logfile="/tmp/planetdump.log.$$"
34 exec > "${logfile}" 2>&1
35
36 # Create lock file
37 echo $$ > /tmp/planetdump.lock
38
39 # Define cleanup function
40 function cleanup {
41     # Remove the lock file
42     rm /tmp/planetdump.lock
43
44     # Send an email with the output, since incron doesn't yet
45     # support doing this in the incrontab
46     if [[ -s "$logfile" ]]
47     then
48         mailx -s "Planet dump output: ${file}" zerebubuth@gmail.com < "${logfile}"
49     fi
50
51     # Remove the log file
52     rm -f "${logfile}"
53 }
54
55 # Remove lock on exit
56 trap cleanup EXIT
57
58 # Change to working directory
59 cd /store/planetdump
60
61 # Cleanup
62 rm -rf users
63 rm -rf changesets changeset_tags changeset_comments
64 rm -rf nodes node_tags
65 rm -rf ways way_tags way_nodes
66 rm -rf relations relation_tags relation_members
67
68 # Run the dump
69 time nice -n 19 /opt/planet-dump-ng/planet-dump-ng \
70      --max-concurrency=4 \
71      -c "pbzip2 -c" -f "/store/backup/${file}" --dense-nodes=1 \
72      -C "changesets-${date}.osm.bz2" \
73      -D "discussions-${date}.osm.bz2" \
74      -x "planet-${date}.osm.bz2" -X "history-${date}.osm.bz2" \
75      -p "planet-${date}.osm.pbf" -P "history-${date}.osm.pbf"
76
77 # Function to create bittorrent files
78 function mk_torrent {
79   type="$1"
80   format="$2"
81   web_dir="$3"
82   name="${type}-${date}.osm.${format}"
83   web_path="${web_dir}/${name}"
84   rss_web_dir="https://planet.openstreetmap.org/${web_dir}"
85   rss_file="${type}-${format}-rss.xml"
86   torrent_file="${name}.torrent"
87   torrent_url="${rss_web_dir}/${torrent_file}"
88
89   # create .torrent file
90   mktorrent -l 22 ${name} \
91      -a udp://tracker.opentrackr.org:1337 \
92      -a udp://tracker.datacenterlight.ch:6969/announce,http://tracker.datacenterlight.ch:6969/announce \
93      -a udp://tracker.torrent.eu.org:451 \
94      -a udp://tracker-udp.gbitt.info:80/announce,http://tracker.gbitt.info/announce,https://tracker.gbitt.info/announce \
95      -a http://retracker.local/announce \
96      -w https://planet.openstreetmap.org/${web_path} \
97      -w https://ftp5.gwdg.de/pub/misc/openstreetmap/planet.openstreetmap.org/${web_path} \
98      -w https://ftpmirror.your.org/pub/openstreetmap/${web_path} \
99      -w https://mirror.init7.net/openstreetmap/${web_path} \
100      -w https://free.nchc.org.tw/osm.planet/${web_path} \
101      -w https://ftp.fau.de/osm-planet/${web_path} \
102      -w https://ftp.spline.de/pub/openstreetmap/${web_path} \
103      -w https://osm.openarchive.site/${name} \
104      -w https://downloads.opencagedata.com/planet/${name} \
105      -w https://planet.osm-hr.org/${web_path} \
106      -c "OpenStreetMap ${type} data export, licensed under https://opendatacommons.org/licenses/odbl/ by OpenStreetMap contributors" \
107      -o ${torrent_file}
108
109   # create .xml global RSS headers if missing
110   torrent_time_rfc="`date -R -r ${torrent_file}`"
111   test -f "${rss_file}" || echo "<x/>" | xmlstarlet select --xml-decl --indent \
112         -N "atom=http://www.w3.org/2005/Atom" \
113         -N "dcterms=http://purl.org/dc/terms/" \
114         -N "content=http://purl.org/rss/1.0/modules/content/" \
115         --encode "UTF-8" \
116         --template \
117         --match / \
118         --elem "rss" --attr "version" --output "2.0" --break \
119         --elem "channel" \
120         --elem "title" --output "OpenStreetMap ${type} ${format} torrent RSS" --break \
121         --elem "link"  --output "${rss_web_dir}" --break \
122         --elem "atom:link" \
123                 --attr "href" --output "${rss_web_dir}/${rss_file}" --break \
124                 --attr "rel" --output "self" --break \
125                 --attr "type" --output "application/rss+xml" --break \
126                 --break \
127         --elem "description" --output "${type}.osm.${format}.torrent RSS feed" --break \
128         --elem "generator" --output "OpenStreetMap xmlstarlet powered shell script v1.0" --break \
129         --elem "language" --output "en" --break \
130         --elem "lastBuildDate" --output "${torrent_time_rfc}" \
131         > ${rss_file}
132
133   # add newly created .torrent file as new entry to .xml RSS feed, removing excess entries
134   xmlstarlet edit --inplace \
135         -s /rss/channel -t elem -n NEW_item  \
136         -s //NEW_item -t elem -n "title" -v "${torrent_file}" \
137         -s //NEW_item -t elem -n "guid" -v "${torrent_url}" \
138         -s //NEW_item -t elem -n "pubDate" -v "${torrent_time_rfc}" \
139         -s //NEW_item -t elem -n "category" -v "OpenStreetMap data" \
140         -s //NEW_item -t elem -n "link" -v "${torrent_url}" \
141         -s //NEW_item -t elem -n "enclosure" \
142                 -s //NEW_item/enclosure -t attr -n "type" -v "application/x-bittorrent" \
143                 -s //NEW_item/enclosure -t attr -n "url" -v "${torrent_url}" \
144         -s //NEW_item -t elem -n "description" -v "OpenStreetMap torrent ${torrent_file}" \
145         -s //NEW_item -t elem -n "comments" -v "Source: OpenStreetMap contributors, under ODbL 1.0 licence" \
146         -r //NEW_item -v "item" \
147         -u /rss/channel/lastBuildDate -v "${torrent_time_rfc}" \
148         -d '/rss/channel/item[position() <= count(/rss/channel/item) - 5]' \
149         ${rss_file}
150 }
151
152 # Function to install a dump in place
153 function install_dump {
154   type="$1"
155   format="$2"
156   dir="$3"
157   year="$4"
158   name="${type}-${date}.osm.${format}"
159   latest="${type}-latest.osm.${format}"
160   rss_file="${type}-${format}-rss.xml"
161
162   md5sum "${name}" > "${name}.md5"
163   mkdir -p "${dir}/${year}"
164   mv "${name}" "${name}.md5" "${dir}/${year}"
165   ln -sf "${year:-.}/${name}" "${dir}/${latest}"
166   test -f "${name}.torrent" && mv "${name}.torrent" "${dir}/${year}" && ln -sf "${year:-.}/${name}.torrent" "${dir}/${latest}.torrent"
167   test -f "${rss_file}" && xmllint --noout "${rss_file}" && cp -f "${rss_file}" "${dir}/${year}"
168   rm -f "${dir}/${latest}.md5"
169   sed -e "s/${name}/${latest}/" "${dir}/${year}/${name}.md5" > "${dir}/${latest}.md5"
170 }
171
172 # Create *.torrent files
173 mk_torrent "changesets" "bz2" "planet/${year}"
174 mk_torrent "discussions" "bz2" "planet/${year}"
175 mk_torrent "planet" "bz2" "planet/${year}"
176 mk_torrent "history" "bz2" "planet/full-history/${year}"
177 mk_torrent "planet" "pbf" "pbf"
178 mk_torrent "history" "pbf" "pbf/full-history"
179
180 # Move dumps into place
181 install_dump "changesets" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
182 install_dump "discussions" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
183 install_dump "planet" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
184 install_dump "history" "bz2" "<%= node[:planet][:dump][:xml_history_directory] %>" "${year}"
185 install_dump "planet" "pbf" "<%= node[:planet][:dump][:pbf_directory] %>"
186 install_dump "history" "pbf" "<%= node[:planet][:dump][:pbf_history_directory] %>"
187
188 # Remove pbf dumps older than 90 days
189 find "<%= node[:planet][:dump][:pbf_directory] %>" "<%= node[:planet][:dump][:pbf_history_directory] %>" \
190      -maxdepth 1 -mindepth 1 -type f -mtime +90 \
191      \( \
192      -iname 'planet-*.pbf' \
193      -o -iname 'history-*.pbf' \
194      -o -iname 'planet-*.pbf.md5' \
195      -o -iname 'history-*.pbf.md5' \
196      \) \
197      -delete