File: openstreetmap-tiles-update-expire

package info (click to toggle)
libapache2-mod-tile 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,872 kB
  • sloc: cpp: 18,151; ansic: 7,574; sh: 980; makefile: 163; xml: 27
file content (129 lines) | stat: -rwxr-xr-x 3,691 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh
#
# Copyright (c) 2007 - 2023 by mod_tile contributors (see AUTHORS file)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see http://www.gnu.org/licenses/.

set -e

#*************************************************************************
#*************************************************************************
OSMOSIS_BIN=osmosis
OSM2PGSQL_BIN=osm2pgsql
OSM2PGSQL_OPTIONS=
#OSM2PGSQL_OPTIONS="--flat-nodes /path/to/flatnodes --hstore"

BASE_DIR=/var/cache/renderd/tiles
LOG_DIR=/var/log/tiles/
WORKOSM_DIR=$BASE_DIR/.osmosis

LOCK_FILE=/tmp/openstreetmap-update-expire-lock.txt
CHANGE_FILE=$BASE_DIR/changes.osc.gz
EXPIRY_FILE=$BASE_DIR/dirty_tiles
STOP_FILE=$BASE_DIR/stop.txt

OSMOSISLOG=$LOG_DIR/osmosis.log
PGSQLLOG=$LOG_DIR/osm2pgsql.log
EXPIRYLOG=$LOG_DIR/expiry.log
RUNLOG=$LOG_DIR/run.log

EXPIRY_MINZOOM=10
EXPIRY_MAXZOOM=18

#*************************************************************************
#*************************************************************************

m_info() {
  echo "[$(date +"%Y-%m-%d %H:%M:%S")] $$ $1" >>"$RUNLOG"
}

m_error() {
  echo "[$(date +"%Y-%m-%d %H:%M:%S")] $$ [error] $1" >>"$RUNLOG"

  m_info "resetting state"
  /bin/cp $WORKOSM_DIR/last.state.txt $WORKOSM_DIR/state.txt || true

  rm "$CHANGE_FILE" || true
  rm "$EXPIRY_FILE.$$" || true
  rm "$LOCK_FILE"
  exit
}

m_ok() {
  echo "[$(date +"%Y-%m-%d %H:%M:%S")] $$ $1" >>"$RUNLOG"
}

getlock() {
  if [ -s $1 ]; then
    if [ "$(ps -p $(cat $1) | wc -l)" -gt 1 ]; then
      return 1 #false
    fi
  fi

  echo $$ >"$1"
  return 0 #true
}

freelock() {
  rm "$1"
  rm "$CHANGE_FILE"
}

if [ $# -eq 1 ]; then
  m_info "Initialising Osmosis replication system to $1"
  mkdir $WORKOSM_DIR
  $OSMOSIS_BIN --read-replication-interval-init workingDirectory=$WORKOSM_DIR 1>&2 2>"$OSMOSISLOG"
  wget "https://replicate-sequences.osm.mazdermind.de/?"$1"T00:00:00Z" -O $WORKOSM_DIR/state.txt
else
  # make sure the lockfile is removed when we exit and then claim it

  if ! getlock "$LOCK_FILE"; then
    m_info "pid $(cat $LOCK_FILE) still running"
    exit 3
  fi

  if [ -e $STOP_FILE ]; then
    m_info "stopped"
    exit 2
  fi

  seq=$(cat $WORKOSM_DIR/state.txt | grep sequenceNumber | cut -d= -f2)

  m_ok "start import from seq-nr $seq, replag is $(osmosis-db_replag -h)"

  /bin/cp $WORKOSM_DIR/state.txt $WORKOSM_DIR/last.state.txt
  m_ok "downloading diff"

  if ! $OSMOSIS_BIN --read-replication-interval workingDirectory=$WORKOSM_DIR --simplify-change --write-xml-change $CHANGE_FILE 1>&2 2>"$OSMOSISLOG"; then
    m_error "Osmosis error"
  fi

  m_ok "importing diff"
  EXPIRY_METAZOOM=$(expr $EXPIRY_MAXZOOM - 3)
  if ! $OSM2PGSQL_BIN -a --slim -e$EXPIRY_METAZOOM:$EXPIRY_METAZOOM $OSM2PGSQL_OPTIONS -o "$EXPIRY_FILE.$$" $CHANGE_FILE 1>&2 2>"$PGSQLLOG"; then
    m_error "osm2pgsql error"
  fi

  freelock "$LOCK_FILE"

  m_ok "expiring tiles"
  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
    m_info "Expiry failed"
  fi

  rm "$EXPIRY_FILE.$$"

  m_ok "Done with import"

fi