Files
xgj/setup-env/scripts/setup-environment.sh

205 lines
5.2 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 输出函数
log_info() {
echo -e "${BLUE} $1${NC}"
}
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}"
}
# 检查是否为 root 用户
check_root() {
if [[ $EUID -ne 0 ]]; then
log_error "此脚本需要 root 权限运行"
exit 1
fi
}
# 配置阿里云镜像源
setup_aliyun_sources() {
log_info "配置阿里云镜像源..."
# 备份原始源列表
if [[ -f /etc/apt/sources.list ]]; then
cp /etc/apt/sources.list /etc/apt/sources.list.backup
fi
# 设置阿里云镜像源
cat > /etc/apt/sources.list << 'EOF'
deb https://mirrors.aliyun.com/debian/ bullseye main contrib non-free
deb https://mirrors.aliyun.com/debian-security bullseye-security main contrib non-free
deb https://mirrors.aliyun.com/debian/ bullseye-updates main contrib non-free
EOF
log_success "阿里云镜像源配置完成"
}
# 安装基础依赖
install_base_packages() {
log_info "更新包列表并安装基础依赖..."
apt-get update
apt-get install -y \
ca-certificates \
curl \
gnupg \
coreutils \
lsb-release \
software-properties-common \
apt-transport-https
log_success "基础依赖安装完成"
}
# 安装 Docker
install_docker() {
log_info "安装 Docker..."
# 创建密钥目录
mkdir -p /etc/apt/keyrings
# 下载 Docker GPG 密钥
if [[ "${USE_ALIYUN_MIRROR:-true}" == "true" ]]; then
log_info "使用阿里云 Docker 镜像源..."
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | \
gpg --batch --yes --dearmor -o /etc/apt/keyrings/docker.gpg
else
log_info "使用官方 Docker 源..."
curl -fsSL https://download.docker.com/linux/debian/gpg | \
gpg --batch --yes --dearmor -o /etc/apt/keyrings/docker.gpg
fi
chmod a+r /etc/apt/keyrings/docker.gpg
# 添加 Docker 源
if [[ "${USE_ALIYUN_MIRROR:-true}" == "true" ]]; then
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
else
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
# 更新源并安装 Docker
apt-get update
apt-get install -y \
docker-ce-cli \
docker-buildx-plugin \
docker-compose-plugin
log_success "Docker 安装完成"
}
# 安装 kubectl
install_kubectl() {
if [[ "${SKIP_KUBECTL:-false}" == "true" ]]; then
log_info "跳过 kubectl 安装"
return 0
fi
log_info "安装 kubectl..."
# 添加 Kubernetes GPG 密钥
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key | \
gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# 添加 Kubernetes apt 仓库
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.33/deb/ /' | \
tee /etc/apt/sources.list.d/kubernetes.list
chmod 644 /etc/apt/sources.list.d/kubernetes.list
# 更新包索引并安装 kubectl
apt-get update
apt-get install -y kubectl
log_success "kubectl 安装完成"
}
# 验证安装
verify_installation() {
log_info "验证安装..."
# 验证 Docker
if command -v docker &> /dev/null; then
DOCKER_VERSION=$(docker --version | cut -d' ' -f3 | cut -d',' -f1)
log_success "Docker 版本: $DOCKER_VERSION"
else
log_error "Docker 安装失败"
exit 1
fi
# 验证 kubectl (如果安装了)
if [[ "${SKIP_KUBECTL:-false}" != "true" ]]; then
if command -v kubectl &> /dev/null; then
KUBECTL_VERSION=$(kubectl version --client --short 2>/dev/null | cut -d' ' -f3 || echo "已安装")
log_success "kubectl 版本: $KUBECTL_VERSION"
else
log_error "kubectl 安装失败"
exit 1
fi
fi
}
# 清理临时文件
cleanup() {
log_info "清理临时文件..."
apt-get clean
rm -rf /var/lib/apt/lists/*
log_success "清理完成"
}
# 主函数
main() {
log_info "开始配置构建环境..."
# 检查 root 权限
check_root
# 配置镜像源
if [[ "${USE_ALIYUN_MIRROR:-true}" == "true" ]]; then
setup_aliyun_sources
fi
# 安装基础包
install_base_packages
# 安装 Docker
install_docker
# 安装 kubectl
install_kubectl
# 验证安装
verify_installation
# 清理
cleanup
log_success "环境配置完成!"
}
# 错误处理
trap 'log_error "脚本执行失败,退出码: $?"' ERR
# 执行主函数
main "$@"