Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
933c488444 | |||
0242b4f541 | |||
0954c2322a | |||
8c1e9751af | |||
da2c453430 | |||
2668a880ee | |||
5891409fdb | |||
0e9eba4900 |
50
.github/workflows/python_publish.yml
vendored
50
.github/workflows/python_publish.yml
vendored
@ -55,7 +55,7 @@ jobs:
|
|||||||
name: python-package-distributions
|
name: python-package-distributions
|
||||||
path: dist/
|
path: dist/
|
||||||
- name: Publish distribution 📦 to PyPI
|
- name: Publish distribution 📦 to PyPI
|
||||||
uses: pypa/gh-action-pypi-publish@release/v1
|
uses: pypa/gh-action-pypi-publish@0ab0b79471669eb3a4d647e625009c62f9f3b241
|
||||||
with:
|
with:
|
||||||
password: ${{ secrets.PYPI_API_TOKEN }}
|
password: ${{ secrets.PYPI_API_TOKEN }}
|
||||||
|
|
||||||
@ -106,31 +106,31 @@ jobs:
|
|||||||
# '${{ github.ref_name }}' dist/**
|
# '${{ github.ref_name }}' dist/**
|
||||||
# --repo '${{ github.repository }}'
|
# --repo '${{ github.repository }}'
|
||||||
|
|
||||||
publish-to-testpypi:
|
# publish-to-testpypi:
|
||||||
name: Publish Python 🐍 distribution 📦 to TestPyPI
|
# name: Publish Python 🐍 distribution 📦 to TestPyPI
|
||||||
needs:
|
# needs:
|
||||||
- build
|
# - build
|
||||||
runs-on: ubuntu-latest
|
# runs-on: ubuntu-latest
|
||||||
|
|
||||||
environment:
|
# environment:
|
||||||
name: testpypi
|
# name: testpypi
|
||||||
url: https://test.pypi.org/p/goat_monitor
|
# url: https://test.pypi.org/p/goat_monitor
|
||||||
|
|
||||||
permissions:
|
# permissions:
|
||||||
id-token: write # IMPORTANT: mandatory for trusted publishing
|
# id-token: write # IMPORTANT: mandatory for trusted publishing
|
||||||
|
|
||||||
env:
|
# env:
|
||||||
RUNNER_TOOL_CACHE: /toolcache # https://about.gitea.com/resources/tutorials/enable-gitea-actions-cache-to-accelerate-cicd
|
# 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
|
# AGENT_TOOLSDIRECTORY: /toolcache # https://github.com/actions/setup-python/issues/824
|
||||||
|
|
||||||
steps:
|
# steps:
|
||||||
- name: Download all the dists
|
# - name: Download all the dists
|
||||||
uses: actions/download-artifact@v3
|
# uses: actions/download-artifact@v3
|
||||||
with:
|
# with:
|
||||||
name: python-package-distributions
|
# name: python-package-distributions
|
||||||
path: dist/
|
# path: dist/
|
||||||
- name: Publish distribution 📦 to TestPyPI
|
# - name: Publish distribution 📦 to TestPyPI
|
||||||
uses: pypa/gh-action-pypi-publish@release/v1
|
# uses: pypa/gh-action-pypi-publish@release/v1
|
||||||
with:
|
# with:
|
||||||
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
# password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
||||||
repository-url: https://test.pypi.org/legacy/
|
# repository-url: https://test.pypi.org/legacy/
|
@ -11,7 +11,6 @@ Configuration lives in a TOML file which should have (at a minimum) the followin
|
|||||||
```toml
|
```toml
|
||||||
server = "https://gotify.example.com"
|
server = "https://gotify.example.com"
|
||||||
app_token = "app_token_from_gotify"
|
app_token = "app_token_from_gotify"
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
@ -34,7 +34,13 @@ from goat_monitor._version import __version__ # type: ignore
|
|||||||
default=False,
|
default=False,
|
||||||
help="Print version and exit",
|
help="Print version and exit",
|
||||||
)
|
)
|
||||||
def wrap(command: List[str], config: Path, retries: int, version):
|
@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):
|
||||||
"""Wrap an arbitrary command with gotify notifications"""
|
"""Wrap an arbitrary command with gotify notifications"""
|
||||||
|
|
||||||
if version:
|
if version:
|
||||||
@ -46,13 +52,14 @@ def wrap(command: List[str], config: Path, retries: int, version):
|
|||||||
settings = toml.load(f)
|
settings = toml.load(f)
|
||||||
|
|
||||||
# gotify configuration
|
# gotify configuration
|
||||||
url = settings["server"]
|
if not dry_run:
|
||||||
app_token = settings["app_token"]
|
url = settings["server"]
|
||||||
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
|
app_token = settings["app_token"]
|
||||||
# this is just to test configuration.
|
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
|
||||||
# I don't know how long a gotify connection will stay up so rather than thinking about it
|
# this is just to test configuration.
|
||||||
# I'm just creating a new one whenever I need to send a message
|
# I don't know how long a gotify connection will stay up so rather than thinking about it
|
||||||
pass
|
# I'm just creating a new one whenever I need to send a message
|
||||||
|
pass
|
||||||
|
|
||||||
if retries == -1:
|
if retries == -1:
|
||||||
retries = np.inf
|
retries = np.inf
|
||||||
@ -62,20 +69,25 @@ def wrap(command: List[str], config: Path, retries: int, version):
|
|||||||
attempt = 0
|
attempt = 0
|
||||||
while attempt <= retries:
|
while attempt <= retries:
|
||||||
# run the command
|
# run the command
|
||||||
result = subprocess.run(
|
with subprocess.Popen(
|
||||||
" ".join(command),
|
" ".join(command),
|
||||||
shell=True,
|
shell=True,
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
encoding="utf-8",
|
) 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()
|
||||||
|
|
||||||
# TODO: print lines real time as the subprocess runs
|
if return_code:
|
||||||
print(result.stdout, end="")
|
|
||||||
|
|
||||||
if result.returncode:
|
|
||||||
# failed
|
# failed
|
||||||
title = f"Command failed with exit code {result.returncode}"
|
title = f"Command failed with exit code {return_code}"
|
||||||
if attempt < retries:
|
if attempt < retries:
|
||||||
title += f" - Retrying (attempt {attempt+1}/{retries})"
|
title += f" - Retrying (attempt {attempt+1}/{retries})"
|
||||||
else:
|
else:
|
||||||
@ -85,26 +97,26 @@ def wrap(command: List[str], config: Path, retries: int, version):
|
|||||||
title = "Command succeeded"
|
title = "Command succeeded"
|
||||||
|
|
||||||
MAX_LINES = 20
|
MAX_LINES = 20
|
||||||
lines = result.stdout.splitlines()
|
lines = stdout.splitlines()
|
||||||
|
truncated = False
|
||||||
if len(lines) > MAX_LINES:
|
if len(lines) > MAX_LINES:
|
||||||
|
truncated = True
|
||||||
lines = lines[-MAX_LINES:]
|
lines = lines[-MAX_LINES:]
|
||||||
message = (
|
message = (
|
||||||
"Command:\n"
|
"Command:\n" + " ".join(command) + f'\n\nResult{ " (truncated)" if truncated else ""}:\n' + "\n".join(lines)
|
||||||
+ " ".join(command)
|
|
||||||
+ f'\n\nResult{ " (truncated)" if len(lines) > MAX_LINES else ""}:\n'
|
|
||||||
+ "\n".join(lines)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
|
if not dry_run:
|
||||||
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 result.returncode:
|
if not return_code:
|
||||||
# only retry on failure
|
# only retry on failure
|
||||||
break
|
break
|
||||||
|
|
||||||
attempt += 1
|
attempt += 1
|
||||||
|
|
||||||
sys.exit(result.returncode)
|
sys.exit(return_code)
|
||||||
|
|
||||||
|
|
||||||
# %% main
|
# %% main
|
||||||
|
Loading…
x
Reference in New Issue
Block a user