Skip to content

Commit

Permalink
Add mgmnt job to keep geo IP data up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchrisadams committed Feb 22, 2024
1 parent a664be7 commit ca51024
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
64 changes: 64 additions & 0 deletions apps/greencheck/management/commands/update_geo_ip_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from django.core.management.base import BaseCommand
import tarfile
import shutil
import requests
from django.conf import settings


class Command(BaseCommand):
help = "Import Geo IP data from Geo IP Provider"

def _fetch_latest_tarfile(self):
"""
Fetch the latest Geo IP data tarball from Maxmind,
using the credentials to authenticate.
"""
fetch_url = settings.GEOIP_PROVIDER_DOWNLOAD_URL
username = settings.GEOIP_USER
password = settings.GEOIP_PASSWORD
output_file = "maxmind.geolite2-city.tar.gz"

if not username or not password:
raise ValueError("GEOIP_USER and GEOIP_PASSWORD must be set")

response = requests.get(
fetch_url,
auth=(username, password),
)
with open(output_file, "wb") as f:
f.write(response.content)

def _extract_ip_db_from_tarfile(self):
# Path to the tarball and the file to be extracted
tarball_path = "maxmind.geolite2-city.tar.gz"
file_to_extract = "GeoLite2-City.mmdb"

# Open the tarball
with tarfile.open(tarball_path, "r:gz") as tar:
# Extract the file
# We don't know the name of the enclosing directory,
# so we have filter for a file path that matches
# GeoLite2-City.mmdb
mmdb_file, *rest = [
filename for filename in tar.getnames() if file_to_extract in filename
]
mmdb_file = tar.extractfile(mmdb_file)
with open("GeoLite2-City.mmdb", "wb") as f:
f.write(mmdb_file.read())

def _move_ip_db_to_destination(self):
# Destination path for the extracted file
destination_path = settings.GEOIP_PATH
# Check if the file already exists in the destination and
# overwrite if necessary
if destination_path.exists():
settings.GEOIP_PATH.unlink()
shutil.move("GeoLite2-City.mmdb", destination_path)

def handle(self, *args, **options):

self._fetch_latest_tarfile()
self._extract_ip_db_from_tarfile()
self._move_ip_db_to_destination()

self.stdout.write(self.style.SUCCESS("Geo IP data imported successfully"))
5 changes: 5 additions & 0 deletions greenweb/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@

# Geo IP database
GEOIP_PATH = pathlib.Path(ROOT) / "data" / "GeoLite2-City.mmdb"
GEOIP_PROVIDER_DOWNLOAD_URL = (
"https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz"
)
GEOIP_USER = env("MAXMIND_USER_ID", default=None)
GEOIP_PASSWORD = env("MAXMIND_LICENCE_KEY", default=None)

# Allow requests from any origin, but only make the API urls available
# CORS_URLS_REGEX = r"^/api/.*$"
Expand Down

0 comments on commit ca51024

Please sign in to comment.