From c9c440f8e61a52b654209661cf9001efb3cde5b2 Mon Sep 17 00:00:00 2001 From: Zhang Peng Date: Fri, 23 Feb 2018 13:30:59 +0800 Subject: [PATCH] =?UTF-8?q?:bookmark:=20=E6=9B=BF=E6=8D=A2=20yum=20repo=20?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/deploy/sys/init.sh | 5 +++- codes/deploy/sys/yum/CentOS-Base.repo | 35 ++++++++++++++++++++++ codes/deploy/sys/yum/change-yum-repo.sh | 40 +++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 codes/deploy/sys/yum/CentOS-Base.repo create mode 100644 codes/deploy/sys/yum/change-yum-repo.sh diff --git a/codes/deploy/sys/init.sh b/codes/deploy/sys/init.sh index 6fbbdb2..a4cf407 100644 --- a/codes/deploy/sys/init.sh +++ b/codes/deploy/sys/init.sh @@ -14,7 +14,10 @@ EOF filepath=$(cd "$(dirname "$0")"; pwd) # 设置环境配置,不了解具体修改内容的情况下,请勿执行 -# ./set-config.sh +# ${filepath}/set-config.sh + +# 替换 yum 镜像 +${filepath}/yum/change-yum-repo.sh # 安装命令行工具 ${filepath}/install-cmd-tool.sh diff --git a/codes/deploy/sys/yum/CentOS-Base.repo b/codes/deploy/sys/yum/CentOS-Base.repo new file mode 100644 index 0000000..1871197 --- /dev/null +++ b/codes/deploy/sys/yum/CentOS-Base.repo @@ -0,0 +1,35 @@ +[base] +name=CentOS-$releasever - Base +baseurl=http://vault.centos.org/$releasever/os/$basearch/ +gpgcheck=1 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever + +#released updates +[updates] +name=CentOS-$releasever - Updates +baseurl=http://vault.centos.org/$releasever/updates/$basearch/ +gpgcheck=1 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever + +#additional packages that may be useful +[extras] +name=CentOS-$releasever - Extras +baseurl=http://vault.centos.org/$releasever/extras/$basearch/ +gpgcheck=1 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever + +#additional packages that extend functionality of existing packages +[centosplus] +name=CentOS-$releasever - Plus +baseurl=http://vault.centos.org/$releasever/centosplus/$basearch/ +gpgcheck=1 +enabled=0 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever + +#contrib - packages by Centos Users +[contrib] +name=CentOS-$releasever - Contrib +baseurl=http://vault.centos.org/$releasever/contrib/$basearch/ +gpgcheck=1 +enabled=0 +gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever diff --git a/codes/deploy/sys/yum/change-yum-repo.sh b/codes/deploy/sys/yum/change-yum-repo.sh new file mode 100644 index 0000000..b2af1d6 --- /dev/null +++ b/codes/deploy/sys/yum/change-yum-repo.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +################################################################################### +# 本脚本用于替换 yum repo +# 要求:仅适用于 Linux Centos 发行版本,并且环境必须已支持 yum 、lsb_release 命令 +# Author: Zhang Peng +################################################################################### + +echo -e "\n>>>>>>>>> 替换 yum repo 源" + +# 备份 +cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak + +# 执行 lsb_release 命令,获取系统发行版本 +version=`lsb_release -r | awk '{print substr($2,1,1)}'` + +# 根据发型版本选择相应 yum 镜像 +if [ ${version} == 5 ]; then + # Cento5 已废弃,只能使用 http://vault.centos.org/ 替换,但由于是国外镜像,速度较慢 + wget --no-cookie --no-check-certificate -O /etc/yum.repos.d/CentOS-Base.repo https://raw.githubusercontent.com/dunwu/linux/master/codes/deploy/sys/yum/CentOS-Base.repo + + # 根据实际发型版本情况替换 + detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'` + sed -i 's/$releasever/'"${detailVersion}"'/g' /etc/yum.repos.d/CentOS-Base.repo + + # 不替换下面的开关,可能会出现错误:Could not open/read repomd.xml + sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo +else + # 国内 aliyun 镜像 + wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-${version}.repo + + # 国内 163 镜像(备选) + #wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS${version}-Base-163.repo +fi + +# 更新缓存 +yum clean all +yum makecache + +echo -e "\n>>>>>>>>> 替换 yum repo 源成功"