Skip to content

Instantly share code, notes, and snippets.

@loomsen
Created July 14, 2014 10:50
Show Gist options
  • Save loomsen/00323fbc925a56f1bfef to your computer and use it in GitHub Desktop.
Save loomsen/00323fbc925a56f1bfef to your computer and use it in GitHub Desktop.
#!/bin/bash
#set -x
:<<-'literal'
This script updates an imposm imported database.
Your database must exist or this script will fail!
If called with `cleanup' it will just remove the old_planet_osm tables, this is handy to use with a cron job
literal
## constants
progname="${0##*/}"
version="5.0-imposm"
## +++ CONFIG +++ ##
urls=( [0]="http://download.geofabrik.de/europe-latest.osm.pbf"
[1]="http://download.geofabrik.de/asia/gcc-states-latest.osm.pbf"
[2]="http://download.geofabrik.de/africa/canary-islands-latest.osm.pbf"
)
#urls=( "http://download.geofabrik.de/europe/andorra-latest.osm.pbf"
# "http://download.geofabrik.de/europe/liechtenstein-latest.osm.pbf"
# "http://download.geofabrik.de/europe/spain-latest.osm.pbf"
#)
gopath=/usr/local/src/imposm
## +++ FUNCTIONS +++ ##
error_exit() {
echo "${progname}: ${1:-"Unknown Error"}" >&2
clean_up
exit 1
}
graceful_exit() {
clean_up
exit 0
}
signal_exit() {
case $1 in
INT) echo "$progname: Program aborted by user" >&2
clean_up
exit
;;
TERM) echo "$progname: Program terminated" >&2
clean_up
exit
;;
*) error_exit "$progname: Terminating on unknown signal"
clean_up
;;
esac
}
clean_up() {
echo "cleaning up"
# rm -f /opt/osm/"${urls[@]##*/}"
}
clean_cache() {
rm -rf /opt/osm/*/
}
get_pbf() {
# create an indexed array to hold the URLs, if called without argument, use the globally defined URLs
declare -a j
j=( "${@:-${urls[@]}}" )
pushd /opt/osm
clean_cache
for i in "${!j[@]}"; do
until wget -q --tries 5 --timeout 2 "${j[$i]}".md5 -O "${j[$i]##*/}".md5; do
sleep 5
done
until wget -q --tries 5 --timeout 2 "${j[$i]}" -O "${j[$i]##*/}"; do
sleep 5
done
if ! md5sum --quiet -c "${j[$i]##*/}".md5; then
echo md5sum failed, redownloading in one minute
rm -f "${j[$i]##*/}" "${j[$i]##*/}".md5
sleep 1m
get_pbf "${j[$i]}"
fi
done
popd
}
import_db() {
pushd /opt/osm
local imposm="$gopath"/bin/imposm3
# imposm --read --write --optimize --connection=postgis://osm:osm@localhost:5432/osm -m /etc/mapproxy/imposm-mapping.py --deploy-production-tables --overwrite-cache "${urls[@]##*/}"
# popd
[[ ! -x "$imposm" ]] && return 1
[[ ! -f config.json ]] && return 1
for i in "${!urls[@]}"; do
(( i == 0 )) && "$imposm" import -config config.json -overwritecache -read "${urls[i]##*/}" || \
"$imposm" import -config config.json -appendcache -read "${urls[i]##*/}"
done && \
"$imposm" import -config config.json -write -deployproduction -optimize || return 1
popd
}
start_seed() {
echo "++++ $FUNCNAME running... ++++"
sudo -u mapproxy mapproxy-seed -f /etc/mapproxy/mapproxy_mapnik_seed.yaml --progress-file=/var/log/mapproxy/seed-progress.log -c 10 /etc/mapproxy/seed.yaml
# rsync auf andere nodes
# copy_cache_to_osm2
swap_cache
# remove_old_cache_osm2
}
swap_cache() {
echo "++++ $FUNCNAME running... ++++"
mv -v /var/cache/mapproxy/cache_data{,.old}
mv -v /var/cache/mapproxy/seed/cache_data /var/cache/mapproxy/
# service mapproxy restart
# service nginx restart
systemctl restart uwsgi.service
rm -rf /var/cache/mapproxy/cache_data.old
}
copy_cache_to_osm2() {
echo "++++ $FUNCNAME running... ++++"
rsync -avzp /var/cache/mapproxy/seed osm2-hh:/var/cache/mapproxy/
ssh osm2-hh -o Batchmode=yes "mv -v /var/cache/mapproxy/cache_data{,.old} && mv -v /var/cache/mapproxy/seed/cache_data /var/cache/mapproxy/ && systemctl restart uwsgi.service"
}
remove_old_cache_osm2() {
echo "++++ $FUNCNAME running... ++++"
ssh osm2-hh -o Batchmode=yes "rm -rf /var/cache/mapproxy/cache_data.old"
}
# --- FUNCTIONS --- ##
##+++ Program starts here
trap 'signal_exit INT' INT
trap 'signal_exit TERM' TERM
exec > /tmp/$progname.log 2>&1
set -x
#get_pbf || error_exit "pbf download failed"
import_db || error_exit "could not import db"
#start_seed || error_exit "seeding on osm1 failed, please check"
#clean_cache
trap - INT TERM
graceful_exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment