def db_import_system(upload_data, vmaas_json: str, repo_list: list):
    """Import initial system record to the DB, report back on what we did."""

    status = ImportStatus.FAILED

    with DatabasePoolConnection() as conn:
        with conn.cursor() as cur:
            try:
                host = upload_data["host"]
                import_status, system_id = db_import_system_platform(
                    cur, host['id'], host['account'],
                    upload_data['platform_metadata']['url'],
                    host.get("display_name"), host.get('stale_timestamp'),
                    host.get('stale_warning_timestamp'),
                    host.get('culled_timestamp'), vmaas_json)
                if import_status is None:
                    return status
                status |= import_status

                db_import_repos(cur, repo_list)
                db_import_system_repos(cur, repo_list, system_id)

                db_delete_other_system_repos(cur, repo_list, system_id)

                conn.commit()

                status -= ImportStatus.FAILED
            except DatabaseError:
                DATABASE_ERROR.inc()
                LOGGER.exception("Error importing system: ")
                FailedCache.push(FailedCache.upload_cache, upload_data)
                LOGGER.info("Remembered upload %s", str(upload_data))
                conn.rollback()
    return status
def db_delete_system(msg_dict):
    """Delete system with inventory ID."""

    rtrn = {'deleted': False, 'failed': True}

    with DatabasePoolConnection() as conn:
        with conn.cursor() as cur:
            try:
                rh_account_id = db_account_lookup(cur, msg_dict['account'])
                cur.execute(
                    """INSERT INTO system_platform
                            (inventory_id, rh_account_id, opt_out, stale, when_deleted)
                            VALUES (%s, %s, true, true, now())
                            ON CONFLICT (inventory_id) DO UPDATE SET
                            opt_out = EXCLUDED.opt_out, stale = EXCLUDED.stale, when_deleted = EXCLUDED.when_deleted
                            RETURNING (xmax = 0) AS inserted""", (
                        msg_dict['id'],
                        rh_account_id,
                    ))

                inserted, = cur.fetchone()
                rtrn['deleted'] = not inserted
                conn.commit()
                rtrn['failed'] = False
            except DatabaseError:
                DATABASE_ERROR.inc()
                LOGGER.exception("Error deleting system: ")
                FailedCache.push(FailedCache.delete_cache, msg_dict)
                LOGGER.info("Remembered deleting %s", str(msg_dict))
                conn.rollback()
    return rtrn
예제 #3
0
    def process_upload_or_re_evaluate(self, msg_dict: dict, loop=None):
        """
        Process function to upload new file or re-evaluate system
        """
        with DatabasePoolConnection() as conn:
            with conn.cursor() as cur:
                try:
                    LOGGER.info("Received message type: %s", msg_dict['type'])
                    # Lock the system for processing
                    cur.execute("""SELECT id, inventory_id, vmaas_json, rh_account_id,
                                        opt_out FROM system_platform
                                WHERE inventory_id = %s
                                FOR UPDATE""", (msg_dict['host']['id'],))
                    system_platform = cur.fetchone()
                    if system_platform is not None:
                        self.evaluate_vmaas(system_platform, cur, loop=loop)
                        conn.commit()
                        if msg_dict['type'] == 'upload_new_file':
                            send_msg_to_payload_tracker(PAYLOAD_TRACKER_PRODUCER, msg_dict, 'success', loop=loop)

                    else:
                        INV_ID_NOT_FOUND.inc()
                        LOGGER.error("System with inventory_id not found in DB: %s", msg_dict['host']['id'])
                        send_msg_to_payload_tracker(PAYLOAD_TRACKER_PRODUCER, msg_dict, 'error',
                                                    status_msg='System with inventory_id not found in DB: %s' % msg_dict['host']['id'],
                                                    loop=loop)
                except DatabaseError:
                    LOGGER.exception("Unable to store data: ")
                    FailedCache.push(FailedCache.upload_cache, msg_dict)
                    LOGGER.info("Remembered failed upload: %s", str(msg_dict))
                    conn.rollback()
예제 #4
0
async def process_upload_or_re_evaluate(msg_dict: dict):
    """
    Process function to upload new file or re-evaluate system
    """
    async with DB_POOL.acquire() as conn:
        try:
            async with conn.transaction():
                LOGGER.info("Received message type: %s", msg_dict['type'])
                # Lock the system for processing
                system_platform = await conn.fetchrow(
                    """SELECT id,
                                                                inventory_id,
                                                                vmaas_json,
                                                                rh_account_id,
                                                                opt_out,
                                                                stale
                                                           FROM system_platform
                                                          WHERE inventory_id = $1
                                                            AND when_deleted IS NULL
                                                     FOR UPDATE""",
                    msg_dict['host']['id'])
                if system_platform is not None:
                    await evaluate_vmaas(system_platform, conn)
                    if msg_dict['type'] == 'upload_new_file':
                        send_msg_to_payload_tracker(PAYLOAD_TRACKER_PRODUCER,
                                                    msg_dict,
                                                    'success',
                                                    loop=MAIN_LOOP)

                else:
                    INV_ID_NOT_FOUND.inc()
                    LOGGER.error(
                        "System with inventory_id not found in DB: %s",
                        msg_dict['host']['id'])
                    if msg_dict['type'] == 'upload_new_file':
                        send_msg_to_payload_tracker(
                            PAYLOAD_TRACKER_PRODUCER,
                            msg_dict,
                            'error',
                            status_msg=
                            'System with inventory_id not found in DB: %s' %
                            msg_dict['host']['id'],
                            loop=MAIN_LOOP)

        # pylint: disable=broad-except
        except Exception:
            LOGGER.exception("Unable to store data: ")
            FailedCache.push(FailedCache.upload_cache, msg_dict)
            LOGGER.info("Remembered failed upload: %s", str(msg_dict))
def db_delete_system(msg_dict):
    """Delete system with inventory ID."""

    rtrn = {'deleted': False, 'failed': True}

    with DatabasePoolConnection() as conn:
        with conn.cursor() as cur:
            try:
                curr_time = datetime.now(tz=pytz.utc)
                cur.execute(
                    """INSERT INTO deleted_systems
                            (inventory_id, when_deleted) VALUES (%s, %s)
                            ON CONFLICT (inventory_id) DO UPDATE SET
                            when_deleted = EXCLUDED.when_deleted
                            """, (
                        msg_dict['id'],
                        curr_time,
                    ))
                cur.execute(
                    """DELETE FROM deleted_systems WHERE when_deleted < %s
                            """,
                    (curr_time - timedelta(hours=SYSTEM_DELETION_THRESHOLD), ))
                cur.execute(
                    """SELECT deleted_inventory_id FROM delete_system(%s)""",
                    (msg_dict['id'], ))
                system_platform = cur.fetchone()
                if system_platform is not None:
                    rtrn['deleted'] = True
                conn.commit()
                rtrn['failed'] = False
            except DatabaseError:
                DATABASE_ERROR.inc()
                LOGGER.exception("Error deleting system: ")
                FailedCache.push(FailedCache.delete_cache, msg_dict)
                LOGGER.info("Remembered deleting %s", str(msg_dict))
                conn.rollback()
    return rtrn