Skip to content

Commit

Permalink
Do not use RHSM in the nspawn container
Browse files Browse the repository at this point in the history
- it is not safe to use subscription-manager in a container
  https://bugzilla.redhat.com/show_bug.cgi?id=1702691

Co-authored-by: Petr Stodulka <[email protected]>
  • Loading branch information
bocekm and pirat89 committed Mar 16, 2020
1 parent f49d2df commit 30958f3
Show file tree
Hide file tree
Showing 19 changed files with 778 additions and 260 deletions.
6 changes: 3 additions & 3 deletions repos/system_upgrade/el7toel8/actors/checkrhsmsku/actor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from leapp.actors import Actor
from leapp.libraries.common import rhsm
from leapp.models import Report, SourceRHSMInfo
from leapp.models import Report, RHSMInfo
from leapp.reporting import create_report
from leapp import reporting
from leapp.tags import IPUWorkflowTag, ChecksPhaseTag
Expand All @@ -15,13 +15,13 @@ class CheckRedHatSubscriptionManagerSKU(Actor):
"""

name = 'check_rhsmsku'
consumes = (SourceRHSMInfo,)
consumes = (RHSMInfo,)
produces = (Report,)
tags = (IPUWorkflowTag, ChecksPhaseTag)

def process(self):
if not rhsm.skip_rhsm():
for info in self.consume(SourceRHSMInfo):
for info in self.consume(RHSMInfo):
if not info.attached_skus:
create_report([
reporting.Title('The system is not registered or subscribed.'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
from leapp.libraries.common import rhsm
from leapp.models import Report, SourceRHSMInfo
from leapp.models import Report, RHSMInfo


def test_sku_report_skipped(monkeypatch, current_actor_context):
with monkeypatch.context() as context:
context.setenv('LEAPP_DEVEL_SKIP_RHSM', '1')
current_actor_context.feed(SourceRHSMInfo(attached_skus=[]))
current_actor_context.feed(RHSMInfo(attached_skus=[]))
current_actor_context.run()
assert not list(current_actor_context.consume(Report))


def test_sku_report_has_skus(monkeypatch, current_actor_context):
with monkeypatch.context() as context:
context.setenv('LEAPP_DEVEL_SKIP_RHSM', '0')
current_actor_context.feed(SourceRHSMInfo(attached_skus=['testing-sku']))
current_actor_context.feed(RHSMInfo(attached_skus=['testing-sku']))
current_actor_context.run()
assert not list(current_actor_context.consume(Report))


def test_sku_report_has_no_skus(monkeypatch, current_actor_context):
with monkeypatch.context() as context:
context.setenv('LEAPP_DEVEL_SKIP_RHSM', '0')
current_actor_context.feed(SourceRHSMInfo(attached_skus=[]))
current_actor_context.feed(RHSMInfo(attached_skus=[]))
current_actor_context.run()
reports = list(current_actor_context.consume(Report))
assert reports and len(reports) == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from leapp.actors import Actor
from leapp.libraries.common import dnfplugin
from leapp.libraries.stdlib import run
from leapp.models import (FilteredRpmTransactionTasks, SourceRHSMInfo, StorageInfo, TargetUserSpaceInfo,
from leapp.models import (FilteredRpmTransactionTasks, RHSMInfo, StorageInfo, TargetUserSpaceInfo,
TransactionCompleted, UsedTargetRepositories)
from leapp.tags import IPUWorkflowTag, RPMUpgradePhaseTag

Expand All @@ -17,20 +17,12 @@ class DnfUpgradeTransaction(Actor):
"""

name = 'dnf_upgrade_transaction'
consumes = (FilteredRpmTransactionTasks, SourceRHSMInfo, StorageInfo, TargetUserSpaceInfo, UsedTargetRepositories)
consumes = (FilteredRpmTransactionTasks, RHSMInfo, StorageInfo, TargetUserSpaceInfo, UsedTargetRepositories)
produces = (TransactionCompleted,)
tags = (RPMUpgradePhaseTag, IPUWorkflowTag)

def process(self):
# FIXME: we hitting issue now because the network is down and rhsm
# # is trying to connect to the server. Commenting this out for now
# # so people will not be affected in case they do not have set a
# # release and we will have time to fix it properly.
# Make sure Subscription Manager OS Release is unset
# cmd = ['subscription-manager', 'release', '--unset']
# run(cmd)

src_rhsm_info = next(self.consume(SourceRHSMInfo), None)
src_rhsm_info = next(self.consume(RHSMInfo), None)
if src_rhsm_info:
for prod_cert in src_rhsm_info.existing_product_certificates:
run(['rm', '-f', prod_cert])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from leapp.actors import Actor
from leapp.libraries.actor import library
from leapp.models import UsedTargetRepositories, TargetRHSMInfo
from leapp.models import UsedTargetRepositories
from leapp.tags import FirstBootPhaseTag, IPUWorkflowTag


Expand All @@ -20,7 +20,7 @@ class EnableRHSMReposOnRHEL8(Actor):
"""

name = 'enable_rhsm_repos_on_rhel8'
consumes = (UsedTargetRepositories, TargetRHSMInfo)
consumes = (UsedTargetRepositories,)
produces = ()
tags = (IPUWorkflowTag, FirstBootPhaseTag)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from leapp.libraries.common import mounting, rhsm
from leapp.libraries.stdlib import CalledProcessError, api, run
from leapp.models import TargetRHSMInfo, UsedTargetRepositories
from leapp.models import UsedTargetRepositories


def set_rhsm_release():
"""Set the RHSM release to the target RHEL 8 minor version."""
info = next(api.consume(TargetRHSMInfo), None)
if info and info.release:
try:
rhsm.set_release(mounting.NotIsolatedActions(base_dir='/'), info.release)
except CalledProcessError as err:
api.current_logger().warning('Unable to set the {0} release through subscription-manager. When using dnf,'
' content of the latest RHEL 8 minor version will be downloaded.\n{1}'
.format(info.release, str(err)))
else:
if rhsm.skip_rhsm():
api.current_logger().debug('Skipping setting the RHSM release due to the use of LEAPP_DEVEL_SKIP_RHSM.')
return

target_version = api.current_actor().configuration.version.target
try:
rhsm.set_release(mounting.NotIsolatedActions(base_dir='/'), target_version)
except CalledProcessError as err:
api.current_logger().warning('Unable to set the {0} release through subscription-manager. When using dnf,'
' content of the latest RHEL 8 minor version will be downloaded.\n{1}'
.format(target_version, str(err)))


def enable_rhsm_repos():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys
from collections import namedtuple

import pytest

from leapp.exceptions import StopActorExecutionError
from leapp.libraries.actor import library
from leapp.libraries.common import mounting, rhsm
from leapp.libraries.stdlib import CalledProcessError, api
from leapp.models import (TargetRHSMInfo, UsedTargetRepositories,
UsedTargetRepository)
from leapp.models import UsedTargetRepositories, UsedTargetRepository, Version


def not_isolated_actions(raise_err=False):
Expand Down Expand Up @@ -61,27 +61,24 @@ def __call__(self):
return self


def test_setrelease(monkeypatch):
commands_called, klass = not_isolated_actions()
monkeypatch.setattr(mounting, 'NotIsolatedActions', klass)
monkeypatch.setattr(api, 'consume', lambda *x: (x for x in (TargetRHSMInfo(release='6.6'),)))
library.set_rhsm_release()
assert commands_called and len(commands_called) == 1
assert commands_called[0][0][-1] == '6.6'
class CurrentActorMocked(object):
configuration = namedtuple('configuration', ['version'])(
Version(source='7.6', target='8.0'))


def test_setrelease_no_message(monkeypatch):
def test_setrelease(monkeypatch):
commands_called, klass = not_isolated_actions()
monkeypatch.setattr(mounting, 'NotIsolatedActions', klass)
monkeypatch.setattr(api, 'consume', lambda *x: (x for x in ()))
monkeypatch.setattr(api, 'current_actor', CurrentActorMocked)
library.set_rhsm_release()
assert not commands_called
assert commands_called and len(commands_called) == 1
assert commands_called[0][0][-1] == '8.0'


def test_setrelease_submgr_throwing_error(monkeypatch):
_, klass = not_isolated_actions(raise_err=True)
monkeypatch.setattr(mounting, 'NotIsolatedActions', klass)
monkeypatch.setattr(api, 'consume', lambda *x: (x for x in (TargetRHSMInfo(release='6.6'),)))
monkeypatch.setattr(api, 'current_actor', CurrentActorMocked)
# free the set_release funtion from the @_rhsm_retry decorator which would otherwise cause 25 sec delay of the test
if sys.version_info.major < 3:
monkeypatch.setattr(rhsm, 'set_release', rhsm.set_release.func_closure[0].cell_contents)
Expand All @@ -92,12 +89,10 @@ def test_setrelease_submgr_throwing_error(monkeypatch):


def test_setrelease_skip_rhsm(monkeypatch):
commands_called, klass = not_isolated_actions()
commands_called, _ = not_isolated_actions()
monkeypatch.setenv('LEAPP_DEVEL_SKIP_RHSM', '1')
# To make this work we need to re-apply the decorator, so it respects the environment variable
monkeypatch.setattr(rhsm, 'set_release', rhsm.with_rhsm(rhsm.set_release))
monkeypatch.setattr(mounting, 'NotIsolatedActions', klass)
monkeypatch.setattr(api, 'consume', lambda *x: (x for x in (TargetRHSMInfo(release='6.6'),)))
library.set_rhsm_release()
assert not commands_called

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from leapp import reporting
from leapp.actors import Actor
from leapp.models import Report, TargetRHSMInfo
from leapp.reporting import create_report
from leapp.libraries.actor import library
from leapp.models import Report
from leapp.tags import IPUWorkflowTag, TargetTransactionChecksPhaseTag


Expand All @@ -11,22 +10,9 @@ class ReportSetTargetRelease(Actor):
"""

name = 'report_set_target_release'
consumes = (TargetRHSMInfo,)
consumes = ()
produces = (Report,)
tags = (IPUWorkflowTag, TargetTransactionChecksPhaseTag)

def process(self):
info = next(self.consume(TargetRHSMInfo), None)
if info and info.release:
create_report([
reporting.Title(
'The subscription-manager release is going to be set to {release}'.format(release=info.release)),
reporting.Summary(
'After the upgrade has completed the release of the subscription-manager will be set to {release}.'
' This will ensure that you will receive and keep the version you choose to upgrade to.'.
format(release=info.release)
),
reporting.Severity(reporting.Severity.LOW),
reporting.Tags([reporting.Tags.UPGRADE_PROCESS]),
reporting.RelatedResource('package', 'subscription-manager')
])
library.process()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from leapp import reporting
from leapp.libraries.stdlib import api


def process():
# TODO: skip if users are not using rhsm at all (RHELLEAPP-201)
target_version = api.current_actor().configuration.version.target
reporting.create_report([
reporting.Title(
'The subscription-manager release is going to be set after the upgrade'),
reporting.Summary(
'After the upgrade has completed the release of the subscription-manager will be set to {release}.'
' This will ensure that you will receive and keep the version you choose to upgrade to.'
.format(release=target_version)
),
reporting.Severity(reporting.Severity.LOW),
reporting.Remediation(
hint='If you wish to receive updates for the latest released version of RHEL 8, run `subscription-manager'
' release --unset` after the upgrade.'),
reporting.Tags([reporting.Tags.UPGRADE_PROCESS]),
reporting.RelatedResource('package', 'subscription-manager')
])
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from leapp.models import Report, TargetRHSMInfo
from collections import namedtuple

import pytest

def test_report_target_version(current_actor_context):
current_actor_context.feed(TargetRHSMInfo(release='6.6.6'))
current_actor_context.run()
reports = list(current_actor_context.consume(Report))
assert reports and len(reports) == 1
report_fields = reports[0].report
assert '6.6.6' in report_fields.get('summary', '')
assert '6.6.6' in report_fields.get('title', '')
from leapp import reporting
from leapp.libraries.actor import library
from leapp.libraries.common.testutils import create_report_mocked
from leapp.libraries.stdlib import api


def test_report_target_version_notset(current_actor_context):
current_actor_context.feed(TargetRHSMInfo(release=''))
current_actor_context.run()
reports = list(current_actor_context.consume(Report))
assert not reports
class CurrentActorMocked(object):
def __init__(self, src_ver='7.6', dst_ver='8.1'):

version = namedtuple('Version', ['source', 'target'])(src_ver, dst_ver)
self.configuration = namedtuple('configuration', ['version'])(version)

def test_report_target_version_nomessage(current_actor_context):
current_actor_context.run()
reports = list(current_actor_context.consume(Report))
assert not reports
def __call__(self):
return self


@pytest.mark.parametrize('version', ['8.{}'.format(i) for i in range(4)])
def test_report_target_version(monkeypatch, version):
monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(dst_ver=version))
monkeypatch.setattr(reporting, 'create_report', create_report_mocked())
SUMMARY_FMT = 'will be set to {}.'
library.process()
assert reporting.create_report.called == 1
assert SUMMARY_FMT.format(version) in reporting.create_report.report_fields['summary']
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from leapp.actors import Actor
from leapp.tags import IPUWorkflowTag, FactsPhaseTag
from leapp.libraries.actor import scanrhsm
from leapp.models import SourceRHSMInfo
from leapp.models import RHSMInfo


class ScanSubscriptionManagerInfo(Actor):
Expand All @@ -14,7 +14,7 @@ class ScanSubscriptionManagerInfo(Actor):

name = 'scan_subscription_manager_info'
consumes = ()
produces = (SourceRHSMInfo,)
produces = (RHSMInfo,)
tags = (IPUWorkflowTag, FactsPhaseTag)

def process(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from leapp.models import SourceRHSMInfo
from leapp.libraries.common import rhsm
from leapp.libraries.common.mounting import NotIsolatedActions
from leapp.libraries.stdlib import api


@rhsm.with_rhsm
def scan():
info = SourceRHSMInfo()
context = NotIsolatedActions(base_dir='/')
rhsm.scan_rhsm_info(context, info)
info = rhsm.scan_rhsm_info(context)
api.produce(info)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from leapp.actors import Actor
from leapp.libraries.actor import userspacegen
from leapp.libraries.common.config import get_env, version
from leapp.models import (RepositoriesMap, RequiredTargetUserspacePackages, SourceRHSMInfo,
StorageInfo, TargetRepositories, TargetRHSMInfo, TargetUserSpaceInfo, UsedTargetRepositories,
from leapp.models import (RepositoriesMap, RequiredTargetUserspacePackages,
RHSMInfo, StorageInfo, TargetRepositories,
TargetUserSpaceInfo, UsedTargetRepositories,
XFSPresence)
from leapp.tags import TargetTransactionFactsPhaseTag, IPUWorkflowTag
from leapp.tags import IPUWorkflowTag, TargetTransactionFactsPhaseTag


class TargetUserspaceCreator(Actor):
Expand All @@ -20,8 +21,8 @@ class TargetUserspaceCreator(Actor):

name = 'target_userspace_creator'
consumes = (RepositoriesMap, RequiredTargetUserspacePackages,
StorageInfo, SourceRHSMInfo, TargetRepositories, XFSPresence)
produces = (TargetRHSMInfo, TargetUserSpaceInfo, UsedTargetRepositories)
StorageInfo, RHSMInfo, TargetRepositories, XFSPresence)
produces = (TargetUserSpaceInfo, UsedTargetRepositories)
tags = (IPUWorkflowTag, TargetTransactionFactsPhaseTag)

def process(self):
Expand Down
Loading

0 comments on commit 30958f3

Please sign in to comment.