Debian Codegraph @

utils/openstreetmap-tiles-update-expire

raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 - 2023 by mod_tile contributors (see AUTHORS file)
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; If not, see http://www.gnu.org/licenses/.
17
18 set -e
19
20 #*************************************************************************
21 #*************************************************************************
22 OSMOSIS_BIN=osmosis
23 OSM2PGSQL_BIN=osm2pgsql
24 OSM2PGSQL_OPTIONS=
25 #OSM2PGSQL_OPTIONS="--flat-nodes /path/to/flatnodes --hstore"
26
27 BASE_DIR=/var/cache/renderd/tiles
28 LOG_DIR=/var/log/tiles/
29 WORKOSM_DIR=$BASE_DIR/.osmosis
30
31 LOCK_FILE=/tmp/openstreetmap-update-expire-lock.txt
32 CHANGE_FILE=$BASE_DIR/changes.osc.gz
33 EXPIRY_FILE=$BASE_DIR/dirty_tiles
34 STOP_FILE=$BASE_DIR/stop.txt
35
36 OSMOSISLOG=$LOG_DIR/osmosis.log
37 PGSQLLOG=$LOG_DIR/osm2pgsql.log
38 EXPIRYLOG=$LOG_DIR/expiry.log
39 RUNLOG=$LOG_DIR/run.log
40
41 EXPIRY_MINZOOM=10
42 EXPIRY_MAXZOOM=18
43
44 #*************************************************************************
45 #*************************************************************************
46
47 m_info() {
48 echo "[$(date +"%Y-%m-%d %H:%M:%S")] $$ $1" >>"$RUNLOG"
49 }
50
51 m_error() {
52 echo "[$(date +"%Y-%m-%d %H:%M:%S")] $$ [error] $1" >>"$RUNLOG"
53
54 m_info "resetting state"
55 /bin/cp $WORKOSM_DIR/last.state.txt $WORKOSM_DIR/state.txt || true
56
57 rm "$CHANGE_FILE" || true
58 rm "$EXPIRY_FILE.$$" || true
59 rm "$LOCK_FILE"
60 exit
61 }
62
63 m_ok() {
64 echo "[$(date +"%Y-%m-%d %H:%M:%S")] $$ $1" >>"$RUNLOG"
65 }
66
67 getlock() {
68 if [ -s $1 ]; then
69 if [ "$(ps -p $(cat $1) | wc -l)" -gt 1 ]; then
70 return 1 #false
71 fi
72 fi
73
74 echo $$ >"$1"
75 return 0 #true
76 }
77
78 freelock() {
79 rm "$1"
80 rm "$CHANGE_FILE"
81 }
82
83 if [ $# -eq 1 ]; then
84 m_info "Initialising Osmosis replication system to $1"
85 mkdir $WORKOSM_DIR
86 $OSMOSIS_BIN --read-replication-interval-init workingDirectory=$WORKOSM_DIR 1>&2 2>"$OSMOSISLOG"
87 wget "https://replicate-sequences.osm.mazdermind.de/?"$1"T00:00:00Z" -O $WORKOSM_DIR/state.txt
88 else
89 # make sure the lockfile is removed when we exit and then claim it
90
91 if ! getlock "$LOCK_FILE"; then
92 m_info "pid $(cat $LOCK_FILE) still running"
93 exit 3
94 fi
95
96 if [ -e $STOP_FILE ]; then
97 m_info "stopped"
98 exit 2
99 fi
100
101 seq=$(cat $WORKOSM_DIR/state.txt | grep sequenceNumber | cut -d= -f2)
102
103 m_ok "start import from seq-nr $seq, replag is $(osmosis-db_replag -h)"
104
105 /bin/cp $WORKOSM_DIR/state.txt $WORKOSM_DIR/last.state.txt
106 m_ok "downloading diff"
107
108 if ! $OSMOSIS_BIN --read-replication-interval workingDirectory=$WORKOSM_DIR --simplify-change --write-xml-change $CHANGE_FILE 1>&2 2>"$OSMOSISLOG"; then
109 m_error "Osmosis error"
110 fi
111
112 m_ok "importing diff"
113 EXPIRY_METAZOOM=$(expr $EXPIRY_MAXZOOM - 3)
114 if ! $OSM2PGSQL_BIN -a --slim -e$EXPIRY_METAZOOM:$EXPIRY_METAZOOM $OSM2PGSQL_OPTIONS -o "$EXPIRY_FILE.$$" $CHANGE_FILE 1>&2 2>"$PGSQLLOG"; then
115 m_error "osm2pgsql error"
116 fi
117
118 freelock "$LOCK_FILE"
119
120 m_ok "expiring tiles"
121 if ! render_expired --min-zoom=$EXPIRY_MINZOOM --max-zoom=$EXPIRY_MAXZOOM --touch-from=$EXPIRY_MINZOOM -s /run/renderd.sock <"$EXPIRY_FILE.$$" 2>&1 | tail -8 >>"$EXPIRYLOG"; then
122 m_info "Expiry failed"
123 fi
124
125 rm "$EXPIRY_FILE.$$"
126
127 m_ok "Done with import"
128
129 fi