Скрипт для установки репозиториев с Nexus

от автора

в ,

Скрипт написан на баше. Перед использованием — необходимо ознакомиться с двумя «кейсами» (case), чтобы понимать, как тут все устроено.

Перед запуском, обязательно дайте скрипту права на запуск

[root@test-vm ~]# chmod +x repo.sh

Непосредственно запускаем скрипт

[root@test-vm ~]# ./repo.sh

Разберем начинку скрипта.

После запуска, у вас вылезет список с репозиториями, которые вы прописали в скрипте. Вам необходимо вписать число для продолжения.

#!/bin/bash
echo -n "1. CentOS 6
2. CentOS 7
3. Oracle Linux 5.11
4. Oracle Linux 7
5. Rocky Linux 9
6. RedHat 7
7. Ubuntu 22.04
8. CentOS 9 Stream
Select your Operation System: "
read OS

Далее, вас спросят, что необходимо сделать. Первое — непосредственно установить репозитории. Второе — вернуть все как было, в случае фейла. Ответ так же вписываете цифрами (1 или 2)

case $OS in
	1 | CentOS | Centos | centos | "CentOS 6" | CENTOS | "CENTOS 6")

		echo "CentOS 6
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

При выборе первого варианта, скрипт на всякий случай пойдет зачищать то, что могло быть создано им ранее, если человек повторно решил произвести установку, или обновить репозитории

case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Centos_6.repo;

Создаем бекап, где будут лежать копии всех файлов с репозиториями

mkdir /etc/yum.repos.d/backup/;
cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

У оставшихся файлов, через команду sed меняем состояние enabled с 1 на 0, чтобы при работе с yum/apt они не были задействованы

REPOS=/etc/yum.repos.d/*.repo
for r in $REPOS
do
	echo "Editing $r";
	sed -i '/#/d' "$r";
	sed -i '/enabled=/d' "$r";
        sed -i '/\[*\]/a enabled=0' "$r";
done

Скачиваем наш файл с прописанными репами (источником может что угодно) и двигаем его в директорию ко всем репозиториям. Готово, репозитории установлены. Примеры .repo файлов приведу в конце.

echo "Installing Nexus repository for CentOS 6"
curl -X GET https://nexus.example.com/repository/reponame/Centos_6.repo -O -k;
mv Centos_6.repo /etc/yum.repos.d/;
echo "All repositories were disabled";
echo "New Nexus repository was installed";
;;

При выборе второго варианта, мы вернем нашу систему к прежнему состоянию, как до установки. Чистим все активные репозитории. Двигаем репозитории из бекапа в папку с активными репозиториями. И в конце зачищаем папку с бекапами.

2 | Revert )

		rm -f /etc/yum.repos.d/*.repo;
		mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
		rmdir /etc/yum.repos.d/backup;
		;;

Собственно все

#!/bin/bash
echo -n "1. CentOS 6
2. CentOS 7
3. Oracle Linux 5.11
4. Oracle Linux 7
5. Rocky Linux 9
6. RedHat 7
7. Ubuntu 22.04
8. CentOS 9 Stream
Select your Operation System: "
read OS

case $OS in
	1 | CentOS | Centos | centos | "CentOS 6" | CENTOS | "CENTOS 6")

		echo "CentOS 6
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Centos_6.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for CentOS 6"
				curl -X GET https://nexus.example.com/repository/reponame/Centos_6.repo -O -k;
				mv Centos_6.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;

	2 | CentOS | Centos | centos | "CentOS 7" | CENTOS | "CENTOS 7")

		echo "CentOS 7
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Centos_7.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for CentOS 7"
				curl -X GET https://nexus.example.com/repository/reponame/Centos_7.repo -O -k;
				mv Centos_7.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;

	3 | "Oracle Linux 5.11")

		echo "Oracle Linux 5.11
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Oracle_5.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for Oracle Linux 5.11"
				curl -X GET https://nexus.example.com/repository/reponame/Oracle_5.repo -O -k;
				mv Oracle_5.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;

	4 | "Oracle Linux 7")

		echo "Oracle Linux 7
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Oracle_7.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for Oracle Linux 7"
				curl -X GET https://nexus.example.com/repository/reponame/repos/Oracle_7.repo -O -k;
				mv Oracle_7.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;

	5 | "Rocky Linux 9")

		echo "Rocky Linux 9
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Rocky_9.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for Rocky Linux 9"
				curl -X GET https://nexus.example.com/repository/reponame/Rocky_9.repo -O -k;
				mv Rocky_9.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;

	6 | "RedHat 7")

		echo "RedHat 7
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION
		
		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Redhat_7.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for Redhat_7"
				curl -X GET https://nexus.example.com/repository/reponame/Redhat_7.repo -O -k;
				mv Redhat_7.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;

	7 | "Ubuntu 22.04")

		echo "Ubuntu 22.04
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				mkdir /etc/apt/backup/;
				cp /etc/apt/sources.list /etc/apt/backup/;
				rm -f /etc/apt/sources.list;
				rm -f /etc/apt/apt.conf.d/98nexus-ssl-disable.conf

				echo "Installing config"
				touch /etc/apt/apt.conf.d/98nexus-ssl-disable.conf
				echo "Acquire::https::nexus.example.com::Verify-Peer false;" > /etc/apt/apt.conf.d/98nexus-ssl-disable.conf;

				echo "Installing Nexus repository for Ubuntu 22.04";
				curl -X GET https://nexus.example.com/repository/reponame/Ubuntu_22.04.repo -O -k;
				mv Ubuntu_22.04.repo /etc/apt/sources.list;
				chmod 777 /etc/apt/sources.list;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;
	
			2 | Revert )

				rm -f /etc/apt/apt.conf.d/98nexus-ssl-disable.conf
				rm -f /etc/apt/sources.list;
				mv -f /etc/apt/backup/* /etc/apt/;
				rmdir /etc/apt/backup;
				;;
			esac
		;;

	8 | "CentOS 9" | "CENTOS 9")

		echo "CentOS 9
		";
		echo -n "1. Install new repository
2. Revert previous settings back
Select an action: ";
		read ACTION

		case $ACTION in

			1 | Install )

				rm -f /etc/yum.repos.d/Centos_9.repo;
				mkdir /etc/yum.repos.d/backup/;
				cp /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/;

				REPOS=/etc/yum.repos.d/*.repo
				for r in $REPOS
				do
					echo "Editing $r";
					sed -i '/#/d' "$r";
					sed -i '/enabled=/d' "$r";
					sed -i '/\[*\]/a enabled=0' "$r";
				done

				echo "Installing Nexus repository for CentOS 9"
				curl -X GET https://nexus.example.com/repository/reponame/Centos_9.repo -O -k;
				mv Centos_9.repo /etc/yum.repos.d/;
				echo "All repositories were disabled";
				echo "New Nexus repository was installed";
				;;

			2 | Revert )

				rm -f /etc/yum.repos.d/*.repo;
				mv /etc/yum.repos.d/backup/* /etc/yum.repos.d/;
				rmdir /etc/yum.repos.d/backup;
				;;
			esac
		;;
esac

Примеры готовых .repo файлов

CentOS 9 :

[centos-base]
name=CentOS 9 Base
baseurl=https://nexus.example.com/repository/Centos_9_Base/
enabled=1
gpgcheck=0
sslverify=0

[centos-extras]
name=CentOS 9 AppStream
baseurl=https://nexus.example.com/repository/Centos_9_AppStream/
enabled=1
gpgcheck=0
sslverify=0

Ubuntu 22.04 :

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy main restricted
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-updates main restricted
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy universe
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy universe
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-updates universe
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy multiverse
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy multiverse
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-updates multiverse
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-backports main restricted universe multiverse
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-backports main restricted universe multiverse

deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-security main restricted
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-security main restricted
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-security universe
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-security universe
deb https://nexus.example.com/repository/Ubuntu_22.04 jammy-security multiverse
# deb-src https://nexus.example.com/repository/Ubuntu_22.04 jammy-security multiverse

Oracle Linux 7 :

[oracle-base]
name=Oracle 7 Base x86_64
baseurl=https://nexus.example.com/repository/Oracle_7_Base/
enabled=1
gpgcheck=0
sslverify=0

[oracle-epel]
name=Oracle 7 EPEL x86_64
baseurl=https://nexus.example.com/repository/Epel-7_x86-64/
enabled=1
gpgcheck=0
sslverify=0

[oracle-uekr6]
name=Oracle 7 UEKR6 x86_64
baseurl=https://nexus.example.com/repository/Oracle_7_UEKR6/
enabled=1
gpgcheck=0
sslverify=0


Комментарии

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *