Spaces:
Running
Running
Commit
·
053d168
1
Parent(s):
b531a07
Refactor Dockerfile and migration script: simplify PYTHONPATH configuration in Dockerfile, enhance directory creation, and improve error handling in migrate.py by logging directory contents on import failure.
Browse files- Dockerfile +6 -5
- scripts/migrate.py +22 -29
Dockerfile
CHANGED
@@ -4,7 +4,7 @@ FROM python:3.10-slim
|
|
4 |
# Set environment variables
|
5 |
ENV PYTHONUNBUFFERED=1 \
|
6 |
PYTHONDONTWRITEBYTECODE=1 \
|
7 |
-
PYTHONPATH="/app
|
8 |
|
9 |
# Set working directory
|
10 |
WORKDIR /app
|
@@ -20,13 +20,14 @@ RUN apt-get update && apt-get install -y \
|
|
20 |
COPY requirements.txt .
|
21 |
RUN pip install --no-cache-dir -r requirements.txt
|
22 |
|
|
|
|
|
|
|
23 |
# Copy application code
|
24 |
COPY . .
|
25 |
|
26 |
-
#
|
27 |
-
RUN
|
28 |
-
# Make run script executable
|
29 |
-
chmod +x /app/scripts/run.sh && \
|
30 |
# Create app user
|
31 |
useradd -m -u 1000 app && \
|
32 |
# Set ownership of all files and directories
|
|
|
4 |
# Set environment variables
|
5 |
ENV PYTHONUNBUFFERED=1 \
|
6 |
PYTHONDONTWRITEBYTECODE=1 \
|
7 |
+
PYTHONPATH="/app"
|
8 |
|
9 |
# Set working directory
|
10 |
WORKDIR /app
|
|
|
20 |
COPY requirements.txt .
|
21 |
RUN pip install --no-cache-dir -r requirements.txt
|
22 |
|
23 |
+
# Create necessary directories
|
24 |
+
RUN mkdir -p /app/app/models /app/storage/audio /app/storage/text /app/storage/temp
|
25 |
+
|
26 |
# Copy application code
|
27 |
COPY . .
|
28 |
|
29 |
+
# Set up permissions
|
30 |
+
RUN chmod +x /app/scripts/run.sh && \
|
|
|
|
|
31 |
# Create app user
|
32 |
useradd -m -u 1000 app && \
|
33 |
# Set ownership of all files and directories
|
scripts/migrate.py
CHANGED
@@ -5,31 +5,10 @@ import logging
|
|
5 |
from alembic.config import Config
|
6 |
from alembic import command
|
7 |
|
8 |
-
# Add
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# Local development path
|
13 |
-
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
14 |
-
]
|
15 |
-
|
16 |
-
# Add paths to Python path
|
17 |
-
for path in possible_paths:
|
18 |
-
if path not in sys.path and os.path.exists(path):
|
19 |
-
sys.path.insert(0, path)
|
20 |
-
|
21 |
-
try:
|
22 |
-
from app.models.database import Base
|
23 |
-
except ImportError as e:
|
24 |
-
print(f"Error importing Base: {e}")
|
25 |
-
print("Current sys.path:", sys.path)
|
26 |
-
print("Contents of /app directory:")
|
27 |
-
if os.path.exists("/app"):
|
28 |
-
print(os.listdir("/app"))
|
29 |
-
print("Contents of app directory:")
|
30 |
-
if os.path.exists("app"):
|
31 |
-
print(os.listdir("app"))
|
32 |
-
raise
|
33 |
|
34 |
# Configure logging
|
35 |
logging.basicConfig(
|
@@ -38,6 +17,24 @@ logging.basicConfig(
|
|
38 |
)
|
39 |
logger = logging.getLogger(__name__)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def run_migrations():
|
42 |
"""Run database migrations using Alembic."""
|
43 |
try:
|
@@ -46,10 +43,6 @@ def run_migrations():
|
|
46 |
# Get the project root directory (one level up)
|
47 |
project_root = os.path.dirname(current_dir)
|
48 |
|
49 |
-
# In Docker, the project root is /app
|
50 |
-
if os.path.exists("/app/alembic.ini"):
|
51 |
-
project_root = "/app"
|
52 |
-
|
53 |
# Create Alembic configuration
|
54 |
alembic_cfg = Config(os.path.join(project_root, "alembic.ini"))
|
55 |
|
|
|
5 |
from alembic.config import Config
|
6 |
from alembic import command
|
7 |
|
8 |
+
# Add the project root to Python path
|
9 |
+
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
10 |
+
if project_root not in sys.path:
|
11 |
+
sys.path.insert(0, project_root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Configure logging
|
14 |
logging.basicConfig(
|
|
|
17 |
)
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
+
try:
|
21 |
+
from app.models.database import Base
|
22 |
+
except ImportError as e:
|
23 |
+
logger.error(f"Error importing Base: {e}")
|
24 |
+
logger.info(f"Current sys.path: {sys.path}")
|
25 |
+
logger.info("Contents of project root:")
|
26 |
+
if os.path.exists(project_root):
|
27 |
+
logger.info(os.listdir(project_root))
|
28 |
+
logger.info("Contents of app directory:")
|
29 |
+
app_dir = os.path.join(project_root, "app")
|
30 |
+
if os.path.exists(app_dir):
|
31 |
+
logger.info(os.listdir(app_dir))
|
32 |
+
logger.info("Contents of models directory:")
|
33 |
+
models_dir = os.path.join(app_dir, "models")
|
34 |
+
if os.path.exists(models_dir):
|
35 |
+
logger.info(os.listdir(models_dir))
|
36 |
+
raise
|
37 |
+
|
38 |
def run_migrations():
|
39 |
"""Run database migrations using Alembic."""
|
40 |
try:
|
|
|
43 |
# Get the project root directory (one level up)
|
44 |
project_root = os.path.dirname(current_dir)
|
45 |
|
|
|
|
|
|
|
|
|
46 |
# Create Alembic configuration
|
47 |
alembic_cfg = Config(os.path.join(project_root, "alembic.ini"))
|
48 |
|