wwforonce commited on
Commit
2509e3a
·
1 Parent(s): 0f909e7

fix storage permission error

Browse files
Files changed (3) hide show
  1. Dockerfile +7 -1
  2. start_with_sync.sh +18 -6
  3. sync_storage.py +71 -10
Dockerfile CHANGED
@@ -14,9 +14,15 @@ RUN pip install --no-cache-dir \
14
  huggingface_hub \
15
  datasets
16
 
17
- # Set HuggingFace cache directory to tmp (usually writable)
18
  ENV HF_HOME=/tmp/hf_cache
19
  ENV HUGGINGFACE_HUB_CACHE=/tmp/hf_cache
 
 
 
 
 
 
20
 
21
  # Copy sync scripts
22
  COPY sync_storage.py /app/sync_storage.py
 
14
  huggingface_hub \
15
  datasets
16
 
17
+ # Set all cache directories to /tmp (writable)
18
  ENV HF_HOME=/tmp/hf_cache
19
  ENV HUGGINGFACE_HUB_CACHE=/tmp/hf_cache
20
+ ENV TRANSFORMERS_CACHE=/tmp/hf_cache
21
+ ENV SENTENCE_TRANSFORMERS_HOME=/tmp/hf_cache
22
+
23
+ # Override Open WebUI cache directories
24
+ ENV DATA_DIR=/tmp/open-webui-data
25
+ ENV STATIC_DIR=/tmp/static
26
 
27
  # Copy sync scripts
28
  COPY sync_storage.py /app/sync_storage.py
start_with_sync.sh CHANGED
@@ -2,22 +2,34 @@
2
 
3
  set -e
4
 
5
- # Use /tmp for all writable data (usually works in restricted environments)
6
  export DATA_DIR="/tmp/open-webui-data"
7
- export HF_STORAGE_REPO="${HF_STORAGE_REPO:-your-username/open-webui-storage}"
8
  export SYNC_INTERVAL="${SYNC_INTERVAL:-300}"
9
 
10
- # Set HuggingFace cache to tmp
11
  export HF_HOME="/tmp/hf_cache"
12
  export HUGGINGFACE_HUB_CACHE="/tmp/hf_cache"
 
 
 
 
 
 
13
 
14
  echo "Starting Open WebUI with HF Dataset persistence..."
15
  echo "Data directory: $DATA_DIR"
16
  echo "HF Repository: $HF_STORAGE_REPO"
17
  echo "HF Cache: $HF_HOME"
18
 
19
- # Create directories in /tmp (usually writable)
20
- mkdir -p "$DATA_DIR" "$HF_HOME"
 
 
 
 
 
 
21
 
22
  # Test write permissions
23
  if touch "$DATA_DIR/test" 2>/dev/null; then
@@ -78,7 +90,7 @@ background_sync() {
78
  background_sync &
79
  SYNC_PID=$!
80
 
81
- # Start Open WebUI with custom data directory
82
  echo "Starting Open WebUI..."
83
 
84
  # Set environment variables for Open WebUI
 
2
 
3
  set -e
4
 
5
+ # Use /tmp for all writable data
6
  export DATA_DIR="/tmp/open-webui-data"
7
+ export HF_STORAGE_REPO="${HF_STORAGE_REPO:-nxdev-org/open-webui-storage}"
8
  export SYNC_INTERVAL="${SYNC_INTERVAL:-300}"
9
 
10
+ # Set all HuggingFace and cache directories to /tmp
11
  export HF_HOME="/tmp/hf_cache"
12
  export HUGGINGFACE_HUB_CACHE="/tmp/hf_cache"
13
+ export TRANSFORMERS_CACHE="/tmp/hf_cache"
14
+ export SENTENCE_TRANSFORMERS_HOME="/tmp/hf_cache"
15
+
16
+ # Override Open WebUI environment variables
17
+ export STATIC_DIR="/tmp/static"
18
+ export UPLOAD_DIR="/tmp/uploads"
19
 
20
  echo "Starting Open WebUI with HF Dataset persistence..."
21
  echo "Data directory: $DATA_DIR"
22
  echo "HF Repository: $HF_STORAGE_REPO"
23
  echo "HF Cache: $HF_HOME"
24
 
25
+ # Create all necessary directories
26
+ mkdir -p "$DATA_DIR" "$HF_HOME" "$STATIC_DIR" "$UPLOAD_DIR"
27
+
28
+ # Copy static files to writable location
29
+ if [ -d "/app/backend/open_webui/static" ]; then
30
+ echo "Copying static files to writable location..."
31
+ cp -r /app/backend/open_webui/static/* "$STATIC_DIR/" 2>/dev/null || true
32
+ fi
33
 
34
  # Test write permissions
35
  if touch "$DATA_DIR/test" 2>/dev/null; then
 
90
  background_sync &
91
  SYNC_PID=$!
92
 
93
+ # Start Open WebUI
94
  echo "Starting Open WebUI..."
95
 
96
  # Set environment variables for Open WebUI
sync_storage.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import shutil
4
  import json
5
  from pathlib import Path
6
- from huggingface_hub import HfApi
7
  import tarfile
8
  import tempfile
9
 
@@ -16,6 +16,65 @@ class HFStorageSync:
16
  # Initialize API with token directly
17
  self.api = HfApi(token=token) if token else HfApi()
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def download_data(self):
20
  """Download and extract data from HF dataset repo"""
21
  try:
@@ -38,12 +97,9 @@ class HFStorageSync:
38
  print("No HF_TOKEN provided, skipping download")
39
  return
40
 
41
- # Check if repo exists
42
- try:
43
- repo_info = self.api.repo_info(repo_id=self.repo_id, repo_type="dataset")
44
- print(f"Found repository: {self.repo_id}")
45
- except Exception as e:
46
- print(f"Repository {self.repo_id} not found: {e}")
47
  return
48
 
49
  # Try to download the data archive
@@ -61,7 +117,7 @@ class HFStorageSync:
61
  print(f"Data extracted to {self.data_dir}")
62
 
63
  except Exception as e:
64
- print(f"No data.tar.gz found or error extracting: {e}")
65
 
66
  except Exception as e:
67
  print(f"Error during download: {e}")
@@ -79,11 +135,16 @@ class HFStorageSync:
79
  print("No data to upload")
80
  return
81
 
 
 
 
 
 
82
  # Create temporary archive
83
  with tempfile.NamedTemporaryFile(suffix='.tar.gz', delete=False) as tmp:
84
  with tarfile.open(tmp.name, 'w:gz') as tar:
85
  for item in self.data_dir.iterdir():
86
- if item.name != "test_write": # Skip test files
87
  tar.add(item, arcname=item.name)
88
 
89
  # Upload to HF
@@ -107,7 +168,7 @@ class HFStorageSync:
107
  def main():
108
  import sys
109
 
110
- repo_id = os.getenv("HF_STORAGE_REPO", "your-username/open-webui-storage")
111
  token = os.getenv("HF_TOKEN")
112
  data_dir = os.getenv("DATA_DIR", "/tmp/open-webui-data")
113
 
 
3
  import shutil
4
  import json
5
  from pathlib import Path
6
+ from huggingface_hub import HfApi, create_repo
7
  import tarfile
8
  import tempfile
9
 
 
16
  # Initialize API with token directly
17
  self.api = HfApi(token=token) if token else HfApi()
18
 
19
+ def ensure_repo_exists(self):
20
+ """Create repository if it doesn't exist"""
21
+ if not self.token:
22
+ print("No token provided, cannot create repository")
23
+ return False
24
+
25
+ try:
26
+ # Check if repo exists
27
+ repo_info = self.api.repo_info(repo_id=self.repo_id, repo_type="dataset")
28
+ print(f"Repository {self.repo_id} exists")
29
+ return True
30
+ except Exception as e:
31
+ print(f"Repository {self.repo_id} not found, attempting to create...")
32
+ try:
33
+ create_repo(
34
+ repo_id=self.repo_id,
35
+ repo_type="dataset",
36
+ token=self.token,
37
+ private=True, # Make it private by default
38
+ exist_ok=True
39
+ )
40
+ print(f"Created repository {self.repo_id}")
41
+
42
+ # Create initial README
43
+ readme_content = """# Open WebUI Storage
44
+
45
+ This dataset stores persistent data for Open WebUI deployment.
46
+
47
+ ## Contents
48
+
49
+ - `data.tar.gz`: Compressed archive containing all Open WebUI data including:
50
+ - User configurations
51
+ - Chat histories
52
+ - Uploaded files
53
+ - Database files
54
+
55
+ This repository is automatically managed by the Open WebUI sync system.
56
+ """
57
+
58
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as tmp:
59
+ tmp.write(readme_content)
60
+ tmp.flush()
61
+
62
+ self.api.upload_file(
63
+ path_or_fileobj=tmp.name,
64
+ path_in_repo="README.md",
65
+ repo_id=self.repo_id,
66
+ repo_type="dataset",
67
+ commit_message="Initial repository setup",
68
+ token=self.token
69
+ )
70
+
71
+ os.unlink(tmp.name)
72
+
73
+ return True
74
+ except Exception as create_error:
75
+ print(f"Failed to create repository: {create_error}")
76
+ return False
77
+
78
  def download_data(self):
79
  """Download and extract data from HF dataset repo"""
80
  try:
 
97
  print("No HF_TOKEN provided, skipping download")
98
  return
99
 
100
+ # Ensure repository exists
101
+ if not self.ensure_repo_exists():
102
+ print("Could not access or create repository")
 
 
 
103
  return
104
 
105
  # Try to download the data archive
 
117
  print(f"Data extracted to {self.data_dir}")
118
 
119
  except Exception as e:
120
+ print(f"No existing data found (this is normal for first run): {e}")
121
 
122
  except Exception as e:
123
  print(f"Error during download: {e}")
 
135
  print("No data to upload")
136
  return
137
 
138
+ # Ensure repository exists
139
+ if not self.ensure_repo_exists():
140
+ print("Could not access or create repository")
141
+ return
142
+
143
  # Create temporary archive
144
  with tempfile.NamedTemporaryFile(suffix='.tar.gz', delete=False) as tmp:
145
  with tarfile.open(tmp.name, 'w:gz') as tar:
146
  for item in self.data_dir.iterdir():
147
+ if item.name not in ["test_write", ".gitkeep"]: # Skip test files
148
  tar.add(item, arcname=item.name)
149
 
150
  # Upload to HF
 
168
  def main():
169
  import sys
170
 
171
+ repo_id = os.getenv("HF_STORAGE_REPO", "nxdev-org/open-webui-storage")
172
  token = os.getenv("HF_TOKEN")
173
  data_dir = os.getenv("DATA_DIR", "/tmp/open-webui-data")
174