Exemplo n.º 1
0
def _statuspage_create_incident(
    headers,
    component_id,
    tree,
    status_from,
    status_to,
):
    page_id = flask.current_app.config.get('STATUSPAGE_PAGE_ID')
    if not page_id:
        log.error('STATUSPAGE_PAGE_ID not defined in app config.')
        return

    data = _statuspage_data(
        False,
        component_id,
        tree,
        status_from,
        status_to,
    )
    log.debug(
        f'Create statuspage incident for tree `{tree.tree}` under page `{page_id}`',
        data=data)
    response = requests.post(
        f'{STATUSPAGE_URL}/pages/{page_id}/incidents',
        headers=headers,
        json=data,
    )
    try:
        response.raise_for_status()
    except Exception as e:
        log.exception(e)
        _statuspage_send_email_on_error(
            subject=f'[treestatus] Error when creating statuspage incident',
            content=STATUSPAGE_ERROR_ON_CREATE.format(tree=tree.tree),
        )
Exemplo n.º 2
0
def _statuspage_send_email_on_error(subject, content, incident_id=None):
    page_id = flask.current_app.config.get('STATUSPAGE_PAGE_ID')
    address = flask.current_app.config.get('STATUSPAGE_NOTIFY_ON_ERROR')
    if not address or not page_id:
        log.error(
            'STATUSPAGE_NOTIFY_ON_ERROR and/or STATUSPAGE_PAGE_ID not defined in app config.'
        )
        return

    link = {
        'href': f'https://manage.statuspage.io/pages/{page_id}',
        'text': 'Visit statuspage',
    }
    if incident_id:
        link = {
            'href':
            f'https://manage.statuspage.io/pages/{page_id}/incidents/{incident_id}',
            'text': 'Visit statuspage incident',
        }
    flask.current_app.notify.email({
        'address': address,
        'subject': subject,
        'content': content,
        'link': link,
    })
Exemplo n.º 3
0
def _statuspage_send_email_on_error(subject, content, incident_id=None):
    page_id = flask.current_app.config.get("STATUSPAGE_PAGE_ID")
    address = flask.current_app.config.get("STATUSPAGE_NOTIFY_ON_ERROR")
    if not address or not page_id:
        log.error(
            "STATUSPAGE_NOTIFY_ON_ERROR and/or STATUSPAGE_PAGE_ID not defined in app config."
        )
        return

    link = {
        "href": f"https://manage.statuspage.io/pages/{page_id}",
        "text": "Visit statuspage"
    }
    if incident_id:
        link = {
            "href":
            f"https://manage.statuspage.io/pages/{page_id}/incidents/{incident_id}",
            "text": "Visit statuspage incident"
        }
    flask.current_app.notify.email({
        "address": address,
        "subject": subject,
        "content": content,
        "link": link
    })
Exemplo n.º 4
0
def _notify_status_change(trees_changes):
    if flask.current_app.config.get('STATUSPAGE_ENABLE'):
        log.debug('Notify statuspage about trees changes.')

        statuspage_tags = flask.current_app.config.get('STATUSPAGE_TAGS', [])
        components = flask.current_app.config.get('STATUSPAGE_COMPONENTS', {})
        token = flask.current_app.config.get('STATUSPAGE_TOKEN')
        if not token:
            log.error('STATUSPAGE_PAGE_ID not defined in app config.')
        else:
            headers = {'Authorization': f'OAuth {token}'}

            for tree_change in trees_changes:
                tree, status_from, status_to, tags = tree_change

                if tree.tree not in components.keys():
                    continue

                if len(tags) > 1:
                    log.error(f'Multiple tags defined: {tags}')
                    continue
                elif len(tags) == 1:
                    tag = tags[0]
                else:
                    tag = ''

                log.debug(f'Notify statuspage about: {tree.tree}')
                component_id = components[tree.tree]

                # create an accident
                if status_from in ['open', 'approval required'] and \
                   status_to == 'closed' and \
                   tag in statuspage_tags:
                    _statuspage_create_incident(
                        headers,
                        component_id,
                        tree,
                        status_from,
                        status_to,
                    )

                # close an accident
                elif status_from == 'closed' and status_to in [
                        'open', 'approval required'
                ]:
                    _statuspage_resolve_incident(
                        headers,
                        component_id,
                        tree,
                        status_from,
                        status_to,
                    )

    if flask.current_app.config.get('PULSE_TREESTATUS_ENABLE'):
        routing_key_pattern = 'tree/{0}/status_change'
        exchange = flask.current_app.config.get('PULSE_TREESTATUS_EXCHANGE')

        for tree_change in trees_changes:
            tree, status_from, status_to, tags = tree_change

            payload = {
                'status_from': status_from,
                'status_to': status_to,
                'tree': tree.to_dict(),
                'tags': tags
            }
            routing_key = routing_key_pattern.format(tree.tree)

            log.info('Sending pulse to {} for tree: {}'.format(
                exchange,
                tree.tree,
            ))

            try:
                flask.current_app.pulse.publish(exchange, routing_key, payload)
            except Exception as e:
                import traceback
                msg = 'Can\'t send notification to pulse.'
                trace = traceback.format_exc()
                log.error('{0}\nException:{1}\nTraceback: {2}'.format(
                    msg, e, trace))  # noqa
Exemplo n.º 5
0
def _notify_status_change(trees_changes):
    if flask.current_app.config.get("STATUSPAGE_ENABLE"):
        log.debug("Notify statuspage about trees changes.")

        statuspage_tags = flask.current_app.config.get("STATUSPAGE_TAGS", [])
        components = flask.current_app.config.get("STATUSPAGE_COMPONENTS", {})
        token = flask.current_app.config.get("STATUSPAGE_TOKEN")
        if not token:
            log.error("STATUSPAGE_PAGE_ID not defined in app config.")
        else:
            headers = {"Authorization": f"OAuth {token}"}

            for tree_change in trees_changes:
                tree, status_from, status_to, tags = tree_change

                if tree.tree not in components.keys():
                    continue

                if len(tags) > 1:
                    log.error(f"Multiple tags defined: {tags}")
                    continue
                elif len(tags) == 1:
                    tag = tags[0]
                else:
                    tag = ""

                log.debug(f"Notify statuspage about: {tree.tree}")
                component_id = components[tree.tree]

                # create an accident
                if status_from in [
                        "open", "approval required"
                ] and status_to == "closed" and tag in statuspage_tags:
                    _statuspage_create_incident(headers, component_id, tree,
                                                status_from, status_to)

                # close an accident
                elif status_from == "closed" and status_to in [
                        "open", "approval required"
                ]:
                    _statuspage_resolve_incident(headers, component_id, tree,
                                                 status_from, status_to)

    if flask.current_app.config.get("PULSE_TREESTATUS_ENABLE"):
        routing_key_pattern = "tree/{0}/status_change"
        exchange = flask.current_app.config.get("PULSE_TREESTATUS_EXCHANGE")

        for tree_change in trees_changes:
            tree, status_from, status_to, tags = tree_change

            payload = {
                "status_from": status_from,
                "status_to": status_to,
                "tree": tree.to_dict(),
                "tags": tags
            }
            routing_key = routing_key_pattern.format(tree.tree)

            log.info("Sending pulse to {} for tree: {}".format(
                exchange, tree.tree))

            try:
                flask.current_app.pulse.publish(exchange, routing_key, payload)
            except Exception as e:
                import traceback

                msg = "Can't send notification to pulse."
                trace = traceback.format_exc()
                log.error("{0}\nException:{1}\nTraceback: {2}".format(
                    msg, e, trace))  # noqa