#!/bin/bash # 本地Git服务器配置脚本 - 从其他机器访问BEVFusion仓库 # 支持SSH和Git Daemon两种方式 set -e # 颜色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' print_header() { echo -e "${BLUE}================================================${NC}" echo -e "${BLUE} 本地Git服务器配置工具${NC}" echo -e "${BLUE} 从其他机器访问BEVFusion仓库${NC}" echo -e "${BLUE}================================================${NC}" echo } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_info() { echo -e "${BLUE}ℹ️ $1${NC}" } # 获取容器IP get_container_ip() { hostname -I | awk '{print $1}' 2>/dev/null || echo "localhost" } # 配置SSH + Git服务 setup_ssh_git() { echo print_info "配置SSH + Git服务..." # 检查SSH服务 if systemctl is-active ssh &>/dev/null; then print_success "SSH服务正在运行" else print_warning "SSH服务未运行,尝试启动..." sudo systemctl start ssh 2>/dev/null && print_success "SSH服务已启动" || print_error "SSH服务启动失败" fi # 创建共享目录 SHARED_DIR="/shared/git" print_info "创建共享Git目录: $SHARED_DIR" sudo mkdir -p "$SHARED_DIR" sudo chown -R "$USER:$USER" "$SHARED_DIR" # 创建裸仓库 REPO_DIR="$SHARED_DIR/bevfusion.git" if [ ! -d "$REPO_DIR" ]; then print_info "创建裸仓库..." git clone --bare /workspace/bevfusion "$REPO_DIR" cd "$REPO_DIR" git config --global user.name "bevfusion-server" git config --global user.email "server@local" print_success "裸仓库创建完成" else print_info "仓库已存在,更新..." cd /workspace/bevfusion git remote add local "$REPO_DIR" 2>/dev/null || true git push local main 2>/dev/null || print_warning "推送失败,可能需要手动处理" fi # 配置权限 print_info "配置仓库权限..." sudo chmod -R 755 "$SHARED_DIR" # 显示访问信息 CONTAINER_IP=$(get_container_ip) echo print_success "SSH + Git服务配置完成!" echo echo "📋 访问信息:" echo "仓库地址: ssh://$USER@$CONTAINER_IP$REPO_DIR" echo "克隆命令: git clone ssh://$USER@$CONTAINER_IP$REPO_DIR" echo echo "🔐 认证方式:" echo "1. 密码认证: 使用容器用户名和密码" echo "2. 密钥认证: 配置SSH密钥 (推荐)" echo echo "🧪 测试命令:" echo "ssh $USER@$CONTAINER_IP 'ls $REPO_DIR'" } # 配置Git Daemon (只读) setup_git_daemon() { echo print_info "配置Git Daemon (只读服务)..." SHARED_DIR="/shared/git" # 确保仓库存在 if [ ! -d "$SHARED_DIR/bevfusion.git" ]; then print_info "创建共享仓库..." sudo mkdir -p "$SHARED_DIR" sudo chown -R "$USER:$USER" "$SHARED_DIR" git clone --bare /workspace/bevfusion "$SHARED_DIR/bevfusion.git" fi # 启动Git Daemon print_info "启动Git Daemon服务..." git daemon --reuseaddr --base-path="$SHARED_DIR" --export-all --detach --pid-file=/tmp/git-daemon.pid if [ $? -eq 0 ]; then print_success "Git Daemon启动成功" else print_error "Git Daemon启动失败" return 1 fi # 显示访问信息 CONTAINER_IP=$(get_container_ip) echo print_success "Git Daemon配置完成!" echo echo "📋 访问信息:" echo "服务端口: 9418" echo "仓库地址: git://$CONTAINER_IP:9418/bevfusion.git" echo "克隆命令: git clone git://$CONTAINER_IP:9418/bevfusion.git" echo echo "⚠️ 注意事项:" echo "- 只读访问,无法推送更改" echo "- 适合代码分发,不适合协作开发" echo echo "🛑 停止服务:" echo "kill \$(cat /tmp/git-daemon.pid)" } # 配置Docker Volume方案 setup_docker_volume() { echo print_info "配置Docker Volume方案..." VOLUME_DIR="/shared/git" print_info "复制仓库到共享目录..." if [ ! -d "$VOLUME_DIR/bevfusion" ]; then cp -r /workspace/bevfusion "$VOLUME_DIR/" print_success "仓库复制完成" else print_info "目录已存在,更新仓库..." cd /workspace/bevfusion rsync -av --exclude='.git' . "$VOLUME_DIR/bevfusion/" 2>/dev/null || true cd "$VOLUME_DIR/bevfusion" git add . && git commit -m "更新自容器" 2>/dev/null || true fi echo print_success "Docker Volume配置完成!" echo echo "📋 访问方式:" echo "1. 从宿主机访问:" echo " ssh user@host-ip" echo " cd /path/to/volume/bevfusion" echo " git status" echo echo "2. 如果挂载了volume,从其他容器访问:" echo " docker exec -it other-container bash" echo " cd /shared/git/bevfusion" echo " git status" } # 显示网络信息 show_network_info() { echo print_info "网络连接信息" CONTAINER_IP=$(get_container_ip) echo "容器IP: $CONTAINER_IP" echo "开放端口:" netstat -tlnp 2>/dev/null | grep -E ":(22|9418|80|443)" | awk '{print " " $4 " (" $7 ")"}' || echo " 无相关端口" echo echo "🔍 连通性测试:" echo "SSH测试: ssh $USER@$CONTAINER_IP 'echo \"SSH连接正常\"' 2>/dev/null || echo \"SSH未配置\"" } # 主菜单 main_menu() { while true; do echo echo "选择本地Git访问配置方案:" echo "1. SSH + Git服务 (推荐,支持读写)" echo "2. Git Daemon (只读,简单)" echo "3. Docker Volume (宿主机共享)" echo "4. 查看网络信息" echo "5. 退出" echo read -p "请选择 (1-5): " choice case $choice in 1) setup_ssh_git ;; 2) setup_git_daemon ;; 3) setup_docker_volume ;; 4) show_network_info ;; 5) print_success "配置完成!" exit 0 ;; *) print_error "无效选择,请重新输入" ;; esac done } # 主程序 print_header echo "当前Git仓库状态:" echo "- 位置: /workspace/bevfusion" echo "- 分支: $(git branch --show-current 2>/dev/null || echo '未知')" echo "- 状态: $(git status --porcelain | wc -l) 个未提交文件" echo "- 提交: $(git rev-parse --short HEAD 2>/dev/null || echo '无')" echo main_menu