Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTube committed Jul 3, 2024
0 parents commit 2d98c36
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/deploy-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Create and publish a Docker image

on:
push:
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
attestations: write
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM alpine:latest

RUN apk add --update --no-cache python3 py3-aiohttp

WORKDIR /app
COPY truenas-gotify.py ./

CMD ["/usr/bin/env", "python3", "truenas-gotify.py"]
68 changes: 68 additions & 0 deletions truenas-gotify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
import os
import sys
from aiohttp import web
from aiohttp import ClientSession

# The url the alerts should be forwarded to.
# Format: http[s]://{host}:{port}/
GOTIFY_BASEURL = os.environ.get("GOTIFY_URL")
# The token for the gotify application
# Example: cGVla2Fib29v
GOTIFY_TOKEN = os.environ.get("GOTIFY_TOKEN")


routes = web.RouteTableDef()

# Listen to post requests on / and /message
@routes.post("/")
@routes.post("/message")
async def on_message(request):
content = await request.json()
# The content of the alert message
message = content["text"]
print("===== Alert =====")
print(message)

# Forward the alert to gotify
gotify_resp = await send_gotify_message(message, GOTIFY_TOKEN)

# Return the gotify status code to truenas
return web.Response(status=gotify_resp.status)

# Send an arbitrary alert to gotify
async def send_gotify_message(message, token, title=None, priority=None):
# URL parameters
params = {"token": token}
# POST body
json = {"message": message}

# Optional gotify features
if title:
json["title"] = title
if priority:
json["priority"] = priority

async with ClientSession() as session:
async with session.post(GOTIFY_BASEURL, params=params, json=json) as resp:
return resp


if __name__ == "__main__":
# Check if env variable is set
if GOTIFY_BASEURL == None:
sys.exit("Set Gotify Endpoint via 'GOTIFY_URL=http[s]://{host}:{port}/'!")
if GOTIFY_TOKEN == None:
sys.exit("Set Gotify App Token via 'GOTIFY_TOKEN={token}'!")

# Add /message to the url
if not "message" in GOTIFY_BASEURL:
if not GOTIFY_BASEURL[-1] == "/":
GOTIFY_BASEURL += "/"
GOTIFY_BASEURL += "message"


# Listen on default port
app = web.Application()
app.add_routes(routes)
web.run_app(app)

0 comments on commit 2d98c36

Please sign in to comment.