diff --git a/insights/client/__init__.py b/insights/client/__init__.py index 81cb10d50..5e24a6693 100644 --- a/insights/client/__init__.py +++ b/insights/client/__init__.py @@ -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): @@ -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 diff --git a/insights/client/connection.py b/insights/client/connection.py index c63af757e..5c3264b40 100644 --- a/insights/client/connection.py +++ b/insights/client/connection.py @@ -9,6 +9,7 @@ import json import logging import platform +import sys import warnings import errno # import io @@ -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 @@ -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.'): @@ -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.') @@ -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) @@ -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: diff --git a/insights/client/phase/v1.py b/insights/client/phase/v1.py index e0e1aa0ff..ff6716322 100644 --- a/insights/client/phase/v1.py +++ b/insights/client/phase/v1.py @@ -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__) @@ -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) @@ -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']: @@ -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): @@ -217,7 +227,7 @@ 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) @@ -225,8 +235,10 @@ def post_update(client, config): 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 @@ -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 diff --git a/insights/tests/client/phase/test_LEGACY_post_update.py b/insights/tests/client/phase/test_LEGACY_post_update.py index 89f6f845e..31a21a8d7 100644 --- a/insights/tests/client/phase/test_LEGACY_post_update.py +++ b/insights/tests/client/phase/test_LEGACY_post_update.py @@ -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) @@ -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 """ @@ -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. """ @@ -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 """ diff --git a/insights/tests/client/phase/test_post_update.py b/insights/tests/client/phase/test_post_update.py index 7f8be67ea..41dc3a1b4 100644 --- a/insights/tests/client/phase/test_post_update.py +++ b/insights/tests/client/phase/test_post_update.py @@ -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