huggeu commited on
Commit
075863d
·
verified ·
1 Parent(s): 1d57805

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +51 -52
Dockerfile CHANGED
@@ -1,5 +1,9 @@
1
  FROM linuxserver/code-server
2
 
 
 
 
 
3
  RUN apt-get update && apt-get upgrade -y && apt-get install -y \
4
  lsb-release ca-certificates apt-transport-https wget software-properties-common gnupg2 \
5
  python3 python3-pip git openssh-client build-essential procps && \
@@ -10,7 +14,8 @@ RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C &&
10
  apt-get update && apt-get install -y --fix-broken php7.4 php7.4-gd php7.4-zip php7.4-bcmath php7.4-curl && \
11
  rm -rf /var/lib/apt/lists/*
12
 
13
- RUN mkdir -p /home/www /home/www/cs /config/.opencode /config/.cache/huggingface
 
14
 
15
  RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages --no-warn-script-location && \
16
  chmod -R 777 /config/.cache/huggingface /config/.opencode /home/www
@@ -53,71 +58,65 @@ echo "Done. Services should be restarted."
53
  EOF
54
  RUN chmod +x /usr/local/bin/restartp
55
 
 
56
  RUN cat > /usr/local/bin/sync.py << 'EOF'
57
  import os, sys, tarfile
58
  from huggingface_hub import HfApi, hf_hub_download
59
  from datetime import datetime, timedelta
 
60
  api = HfApi()
61
  repo_id = os.getenv("HF_DATASET")
62
  token = os.getenv("HF_TOKEN")
 
63
  def restore():
64
- try:
65
- print(f"--- [SYNC] Starting restore process, target repo: {repo_id} ---")
66
- if not repo_id or not token:
67
- print("--- [SYNC] Skip restore: HF_DATASET/HF_TOKEN not configured ---")
68
- return False
69
- files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
70
- now = datetime.now()
71
- for i in range(5):
72
- day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
73
- name = f"backup_{day}.tar.gz"
74
- if name in files:
75
- print(f"--- [SYNC] Found backup file: {name}, Downloading... ---")
76
- path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
77
- with tarfile.open(path, "r:gz") as tar:
78
- tar.extractall(path="/")
79
- print(f"--- [SYNC] Restore success! Restored /home/www and /config/.opencode ---")
80
- return True
81
- print("--- [SYNC] No backup found in recent 5 days ---")
82
- except Exception as e: print(f"--- [SYNC] Restore error: {e} ---")
 
 
83
  def backup():
84
- try:
85
- day = datetime.now().strftime("%Y-%m-%d")
86
- name = f"backup_{day}.tar.gz"
87
- print(f"--- [SYNC] Starting full backup: {name} ---")
88
- with tarfile.open(name, "w:gz") as tar:
89
- www_dir = "/home/www"
90
- if os.path.exists(www_dir):
91
- tar.add(www_dir, arcname=www_dir)
92
- opencode_dir = "/config/.opencode"
93
- if os.path.exists(opencode_dir):
94
- for root, dirs, files in os.walk(opencode_dir):
95
- rel_path = os.path.relpath(root, start=opencode_dir)
96
- if rel_path == '.':
97
- arc_root = opencode_dir
98
- else:
99
- arc_root = os.path.join(opencode_dir, rel_path)
100
- def exclude_opencode(tarinfo):
101
- if tarinfo.name == '/config/.opencode/bin/opencode':
102
- return None
103
- return tarinfo
104
- tar.add(opencode_dir, arcname=opencode_dir, filter=exclude_opencode)
105
- api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
106
- if os.path.exists(name):
107
- os.remove(name)
108
- print(f"--- [SYNC] Backup uploaded! Included /home/www and /config/.opencode ---")
109
- except Exception as e: print(f"--- [SYNC] Backup error: {e} ---")
110
  if __name__ == "__main__":
111
- if len(sys.argv) > 1 and sys.argv[1] == "backup": backup()
112
- else: restore()
 
 
113
  EOF
114
 
115
  RUN mkdir -p /etc/services.d/backup-service && \
116
  echo '#!/usr/bin/with-contenv bash\n\
117
  mkdir -p /config/.opencode /home/www\n\
118
- # 打印调试信息\n\
119
  echo "Debug: HF_DATASET=$HF_DATASET, HF_TOKEN=$HF_TOKEN"\n\
120
- # 修复:确保使用with-contenv传递的环境变量\n\
121
  if [ -n "$HF_DATASET" ] && [ -n "$HF_TOKEN" ]; then\n\
122
  echo "Starting restore with repo: $HF_DATASET"\n\
123
  python3 /usr/local/bin/sync.py restore\n\
@@ -138,5 +137,5 @@ RUN mkdir -p /etc/services.d/opencode-web && \
138
  source /etc/profile\n\
139
  exec opencode web --port 8080" > /etc/services.d/opencode-web/run && \
140
  chmod +x /etc/services.d/opencode-web/run
141
-
142
- EXPOSE 8443
 
1
  FROM linuxserver/code-server
2
 
3
+ # 设置环境变量,将所有需要持久化的数据重定向到 /home/www
4
+ ENV OPENCODE_CONFIG=/home/www/opencode/opencode.json
5
+ ENV XDG_DATA_HOME=/home/www/opencode/share
6
+
7
  RUN apt-get update && apt-get upgrade -y && apt-get install -y \
8
  lsb-release ca-certificates apt-transport-https wget software-properties-common gnupg2 \
9
  python3 python3-pip git openssh-client build-essential procps && \
 
14
  apt-get update && apt-get install -y --fix-broken php7.4 php7.4-gd php7.4-zip php7.4-bcmath php7.4-curl && \
15
  rm -rf /var/lib/apt/lists/*
16
 
17
+ # 创建必要的目录(注意:OpenCode 安装目录 /config/.opencode 依然保留)
18
+ RUN mkdir -p /home/www /home/www/cs /home/www/opencode /home/www/opencode/share /config/.opencode /config/.cache/huggingface
19
 
20
  RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages --no-warn-script-location && \
21
  chmod -R 777 /config/.cache/huggingface /config/.opencode /home/www
 
58
  EOF
59
  RUN chmod +x /usr/local/bin/restartp
60
 
61
+ # 简化的备份脚本:只备份 /home/www
62
  RUN cat > /usr/local/bin/sync.py << 'EOF'
63
  import os, sys, tarfile
64
  from huggingface_hub import HfApi, hf_hub_download
65
  from datetime import datetime, timedelta
66
+
67
  api = HfApi()
68
  repo_id = os.getenv("HF_DATASET")
69
  token = os.getenv("HF_TOKEN")
70
+
71
  def restore():
72
+ try:
73
+ print(f"--- [SYNC] Starting restore process, target repo: {repo_id} ---")
74
+ if not repo_id or not token:
75
+ print("--- [SYNC] Skip restore: HF_DATASET/HF_TOKEN not configured ---")
76
+ return False
77
+ files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)
78
+ now = datetime.now()
79
+ for i in range(5):
80
+ day = (now - timedelta(days=i)).strftime("%Y-%m-%d")
81
+ name = f"backup_{day}.tar.gz"
82
+ if name in files:
83
+ print(f"--- [SYNC] Found backup file: {name}, Downloading... ---")
84
+ path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)
85
+ with tarfile.open(path, "r:gz") as tar:
86
+ tar.extractall(path="/")
87
+ print(f"--- [SYNC] Restore success! Restored /home/www ---")
88
+ return True
89
+ print("--- [SYNC] No backup found in recent 5 days ---")
90
+ except Exception as e:
91
+ print(f"--- [SYNC] Restore error: {e} ---")
92
+
93
  def backup():
94
+ try:
95
+ day = datetime.now().strftime("%Y-%m-%d")
96
+ name = f"backup_{day}.tar.gz"
97
+ print(f"--- [SYNC] Starting full backup: {name} ---")
98
+ with tarfile.open(name, "w:gz") as tar:
99
+ www_dir = "/home/www"
100
+ if os.path.exists(www_dir):
101
+ tar.add(www_dir, arcname=www_dir)
102
+ api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)
103
+ if os.path.exists(name):
104
+ os.remove(name)
105
+ print(f"--- [SYNC] Backup uploaded! Included /home/www ---")
106
+ except Exception as e:
107
+ print(f"--- [SYNC] Backup error: {e} ---")
108
+
 
 
 
 
 
 
 
 
 
 
 
109
  if __name__ == "__main__":
110
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
111
+ backup()
112
+ else:
113
+ restore()
114
  EOF
115
 
116
  RUN mkdir -p /etc/services.d/backup-service && \
117
  echo '#!/usr/bin/with-contenv bash\n\
118
  mkdir -p /config/.opencode /home/www\n\
 
119
  echo "Debug: HF_DATASET=$HF_DATASET, HF_TOKEN=$HF_TOKEN"\n\
 
120
  if [ -n "$HF_DATASET" ] && [ -n "$HF_TOKEN" ]; then\n\
121
  echo "Starting restore with repo: $HF_DATASET"\n\
122
  python3 /usr/local/bin/sync.py restore\n\
 
137
  source /etc/profile\n\
138
  exec opencode web --port 8080" > /etc/services.d/opencode-web/run && \
139
  chmod +x /etc/services.d/opencode-web/run
140
+
141
+ EXPOSE 8443