Exemple #1
0
def _is_client_registered():
    msg_notyet = 'This machine has not yet been registered.'
    msg_unreg = 'This machine has been unregistered.'
    msg_doreg = 'Use --register to register this machine.'
    msg_rereg = 'Use --register if you would like to re-register this machine.'
    msg_exit = 'Exiting...'
    # check reg status w/ API
    reg_check = registration_check()
    if not reg_check['status']:
        # not registered
        if reg_check['unreg_date']:
            # system has been unregistered from the UI
            msg = '\n'.join([msg_unreg, msg_rereg, msg_exit])
            write_unregistered_file(reg_check['unreg_date'])
            return msg, False
        else:
            # no record of system in remote
            msg = '\n'.join([msg_notyet, msg_doreg, msg_exit])
            # clear any local records
            delete_registered_file()
            delete_unregistered_file()
            return msg, False
    else:
        # API confirms reg
        if not os.path.isfile(constants.registered_file):
            write_registered_file()
        # delete any stray unregistered
        delete_unregistered_file()
        return '', True
Exemple #2
0
def _is_client_registered():
    msg_notyet = 'This machine has not yet been registered.'
    msg_unreg = 'This machine has been unregistered.'
    msg_doreg = 'Use --register to register this machine.'
    msg_rereg = 'Use --force-register if you would like to re-register this machine.'
    msg_exit = 'Exiting...'
    # check reg status w/ API
    reg_check = registration_check()
    if not reg_check['status']:
        # not registered
        if reg_check['unreg_date']:
            # system has been unregistered from the UI
            msg = '\n'.join([msg_unreg, msg_rereg, msg_exit])
            write_unregistered_file(reg_check['unreg_date'])
            return msg, False
        else:
            # no record of system in remote
            msg = '\n'.join([msg_notyet, msg_doreg, msg_exit])
            # clear any local records
            delete_registered_file()
            delete_unregistered_file()
            return msg, False
    else:
        # API confirms reg
        if not os.path.isfile(constants.registered_file):
            write_registered_file()
        # delete any stray unregistered
        delete_unregistered_file()
        return '', True
    def check_registration(self):
        """
        Check if we were unregistered
        """
        registration_url = self.api_url + '/v1/systems/' + generate_machine_id()
        logger.debug("Checking registration status: %s", registration_url)
        system_info = self.session.get(registration_url)

        if system_info.status_code == 404:
            # This system hasn't been registered and is it's first upload
            return
        elif system_info.status_code == 200:
            system_info = system_info.json()
            logger.debug("System info: %s", json.dumps(system_info))
        else:
            self.handle_fail_rcs(system_info)
            logger.error("Could not check blacklist")
            sys.exit(1)

        try:
            if system_info['unregistered_at']:
                write_unregistered_file(system_info['unregistered_at'])
            else:
                logger.debug("This machine is registered")
        except LookupError:
            logger.debug("This machine is registered")
 def handle_fail_rcs(self, req):
     """
     Bail out if we get a 401 and leave a message
     """
     if req.status_code >= 400:
         logger.error("ERROR: Upload failed!")
         logger.info("Debug Information:\nHTTP Status Code: %s", req.status_code)
         logger.info("HTTP Status Text: %s", req.reason)
         if req.status_code == 401:
             logger.error("Authorization Required.")
             logger.error("Please ensure correct credentials "
                          "in " + constants.default_conf_file)
             logger.debug("HTTP Response Text: %s", req.text)
         if req.status_code == 402:
             try:
                 logger.error(req.json()["message"])
             except LookupError:
                 logger.error("Got 402 but no message")
                 logger.debug("HTTP Response Text: %s", req.text)
         if req.status_code == 412:
             try:
                 unreg_date = req.json()["unregistered_at"]
                 logger.error(req.json()["message"])
             except LookupError:
                 unreg_date = "412, but no unreg_date or message"
                 logger.debug("HTTP Response Text: %s", req.text)
             write_unregistered_file(unreg_date)
         sys.exit(1)
Exemple #5
0
 def handle_fail_rcs(self, req):
     """
     Bail out if we get a 401 and leave a message
     """
     if req.status_code >= 400:
         logger.error("ERROR: Upload failed!")
         logger.info("Debug Information:\nHTTP Status Code: %s",
                     req.status_code)
         logger.info("HTTP Status Text: %s", req.reason)
         if req.status_code == 401:
             logger.error("Authorization Required.")
             logger.error("Please ensure correct credentials "
                          "in " + constants.default_conf_file)
             logger.debug("HTTP Response Text: %s", req.text)
         if req.status_code == 402:
             try:
                 logger.error(req.json()["message"])
             except LookupError:
                 logger.error("Got 402 but no message")
                 logger.debug("HTTP Response Text: %s", req.text)
         if req.status_code == 412:
             try:
                 unreg_date = req.json()["unregistered_at"]
                 logger.error(req.json()["message"])
             except LookupError:
                 unreg_date = "412, but no unreg_date or message"
                 logger.debug("HTTP Response Text: %s", req.text)
             write_unregistered_file(unreg_date)
         sys.exit(1)
    def handle_fail_rcs(self, req):
        """
        Bail out if we get a 401 and leave a message
        """

        try:
            logger.debug("HTTP Status Code: %s", req.status_code)
            logger.debug("HTTP Response Text: %s", req.text)
            logger.debug("HTTP Response Reason: %s", req.reason)
            logger.debug("HTTP Response Content: %s", req.content)
        except:
            logger.error("Malformed HTTP Request.")

        # attempt to read the HTTP response JSON message
        try:
            logger.debug("HTTP Response Message: %s", req.json()["message"])
        except:
            logger.debug("No HTTP Response message present.")

        # handle specific status codes
        if req.status_code >= 400:
            logger.error("ERROR: Upload failed!")
            logger.info("Debug Information:\nHTTP Status Code: %s",
                        req.status_code)
            logger.info("HTTP Status Text: %s", req.reason)
            if req.status_code == 401:
                logger.error("Authorization Required.")
                logger.error("Please ensure correct credentials "
                             "in " + constants.default_conf_file)
                logger.debug("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.')
                try:
                    logger.error(req.json()["message"])
                except LookupError:
                    logger.error("Got 402 but no message")
                    logger.debug("HTTP Response Text: %s", req.text)
                except:
                    logger.error("Got 402 but no message")
                    logger.debug("HTTP Response Text: %s", req.text)
            if req.status_code == 403 and self.auto_config:
                # Insights disabled in satellite
                rhsm_hostname = urlparse(self.base_url).hostname
                if (rhsm_hostname != 'subscription.rhn.redhat.com' and
                   rhsm_hostname != 'subscription.rhsm.redhat.com'):
                    logger.error('Please enable Insights on Satellite server '
                                 '%s to continue.', rhsm_hostname)
            if req.status_code == 412:
                try:
                    unreg_date = req.json()["unregistered_at"]
                    logger.error(req.json()["message"])
                    write_unregistered_file(unreg_date)
                except LookupError:
                    unreg_date = "412, but no unreg_date or message"
                    logger.debug("HTTP Response Text: %s", req.text)
                except:
                    unreg_date = "412, but no unreg_date or message"
                    logger.debug("HTTP Response Text: %s", req.text)
            return False
 def unregister(self):
     """
     Unregister this system from the insights service
     """
     machine_id = generate_machine_id()
     try:
         logger.debug("Unregistering %s", machine_id)
         self.session.delete(self.api_url + "/v1/systems/" + machine_id)
         logger.info("Successfully unregistered from the Red Hat Access Insights Service")
         write_unregistered_file()
     except requests.ConnectionError as e:
         logger.debug(e)
         logger.error("Could not unregister this system")
 def unregister(self):
     """
     Unregister this system from the insights service
     """
     machine_id = generate_machine_id()
     try:
         logger.debug("Unregistering %s", machine_id)
         self.session.delete(self.api_url + "/v1/systems/" + machine_id)
         logger.info(
             "Successfully unregistered from the Red Hat Insights Service")
         write_unregistered_file()
         InsightsSchedule().remove_scheduling()
     except requests.ConnectionError as e:
         logger.debug(e)
         logger.error("Could not unregister this system")
 def handle_fail_rcs(self, req):
     """
     Bail out if we get a 401 and leave a message
     """
     if req.status_code >= 400:
         logger.error("ERROR: Upload failed!")
         logger.info("Debug Information:\nHTTP Status Code: %s",
                     req.status_code)
         logger.info("HTTP Status Text: %s", req.reason)
         if req.status_code == 401:
             logger.error("Authorization Required.")
             logger.error("Please ensure correct credentials "
                          "in " + constants.default_conf_file)
             logger.debug("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.')
             try:
                 logger.error(req.json()["message"])
             except LookupError:
                 logger.error("Got 402 but no message")
                 logger.debug("HTTP Response Text: %s", req.text)
         if req.status_code == 403 and self.auto_config:
             # Insights disabled in satellite
             from urlparse import urlparse
             rhsm_hostname = urlparse(self.base_url).hostname
             if rhsm_hostname != 'subscription.rhn.redhat.com':
                 logger.error('Please enable Insights on Satellite server '
                              '%s to continue.', rhsm_hostname)
         if req.status_code == 412:
             try:
                 unreg_date = req.json()["unregistered_at"]
                 logger.error(req.json()["message"])
                 write_unregistered_file(unreg_date)
             except LookupError:
                 unreg_date = "412, but no unreg_date or message"
                 logger.debug("HTTP Response Text: %s", req.text)
         sys.exit(1)
Exemple #10
0
def _is_client_registered():

    # If the client is running in container mode, bypass this stuff
    msg_container_mode = 'Client running in container/image mode. Bypassing registration check'
    if config['analyze_container']:
        return msg_container_mode, False

    # All other cases
    msg_notyet = 'This machine has not yet been registered.'
    msg_unreg = 'This machine has been unregistered.'
    msg_doreg = 'Use --register to register this machine.'
    msg_rereg = 'Use --register if you would like to re-register this machine.'

    # check reg status w/ API
    reg_check = registration_check(get_connection())
    if not reg_check['status']:
        # not registered
        if reg_check['unreg_date']:
            # system has been unregistered from the UI
            msg = '\n'.join([msg_unreg, msg_rereg])
            write_unregistered_file(reg_check['unreg_date'])
            return msg, False
        else:
            # no record of system in remote
            msg = '\n'.join([msg_notyet, msg_doreg])
            # clear any local records
            write_to_disk(constants.registered_file, delete=True)
            write_to_disk(constants.unregistered_file, delete=True)
            return msg, False
    else:
        # API confirms reg
        if not os.path.isfile(constants.registered_file):
            write_to_disk(constants.registered_file)
        # delete any stray unregistered
        write_to_disk(constants.unregistered_file, delete=True)
        return '', True