152 lines
3.7 KiB
Bash
152 lines
3.7 KiB
Bash
#!/bin/bash
|
|
|
|
# 设置错误时立即退出
|
|
set -e
|
|
|
|
# 颜色定义
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 日志函数
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# 备份源文件
|
|
backup_sources() {
|
|
log_info "备份源文件..."
|
|
cp /etc/apt/sources.list /etc/apt/sources.list.backup
|
|
}
|
|
|
|
# 配置阿里云镜像源
|
|
configure_aliyun_mirror() {
|
|
log_info "配置阿里云镜像源..."
|
|
|
|
# 创建sources.list文件
|
|
cat > /etc/apt/sources.list << EOF
|
|
# 阿里云镜像源
|
|
deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
|
|
deb http://mirrors.aliyun.com/debian-security/ bullseye-security main
|
|
deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
|
|
deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
|
|
|
|
# 源码镜像
|
|
deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib
|
|
deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main
|
|
deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
|
|
deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
|
|
EOF
|
|
}
|
|
|
|
# 配置pip镜像源
|
|
configure_pip_mirror() {
|
|
log_info "配置pip镜像源..."
|
|
|
|
# 创建pip配置目录
|
|
mkdir -p ~/.pip
|
|
|
|
# 创建pip配置文件
|
|
cat > ~/.pip/pip.conf << EOF
|
|
[global]
|
|
index-url = https://mirrors.aliyun.com/pypi/simple/
|
|
trusted-host = mirrors.aliyun.com
|
|
EOF
|
|
}
|
|
|
|
# 配置npm镜像源
|
|
configure_npm_mirror() {
|
|
log_info "配置npm镜像源..."
|
|
|
|
# 配置npm镜像
|
|
npm config set registry https://registry.npmmirror.com
|
|
npm config set disturl https://npmmirror.com/dist
|
|
npm config set electron_mirror https://npmmirror.com/mirrors/electron/
|
|
npm config set puppeteer_download_host https://npmmirror.com/mirrors
|
|
npm config set chromedriver_cdnurl https://npmmirror.com/mirrors/chromedriver
|
|
npm config set operadriver_cdnurl https://npmmirror.com/mirrors/operadriver
|
|
npm config set phantomjs_cdnurl https://npmmirror.com/mirrors/phantomjs
|
|
npm config set selenium_cdnurl https://npmmirror.com/mirrors/selenium
|
|
npm config set node_inspector_cdnurl https://npmmirror.com/mirrors/node-inspector
|
|
}
|
|
|
|
# 配置go镜像源
|
|
configure_go_mirror() {
|
|
log_info "配置go镜像源..."
|
|
|
|
# 配置GOPROXY
|
|
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
|
|
|
|
# 配置GOSUMDB
|
|
go env -w GOSUMDB=sum.golang.google.cn
|
|
|
|
# 配置GO111MODULE
|
|
go env -w GO111MODULE=on
|
|
}
|
|
|
|
# 配置docker镜像源
|
|
configure_docker_mirror() {
|
|
log_info "配置docker镜像源..."
|
|
|
|
# 创建docker配置目录
|
|
mkdir -p /etc/docker
|
|
|
|
# 创建daemon.json文件
|
|
cat > /etc/docker/daemon.json << EOF
|
|
{
|
|
"registry-mirrors": [
|
|
"https://mirror.ccs.tencentyun.com",
|
|
"https://registry.docker-cn.com",
|
|
"https://docker.mirrors.ustc.edu.cn",
|
|
"https://hub-mirror.c.163.com",
|
|
"https://mirror.baidubce.com"
|
|
]
|
|
}
|
|
EOF
|
|
}
|
|
|
|
# 更新软件包列表
|
|
update_package_list() {
|
|
log_info "更新软件包列表..."
|
|
apt-get update
|
|
}
|
|
|
|
# 主函数
|
|
main() {
|
|
log_info "开始配置镜像源..."
|
|
|
|
# 备份源文件
|
|
backup_sources
|
|
|
|
# 配置阿里云镜像源
|
|
configure_aliyun_mirror
|
|
|
|
# 配置pip镜像源
|
|
configure_pip_mirror
|
|
|
|
# 配置npm镜像源
|
|
configure_npm_mirror
|
|
|
|
# 配置go镜像源
|
|
configure_go_mirror
|
|
|
|
# 配置docker镜像源
|
|
configure_docker_mirror
|
|
|
|
# 更新软件包列表
|
|
update_package_list
|
|
|
|
log_info "镜像源配置完成!"
|
|
}
|
|
|
|
# 执行主函数
|
|
main |