diff --git a/apps/common/utils/ip/geoip/utils.py b/apps/common/utils/ip/geoip/utils.py index 693a0305c..4142a781e 100644 --- a/apps/common/utils/ip/geoip/utils.py +++ b/apps/common/utils/ip/geoip/utils.py @@ -16,6 +16,8 @@ def get_ip_city_by_geoip(ip): global reader if reader is None: path = os.path.join(os.path.dirname(__file__), 'GeoLite2-City.mmdb') + if not os.path.exists(path): + raise FileNotFoundError(f"IP Database not found, please run `./requirements/static_files.sh`") reader = geoip2.database.Reader(path) try: diff --git a/apps/common/utils/ip/ipip/utils.py b/apps/common/utils/ip/ipip/utils.py index e94009b70..6de34df9a 100644 --- a/apps/common/utils/ip/ipip/utils.py +++ b/apps/common/utils/ip/ipip/utils.py @@ -12,6 +12,9 @@ def get_ip_city_by_ipip(ip): global ipip_db if ipip_db is None: ipip_db_path = os.path.join(os.path.dirname(__file__), 'ipipfree.ipdb') + if not os.path.exists(ipip_db_path): + raise FileNotFoundError( + f"IP database not found, please run `bash ./requirements/static_files.sh`") ipip_db = ipdb.City(ipip_db_path) try: info = ipip_db.find_info(ip, 'CN') diff --git a/requirements/static_files.sh b/requirements/static_files.sh index 91d430be9..ed7f0bf34 100644 --- a/requirements/static_files.sh +++ b/requirements/static_files.sh @@ -18,13 +18,19 @@ fi for file in $to_files; do # Check if the file already exists file_path="${BASE_DIR}/$file" - rm -f $file_path if [ -f "$file_path" ]; then echo "File $file already exists, skipping download." continue fi filename=$(basename "$file") + static_path="$BASE_DIR/data/static/$filename" + if [[ "$1" == "static" && -f "$static_path" ]]; then + echo "File $file already exists in static path, copy it." + cp -f "$static_path" "$file_path" + continue + fi + to_dir=$(dirname "$file_path") if [ ! -d "$to_dir" ]; then mkdir -p "$to_dir" @@ -32,5 +38,11 @@ for file in $to_files; do url=""https://github.com/jumpserver/static/releases/download/${VERSION}/$filename"" echo "Download $filename to $file_path" wget "$url" -O "${file_path}" + + if [[ "$1" == "static" ]];then + # Copy the file to the static directory + echo "Copy to static" + cp -f "$file_path" "$static_path" + fi done