Skip to content

Commit

Permalink
Add onlyifrunning option for Service Beacon
Browse files Browse the repository at this point in the history
With this option it's able to only fire events that match the running state
  • Loading branch information
scornelissen85 committed Aug 21, 2024
1 parent 246d066 commit 9cb4544
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/66809.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support in service Beacon for only fire matching configured running state
14 changes: 14 additions & 0 deletions salt/beacons/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def beacon(config):
event when the minion is reload. Applicable only when `onchangeonly` is True.
The default is True.
`is_running_state`: If `is_running_state` is set to a boolean the beacon will only
fire if the running state matches the given value. If its set to `True`, only
when a service running state is `True` it will be fired, if it's set to `False`
then only the beacon will fire when the running state is `False`.
`uncleanshutdown`: If `uncleanshutdown` is present it should point to the
location of a pid file for the service. Most services will not clean up
this pid file if they are shutdown uncleanly (e.g. via `kill -9`) or if they
Expand Down Expand Up @@ -141,6 +146,15 @@ def beacon(config):
ret_dict[service]["uncleanshutdown"] = (
True if os.path.exists(filename) else False
)

# If is_running_state does not match the current running state, skip it
if (
"is_running_state" in service_config
and isinstance(service_config["is_running_state"], bool)
and service_config["is_running_state"] != ret_dict[service]["running"]
):
continue

if "onchangeonly" in service_config and service_config["onchangeonly"] is True:
if service not in LAST_STATUS:
LAST_STATUS[service] = ret_dict[service]
Expand Down
34 changes: 34 additions & 0 deletions tests/pytests/unit/beacons/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,37 @@ def test_service_not_running():
"salt-master": {"running": False},
}
]


def test_service_matching_is_running_state():
with patch.dict(
service_beacon.__salt__, {"service.status": MagicMock(return_value=False)}
):
config = [{"services": {"salt-master": {"is_running_state": True}}}]

ret = service_beacon.validate(config)

assert ret == (True, "Valid beacon configuration")

ret = service_beacon.beacon(config)

assert ret == []

config = [
{
"services": {
"salt-master": {"is_running_state": False},
"salt-minion": {"is_running_state": True},
}
}
]

ret = service_beacon.beacon(config)

assert ret == [
{
"service_name": "salt-master",
"tag": "salt-master",
"salt-master": {"running": False},
}
]

0 comments on commit 9cb4544

Please sign in to comment.