Files
macos-tools/scripts/apt-cacher-setup.sh
gaoandClaude 8f47f4e9ea feat: 完善 DevOps 工具链配置和优化 Harbor 服务
- 更新 Gitea runner 配置,支持优化的 CI/CD 环境
- 完善 Harbor HTTPS 配置和远程访问支持
- 优化 Harbor 日志配置,改用 json-file 驱动
- 添加 Docker 构建加速和代理缓存脚本
- 新增完整的文档和使用指南
- 添加 AI 开发工具集成配置

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
2025-11-03 06:39:53 +08:00

405 lines
10 KiB
Bash
Executable File

#!/bin/bash
# APT 缓存代理服务设置脚本
# 在本地或内网搭建 apt 缓存服务,解决包管理器网络超时问题
set -e
# 配置变量
APT_CACHER_PORT="${APT_CACHER_PORT:-3142}"
APT_CACHER_CONTAINER="${APT_CACHER_CONTAINER:-apt-cacher}"
HARBOR_REGISTRY="192.168.1.83:8443"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m'
info() { echo -e "${BLUE}INFO: $1${NC}"; }
success() { echo -e "${GREEN}SUCCESS: $1${NC}"; }
warning() { echo -e "${YELLOW}WARNING: $1${NC}"; }
error() { echo -e "${RED}ERROR: $1${NC}"; }
header() { echo -e "${PURPLE}🚀 $1${NC}"; }
# 检查 Docker 环境
check_docker() {
if ! command -v docker > /dev/null; then
error "Docker 未安装,请先安装 Docker"
exit 1
fi
if ! docker info > /dev/null 2>&1; then
error "无法连接到 Docker 服务"
exit 1
fi
}
# 启动 APT 缓存服务
start_apt_cacher() {
header "启动 APT 缓存服务..."
# 检查是否已存在容器
if docker ps -a | grep -q "$APT_CACHER_CONTAINER"; then
info "APT 缓存容器已存在,启动中..."
docker start "$APT_CACHER_CONTAINER"
else
info "创建并启动 APT 缓存容器..."
docker run -d \
--name "$APT_CACHER_CONTAINER" \
-p "$APT_CACHER_PORT:3142" \
-v apt_cache_data:/var/cache/apt-cacher-ng \
--restart unless-stopped \
sameersbn/apt-cacher-ng:latest
fi
# 等待服务启动
sleep 5
# 检查服务状态
if curl -s "http://localhost:$APT_CACHER_PORT" > /dev/null; then
success "APT 缓存服务启动成功"
info "访问地址: http://localhost:$APT_CACHER_PORT"
info "管理界面: http://localhost:$APT_CACHER_PORT/acng-report.html"
else
error "APT 缓存服务启动失败"
exit 1
fi
}
# 创建 Dockerfile 模板
create_apt_dockerfile_template() {
header "创建 APT 代理 Dockerfile 模板..."
cat > "./Dockerfile.apt-proxy" << 'EOF'
# Dockerfile 模板 - 使用 APT 缓存代理
# 解决 apt-get update 超时问题
ARG APT_PROXY=http://host.docker.internal:3142
# 配置 apt 代理
RUN echo "Acquire::http::Proxy \"${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy && \
echo "Acquire::https::Proxy \"${APT_PROXY}\";" >> /etc/apt/apt.conf.d/01proxy && \
echo "Acquire::Retries \"3\";" >> /etc/apt/apt.conf.d/01proxy && \
echo "Acquire::http::Timeout \"20\";" >> /etc/apt/apt.conf.d/01proxy && \
echo "Acquire::https::Timeout \"20\";" >> /etc/apt/apt.conf.d/01proxy
# 更新包管理器 (带重试机制)
RUN apt-get update || \
(sleep 10 && apt-get update) || \
(sleep 30 && apt-get update)
# 安装基础包
RUN apt-get install -y --no-install-recommends \
ca-certificates \
curl \
wget \
gnupg \
&& rm -rf /var/lib/apt/lists/*
# 后续构建步骤...
# FROM your-base-image
# RUN apt-get update && apt-get install -y your-packages
EOF
success "APT 代理 Dockerfile 模板创建完成"
info "模板文件: ./Dockerfile.apt-proxy"
}
# 创建构建脚本
create_apt_build_script() {
header "创建 APT 代理构建脚本..."
cat > "./build-with-apt-cache.sh" << EOF
#!/bin/bash
# 使用 APT 缓存的构建脚本
APT_PROXY_PORT="${APT_PROXY_PORT:-3142}"
IMAGE_NAME="\${1:-myapp}"
DOCKERFILE="\${2:-Dockerfile.apt-proxy}"
echo "🚀 使用 APT 缓存构建镜像: \$IMAGE_NAME"
echo "🔗 APT 缓存地址: http://host.docker.internal:\$APT_PROXY_PORT"
# 检查 APT 缓存服务
if ! curl -s "http://localhost:\$APT_PROXY_PORT" > /dev/null; then
echo "❌ APT 缓存服务未运行"
echo "启动方法: ./scripts/apt-cacher-setup.sh"
exit 1
fi
# 构建镜像
docker build \\
--build-arg APT_PROXY="http://host.docker.internal:\$APT_PROXY_PORT" \\
-t "\$IMAGE_NAME" \\
-f "\$DOCKERFILE" \\
.
echo "✅ 构建完成"
EOF
chmod +x "./build-with-apt-cache.sh"
success "APT 代理构建脚本创建完成"
info "脚本文件: ./build-with-apt-cache.sh"
}
# 创建 Kubernetes 配置
create_k8s_config() {
header "创建 Kubernetes APT 缓存配置..."
mkdir -p ./k8s
# APT 缓存服务部署
cat > "./k8s/apt-cacher-deployment.yaml" << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: apt-cacher
labels:
app: apt-cacher
spec:
replicas: 1
selector:
matchLabels:
app: apt-cacher
template:
metadata:
labels:
app: apt-cacher
spec:
containers:
- name: apt-cacher
image: sameersbn/apt-cacher-ng:latest
ports:
- containerPort: 3142
volumeMounts:
- name: cache-data
mountPath: /var/cache/apt-cacher-ng
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "500m"
volumes:
- name: cache-data
persistentVolumeClaim:
claimName: apt-cache-pvc
---
apiVersion: v1
kind: Service
metadata:
name: apt-cacher-service
spec:
selector:
app: apt-cacher
ports:
- port: 3142
targetPort: 3142
type: ClusterIP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: apt-cache-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
EOF
# 使用 APT 缓存的 Pod 模板
cat > "./k8s/build-pod-with-apt-cache.yaml" << EOF
apiVersion: v1
kind: Pod
metadata:
name: build-with-apt-cache
spec:
containers:
- name: kaniko
image: gcr.io/kaniko-project/executor:latest
args:
- "--dockerfile=Dockerfile"
- "--context=gs://my-bucket"
- "--destination=$HARBOR_REGISTRY/myapp:latest"
- "--cache=true"
- "--cache-repo=$HARBOR_REGISTRY/kaniko-cache"
env:
- name: HTTP_PROXY
value: "http://apt-cacher-service:3142"
- name: HTTPS_PROXY
value: "http://apt-cacher-service:3142"
restartPolicy: Never
EOF
success "Kubernetes 配置创建完成"
info "配置文件: ./k8s/"
}
# 显示使用指南
show_usage_guide() {
echo ""
header "📚 APT 缓存使用指南"
echo ""
echo "🚀 本地使用:"
echo " 1. 启动 APT 缓存服务:"
echo " ./scripts/apt-cacher-setup.sh"
echo ""
echo " 2. 使用缓存构建:"
echo " ./build-with-apt-cache.sh myapp"
echo ""
echo " 3. 查看缓存统计:"
echo " curl http://localhost:3142/acng-report.html"
echo ""
echo "🔧 在 Dockerfile 中使用:"
echo " ARG APT_PROXY=http://host.docker.internal:3142"
echo " RUN echo 'Acquire::http::Proxy \"\${APT_PROXY}\";' > /etc/apt/apt.conf.d/01proxy"
echo " RUN apt-get update && apt-get install -y your-packages"
echo ""
echo "☸️ Kubernetes 使用:"
echo " 1. 部署 APT 缓存服务:"
echo " kubectl apply -f k8s/apt-cacher-deployment.yaml"
echo ""
echo " 2. 在构建 Pod 中使用:"
echo " env:"
echo " - name: HTTP_PROXY"
echo " value: \"http://apt-cacher-service:3142\""
echo ""
echo "📊 性能优化:"
echo " • 首次下载: 从外网源下载 + 缓存"
echo " • 再次下载: 直接从本地缓存获取"
echo " • 多项目共享: 所有项目共享同一缓存"
echo " • 自动更新: 自动检查并缓存新版本包"
echo ""
success "APT 缓存服务配置完成!现在享受高速的包管理体验吧! 🎉"
}
# 停止 APT 缓存服务
stop_apt_cacher() {
header "停止 APT 缓存服务..."
if docker ps | grep -q "$APT_CACHER_CONTAINER"; then
docker stop "$APT_CACHER_CONTAINER"
success "APT 缓存服务已停止"
else
info "APT 缓存服务未运行"
fi
}
# 清理 APT 缓存
clean_apt_cache() {
header "清理 APT 缓存..."
# 删除容器
if docker ps -a | grep -q "$APT_CACHER_CONTAINER"; then
docker rm -f "$APT_CACHER_CONTAINER"
fi
# 删除数据卷
if docker volume ls | grep -q "apt_cache_data"; then
docker volume rm apt_cache_data
fi
success "APT 缓存清理完成"
}
# 显示缓存状态
show_cache_status() {
header "APT 缓存状态"
if docker ps | grep -q "$APT_CACHER_CONTAINER"; then
success "APT 缓存服务正在运行"
info "访问地址: http://localhost:$APT_CACHER_PORT"
# 获取缓存统计
if curl -s "http://localhost:$APT_CACHER_PORT/acng-report.html" > /dev/null; then
echo ""
info "缓存统计报告: http://localhost:$APT_CACHER_PORT/acng-report.html"
fi
else
warning "APT 缓存服务未运行"
fi
# 检查数据卷
if docker volume ls | grep -q "apt_cache_data"; then
local volume_size=$(docker system df --format "{{.Size}}" | grep "apt_cache_data" || echo "未知")
info "缓存数据卷大小: $volume_size"
fi
}
# 主函数
main() {
case "${1:-start}" in
start)
check_docker
start_apt_cacher
create_apt_dockerfile_template
create_apt_build_script
create_k8s_config
show_usage_guide
;;
stop)
stop_apt_cacher
;;
restart)
stop_apt_cacher
sleep 2
check_docker
start_apt_cacher
;;
clean)
stop_apt_cacher
clean_apt_cache
;;
status)
show_cache_status
;;
templates)
create_apt_dockerfile_template
create_apt_build_script
create_k8s_config
;;
help|--help|-h)
cat << EOF
APT 缓存代理服务设置脚本
用法: $0 [命令]
命令:
start - 启动 APT 缓存服务并创建模板 (默认)
stop - 停止 APT 缓存服务
restart - 重启 APT 缓存服务
clean - 清理 APT 缓存服务和数据
status - 显示缓存状态
templates - 仅创建配置模板
help - 显示此帮助信息
示例:
$0 start # 启动服务
$0 status # 查看状态
$0 stop # 停止服务
EOF
;;
*)
error "未知命令: $1"
$0 help
exit 1
;;
esac
}
# 执行主函数
main "$@"