Exemplo n.º 1
0
def report_create(obj, product_name, output_dir):
    """Create report for a product"""
    client = get_client(obj)

    product = client.product_list(name=product_name)
    if not product:
        fatal_error('Product {} does not exist'.format(product_name))

    product = product[0]

    try:
        subprocess.check_output(['which', 'gnuplot'])
    except subprocess.CalledProcessError:
        fatal_error('Missing system dependency. Please install *gnuplot* system package!')

    with Action('Creating report for product: {}'.format(product_name), nl=True):
        generate_weekly_report(client, product, output_dir)
def main():
    if not SLR_URI:
        logger.error(
            'SLR_URI environment variable is required. Terminating ...')
        sys.exit(1)

    try:
        subprocess.check_output(['which', 'gnuplot'])
    except subprocess.CalledProcessError:
        logger.error(
            'Missing system dependency. Please install *gnuplot* system package!'
        )
        sys.exit(1)

    successful_reports = []

    t_start = datetime.now()

    # Get last reports
    sync_reports(to_local=True)

    try:
        token = SLR_TOKEN if SLR_TOKEN else zign.api.get_token('uid', ['uid'])
        client = Client(SLR_URI, token)

        # 1. Get all products
        logger.info('Starting reports generation ...')
        products = client.product_list(limit=1000)

        for product in products:

            # Token could expire if report generation takes a long time!
            token = SLR_TOKEN if SLR_TOKEN else zign.api.get_token(
                'uid', ['uid'])
            client = Client(SLR_URI, token)

            name = product['name']
            try:
                # Make sure the product has the minimum req for generating a report
                slos = client.slo_list(product)
                if not slos:
                    logger.info(
                        'Skipping generating report for product "{}". Reason: No SLO defined!'
                        .format(name))
                    continue

                slis = client.sli_list(product)
                if not slis:
                    logger.info(
                        'Skipping generating report for product "{}". Reason: No SLI defined!'
                        .format(name))
                    continue

                # Finally, generate the report
                logger.info('Generating report for product: {}'.format(name))
                generate_weekly_report(client, product, OUTPUT_DIR)
                logger.info(
                    'Finished generating report for product: {}'.format(name))

                successful_reports.append(name)

                if not len(successful_reports) % 10:
                    time.sleep(60)

            except KeyboardInterrupt:
                logger.info('Report generation interrupted. Terminating ...')
                return
            except:
                logger.exception(
                    'Failed to generate report for product: {}'.format(name))
    except KeyboardInterrupt:
        logger.info('Report generation interrupted. Terminating ...')
        return
    except:
        logger.exception('Failed in generating reports. Terminating ...')
        sys.exit(1)

    duration = datetime.now() - t_start

    logger.info('Finished generating reports for products: {}'.format(
        successful_reports))
    logger.info(
        'Finished generating reports for {} products successfully in {} minutes'
        .format(len(successful_reports), duration.seconds / 60))

    # Upload latest reports to s3
    sync_reports(to_local=False)

    logger.info('Done!')