Better file permission initialization (#162)

This commit is contained in:
CaliBrain
2025-04-27 01:27:48 -04:00
committed by GitHub
parent 1d2d06a6d3
commit 4ca8298b7e
3 changed files with 51 additions and 22 deletions
-4
View File
@@ -66,7 +66,6 @@ WORKDIR /app
# Install Python dependencies using pip
# Upgrade pip first, then copy requirements and install
# Copying requirements.txt separately leverages build cache
# No --chown needed as it's copied as root
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && \
# Clean root's pip cache
@@ -84,7 +83,6 @@ RUN echo "#!/bin/sh" > /usr/local/bin/pyrequests && \
chmod +x /usr/local/bin/pyrequests
# Copy application code *after* dependencies are installed
# No --chown needed as it's copied as root, entrypoint will handle permissions
COPY . .
# Final setup: permissions and directories in one layer
@@ -92,8 +90,6 @@ COPY . .
# Ownership will be handled by the entrypoint script.
RUN mkdir -p /var/log/cwa-book-downloader /cwa-book-ingest && \
chmod +x /app/entrypoint.sh /app/tor.sh /app/genDebug.sh
# chown is removed
# Expose the application port
EXPOSE ${FLASK_PORT}
+7 -11
View File
@@ -8,7 +8,7 @@ import subprocess
import os
from logger import setup_logger
from config import CUSTOM_SCRIPT, CROSS_FILE_SYSTEM
from config import CUSTOM_SCRIPT
from env import INGEST_DIR, TMP_DIR, MAIN_LOOP_SLEEP_TIME, USE_BOOK_TITLE
from models import book_queue, BookInfo, QueueStatus, SearchFilters
import book_manager
@@ -141,17 +141,13 @@ def _download_book(book_id: str) -> Optional[str]:
final_path = INGEST_DIR / book_name
if os.path.exists(book_path):
if CROSS_FILE_SYSTEM:
logger.info(f"Copying book to ingest directory then renaming: {book_path} -> {intermediate_path} -> {final_path}")
try:
shutil.move(book_path, intermediate_path)
except Exception as e:
logger.debug(f"Error moving book: {e}, will try copying instead")
shutil.copy(book_path, intermediate_path)
os.remove(book_path)
else:
logger.info(f"Moving book to ingest directory: {book_path} -> {intermediate_path}")
logger.info(f"Moving book to ingest directory then renaming: {book_path} -> {intermediate_path} -> {final_path}")
try:
shutil.move(book_path, intermediate_path)
except Exception as e:
logger.debug(f"Error moving book: {e}, will try copying instead")
shutil.copy(book_path, intermediate_path)
os.remove(book_path)
logger.info(f"Renaming book: {intermediate_path} -> {final_path}")
os.rename(intermediate_path, final_path)
return str(final_path)
+44 -7
View File
@@ -6,11 +6,10 @@ LOG_FILE=${LOG_DIR}/cwa-bd_entrypoint.log
# Cleanup any existing files or folders in the log directory
rm -rf $LOG_DIR/*
(
if [ "$USING_TOR" = "true" ]; then
./tor.sh
fi
if [ "$USING_TOR" = "true" ]; then
./tor.sh
fi
)
exec 3>&1 4>&2
@@ -52,11 +51,43 @@ fi
# Get username for the UID (whether we just created it or it existed)
USERNAME=$(getent passwd "$UID" | cut -d: -f1)
echo "Username for UID $UID is $USERNAME"
test_write() {
folder=$1
mkdir -p $folder
set +e
(
sudo -E -u "$USERNAME" HOME=/app echo 0123456789_TEST > $folder/calibre-web-automated-book-downloader_TEST_WRITE
)
set -e
FILE_CONTENT=$(cat $folder/calibre-web-automated-book-downloader_TEST_WRITE)
rm -f $folder/calibre-web-automated-book-downloader_TEST_WRITE
[ "$FILE_CONTENT" = "0123456789_TEST" ]
result=$?
if [ $result -eq 0 ]; then
result_text="true"
else
result_text="false"
fi
echo "Test write to $folder by $USERNAME: $result_text"
return $result
}
# Ensure proper ownership of application directories
change_ownership() {
folder=$1
echo "Changing ownership of $folder to $USERNAME:$GID"
chown -R "${UID}:${GID}" "${folder}" || echo "Failed to change ownership for ${folder}, continuing..."
folder=$1
set +e
mkdir -p $folder
if test_write $folder; then
echo "Successfully wrote to $folder as $USERNAME, no need to change ownership"
else
echo "Failed to write to $folder as $USERNAME"
echo "Changing ownership of $folder to $USERNAME:$GID"
chown -R "${UID}:${GID}" "${folder}" || echo "Failed to change ownership for ${folder}, continuing..."
echo "Changing mode of $folder to group r/w"
chmod g+r,g+w "${folder}" || echo "Failed to change mode for ${folder}, continuing..."
fi
set -e
}
change_ownership /app
@@ -64,6 +95,12 @@ change_ownership /var/log/cwa-book-downloader
change_ownership /cwa-book-ingest
change_ownership /tmp/cwa-book-downloader
# Test write to all folders
test_write /app
test_write /var/log/cwa-book-downloader
test_write /cwa-book-ingest
test_write /tmp/cwa-book-downloader
# Set the command to run based on the environment
is_prod=$(echo "$APP_ENV" | tr '[:upper:]' '[:lower:]')
if [ "$is_prod" = "prod" ]; then