Skip to content

Instantly share code, notes, and snippets.

@inntran
Last active October 14, 2024 01:41
Show Gist options
  • Save inntran/f4c296a66463119e3d20e89af4b68959 to your computer and use it in GitHub Desktop.
Save inntran/f4c296a66463119e3d20e89af4b68959 to your computer and use it in GitHub Desktop.
Patch CentOS 7 and EPEL 7 repos to use archives
#!/bin/bash
ACTIVE_REPO_DIR="/etc/yum.repos.d"
BACKUP_REPO_DIR="$ACTIVE_REPO_DIR/backup"
STATUS_FILE="/etc/yum.repos.d/CENTOS7_EOL_REPO_URL_PATCHED.txt"
CENTOS_VAULT_SERVER_BASE="https://mirrors.aliyun.com/centos-vault"
EPEL_RPM_URL="https://mirrors.aliyun.com/epel-archive/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm"
EPEL_REPO="epel.repo"
EPEL_VAULT_SERVER_BASE="https://mirrors.aliyun.com/epel-archive"
# Check if the script has already been run
if [ -f "$STATUS_FILE" ]; then
echo "The script has already been run. Exiting."
cat $STATUS_FILE
exit 1
fi
# Create backup directory
mkdir -p "$BACKUP_REPO_DIR"
# Function to backup and modify repo files
process_repo() {
local repo="$1"
local baseurl="$2"
if [ ! -f "$BACKUP_REPO_DIR/$repo" ]; then
cp "$ACTIVE_REPO_DIR/$repo" "$BACKUP_REPO_DIR"
fi
if [ -f "$ACTIVE_REPO_DIR/$repo" ]; then
if [[ "$repo" == CentOS-* ]]; then
# For CentOS repos
sed -i -e "s|^mirrorlist=|#mirrorlist=|g" \
-e "s|^#baseurl=http://mirror.centos.org/|baseurl=$baseurl/|g" \
"$ACTIVE_REPO_DIR/$repo"
elif [[ "$repo" == "$EPEL_REPO" ]]; then
# For EPEL repos
sed -i -e "s|^metalink=|#metalink=|g" \
-e "s|^#baseurl=http://download.example/pub/epel/|baseurl=$baseurl/|g" \
"$ACTIVE_REPO_DIR/$repo"
fi
else
echo "File ($ACTIVE_REPO_DIR/$repo) does not exist, skipping replacement"
fi
}
# Process CentOS repos
for repo in CentOS-{Base,fasttrack,x86_64-kernel}.repo; do
process_repo "$repo" "$CENTOS_VAULT_SERVER_BASE"
done
# Import GPG keys and install EPEL
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
yum install -y epel-release
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
yum install -y "$EPEL_RPM_URL"
# Process EPEL repo
process_repo "$EPEL_REPO" "$EPEL_VAULT_SERVER_BASE"
# Create status file to indicate the script has run
echo "YUM Repositories of EOL'd CentOS 7 and EPEL 7 on this node, had been patched." > "$STATUS_FILE"
echo "Script completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment