Metadata-Version: 2.5
Name: otb-airflow-triggers
Version: 1.1.0
Summary: Shared Airflow triggers for otb.
Project-URL: Repository, https://github.com/openthebox/airflow-triggers
Project-URL: Documentation, https://github.com/openthebox/airflow-triggers/blob/main/README.md
Project-URL: Issues, https://github.com/openthebox/airflow-triggers/issues
Requires-Python: >=3.12
Requires-Dist: apache-airflow-providers-amazon[aiobotocore]==9.36.0
Requires-Dist: apache-airflow==3.3.2
Provides-Extra: dev
Requires-Dist: bandit; extra == 'dev'
Requires-Dist: botocore-stubs; extra == 'dev'
Requires-Dist: build; extra == 'dev'
Requires-Dist: coverage[toml]; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: packaging; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Description-Content-Type: text/markdown

# otb-airflow-triggers

[![Quality checks](https://github.com/openthebox/airflow-triggers/actions/workflows/ci.yml/badge.svg)](https://github.com/openthebox/airflow-triggers/actions/workflows/ci.yml)
[![PyPI version](https://img.shields.io/pypi/v/otb-airflow-triggers)](https://pypi.org/project/otb-airflow-triggers/)
[![Python 3.12+](https://img.shields.io/badge/python-3.12%2B-blue)](https://github.com/openthebox/airflow-triggers/blob/main/pyproject.toml)
[![Release status](https://github.com/openthebox/airflow-triggers/actions/workflows/release.yml/badge.svg)](https://github.com/openthebox/airflow-triggers/actions/workflows/release.yml)

## Introduction

This repository contains custom openthebox Airflow triggers for the
[openthebox Airflow repository](https://github.com/openthebox/airflow-dag).

The triggers must be available to the Airflow triggerer service as well as the worker pods.
They therefore need a separate package that each service can install independently.
Code available only in worker pods cannot run in the triggerer service.

## Triggers

| Trigger                                                            | Description                                        |
| ------------------------------------------------------------------ | -------------------------------------------------- |
| [`aws.ec2.CommandTrigger`](src/otb_airflow_triggers/aws/README.md) | Monitor an SSM command running on an EC2 instance. |

## AWS permissions

Grant these permissions to the AWS identity that the triggerer uses.

| Permission                        | Purpose                                                | Triggers         |
| --------------------------------- | ------------------------------------------------------ | ---------------- |
| `ssm:ListCommandInvocations`      | Read command status and document step results.         | `CommandTrigger` |
| `ssm:GetCommandInvocation`        | Read failure output and exit codes.                    | `CommandTrigger` |
| `ssm:DescribeInstanceInformation` | Check the SSM agent connection.                        | `CommandTrigger` |
| `ec2:DescribeInstanceStatus`      | Read instance state and EC2 health results.            | `CommandTrigger` |
| `ec2:StopInstances`               | Stop an instance after repeated health failures.       | `CommandTrigger` |
| `logs:DescribeLogStreams`         | Find command output streams in CloudWatch.             | `CommandTrigger` |
| `logs:GetLogEvents`               | Read standard output and error output from CloudWatch. | `CommandTrigger` |

## Installation & usage

Use Python 3.12 or later.
Install the same package version in the Airflow triggerer and worker environments.

```bash
python -m pip install otb-airflow-triggers
```

After submitting an SSM command, defer the task with `CommandTrigger` from your custom operator's `execute()` method.
Use the submitted command's ID and the target instance's ID.

```python
from otb_airflow_triggers.aws.ec2 import CommandTrigger

self.defer(
    trigger=CommandTrigger(
        command_id=command_id,
        instance_id=instance_id,
        aws_conn_id="aws_default",
        region_name="eu-west-1",
        waiter_delay=30,
        waiter_max_attempts=120,
    ),
    method_name="execute_complete",
)
```

Implement `execute_complete(self, context, event)` in the operator to handle the result.
Check `event["status"]` and raise `AirflowException` when it is `failed`.
Failure events include a `message` with the reason.
See the [CommandTrigger documentation](src/otb_airflow_triggers/aws/README.md) for configuration and result details.

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for dependencies, local development, GitHub workflows, and deployment.
