diff --git a/ingestion-beam/bin/download-geolite2 b/ingestion-beam/bin/download-geolite2 index 208a1f12e..ea6854105 100755 --- a/ingestion-beam/bin/download-geolite2 +++ b/ingestion-beam/bin/download-geolite2 @@ -4,15 +4,31 @@ # This script downloads and extracts the free GeoLite2 city database # from MaxMind for use in development. +set -e + +: ${MM_LICENSE_KEY?"maxmind license must be set, see decoder docs for more details"} BASE_DIR="$(dirname "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )")" DOWNLOAD_URL="https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=${MM_LICENSE_KEY}&suffix=tar.gz" -LOCAL_ARCHIVE=/tmp/GeoLite2-City.mmdb.gz -LOCAL_EXTRACTED=/tmp/GeoLite2-City.mmdb +DB_NAME="GeoLite2-City.mmdb" -set -e +# The GeoLite2-City archive is structured as follows: +# +# x GeoLite2-City_20200428/ +# x GeoLite2-City_20200428/README.txt +# x GeoLite2-City_20200428/COPYRIGHT.txt +# x GeoLite2-City_20200428/GeoLite2-City.mmdb +# x GeoLite2-City_20200428/LICENSE.txt +# +# Download and extract the mmdb into the current directory if it doesn't exist. +test -f "$BASE_DIR/$DB_NAME" || ( + working=$(mktemp -d) + cd "$working" + + archive=GeoLite2-City.mmdb.gz + curl -o "$archive" "$DOWNLOAD_URL" + tar -zxvf "$archive" + extracted=$(find "$working" -name "$DB_NAME") -test -f "$BASE_DIR/$(basename "$LOCAL_EXTRACTED")" || ( - curl -o "$LOCAL_ARCHIVE" "$DOWNLOAD_URL" - gunzip -f "$LOCAL_ARCHIVE" - mv "$LOCAL_EXTRACTED" "$BASE_DIR" + cd - + mv "$extracted" "$BASE_DIR" )