Compare commits

..

No commits in common. "main" and "v0.0.5" have entirely different histories.
main ... v0.0.5

3 changed files with 51 additions and 62 deletions

View File

@ -55,7 +55,7 @@ jobs:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@0ab0b79471669eb3a4d647e625009c62f9f3b241
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
@ -106,31 +106,31 @@ jobs:
# '${{ github.ref_name }}' dist/**
# --repo '${{ github.repository }}'
# publish-to-testpypi:
# name: Publish Python 🐍 distribution 📦 to TestPyPI
# needs:
# - build
# runs-on: ubuntu-latest
publish-to-testpypi:
name: Publish Python 🐍 distribution 📦 to TestPyPI
needs:
- build
runs-on: ubuntu-latest
# environment:
# name: testpypi
# url: https://test.pypi.org/p/goat_monitor
environment:
name: testpypi
url: https://test.pypi.org/p/goat_monitor
# permissions:
# id-token: write # IMPORTANT: mandatory for trusted publishing
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
# env:
# RUNNER_TOOL_CACHE: /toolcache # https://about.gitea.com/resources/tutorials/enable-gitea-actions-cache-to-accelerate-cicd
# AGENT_TOOLSDIRECTORY: /toolcache # https://github.com/actions/setup-python/issues/824
env:
RUNNER_TOOL_CACHE: /toolcache # https://about.gitea.com/resources/tutorials/enable-gitea-actions-cache-to-accelerate-cicd
AGENT_TOOLSDIRECTORY: /toolcache # https://github.com/actions/setup-python/issues/824
# steps:
# - name: Download all the dists
# uses: actions/download-artifact@v3
# with:
# name: python-package-distributions
# path: dist/
# - name: Publish distribution 📦 to TestPyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# password: ${{ secrets.TEST_PYPI_API_TOKEN }}
# repository-url: https://test.pypi.org/legacy/
steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository-url: https://test.pypi.org/legacy/

View File

@ -11,6 +11,7 @@ Configuration lives in a TOML file which should have (at a minimum) the followin
```toml
server = "https://gotify.example.com"
app_token = "app_token_from_gotify"
```
## Usage

View File

@ -34,13 +34,7 @@ from goat_monitor._version import __version__ # type: ignore
default=False,
help="Print version and exit",
)
@click.option(
"--dry-run",
is_flag=True,
default=False,
help="Run command without sending any notifications",
)
def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run: bool):
def wrap(command: List[str], config: Path, retries: int, version):
"""Wrap an arbitrary command with gotify notifications"""
if version:
@ -52,14 +46,13 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
settings = toml.load(f)
# gotify configuration
if not dry_run:
url = settings["server"]
app_token = settings["app_token"]
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
# this is just to test configuration.
# I don't know how long a gotify connection will stay up so rather than thinking about it
# I'm just creating a new one whenever I need to send a message
pass
url = settings["server"]
app_token = settings["app_token"]
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
# this is just to test configuration.
# I don't know how long a gotify connection will stay up so rather than thinking about it
# I'm just creating a new one whenever I need to send a message
pass
if retries == -1:
retries = np.inf
@ -69,25 +62,20 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
attempt = 0
while attempt <= retries:
# run the command
with subprocess.Popen(
result = subprocess.run(
" ".join(command),
shell=True,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
) as proc:
stdout = ""
while True:
c = proc.stdout.read(1)
if c == b"":
break
s = c.decode("utf-8")
stdout += s
print(s, end="", flush=True)
return_code = proc.wait()
encoding="utf-8",
)
if return_code:
# TODO: print lines real time as the subprocess runs
print(result.stdout, end="")
if result.returncode:
# failed
title = f"Command failed with exit code {return_code}"
title = f"Command failed with exit code {result.returncode}"
if attempt < retries:
title += f" - Retrying (attempt {attempt+1}/{retries})"
else:
@ -97,26 +85,26 @@ def wrap(command: List[str], config: Path, retries: int, version: bool, dry_run:
title = "Command succeeded"
MAX_LINES = 20
lines = stdout.splitlines()
truncated = False
lines = result.stdout.splitlines()
if len(lines) > MAX_LINES:
truncated = True
lines = lines[-MAX_LINES:]
message = (
"Command:\n" + " ".join(command) + f'\n\nResult{ " (truncated)" if truncated else ""}:\n' + "\n".join(lines)
"Command:\n"
+ " ".join(command)
+ f'\n\nResult{ " (truncated)" if len(lines) > MAX_LINES else ""}:\n'
+ "\n".join(lines)
)
if not dry_run:
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
gotify_connection.create_message(message=message, title=title)
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
gotify_connection.create_message(message=message, title=title)
if not return_code:
if not result.returncode:
# only retry on failure
break
attempt += 1
sys.exit(return_code)
sys.exit(result.returncode)
# %% main