diff --git a/docs/reference/scripts-library.md b/docs/reference/scripts-library.md index 15279a285..1a1ce1ee8 100644 --- a/docs/reference/scripts-library.md +++ b/docs/reference/scripts-library.md @@ -34,13 +34,13 @@ Usage: `delete_file_if_exist ` Creates the given directory if it does not exist -Usage: `create_directory_if_not_exist ` ### delete_directory_if_exist Deletes the given directory if it exists. -Usage: `delete_directory_if_exist ` ## log @@ -62,6 +62,20 @@ Displays error messages. Usage: `log_error ` +## mapcache + +### delete_mapcache_tiles + +Deletes cached tiles from the given layer. You can control the zoom levels from which it will delete tiles using `from_z` and `to_z` (both included in the deletion). You can also restrict deletion to a given area using a geojson file for example (not required). + +Usage: `delete_mapcache_tiles [area]` + +### seed_mapcache_tiles + +Seed tiles for the given layer. You can control the zoom levels for which it will seed tiles using `from_z` and `to_z` (both included in the seeding process). You can also restrict seeding to a given area using a geojson file for example (not required). + +Usage: `seed_mapcache_tiles [area]` + ## mariadb ### mariadb_db_exists diff --git a/scripts/mapcache.sh b/scripts/mapcache.sh new file mode 100644 index 000000000..10f6b7609 --- /dev/null +++ b/scripts/mapcache.sh @@ -0,0 +1,87 @@ +#!/bin/bash +[[ ! -z "${K_MAPCACHE-}" ]] && return +K_MAPCACHE=1 + +# Include useful scripts +source ${KARGO_SCRIPTS_PATH}/log.sh +source ${KARGO_SCRIPTS_PATH}/file.sh +source ${KARGO_SCRIPTS_PATH}/swarm.sh + +# Define common variables +K_MAPCACHE_CONFIG_PATH="${KARGO_BUILD_PATH}/configs/mapcache" +K_MAPCACHE_DOCKER_RUN="docker run --rm \ + --network=${DOCKER_BACK_NETWORK} \ + --user=www-data:www-data \ + --volume=${MAPCACHE_DATA_PATH}:/mnt/data \ + --volume=${K_MAPCACHE_CONFIG_PATH}:/etc/mapcache \ + ${MAPCACHE_IMAGE}:${MAPCACHE_TAG}" + +check_mapcache_area_file() { + # Make sure ${AREA} file is in the mapcache config folder + local FILENAME=${K_MAPCACHE_CONFIG_PATH}/$1 + if ! file_exists $FILENAME; then + log_error \"$1\" file must be located in ${K_MAPCACHE_CONFIG_PATH} + return 1 + fi + + return 0 +} + +delete_mapcache_tiles() { + local LAYER=$1 + local FROM_Z=$2 + local TO_Z=$3 + local AREA=${4:-} + + local MAPCACHE_SEED="mapcache_seed -c /etc/mapcache/mapcache.xml -t ${LAYER} -z ${FROM_Z},${TO_Z} -m delete" + if [ -z "${AREA}" ]; then + log_info deleting ${LAYER} [${FROM_Z} ${TO_Z}] over the planet + echo "${K_MAPCACHE_DOCKER_RUN} ${MAPCACHE_SEED}" + else + if ! check_mapcache_area_file ${AREA}; then + return 1 + fi + log_info deleting ${LAYER} [${FROM_Z} ${TO_Z}] over ${AREA} + echo "${K_MAPCACHE_DOCKER_RUN} ${MAPCACHE_SEED} -d ${AREA}" + fi +} + +seed_mapcache_tiles() { + local LAYER=$1 + local FROM_Z=$2 + local TO_Z=$3 + local NUM_THREADS=$4 + local AREA=${5:-} + + local MAPCACHE_SEED="mapcache_seed -c /etc/mapcache/mapcache.xml -t ${LAYER} -z ${FROM_Z},${TO_Z} -n ${NUM_THREADS} -m seed" + if [ -z "${AREA}" ]; then + log_info seeding ${LAYER} [${FROM_Z} ${TO_Z}] over the planet + echo "${K_MAPCACHE_DOCKER_RUN} ${MAPCACHE_SEED}" + else + if ! check_mapcache_area_file ${AREA}; then + return 1 + fi + log_info seeding ${LAYER} [${FROM_Z} ${TO_Z}] over ${AREA} + echo "${K_MAPCACHE_DOCKER_RUN} ${MAPCACHE_SEED} -d ${AREA}" + fi +} + +# rsync_mapcache_local_cache() { +# local LAYER=${1:-} + +# local CACHE_PATH=$MAPCACHE_DATA_PATH +# if [ -n "$LAYER" ] && [ $LAYER != "all" ]; then +# CACHE_PATH=$MAPCACHE_DATA_PATH/$LAYER +# fi + +# if ! directory_exists $CACHE_PATH; then +# log_error the cache directory \"${CACHE_PATH}\" doesn\'t exist +# return 1 +# fi + +# local THIS_NODE=$(this_swarm_node) +# local OTHER_NODES=$(other_swarm_nodes) +# for NODE in ${OTHER_NODES}; do +# echo "k-node-exec -c $NODE \"cd $CACHE_PATH && sudo rm -fR * && sudo rsync -e \"ssh -o StrictHostKeyChecking=accept-new -i $HOME/.ssh/ssh.pem\" -a --no-i-r --info=progress2 $THIS_NODE:$MAPCACHE_DATA_PATH/ . && sudo chown -R www-data:www-data *\"" +# done +# }