|
|
|
@ -17,7 +17,8 @@ from goat_monitor._version import __version__ # type: ignore
|
|
|
|
|
@click.argument("command", nargs=-1, required=False, type=str)
|
|
|
|
|
@click.option(
|
|
|
|
|
"--config",
|
|
|
|
|
default="~/.config/goat_monitor.toml",
|
|
|
|
|
# default="~/.config/goat_monitor.toml",
|
|
|
|
|
required=True,
|
|
|
|
|
type=click.Path(path_type=Path),
|
|
|
|
|
help="Use a .toml configuration file",
|
|
|
|
|
)
|
|
|
|
@ -33,7 +34,13 @@ from goat_monitor._version import __version__ # type: ignore
|
|
|
|
|
default=False,
|
|
|
|
|
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"""
|
|
|
|
|
|
|
|
|
|
if version:
|
|
|
|
@ -45,35 +52,42 @@ def wrap(command: List[str], config: Path, retries: int, version):
|
|
|
|
|
settings = toml.load(f)
|
|
|
|
|
|
|
|
|
|
# gotify configuration
|
|
|
|
|
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 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
|
|
|
|
|
|
|
|
|
|
if retries == -1:
|
|
|
|
|
retries = np.inf
|
|
|
|
|
if retries < 0:
|
|
|
|
|
raise ValueError("Invalid number of retries specified")
|
|
|
|
|
|
|
|
|
|
for attempt in range(retries + 1):
|
|
|
|
|
attempt = 0
|
|
|
|
|
while attempt <= retries:
|
|
|
|
|
# run the command
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
with subprocess.Popen(
|
|
|
|
|
" ".join(command),
|
|
|
|
|
shell=True,
|
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
|
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
|
|
|
|
|
print(result.stdout, end="")
|
|
|
|
|
|
|
|
|
|
if result.returncode:
|
|
|
|
|
if return_code:
|
|
|
|
|
# failed
|
|
|
|
|
title = f"Command failed with exit code {result.returncode}"
|
|
|
|
|
title = f"Command failed with exit code {return_code}"
|
|
|
|
|
if attempt < retries:
|
|
|
|
|
title += f" - Retrying (attempt {attempt+1}/{retries})"
|
|
|
|
|
else:
|
|
|
|
@ -83,7 +97,7 @@ def wrap(command: List[str], config: Path, retries: int, version):
|
|
|
|
|
title = "Command succeeded"
|
|
|
|
|
|
|
|
|
|
MAX_LINES = 20
|
|
|
|
|
lines = result.stdout.splitlines()
|
|
|
|
|
lines = stdout.splitlines()
|
|
|
|
|
if len(lines) > MAX_LINES:
|
|
|
|
|
lines = lines[-MAX_LINES:]
|
|
|
|
|
message = (
|
|
|
|
@ -93,14 +107,17 @@ def wrap(command: List[str], config: Path, retries: int, version):
|
|
|
|
|
+ "\n".join(lines)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with gotify.Gotify(base_url=url, app_token=app_token) as gotify_connection:
|
|
|
|
|
gotify_connection.create_message(message=message, title=title)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
if not result.returncode:
|
|
|
|
|
if not return_code:
|
|
|
|
|
# only retry on failure
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
sys.exit(result.returncode)
|
|
|
|
|
attempt += 1
|
|
|
|
|
|
|
|
|
|
sys.exit(return_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# %% main
|
|
|
|
|