Beispiel #1
0
def send_upgraded_master_notifications(master_version, logger=None):
    if not settings.MASTER_UPGRADE_NOTIFY:
        return

    if logger is None:
        logger = logging.getLogger("lava_scheduler_app")
    emails = ()
    workers_set = set()

    # List the users that are superuser or admin of workers
    worker_admin_groups = GroupWorkerPermission.objects.all().values("group")
    users = User.objects.filter(
        Q(groups__in=worker_admin_groups) | Q(is_superuser=True))
    users = users.exclude(email="")
    users = users.distinct()

    # Loop on every users to send a mail to each user with the list of workers
    # they are administrating
    for user in users:
        workers = Worker.objects.filter(health=Worker.HEALTH_ACTIVE)
        workers = workers.exclude(
            Q(version=master_version) | Q(version__isnull=True))
        workers = workers.exclude(master_version_notified=master_version)
        workers = workers.accessible_by_user(user, Worker.CHANGE_PERMISSION)

        if workers:
            workers_set.update([w.hostname for w in workers])
            title = "LAVA: Worker upgrade notification for %s" % (
                user.username, )
            template_env = Notification.TEMPLATES_ENV
            body = template_env.get_template(
                Worker.MASTER_UPGRADE_NOTIFICATION).render(
                    hostname=dbutils.get_domain(),
                    user=user,
                    version=master_version,
                    workers=workers,
                )
            emails += ((title, body, settings.SERVER_EMAIL, [user.email]), )

    try:
        logger.info("[INIT] sending worker upgrade notifications:")
        for worker in workers_set:
            logger.info("[INIT] * %s", worker)
        send_mass_mail(emails)
    except Exception as exc:
        logger.exception(exc)
        logger.warning("failed to send worker upgrade notifications")
    # If all went well, update relevant field to current master version
    Worker.objects.filter(hostname__in=workers_set).update(
        master_version_notified=master_version)
Beispiel #2
0
def create_irc_notification(job):
    args = {}
    args["job"] = job
    args["url_prefix"] = "http://%s" % dbutils.get_domain()
    return create_notification_body(Notification.DEFAULT_IRC_TEMPLATE, **args)
Beispiel #3
0
def get_notification_args(job):
    args = {}
    args["job"] = job
    args["url_prefix"] = "http://%s" % dbutils.get_domain()
    # Get lava.job result if available
    with contextlib.suppress(TestCase.DoesNotExist):
        lava_job_obj = TestCase.objects.get(suite__job=job,
                                            suite__name="lava",
                                            name="job")
        args["lava_job_result"] = lava_job_obj.action_metadata

    args["query"] = {}
    if job.notification.query_name or job.notification.entity:
        args["query"]["results"] = get_query_results(job.notification)
        args["query"]["link"] = get_query_link(job.notification)
        # Find the first job which has health HEALTH_COMPLETE and is not the
        # current job (this can happen with custom queries) for comparison.
        compare_index = None
        for index, result in enumerate(args["query"]["results"]):
            if result.health == TestJob.HEALTH_COMPLETE and job != result:
                compare_index = index
                break

        args["query"]["compare_index"] = compare_index
        if compare_index is not None and job.notification.blacklist:
            # Get testsuites diffs between current job and latest complete
            # job from query.
            new_suites = job.testsuite_set.all().exclude(
                name__in=job.notification.blacklist)
            old_suites = (args["query"]["results"]
                          [compare_index].testsuite_set.all().exclude(
                              name__in=job.notification.blacklist))
            left_suites_diff = new_suites.exclude(
                name__in=old_suites.values_list("name", flat=True))
            right_suites_diff = old_suites.exclude(
                name__in=new_suites.values_list("name", flat=True))

            args["query"]["left_suites_diff"] = left_suites_diff
            args["query"]["right_suites_diff"] = right_suites_diff

            # Get testcases diffs between current job and latest complete
            # job from query.
            new_cases = (TestCase.objects.filter(suite__job=job).exclude(
                name__in=job.notification.blacklist).exclude(
                    suite__name__in=job.notification.blacklist))
            old_cases = (TestCase.objects.filter(
                suite__job=args["query"]["results"][compare_index]).exclude(
                    name__in=job.notification.blacklist).exclude(
                        suite__name__in=job.notification.blacklist))

            left_cases_diff = new_cases.exclude(
                name__in=old_cases.values_list("name", flat=True))
            right_cases_diff = old_cases.exclude(
                name__in=new_cases.values_list("name", flat=True))

            args["query"]["left_cases_diff"] = left_cases_diff
            args["query"]["right_cases_diff"] = right_cases_diff

            left_suites_intersection = new_suites.filter(
                name__in=old_suites.values_list("name", flat=True))

            # Format results.
            left_suites_count = {}
            for suite in left_suites_intersection:
                left_suites_count[suite.name] = (
                    suite.testcase_set.filter(
                        result=TestCase.RESULT_PASS).count(),
                    suite.testcase_set.filter(
                        result=TestCase.RESULT_FAIL).count(),
                    suite.testcase_set.filter(
                        result=TestCase.RESULT_SKIP).count(),
                )

            right_suites_intersection = old_suites.filter(
                name__in=new_suites.values_list("name", flat=True))

            # Format results.
            right_suites_count = {}
            for suite in right_suites_intersection:
                right_suites_count[suite.name] = (
                    suite.testcase_set.filter(
                        result=TestCase.RESULT_PASS).count(),
                    suite.testcase_set.filter(
                        result=TestCase.RESULT_FAIL).count(),
                    suite.testcase_set.filter(
                        result=TestCase.RESULT_SKIP).count(),
                )

            args["query"]["left_suites_count"] = left_suites_count
            args["query"]["right_suites_count"] = right_suites_count

            # Format {<Testcase>: old_result, ...}
            testcases_changed = {}
            for suite in left_suites_intersection:
                try:
                    old_suite = TestSuite.objects.get(
                        name=suite.name,
                        job=args["query"]["results"][compare_index])
                except TestSuite.DoesNotExist:
                    continue  # No matching suite, move on.
                for testcase in suite.testcase_set.all():
                    try:
                        old_testcase = TestCase.objects.get(suite=old_suite,
                                                            name=testcase.name)
                        if old_testcase and testcase.result != old_testcase.result:
                            testcases_changed[
                                testcase] = old_testcase.get_result_display()
                    except TestCase.DoesNotExist:
                        continue  # No matching TestCase, move on.
                    except TestCase.MultipleObjectsReturned:
                        logging.info(
                            "Multiple Test Cases with the equal name in TestSuite %s, could not compare",
                            old_suite,
                        )

            args["query"]["testcases_changed"] = testcases_changed

    return args