From 921e756aefebfdea8b74b636fb5bea4dfc14f695 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Wed, 5 Feb 2025 16:50:05 -0500 Subject: [PATCH] feat: Enhance CDN management with entrypoint script and update country data handling --- .../management/commands/download-countries.py | 3 ++- cdn/Dockerfile | 20 +++++++++---------- cdn/entrypoint.sh | 9 +++++++++ cdn/main.py | 2 +- 4 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 cdn/entrypoint.sh diff --git a/backend/server/worldtravel/management/commands/download-countries.py b/backend/server/worldtravel/management/commands/download-countries.py index 526a4f4..3a1cd11 100644 --- a/backend/server/worldtravel/management/commands/download-countries.py +++ b/backend/server/worldtravel/management/commands/download-countries.py @@ -60,7 +60,7 @@ class Command(BaseCommand): f.write(cdn_version) self.stdout.write(self.style.SUCCESS('Version updated successfully to ' + cdn_version)) else: - self.stdout.write(self.style.SUCCESS('Data is already up-to-date.')) + self.stdout.write(self.style.SUCCESS('Data is already up-to-date. Run with --force to re-download')) return else: self.stdout.write(self.style.ERROR('Error downloading version.json')) @@ -78,6 +78,7 @@ class Command(BaseCommand): countries_json_path = os.path.join(media_root, 'countries_states_cities.json') if os.path.exists(countries_json_path): os.remove(countries_json_path) + self.stdout.write(self.style.SUCCESS('countries_states_cities.json deleted successfully')) # Download the latest countries, regions, and cities json file res = requests.get(f'{ADVENTURELOG_CDN_URL}/data/countries_states_cities.json') diff --git a/cdn/Dockerfile b/cdn/Dockerfile index 544f8d4..f47e79a 100644 --- a/cdn/Dockerfile +++ b/cdn/Dockerfile @@ -7,20 +7,16 @@ WORKDIR /app # Install required Python packages RUN pip install --no-cache-dir requests osm2geojson -# Copy the script and data folder into the container +# Copy the script into the container COPY main.py /app/main.py -COPY data /app/data/ -# Ensure the data folder exists -RUN mkdir -p /app/data - -# Run the script to generate GeoJSON files -RUN python /app/main.py +# Run the script to generate the data folder and GeoJSON files (this runs inside the container) +RUN python -u /app/main.py # Install Nginx RUN apt update && apt install -y nginx && rm -rf /var/lib/apt/lists/* -# Copy the entire data folder to the Nginx serving directory +# Copy the entire generated data folder to the Nginx serving directory RUN mkdir -p /var/www/html/data && cp -r /app/data/* /var/www/html/data/ # Copy Nginx configuration @@ -32,5 +28,9 @@ COPY index.html /usr/share/nginx/html/index.html # Expose port 80 for Nginx EXPOSE 80 -# Start Nginx in the foreground -CMD ["nginx", "-g", "daemon off;"] +# Copy the entrypoint script into the container +COPY entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh + +# Set the entrypoint script as the default command +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/cdn/entrypoint.sh b/cdn/entrypoint.sh new file mode 100644 index 0000000..fffe244 --- /dev/null +++ b/cdn/entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# Any setup tasks or checks can go here (if needed) +echo "AdventureLog CDN has started!" +echo "Refer to the documentation for information about connecting your AdventureLog instance to this CDN." +echo "Thanks to our data providers for making this possible! You can find them on the CDN site." + +# Start Nginx in the foreground (as the main process) +nginx -g 'daemon off;' diff --git a/cdn/main.py b/cdn/main.py index a33c2dc..2c052fa 100644 --- a/cdn/main.py +++ b/cdn/main.py @@ -70,7 +70,7 @@ def saveCountryFlags(): Downloads the flags of all countries and saves them in the data/flags directory """ # Load the countries data - with open(os.path.join(os.path.dirname(__file__), 'data', f'countries_states_cities_{COUNTRY_REGION_JSON_VERSION}.json')) as f: + with open(os.path.join(os.path.dirname(__file__), 'data', f'countries_states_cities.json')) as f: data = json.load(f) for country in data: