Spaces:
Sleeping
Sleeping
ichieh commited on
Commit ·
f22f614
1
Parent(s): 480c1d4
Improve data persistence with better permission handling and logging
Browse files- Dockerfile +4 -2
- app.py +33 -7
Dockerfile
CHANGED
|
@@ -16,8 +16,9 @@ COPY . /code/
|
|
| 16 |
|
| 17 |
# Create data directory and set permissions
|
| 18 |
RUN mkdir -p /data && \
|
| 19 |
-
chown -R 1000:1000 /data && \
|
| 20 |
chmod -R 777 /data && \
|
|
|
|
|
|
|
| 21 |
chown -R 1000:1000 /code
|
| 22 |
|
| 23 |
# Switch to non-root user
|
|
@@ -25,6 +26,7 @@ USER 1000:1000
|
|
| 25 |
|
| 26 |
# Set environment variables
|
| 27 |
ENV PYTHONUNBUFFERED=1
|
|
|
|
| 28 |
|
| 29 |
-
# Run the application
|
| 30 |
CMD ["python", "app.py"]
|
|
|
|
| 16 |
|
| 17 |
# Create data directory and set permissions
|
| 18 |
RUN mkdir -p /data && \
|
|
|
|
| 19 |
chmod -R 777 /data && \
|
| 20 |
+
chown -R 1000:1000 /data && \
|
| 21 |
+
chmod -R 777 /code && \
|
| 22 |
chown -R 1000:1000 /code
|
| 23 |
|
| 24 |
# Switch to non-root user
|
|
|
|
| 26 |
|
| 27 |
# Set environment variables
|
| 28 |
ENV PYTHONUNBUFFERED=1
|
| 29 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 30 |
|
| 31 |
+
# Run the application with proper permissions
|
| 32 |
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -16,37 +16,63 @@ ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD', '1234') # 預設密碼為 123
|
|
| 16 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 17 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
| 18 |
db_path = '/data/event.db'
|
| 19 |
-
# 確保資料目錄存在
|
| 20 |
os.makedirs('/data', exist_ok=True)
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
retry_count = 0
|
| 24 |
while retry_count < max_retries:
|
| 25 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if os.access('/data', os.W_OK):
|
|
|
|
| 27 |
break
|
|
|
|
| 28 |
print(f"Waiting for /data directory to be writable... Attempt {retry_count + 1}/{max_retries}")
|
| 29 |
-
time.sleep(
|
| 30 |
retry_count += 1
|
| 31 |
except Exception as e:
|
| 32 |
print(f"Error checking /data directory: {e}")
|
| 33 |
-
time.sleep(
|
| 34 |
retry_count += 1
|
|
|
|
|
|
|
|
|
|
| 35 |
else: # 在本地開發環境中
|
| 36 |
db_path = os.path.join(app.instance_path, 'event.db')
|
| 37 |
os.makedirs(app.instance_path, exist_ok=True)
|
| 38 |
|
| 39 |
print(f"Using database path: {db_path}")
|
|
|
|
|
|
|
| 40 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 41 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 42 |
|
| 43 |
# 確保資料庫文件存在並設置權限
|
| 44 |
try:
|
|
|
|
| 45 |
if not os.path.exists(db_path):
|
|
|
|
| 46 |
open(db_path, 'a').close()
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
os.chmod(db_path, 0o666)
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except OSError as e:
|
| 51 |
print(f"Error setting up database: {e}")
|
| 52 |
|
|
|
|
| 16 |
# 設置資料庫路徑 - 使用持久化存儲
|
| 17 |
if os.environ.get('SPACE_ID'): # 在 Hugging Face Spaces 環境中
|
| 18 |
db_path = '/data/event.db'
|
| 19 |
+
# 確保資料目錄存在並有正確權限
|
| 20 |
os.makedirs('/data', exist_ok=True)
|
| 21 |
+
|
| 22 |
+
# 等待資料目錄就緒並確保權限正確
|
| 23 |
+
max_retries = 10
|
| 24 |
retry_count = 0
|
| 25 |
while retry_count < max_retries:
|
| 26 |
try:
|
| 27 |
+
# 檢查目錄權限
|
| 28 |
+
if not os.path.exists('/data'):
|
| 29 |
+
print("Creating /data directory...")
|
| 30 |
+
os.makedirs('/data', exist_ok=True)
|
| 31 |
+
|
| 32 |
+
# 設置權限
|
| 33 |
+
os.chmod('/data', 0o777)
|
| 34 |
+
|
| 35 |
+
# 檢查是否可寫
|
| 36 |
if os.access('/data', os.W_OK):
|
| 37 |
+
print("/data directory is writable")
|
| 38 |
break
|
| 39 |
+
|
| 40 |
print(f"Waiting for /data directory to be writable... Attempt {retry_count + 1}/{max_retries}")
|
| 41 |
+
time.sleep(2)
|
| 42 |
retry_count += 1
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Error checking /data directory: {e}")
|
| 45 |
+
time.sleep(2)
|
| 46 |
retry_count += 1
|
| 47 |
+
|
| 48 |
+
if retry_count >= max_retries:
|
| 49 |
+
print("Warning: Could not verify /data directory permissions after maximum retries")
|
| 50 |
else: # 在本地開發環境中
|
| 51 |
db_path = os.path.join(app.instance_path, 'event.db')
|
| 52 |
os.makedirs(app.instance_path, exist_ok=True)
|
| 53 |
|
| 54 |
print(f"Using database path: {db_path}")
|
| 55 |
+
|
| 56 |
+
# 設置資料庫
|
| 57 |
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
|
| 58 |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 59 |
|
| 60 |
# 確保資料庫文件存在並設置權限
|
| 61 |
try:
|
| 62 |
+
# 如果資料庫文件不存在,創建它
|
| 63 |
if not os.path.exists(db_path):
|
| 64 |
+
print(f"Creating new database file at {db_path}")
|
| 65 |
open(db_path, 'a').close()
|
| 66 |
+
|
| 67 |
+
# 設置資料庫文件權限
|
| 68 |
+
print(f"Setting permissions for database file at {db_path}")
|
| 69 |
os.chmod(db_path, 0o666)
|
| 70 |
+
|
| 71 |
+
# 檢查資料庫文件權限
|
| 72 |
+
if os.access(db_path, os.W_OK):
|
| 73 |
+
print("Database file is writable")
|
| 74 |
+
else:
|
| 75 |
+
print("Warning: Database file is not writable")
|
| 76 |
except OSError as e:
|
| 77 |
print(f"Error setting up database: {e}")
|
| 78 |
|