Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Pebble check-failed and check-recovered events #172

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jhack/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,15 @@ def get_notices(unit: str, container_name: str, model: str = None):
return json.loads(JPopen(shlex.split(cmd), text=True).stdout.read())["result"]


def get_checks(unit: str, container_name: str, model: str = None):
_model = f"{model} " if model else ""
cmd = (
f"juju ssh {_model}{unit} curl --unix-socket /charm/containers/{container_name}"
f"/pebble.socket http://localhost/v1/checks"
)
return json.loads(JPopen(shlex.split(cmd), text=True).stdout.read())["result"]


def get_secrets(model: str = None) -> dict:
_model = f"{model} " if model else ""
cmd = f"juju secrets {_model} --format=json"
Expand Down
65 changes: 64 additions & 1 deletion jhack/utils/simulate_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from jhack.conf.conf import check_destructive_commands_allowed
from jhack.helpers import (
JPopen,
get_checks,
get_current_model,
get_notices,
get_secrets,
Expand Down Expand Up @@ -36,6 +37,8 @@
}
_PEBBLE_READY_SUFFIX = "-pebble-ready"
_PEBBLE_CUSTOM_NOTICE_SUFFIX = "-pebble-custom-notice"
_PEBBLE_CHECK_FAILED_SUFFIX = "-pebble-check-failed"
_PEBBLE_CHECK_RECOVERED_SUFFIX = "-pebble-check-recovered"
OPS_DISPATCH = "OPERATOR_DISPATCH"
juju_context_id = "JUJU_CONTEXT_ID"

Expand Down Expand Up @@ -96,6 +99,7 @@ def build_event_env(
operator_dispatch: bool = False,
model: str = None,
glue: str = " ",
check_name: str = None,
):
current_model = get_current_model()
env = {
Expand Down Expand Up @@ -234,12 +238,58 @@ def build_event_env(
exit("aborted.")
notice = existing_notices[i]
else:
exit(f"unit {unit} has no noticed defined on {container_name}")
exit(f"unit {unit} has no notices defined on {container_name}")

env["JUJU_NOTICE_ID"] = notice["id"]
env["JUJU_NOTICE_TYPE"] = notice["type"]
env["JUJU_NOTICE_KEY"] = notice["key"]

if event.endswith((_PEBBLE_CHECK_FAILED_SUFFIX, _PEBBLE_CHECK_RECOVERED_SUFFIX)):
if event.endswith(_PEBBLE_CHECK_FAILED_SUFFIX):
container_name = event[: -len(_PEBBLE_CHECK_FAILED_SUFFIX)]
else:
container_name = event[: -len(_PEBBLE_CHECK_RECOVERED_SUFFIX)]
env["JUJU_WORKLOAD_NAME"] = container_name

checks_list = get_checks(unit, container_name, model=model)
existing_checks = {c["name"]: c for c in checks_list}
if check_name:
check = existing_checks.get(check_name)
if not check:
if existing_checks:
logger.error(
f"check_name {check_name!r} not found. Try with: {list(existing_checks)}."
)
else:
logger.error(f"container {container_name} has no checks!")
exit(
f"check on container {container_name} with name {check_name} not found on {unit}"
)

elif len(checks_list) == 1:
# if we only have one, we can skip the below
check = checks_list[0]

elif len(checks_list) > 0:
# check picker v0.1
print(f"existing checks for {unit}:{container_name}\nID: \t name")
for c_name, c_dict in existing_checks.items():
print(f"{c_name}: \t {c_dict['key']}")
print()

options = set(map(str, existing_checks)) | {
"abort",
}
prompt = "select a check name to fire (or enter 'abort' to exit): "
while (i := input(prompt)) not in options:
pass
if i == "abort":
exit("aborted.")
check = existing_checks[i]
else:
exit(f"unit {unit} has no checks defined on {container_name}")
env["JUJU_PEBBLE_CHECK_NAME"] = check["name"]

if override:
for opt in override:
if "=" not in opt:
Expand Down Expand Up @@ -286,6 +336,7 @@ def _build_command(
relation_remote: str = None,
relation_id: int = None,
notice_id: str = None,
check_name: str = None,
secret_id_or_label: str = None,
operator_dispatch: bool = False,
env_override: List[str] = None,
Expand All @@ -299,6 +350,7 @@ def _build_command(
secret_id_or_label=secret_id_or_label,
override=env_override,
operator_dispatch=operator_dispatch,
check_name=check_name,
)

_model = f"-m {model} " if model else ""
Expand Down Expand Up @@ -351,6 +403,7 @@ def _simulate_event(
relation_remote: str = None,
relation_id: int = None,
notice_id: str = None,
check_name: str = None,
secret_id_or_label: str = None,
operator_dispatch: bool = False,
env_override: List[str] = None,
Expand All @@ -376,6 +429,7 @@ def _simulate_event(
relation_remote=relation_remote,
relation_id=relation_id,
notice_id=notice_id,
check_name=check_name,
secret_id_or_label=secret_id_or_label,
operator_dispatch=operator_dispatch,
env_override=env_override,
Expand Down Expand Up @@ -459,6 +513,14 @@ def simulate_event(
"we might need to disambiguate between them. If you don't pass one, you will"
"be interactively prompted to select one between the possible options.",
),
check_name: str = typer.Option(
None,
help="Name of the check that a `*-pebble-check-failed` event or "
"`*-pebble-check-recovered` event is about. "
"Given that a check event can represent one of multiple possible checks, "
"we might need to disambiguate between them. If you don't pass one, you will"
"be interactively prompted to select one between the possible options.",
),
relation_id: int = typer.Option(
None,
help="ID of the relation that a `*-relation-*` event is about."
Expand Down Expand Up @@ -509,6 +571,7 @@ def simulate_event(
event,
relation_remote=relation_remote,
notice_id=notice_id,
check_name=check_name,
relation_id=relation_id,
secret_id_or_label=secret,
env_override=env_override,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "jhack"
version = "0.4.2.3"
version = "0.4.3.0"
authors = [
{ name = "Pietro Pasotti", email = "pietro.pasotti@canonical.com" }
]
Expand Down