Skip to content

fix(client): Update log messages when unregistered #4421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions insights/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get_egg_url(self):
else:
raise ConnectionError("%s: %s" % (response.status_code, response.reason))
except ConnectionError as e:
logger.warning("Unable to fetch egg url %s: %s. Defaulting to /release", url, str(e))
logger.debug("Unable to fetch egg url %s: %s. Defaulting to /release", url, str(e))
return '/release'

def fetch(self, force=False):
Expand Down Expand Up @@ -556,7 +556,9 @@ def show_results(self):
print(json.dumps(insights_data, indent=1))
except IOError as e:
if e.errno == errno.ENOENT:
raise Exception("Error: no report found. Run insights-client --check-results to update the report cache: %s" % e)
raise Exception("Error: no report found. "
"Check the results to update the report cache: %s"
"\n# insights-client --check-results" % e)
else:
raise e

Expand Down
37 changes: 29 additions & 8 deletions insights/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import logging
import platform
import sys
import warnings
import errno
# import io
Expand All @@ -30,7 +31,8 @@
write_registered_file,
os_release_info,
largest_spec_in_archive,
size_in_mb)
size_in_mb,
_get_rhsm_identity)
from .cert_auth import rhsmCertificate
from .constants import InsightsConstants as constants
from insights import cleaner, package_info
Expand Down Expand Up @@ -64,7 +66,9 @@


def _host_not_found():
raise Exception("Error: failed to find host with matching machine-id. Run insights-client --status to check registration status")
raise Exception("Error: failed to find host with matching machine-id. "
"Check the registration status:\n"
"# insights-client --status")


def _api_request_failed(exception, message='The Insights API could not be reached.'):
Expand Down Expand Up @@ -445,10 +449,20 @@ def handle_fail_rcs(self, req):
req.status_code)
logger.debug("HTTP Status Text: %s", req.reason)
if req.status_code == 401:
logger.error("Please ensure that the system is registered "
"with RHSM for CERT auth, or that correct "
"credentials are set in %s for BASIC auth.", self.config.conf)
logger.log(NETWORK, "HTTP Response Text: %s", req.text)
# check if the host is registered with subscription-manager
if not _get_rhsm_identity():
logger.error(
"This host is unregistered, please ensure that "
"the system is registered with subscription-manager "
"and then with insights-client.\n"
"\n1. Register with subscription-manager"
"\n# subscription-manager register\n"
"\n2. Register with insights-client"
"\n# insights-client --register"
)
sys.exit(constants.sig_kill_bad)
else:
logger.log(NETWORK, "HTTP Response Text: %s", req.text)
if req.status_code == 402:
# failed registration because of entitlement limit hit
logger.debug('Registration failed by 402 error.')
Expand Down Expand Up @@ -1092,7 +1106,12 @@ def get_advisor_report(self):
if host_details["total"] < 1:
_host_not_found()
if host_details["total"] > 1:
raise Exception("Error: multiple hosts detected (insights_id = %s). To fix this error, run command: insights-client --unregister && insights-client --register" % generate_machine_id())
raise Exception("Error: multiple hosts detected (insights_id = %s). "
"To fix this error, unregister this host first and then register again.\n"
"\n1. Unregister with insights-client"
"\n# insights-client --unregister\n"
"\n2. Register with insights-client"
"\n# insights-client --register" % generate_machine_id())

if not os.path.exists("/var/lib/insights"):
os.makedirs("/var/lib/insights", mode=0o755)
Expand Down Expand Up @@ -1120,7 +1139,9 @@ def checkin(self):
logger.info("Checking in...")

if not self._fetch_system_by_machine_id():
logger.error("This host is not registered. To register, run 'insights-client --register'.")
logger.error("This host is not registered. "
"Use --register to register this host:\n"
"# insights-client --register")
return False

try:
Expand Down
51 changes: 32 additions & 19 deletions insights/client/phase/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from insights.client.config import InsightsConfig
from insights.client.constants import InsightsConstants as constants
from insights.client.support import InsightsSupport
from insights.client.utilities import validate_remove_file, print_egg_versions
from insights.client.utilities import validate_remove_file, print_egg_versions, _get_rhsm_identity
from insights.client.schedule import get_scheduler

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -56,10 +56,6 @@ def get_phases():
@phase
def pre_update(client, config):

# Check if BASIC auth is used to print a WARNING message
if config.authmethod == 'BASIC':
logger.warning('WARN: BASIC authentication method is being deprecated. Please consider using CERT authentication method.')

if config.version:
logger.info(constants.version)
sys.exit(constants.sig_kill_ok)
Expand Down Expand Up @@ -165,6 +161,30 @@ def post_update(client, config):

# -------delete everything below this line-------
if config.legacy_upload:
if config.offline or config.no_upload or config.module:
# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)
if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
elif config.no_upload:
logger.debug("Running client without uploading. Bypassing registration.")
else:
logger.debug('Running a specified module. Bypassing registration.')
return

if not _get_rhsm_identity():
logger.error(
"This host is unregistered, please ensure that "
"the system is registered with subscription-manager "
"and then with insights-client.\n"
"\n1. Register with subscription-manager"
"\n# subscription-manager register\n"
"\n2. Register with insights-client"
"\n# insights-client --register"
)
sys.exit(constants.sig_kill_bad)

if config.status:
reg_check = client.get_registration_status()
for msg in reg_check['messages']:
Expand All @@ -182,16 +202,6 @@ def post_update(client, config):
else:
sys.exit(constants.sig_kill_bad)

if config.offline or config.no_upload:
# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)
if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
else:
logger.debug("Running client without uploading. Bypassing registration.")
return

if config.display_name and not config.register:
# setting display name independent of registration
if client.set_display_name(config.display_name):
Expand All @@ -217,16 +227,18 @@ def post_update(client, config):
return
# -------delete everything above this line-------

if config.offline or config.no_upload or config.payload:
if config.offline or config.no_upload or config.payload or config.module:
# create a machine id first thing. we'll need it for all uploads
logger.debug('Machine ID: %s', client.get_machine_id())
logger.debug("CONFIG: %s", config)
if config.offline:
logger.debug('Running client in offline mode. Bypassing registration.')
elif config.no_upload:
logger.debug("Running client without uploading. Bypassing registration.")
else:
elif config.payload:
logger.debug('Uploading a specified archive. Bypassing registration.')
else:
logger.debug('Running a specified module. Bypassing registration.')
return

# check registration status before anything else
Expand All @@ -251,8 +263,9 @@ def post_update(client, config):

# halt here if unregistered
if not reg_check and not config.register:
logger.info('This host has not been registered. '
'Use --register to register this host.')
logger.error("This host is unregistered. "
"Use --register to register this host.\n"
"# insights-client --register")
sys.exit(constants.sig_kill_bad)

# --register was called
Expand Down
13 changes: 8 additions & 5 deletions insights/tests/client/phase/test_LEGACY_post_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def patch_insights_config(old_function):
"return_value.load_all.return_value.list_specs": False,
"return_value.load_all.return_value.show_results": False,
"return_value.load_all.return_value.check_results": False,
"return_value.load_all.return_value.no_upload": False})
"return_value.load_all.return_value.no_upload": False,
"return_value.load_all.return_value.module": False})
return patcher(old_function)


Expand All @@ -37,9 +38,10 @@ def test_post_update_legacy_upload_off(insights_config, insights_client):
insights_client.return_value.get_machine_id.assert_called_once()


@patch("insights.client.phase.v1._get_rhsm_identity", return_value=True)
@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
def test_post_update_legacy_upload_on(insights_config, insights_client):
def test_post_update_legacy_upload_on(insights_config, insights_client, _get_rhsm_identity):
"""
Registration is processed in legacy_upload=True
"""
Expand All @@ -52,10 +54,10 @@ def test_post_update_legacy_upload_on(insights_config, insights_client):
insights_client.return_value.register.assert_called_once()


@patch("insights.client.phase.v1._get_rhsm_identity", return_value=True)
@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
# @patch("insights.client.phase.v1.InsightsClient")
def test_exit_ok(insights_config, insights_client):
def test_exit_ok(insights_config, insights_client, _get_rhsm_identity):
"""
Support collection replaces the normal client run.
"""
Expand All @@ -64,9 +66,10 @@ def test_exit_ok(insights_config, insights_client):
assert exc_info.value.code == 0


@patch("insights.client.phase.v1._get_rhsm_identity", return_value=True)
@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
def test_post_update_no_upload(insights_config, insights_client):
def test_post_update_no_upload(insights_config, insights_client, _get_rhsm_identity):
"""
No-upload short circuits this phase
"""
Expand Down
3 changes: 2 additions & 1 deletion insights/tests/client/phase/test_post_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def patch_insights_config(old_function):
"return_value.load_all.return_value.list_specs": False,
"return_value.load_all.return_value.show_results": False,
"return_value.load_all.return_value.check_results": False,
"return_value.load_all.return_value.no_upload": False})
"return_value.load_all.return_value.no_upload": False,
"return_value.load_all.return_value.module": False})
return patcher(old_function)

# DRY this at some point... for the love of god
Expand Down
Loading