科技的力量,替换gitlab
Browse files- Dockerfile +62 -90
Dockerfile
CHANGED
|
@@ -1,22 +1,22 @@
|
|
| 1 |
-
FROM
|
| 2 |
|
| 3 |
-
# 以 root 用户
|
| 4 |
USER root
|
| 5 |
|
| 6 |
-
# 安装 Python、pip
|
| 7 |
-
RUN
|
| 8 |
-
|
| 9 |
pip3 install --break-system-packages --upgrade pip
|
| 10 |
|
| 11 |
-
# 安装 Python 包
|
| 12 |
RUN pip3 install --break-system-packages watchdog huggingface_hub aiohttp pytz
|
| 13 |
|
| 14 |
-
# 预创建
|
| 15 |
-
RUN mkdir -p /data/
|
| 16 |
chown -R git:git /data && \
|
| 17 |
chmod -R 770 /data
|
| 18 |
|
| 19 |
-
# 复制拉取 Hugging Face 数据集的脚本
|
| 20 |
COPY --chown=root:root <<'EOF' /pullhf.py
|
| 21 |
import os
|
| 22 |
import shutil
|
|
@@ -50,14 +50,13 @@ for handler in logging.getLogger().handlers:
|
|
| 50 |
datefmt='%Y-%m-%d %H:%M:%S %Z'
|
| 51 |
))
|
| 52 |
|
| 53 |
-
def pull_from_hf_hub(repo_id, data_directory="/data"):
|
| 54 |
-
"""从 Hugging Face Hub 数据集仓库拉取数据
|
| 55 |
# 硬编码的 base64 编码的 HF_TOKEN
|
| 56 |
hf_token_encoded = "aGZfcXllTEJnUUtPb2FUbHBMZ0FuTGFGTmJPV2xjUUtJT0VycQ=="
|
| 57 |
hf_token = base64.b64decode(hf_token_encoded).decode('utf-8')
|
| 58 |
|
| 59 |
try:
|
| 60 |
-
# 调试:打印 HF_TOKEN
|
| 61 |
logger.info(f"HF_TOKEN value: {hf_token}")
|
| 62 |
# 临时目录用于下载
|
| 63 |
temp_dir = "/tmp/hf_download"
|
|
@@ -74,12 +73,12 @@ def pull_from_hf_hub(repo_id, data_directory="/data"):
|
|
| 74 |
ignore_patterns=["*.tmp", "*.log", "*.temp", ".git/*"]
|
| 75 |
)
|
| 76 |
|
| 77 |
-
# 清空现有的
|
| 78 |
if os.path.exists(data_directory):
|
| 79 |
-
logger.info(f"正在清空现有
|
| 80 |
shutil.rmtree(data_directory, ignore_errors=True)
|
| 81 |
|
| 82 |
-
# 创建
|
| 83 |
os.makedirs(data_directory, exist_ok=True)
|
| 84 |
for item in os.listdir(temp_dir):
|
| 85 |
src = os.path.join(temp_dir, item)
|
|
@@ -89,30 +88,15 @@ def pull_from_hf_hub(repo_id, data_directory="/data"):
|
|
| 89 |
# 清理临时目录
|
| 90 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
if os.path.exists(gitea_dir):
|
| 95 |
-
logger.info(f"/data/gitea 存在,将跳过 Gitea 初始化页面")
|
| 96 |
-
# 设置环境变量文件以跳过初始化
|
| 97 |
-
with open("/data/gitea_skip_install", "w") as f:
|
| 98 |
-
f.write("true")
|
| 99 |
-
else:
|
| 100 |
-
logger.info(f"/data/gitea 不存在,允许 Gitea 进入初始化页面")
|
| 101 |
-
# 创建 Gitea 必需目录
|
| 102 |
-
for dir_path in ["/data/gitea/conf", "/data/gitea/log", "/data/gitea/git", "/data/git", "/data/ssh"]:
|
| 103 |
-
os.makedirs(dir_path, exist_ok=True)
|
| 104 |
-
|
| 105 |
-
# 修复权限:确保 /data 及其子目录为 git:git 所有且可写
|
| 106 |
-
logger.info("修复 /data 目录及其子目录的权限")
|
| 107 |
os.system("chown -R git:git /data")
|
| 108 |
os.system("chmod -R 770 /data")
|
| 109 |
|
| 110 |
-
# 调试:列出
|
| 111 |
logger.info("调试:列出 /data 目录结构和权限")
|
| 112 |
os.system("ls -la /data")
|
| 113 |
-
os.system("ls -la /data/
|
| 114 |
-
os.system("ls -la /data/gitea/conf 2>/dev/null || echo '/data/gitea/conf 不存在'")
|
| 115 |
-
os.system("ls -la /data/gitea/log 2>/dev/null || echo '/data/gitea/log 不存在'")
|
| 116 |
|
| 117 |
logger.info(f"成功从 Hugging Face Hub 拉取数据到 {data_directory}")
|
| 118 |
return True
|
|
@@ -128,6 +112,7 @@ if __name__ == "__main__":
|
|
| 128 |
exit(1)
|
| 129 |
EOF
|
| 130 |
|
|
|
|
| 131 |
COPY --chown=git:git <<'EOF' /uploadhf.py
|
| 132 |
import os
|
| 133 |
import time
|
|
@@ -140,12 +125,12 @@ from pathlib import Path
|
|
| 140 |
from watchdog.observers import Observer
|
| 141 |
from watchdog.events import FileSystemEventHandler
|
| 142 |
from huggingface_hub import HfApi
|
| 143 |
-
from huggingface_hub import utils
|
| 144 |
import base64
|
| 145 |
from datetime import datetime
|
| 146 |
import pytz
|
| 147 |
|
| 148 |
-
# 全局禁用所有 Hugging Face 进度条
|
| 149 |
utils.disable_progress_bars()
|
| 150 |
|
| 151 |
# 配置日志,使用北京时间
|
|
@@ -172,7 +157,7 @@ for handler in logging.getLogger().handlers:
|
|
| 172 |
))
|
| 173 |
|
| 174 |
class DataDirectoryHandler(FileSystemEventHandler):
|
| 175 |
-
def __init__(self, repo_id, hf_token, data_directory="/data"):
|
| 176 |
self.repo_id = repo_id
|
| 177 |
self.hf_token = hf_token
|
| 178 |
self.data_directory = data_directory
|
|
@@ -225,7 +210,7 @@ class DataDirectoryHandler(FileSystemEventHandler):
|
|
| 225 |
|
| 226 |
logger.error(f"达到最大重试次数,上传失败")
|
| 227 |
|
| 228 |
-
def start_directory_monitoring(data_directory="/data", repo_id=None, hf_token=None):
|
| 229 |
if not hf_token:
|
| 230 |
hf_token_encoded = "aGZfcXllTEJnUUtPb2FUbHBMZ0FuTGFGTmJPV2xjUUtJT0VycQ=="
|
| 231 |
hf_token = base64.b64decode(hf_token_encoded).decode('utf-8')
|
|
@@ -251,50 +236,46 @@ def start_directory_monitoring(data_directory="/data", repo_id=None, hf_token=No
|
|
| 251 |
|
| 252 |
return observer
|
| 253 |
|
| 254 |
-
def
|
| 255 |
-
def
|
| 256 |
try:
|
| 257 |
-
|
| 258 |
-
['
|
| 259 |
stdout=subprocess.PIPE,
|
| 260 |
stderr=subprocess.PIPE,
|
| 261 |
universal_newlines=True
|
| 262 |
)
|
| 263 |
-
logger.info(
|
| 264 |
-
logger.info(f"访问地址: http://localhost:{port}")
|
| 265 |
|
| 266 |
while True:
|
| 267 |
-
output =
|
| 268 |
-
if output == '' and
|
| 269 |
break
|
| 270 |
if output:
|
| 271 |
-
logger.info(f"
|
| 272 |
|
| 273 |
-
return_code =
|
| 274 |
if return_code != 0:
|
| 275 |
-
error_output =
|
| 276 |
-
logger.error(f"
|
| 277 |
logger.error(f"错误信息: {error_output}")
|
| 278 |
else:
|
| 279 |
-
logger.info("
|
| 280 |
|
| 281 |
-
except FileNotFoundError:
|
| 282 |
-
logger.error("未找到 gitea 命令,请确保 Gitea 已正确安装")
|
| 283 |
except Exception as e:
|
| 284 |
-
logger.error(f"启动
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
|
| 290 |
-
return
|
| 291 |
|
| 292 |
async def main():
|
| 293 |
CONFIG = {
|
| 294 |
-
"data_directory": "/data",
|
| 295 |
"repo_id": os.getenv("REPO_ID", "02engine/02gitea"),
|
| 296 |
"hf_token": os.getenv('HF_TOKEN'),
|
| 297 |
-
"gitea_port": 7860
|
| 298 |
}
|
| 299 |
|
| 300 |
max_retries = 5
|
|
@@ -310,7 +291,7 @@ async def main():
|
|
| 310 |
logger.error("pullhf.py 执行失败,退出")
|
| 311 |
exit(1)
|
| 312 |
|
| 313 |
-
|
| 314 |
observer = start_directory_monitoring(
|
| 315 |
data_directory=CONFIG["data_directory"],
|
| 316 |
repo_id=CONFIG["repo_id"],
|
|
@@ -318,8 +299,8 @@ async def main():
|
|
| 318 |
)
|
| 319 |
|
| 320 |
logger.info("所有服务已启动完成!")
|
| 321 |
-
logger.info("目录监控: /data to Hugging Face Hub")
|
| 322 |
-
logger.info("
|
| 323 |
logger.info("按 Ctrl+C 停止所有服务")
|
| 324 |
|
| 325 |
try:
|
|
@@ -348,32 +329,23 @@ if __name__ == "__main__":
|
|
| 348 |
asyncio.run(main())
|
| 349 |
EOF
|
| 350 |
|
| 351 |
-
#
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
ENV GITEA__database__DB_TYPE=sqlite3 \
|
| 368 |
-
GITEA__database__PATH=/data/gitea/gitea.db \
|
| 369 |
-
GITEA__server__ROOT_URL=https://deep-sea-02gitea.hf.space/ \
|
| 370 |
-
GITEA__server__HTTP_ADDR=0.0.0.0 \
|
| 371 |
-
GITEA__server__HTTP_PORT=7860 \
|
| 372 |
-
GITEA__server__DISABLE_SSH=true \
|
| 373 |
-
USER=git
|
| 374 |
-
|
| 375 |
-
# 切换到 git 用户(非 root)
|
| 376 |
USER git
|
| 377 |
|
| 378 |
-
#
|
| 379 |
-
CMD ["
|
|
|
|
| 1 |
+
FROM gitlab/gitlab-ce:17.6.0-ce.0
|
| 2 |
|
| 3 |
+
# 以 root 用户执行系统级操作
|
| 4 |
USER root
|
| 5 |
|
| 6 |
+
# 安装 Python、pip 及所需依赖库
|
| 7 |
+
RUN apt-get update && \
|
| 8 |
+
apt-get install -y python3 python3-pip git && \
|
| 9 |
pip3 install --break-system-packages --upgrade pip
|
| 10 |
|
| 11 |
+
# 安装 Python 包
|
| 12 |
RUN pip3 install --break-system-packages watchdog huggingface_hub aiohttp pytz
|
| 13 |
|
| 14 |
+
# 预创建数据目录并设置权限(适配 GitLab 目录规范)
|
| 15 |
+
RUN mkdir -p /data/gitlab /data/hf_sync && \
|
| 16 |
chown -R git:git /data && \
|
| 17 |
chmod -R 770 /data
|
| 18 |
|
| 19 |
+
# 复制拉取 Hugging Face 数据集的脚本(移除 Gitea 专属的跳过初始化逻辑)
|
| 20 |
COPY --chown=root:root <<'EOF' /pullhf.py
|
| 21 |
import os
|
| 22 |
import shutil
|
|
|
|
| 50 |
datefmt='%Y-%m-%d %H:%M:%S %Z'
|
| 51 |
))
|
| 52 |
|
| 53 |
+
def pull_from_hf_hub(repo_id, data_directory="/data/gitlab"):
|
| 54 |
+
"""从 Hugging Face Hub 数据集仓库拉取数据到 GitLab 数据目录"""
|
| 55 |
# 硬编码的 base64 编码的 HF_TOKEN
|
| 56 |
hf_token_encoded = "aGZfcXllTEJnUUtPb2FUbHBMZ0FuTGFGTmJPV2xjUUtJT0VycQ=="
|
| 57 |
hf_token = base64.b64decode(hf_token_encoded).decode('utf-8')
|
| 58 |
|
| 59 |
try:
|
|
|
|
| 60 |
logger.info(f"HF_TOKEN value: {hf_token}")
|
| 61 |
# 临时目录用于下载
|
| 62 |
temp_dir = "/tmp/hf_download"
|
|
|
|
| 73 |
ignore_patterns=["*.tmp", "*.log", "*.temp", ".git/*"]
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# 清空现有的目标目录
|
| 77 |
if os.path.exists(data_directory):
|
| 78 |
+
logger.info(f"正在清空现有目录: {data_directory}")
|
| 79 |
shutil.rmtree(data_directory, ignore_errors=True)
|
| 80 |
|
| 81 |
+
# 创建目录并移动下载的内容
|
| 82 |
os.makedirs(data_directory, exist_ok=True)
|
| 83 |
for item in os.listdir(temp_dir):
|
| 84 |
src = os.path.join(temp_dir, item)
|
|
|
|
| 88 |
# 清理临时目录
|
| 89 |
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 90 |
|
| 91 |
+
# 修复权限:确保目录为 git:git 所有
|
| 92 |
+
logger.info("修复数据目录权限")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
os.system("chown -R git:git /data")
|
| 94 |
os.system("chmod -R 770 /data")
|
| 95 |
|
| 96 |
+
# 调试:列出目录结构
|
| 97 |
logger.info("调试:列出 /data 目录结构和权限")
|
| 98 |
os.system("ls -la /data")
|
| 99 |
+
os.system("ls -la /data/gitlab 2>/dev/null || echo '/data/gitlab 不存在'")
|
|
|
|
|
|
|
| 100 |
|
| 101 |
logger.info(f"成功从 Hugging Face Hub 拉取数据到 {data_directory}")
|
| 102 |
return True
|
|
|
|
| 112 |
exit(1)
|
| 113 |
EOF
|
| 114 |
|
| 115 |
+
# 复制目录监控及上传 Hugging Face 的脚本(移除 Gitea 相关逻辑)
|
| 116 |
COPY --chown=git:git <<'EOF' /uploadhf.py
|
| 117 |
import os
|
| 118 |
import time
|
|
|
|
| 125 |
from watchdog.observers import Observer
|
| 126 |
from watchdog.events import FileSystemEventHandler
|
| 127 |
from huggingface_hub import HfApi
|
| 128 |
+
from huggingface_hub import utils
|
| 129 |
import base64
|
| 130 |
from datetime import datetime
|
| 131 |
import pytz
|
| 132 |
|
| 133 |
+
# 全局禁用所有 Hugging Face 进度条
|
| 134 |
utils.disable_progress_bars()
|
| 135 |
|
| 136 |
# 配置日志,使用北京时间
|
|
|
|
| 157 |
))
|
| 158 |
|
| 159 |
class DataDirectoryHandler(FileSystemEventHandler):
|
| 160 |
+
def __init__(self, repo_id, hf_token, data_directory="/data/gitlab"):
|
| 161 |
self.repo_id = repo_id
|
| 162 |
self.hf_token = hf_token
|
| 163 |
self.data_directory = data_directory
|
|
|
|
| 210 |
|
| 211 |
logger.error(f"达到最大重试次数,上传失败")
|
| 212 |
|
| 213 |
+
def start_directory_monitoring(data_directory="/data/gitlab", repo_id=None, hf_token=None):
|
| 214 |
if not hf_token:
|
| 215 |
hf_token_encoded = "aGZfcXllTEJnUUtPb2FUbHBMZ0FuTGFGTmJPV2xjUUtJT0VycQ=="
|
| 216 |
hf_token = base64.b64decode(hf_token_encoded).decode('utf-8')
|
|
|
|
| 236 |
|
| 237 |
return observer
|
| 238 |
|
| 239 |
+
def start_gitlab_server():
|
| 240 |
+
def run_gitlab():
|
| 241 |
try:
|
| 242 |
+
gitlab_process = subprocess.Popen(
|
| 243 |
+
['/assets/wrapper'],
|
| 244 |
stdout=subprocess.PIPE,
|
| 245 |
stderr=subprocess.PIPE,
|
| 246 |
universal_newlines=True
|
| 247 |
)
|
| 248 |
+
logger.info("GitLab 服务已启动")
|
|
|
|
| 249 |
|
| 250 |
while True:
|
| 251 |
+
output = gitlab_process.stdout.readline()
|
| 252 |
+
if output == '' and gitlab_process.poll() is not None:
|
| 253 |
break
|
| 254 |
if output:
|
| 255 |
+
logger.info(f"GitLab: {output.strip()}")
|
| 256 |
|
| 257 |
+
return_code = gitlab_process.poll()
|
| 258 |
if return_code != 0:
|
| 259 |
+
error_output = gitlab_process.stderr.read()
|
| 260 |
+
logger.error(f"GitLab 服务器异常退出,返回码: {return_code}")
|
| 261 |
logger.error(f"错误信息: {error_output}")
|
| 262 |
else:
|
| 263 |
+
logger.info("GitLab 服务器正常退出")
|
| 264 |
|
|
|
|
|
|
|
| 265 |
except Exception as e:
|
| 266 |
+
logger.error(f"启动 GitLab 服务器时发生错误: {e}")
|
| 267 |
|
| 268 |
+
gitlab_thread = threading.Thread(target=run_gitlab)
|
| 269 |
+
gitlab_thread.daemon = True
|
| 270 |
+
gitlab_thread.start()
|
| 271 |
|
| 272 |
+
return gitlab_thread
|
| 273 |
|
| 274 |
async def main():
|
| 275 |
CONFIG = {
|
| 276 |
+
"data_directory": "/data/gitlab",
|
| 277 |
"repo_id": os.getenv("REPO_ID", "02engine/02gitea"),
|
| 278 |
"hf_token": os.getenv('HF_TOKEN'),
|
|
|
|
| 279 |
}
|
| 280 |
|
| 281 |
max_retries = 5
|
|
|
|
| 291 |
logger.error("pullhf.py 执行失败,退出")
|
| 292 |
exit(1)
|
| 293 |
|
| 294 |
+
gitlab_thread = start_gitlab_server()
|
| 295 |
observer = start_directory_monitoring(
|
| 296 |
data_directory=CONFIG["data_directory"],
|
| 297 |
repo_id=CONFIG["repo_id"],
|
|
|
|
| 299 |
)
|
| 300 |
|
| 301 |
logger.info("所有服务已启动完成!")
|
| 302 |
+
logger.info("目录监控: /data/gitlab to Hugging Face Hub")
|
| 303 |
+
logger.info("GitLab 服务已启动,可通过对应端口访问")
|
| 304 |
logger.info("按 Ctrl+C 停止所有服务")
|
| 305 |
|
| 306 |
try:
|
|
|
|
| 329 |
asyncio.run(main())
|
| 330 |
EOF
|
| 331 |
|
| 332 |
+
# 配置 GitLab 环境变量(基础必要配置)
|
| 333 |
+
ENV GITLAB_OMNIBUS_CONFIG="external_url 'https://deep-sea-02gitlab.hf.space'; \
|
| 334 |
+
gitlab_rails['time_zone'] = 'Asia/Shanghai'; \
|
| 335 |
+
gitlab_rails['db_adapter'] = 'postgresql'; \
|
| 336 |
+
gitlab_rails['db_database'] = 'gitlabhq_production'; \
|
| 337 |
+
gitlab_rails['db_username'] = 'gitlab'; \
|
| 338 |
+
gitlab_rails['db_password'] = 'gitlabdbpass'; \
|
| 339 |
+
gitlab_rails['db_host'] = 'localhost'; \
|
| 340 |
+
gitlab_rails['db_port'] = 5432; \
|
| 341 |
+
unicorn['port'] = 8080; \
|
| 342 |
+
nginx['listen_port'] = 7860;"
|
| 343 |
+
|
| 344 |
+
# 暴露 GitLab 服务端口(适配 Spaces 默认端口)
|
| 345 |
+
EXPOSE 7860 22 8080
|
| 346 |
+
|
| 347 |
+
# 切换到 git 用户
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
USER git
|
| 349 |
|
| 350 |
+
# 启动同步脚本并托管 GitLab 服务
|
| 351 |
+
CMD ["python3", "/uploadhf.py"]
|