Skip to content

Commit

Permalink
Update 02 according to comments from Milos
Browse files Browse the repository at this point in the history
* Used 'ShellScript' instead of 'Command'
* Added 'disable' for RHEL 8
* Added 'disable' for RHEL 9
* Tested the patch for CentOS Stream 9
* Tested the patch for RHEL 8

Signed-off-by: Vector Li <[email protected]>
  • Loading branch information
idorax committed Aug 14, 2023
1 parent 9c2b63c commit a3516c6
Showing 1 changed file with 76 additions and 25 deletions.
101 changes: 76 additions & 25 deletions tmt/steps/prepare/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import tmt.steps.prepare
import tmt.utils
from tmt.steps.provision import Guest
from tmt.utils import Command, field
from tmt.utils import ShellScript, field


class Distro(enum.Enum):
Expand All @@ -19,7 +19,7 @@ class Distro(enum.Enum):
RHEL_8 = enum.auto()
RHEL_9 = enum.auto()
RHEL_10 = enum.auto()
CENTOS_LINUX_7 = enum.auto()
CENTOS_7 = enum.auto()
CENTOS_STREAM_8 = enum.auto()
CENTOS_STREAM_9 = enum.auto()
CENTOS_STREAM_10 = enum.auto()
Expand Down Expand Up @@ -59,7 +59,7 @@ def get_guest_distro(self, guest: Guest, logger: tmt.log.Logger) -> Optional[Dis

if os_release.get('NAME') == 'CentOS Linux' and os_release.get(
'VERSION_ID', '').startswith('7'):
return Distro.CENTOS_LINUX_7
return Distro.CENTOS_7

if os_release.get('NAME') == 'CentOS Stream':
if os_release.get('VERSION_ID', '').startswith('8'):
Expand Down Expand Up @@ -88,6 +88,23 @@ class FIPS(ToggleableFeature):
# TBD


FEDORA_REPO = 'powertools'
FEDORA_PACKAGES = ['epel-release', 'epel-next-release']

CENTOS_7_PACKAGES = ['epel-release']

CENTOS_STREAM_9_REPO = 'crb'

RHEL_7_REPO = 'rhel-*-optional-rpms rhel-*-extras-rpms rhel-ha-for-rhel-*-server-rpms'
RHEL_7_PACKAGES = ['https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm']

RHEL_8_REPO = 'codeready-builder-for-rhel-8-$(arch)-rpms'
RHEL_8_PACKAGES = ['https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm']

RHEL_9_REPO = 'codeready-builder-for-rhel-9-$(arch)-rpms'
RHEL_9_PACKAGES = ['https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm']


class EPEL(ToggleableFeature):
KEY = 'epel'

Expand All @@ -96,46 +113,80 @@ def enable(self, guest: Guest, logger: tmt.log.Logger) -> None:
if guest_distro is None:
raise tmt.utils.PrepareError('The distro of the guest is not supported.')

sudo = 'sudo' if self.guest.facts.is_superuser is False else ''

if guest_distro == Distro.FEDORA:
self.info('Enable EPEL on Fedora, do nothing ...')
elif guest_distro == Distro.CENTOS_7:
# yum install epel-release
self.info('Enable EPEL on CentOS 7')
shscript1 = ShellScript(f'{sudo} yum -y install {" ".join(CENTOS_7_PACKAGES)}')
self.guest.execute(shscript1, silent=True)
elif guest_distro == Distro.CENTOS_STREAM_8:
# dnf config-manager --set-enabled powertools
# dnf install epel-release epel-next-release
# dnf -y install epel-release epel-next-release
self.info('Enable EPEL on CentOS Stream 8')
command = Command()
if self.guest.facts.is_superuser is False:
command += Command('sudo')
command1 = command + Command('dnf', 'config-manager', '--set-enabled', 'powertools')
command2 = command + Command('dnf', '-y', 'install',
'epel-release', 'epel-next-release')
self.guest.execute(command1, silent=True)
self.guest.execute(command2, silent=True)
shscript1 = ShellScript(f'{sudo} dnf config-manager --set-enabled {FEDORA_REPO}')
shscript2 = ShellScript(f'{sudo} dnf -y install {" ".join(FEDORA_PACKAGES)}')
self.guest.execute(shscript1 & shscript2, silent=True)
elif guest_distro == Distro.CENTOS_STREAM_9:
# dnf config-manager --set-enabled crb
# dnf -y install epel-release epel-next-release
self.info('Enable EPEL on CentOS Stream 9')
shscript1 = ShellScript(
f'{sudo} dnf config-manager --set-enabled {CENTOS_STREAM_9_REPO}')
shscript2 = ShellScript(f'{sudo} dnf -y install {" ".join(FEDORA_PACKAGES)}')
self.guest.execute(shscript1 & shscript2, silent=True)
elif guest_distro == Distro.RHEL_7:
# subscription-manager repos --enable rhel-*-optional-rpms \
# --enable rhel-*-extras-rpms \
# --enable rhel-ha-for-rhel-*-server-rpms
# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
self.info('Enable EPEL on RHEL 7')
shscript1 = ShellScript(f'{sudo} subscription-manager repos --enable {RHEL_7_REPO}')
shscript2 = ShellScript(f'{sudo} yum -y install {" ".join(RHEL_7_PACKAGES)}')
self.guest.execute(shscript1 + shscript2, silent=True)
elif guest_distro == Distro.RHEL_8:
# subscription-manager repos --enable codeready-builder-for-rhel-8-$(arch)-rpms
# dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
# dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
self.info('Enable EPEL on RHEL 8')
command = Command()
if self.guest.facts.is_superuser is False:
command += Command('sudo')
command1 = command + \
Command('subscription-manager', 'repos', '--enable',
'codeready-builder-for-rhel-8-$(arch)-rpms')
command2 = command + \
Command('dnf', '-y', 'install',
'https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm')
self.guest.execute(command1, silent=True)
self.guest.execute(command2, silent=True)
shscript1 = ShellScript(f'{sudo} subscription-manager repos --enable {RHEL_8_REPO}')
shscript2 = ShellScript(f'{sudo} dnf -y install {" ".join(RHEL_8_PACKAGES)}')
self.guest.execute(shscript1 + shscript2, silent=True)
elif guest_distro == Distro.RHEL_9:
# subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
# dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
self.info('Enable EPEL on RHEL 9')
shscript1 = ShellScript(f'{sudo} subscription-manager repos --enable {RHEL_9_REPO}')
shscript2 = ShellScript(f'{sudo} dnf -y install {" ".join(RHEL_9_PACKAGES)}')
self.guest.execute(shscript1 + shscript2, silent=True)
else:
raise tmt.utils.PrepareError('The distro of the guest is not supported.')
pass

def disable(self, guest: Guest, logger: tmt.log.Logger) -> None:
guest_distro = self.get_guest_distro(guest=guest, logger=logger)
if guest_distro is None:
raise tmt.utils.PrepareError('The distro of the guest is not supported.')

sudo = 'sudo' if self.guest.facts.is_superuser is False else ''

if guest_distro == Distro.FEDORA:
# XXX: What to do?
self.info('Disable epel on Fedora, do nothing ...')
elif guest_distro == Distro.RHEL_8:
# subscription-manager repos --disable codeready-builder-for-rhel-8-$(arch)-rpms
# dnf -y remove epel-release
self.info('Disable EPEL on RHEL 8')
shscript1 = ShellScript(f'{sudo} subscription-manager repos --disable {RHEL_8_REPO}')
shscript2 = ShellScript(f'{sudo} dnf -y remove epel-release')
self.guest.execute(shscript1 + shscript2, silent=True)
elif guest_distro == Distro.RHEL_9:
# subscription-manager repos --disable codeready-builder-for-rhel-9-$(arch)-rpms
# dnf -y remove epel-release
self.info('Enable EPEL on RHEL 9')
shscript1 = ShellScript(f'{sudo} subscription-manager repos --disable {RHEL_9_REPO}')
shscript2 = ShellScript(f'{sudo} dnf -y remove epel-release')
self.guest.execute(shscript1 + shscript2, silent=True)
else:
pass

Expand Down

0 comments on commit a3516c6

Please sign in to comment.