Beispiel #1
0
def history_change_post_save(sender, instance, raw, using, **kwargs):
    if SPLUNK_HOST:
        log_change_to_splunk(instance, 'CHANGE_HISTORY')
Beispiel #2
0
def post_create_change(sender, instance, raw, using, **kwargs):
    registration_type = chdb.CI_CHANGE_REGISTRATION_TYPES.NOT_REGISTERED.id
    user = None
    try:
        """ Classify change, and create record - CIChange """
        logger.debug('Hooking post save CIChange creation.')
        if isinstance(instance, chdb.CIChangeGit):
            if SPLUNK_HOST:
                log_change_to_splunk(instance, 'CHANGE_GIT')
            # register every git change (treat as manual)
            registration_type = chdb.CI_CHANGE_REGISTRATION_TYPES.WAITING.id
            priority = chdb.CI_CHANGE_PRIORITY_TYPES.WARNING.id
            change_type = chdb.CI_CHANGE_TYPES.CONF_GIT.id
            message = instance.comment
            if instance.time:
                time = instance.time
            else:
                time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ci = instance.ci
        elif isinstance(instance, chdb.CIChangeCMDBHistory):
            if SPLUNK_HOST:
                log_change_to_splunk(instance, 'CHANGE_HISTORY')
            # register only user triggered cmdb history
            if instance.user_id:
                registration_type = \
                    chdb.CI_CHANGE_REGISTRATION_TYPES.WAITING.id
                user = instance.user
            change_type = chdb.CI_CHANGE_TYPES.CI.id
            priority = chdb.CI_CHANGE_PRIORITY_TYPES.NOTICE.id
            message = instance.comment
            time = instance.time
            ci = instance.ci
        elif isinstance(instance, chdb.CIChangePuppet):
            if SPLUNK_HOST:
                log_change_to_splunk(instance, 'CHANGE_PUPPET')
            if instance.status == 'failed':
                priority = chdb.CI_CHANGE_PRIORITY_TYPES.ERROR.id
            elif instance.status == 'changed':
                priority = chdb.CI_CHANGE_PRIORITY_TYPES.WARNING.id
            else:
                priority = chdb.CI_CHANGE_PRIORITY_TYPES.NOTICE.id
            change_type = chdb.CI_CHANGE_TYPES.CONF_AGENT.id
            time = instance.time
            ci = instance.ci
            message = 'Puppet log for %s (%s)' % (
                instance.host, instance.configuration_version
            )

        if chdb.CIChange.objects.filter(
            content_type=ContentType.objects.get_for_model(instance),
            object_id=instance.id,
        ).exists():
            # already created parent cichange (e.g while saving twice).
            # Skip it.
            return
        ch = chdb.CIChange()
        ch.time = time
        ch.ci = ci
        ch.registration_type = registration_type
        ch.priority = priority
        ch.type = change_type
        ch.content_object = instance
        ch.message = message
        ch.user = user
        ch.save()
        # register ticket now.
        register_issue_signal.send(sender=instance, change_id=ch.id)
        logger.debug('Hook done.')
    except IntegrityError:
        instance.delete()
        raise
Beispiel #3
0
def post_create_change(sender, instance, raw, using, **kwargs):
    registration_type = chdb.CI_CHANGE_REGISTRATION_TYPES.NOT_REGISTERED.id
    user = None
    try:
        """ Classify change, and create record - CIChange """
        logger.debug('Hooking post save CIChange creation.')
        if isinstance(instance, chdb.CIChangeGit):
            if SPLUNK_HOST:
                log_change_to_splunk(instance, 'CHANGE_GIT')
            # register every git change (treat as manual)
            registration_type = chdb.CI_CHANGE_REGISTRATION_TYPES.WAITING.id
            priority = chdb.CI_CHANGE_PRIORITY_TYPES.WARNING.id
            change_type = chdb.CI_CHANGE_TYPES.CONF_GIT.id
            message = instance.comment
            if instance.time:
                time = instance.time
            else:
                time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ci = instance.ci
        elif isinstance(instance, chdb.CIChangeCMDBHistory):
            if SPLUNK_HOST:
                log_change_to_splunk(instance, 'CHANGE_HISTORY')
            # register only user triggered cmdb history
            if instance.user_id:
                registration_type = \
                    chdb.CI_CHANGE_REGISTRATION_TYPES.WAITING.id
                user = instance.user
            change_type = chdb.CI_CHANGE_TYPES.CI.id
            priority = chdb.CI_CHANGE_PRIORITY_TYPES.NOTICE.id
            message = instance.comment
            time = instance.time
            ci = instance.ci
        elif isinstance(instance, chdb.CIChangePuppet):
            if SPLUNK_HOST:
                log_change_to_splunk(instance, 'CHANGE_PUPPET')
            if instance.status == 'failed':
                priority = chdb.CI_CHANGE_PRIORITY_TYPES.ERROR.id
            elif instance.status == 'changed':
                priority = chdb.CI_CHANGE_PRIORITY_TYPES.WARNING.id
            else:
                priority = chdb.CI_CHANGE_PRIORITY_TYPES.NOTICE.id
            change_type = chdb.CI_CHANGE_TYPES.CONF_AGENT.id
            time = instance.time
            ci = instance.ci
            message = 'Puppet log for %s (%s)' % (
                instance.host, instance.configuration_version
            )

        if chdb.CIChange.objects.filter(
            content_type=ContentType.objects.get_for_model(instance),
            object_id=instance.id,
        ).exists():
            # already created parent cichange (e.g while saving twice).
            # Skip it.
            return
        ch = chdb.CIChange()
        ch.time = time
        ch.ci = ci
        ch.registration_type = registration_type
        ch.priority = priority
        ch.type = change_type
        ch.content_object = instance
        ch.message = message
        ch.user = user
        ch.save()
        # register ticket now.
        register_issue_signal.send(sender=instance, change_id=ch.id)
        logger.debug('Hook done.')
    except IntegrityError:
        instance.delete()
        raise