Skip to content

Commit

Permalink
Add tox-pylint linters, move tox to the project root, update pre-comm…
Browse files Browse the repository at this point in the history
…it (#158)

* Add tox-pylint linters, move tox to the project root

* d

* Solve systemdlib issues
  • Loading branch information
Alex-Izquierdo authored Aug 11, 2023
1 parent 82f8c6b commit a31b892
Show file tree
Hide file tree
Showing 28 changed files with 145 additions and 110 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/tox.ini

This file was deleted.

14 changes: 10 additions & 4 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ on:
# change in any dependency.
schedule:
- cron: '0 */8 * * *'
# Run on demand
workflow_dispatch:
jobs:
tox:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install package dependencies
run: |
sudo apt-get update
sudo apt-get --assume-yes --no-install-recommends install libsystemd0 libsystemd-dev pkg-config
- name: Install deps
run: python -m pip install tox

- name: Move to tox conf file and run tox
run: |
cd .github/workflows
python -m tox -- ../..
- name: Run tox
run: |
python -m tox
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ venv
# tests
tests/output
.pytest_cache/
.tox
.ruff*

# Ide's
.vscode
Expand Down
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=black
20 changes: 14 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
language_version: python3

- repo: local
hooks:
- id: ansible-test-sanity
Expand All @@ -8,10 +15,11 @@ repos:
language: python
additional_dependencies:
- ansible
pass_filenames: false

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
language_version: python3
- id: tox
name: Run tox
entry: tox
language: python
require_serial: true
pass_filenames: false
6 changes: 3 additions & 3 deletions demos/dynatrace-demo/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ rulebook CLI runs a remedy playbook to restart the process.
2. Install Dynatrace OneAgent. Host `lamp` then should appear in Dynatrace
console
3. Have python3 installed
4. Copy `fake_app.py` under path `/root/fake-app`. Run `python3 fake_app.py`.
`fake_app.py` is a web app with two GET endpoints running on port 5080.
`<lamp ip>:5080/health` will return `{"status": "RUNNING"}`.
4. Copy `fake_app.py` under path `/root/fake-app`. Run `python3 fake_app.py`.
`fake_app.py` is a web app with two GET endpoints running on port 5080.
`<lamp ip>:5080/health` will return `{"status": "RUNNING"}`.
`<lamp ip>:5080/down` will force the app to exit, simulating a process
crashing.
5. Make the client node ansible accessible from the rulebook node.
Expand Down
2 changes: 1 addition & 1 deletion demos/dynatrace-demo/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
- name: Restart main
shell: "(cd /root/fake-app; python3 fake_app.py >/dev/null 2>&1 &)"
async: 10
poll: 0
poll: 0
2 changes: 0 additions & 2 deletions extensions/eda/plugins/event_filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@
Event filters are plugins that filter events once they have been received.
Examples of these plugins are filters that remove unnecessary fields from events,
or change dashes to underscores in key names.


21 changes: 10 additions & 11 deletions extensions/eda/plugins/event_filter/dashes_to_underscores.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ def main(event: dict, overwrite: bool = True) -> dict: # noqa: FBT001, FBT002
"""Change dashes in keys to underscores."""
logger = mp.get_logger()
logger.info("dashes_to_underscores")
q = []
q.append(event)
while q:
o = q.pop()
if isinstance(o, dict):
for key in list(o.keys()):
value = o[key]
q.append(value)
queue = [event]
while queue:
obj = queue.pop()
if isinstance(obj, dict):
for key in list(obj.keys()):
value = obj[key]
queue.append(value)
if "-" in key:
new_key = key.replace("-", "_")
del o[key]
if (new_key in o and overwrite) or (new_key not in o):
o[new_key] = value
del obj[key]
if (new_key in obj and overwrite) or (new_key not in obj):
obj[new_key] = value
logger.info("Replacing %s with %s", key, new_key)

return event
6 changes: 3 additions & 3 deletions extensions/eda/plugins/event_filter/insert_hosts_to_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def main(
if isinstance(hosts, str):
hosts = hosts.split(host_separator) if host_separator else [hosts]
elif isinstance(hosts, (list, tuple)): # noqa: UP038
for h in hosts:
if not isinstance(h, str):
msg = f"{h} is not a valid hostname"
for host in hosts:
if not isinstance(host, str):
msg = f"{host} is not a valid hostname"
raise TypeError(msg)
else:
msg = f"{hosts} is not a valid hostname"
Expand Down
33 changes: 18 additions & 15 deletions extensions/eda/plugins/event_filter/json_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import fnmatch


def _matches_include_keys(include_keys: list, s: str) -> bool:
return any(fnmatch.fnmatch(s, pattern) for pattern in include_keys)
def _matches_include_keys(include_keys: list, string: str) -> bool:
return any(fnmatch.fnmatch(string, pattern) for pattern in include_keys)


def _matches_exclude_keys(exclude_keys: list, s: str) -> bool:
return any(fnmatch.fnmatch(s, pattern) for pattern in exclude_keys)
def _matches_exclude_keys(exclude_keys: list, string: str) -> bool:
return any(fnmatch.fnmatch(string, pattern) for pattern in exclude_keys)


def main(
Expand All @@ -38,17 +38,20 @@ def main(
if include_keys is None:
include_keys = []

q = []
q.append(event)
while q:
o = q.pop()
if isinstance(o, dict):
for i in list(o.keys()):
if (i in include_keys) or _matches_include_keys(include_keys, i):
q.append(o[i])
elif (i in exclude_keys) or _matches_exclude_keys(exclude_keys, i):
del o[i]
queue = []
queue.append(event)
while queue:
obj = queue.pop()
if isinstance(obj, dict):
for item in list(obj.keys()):
if (item in include_keys) or _matches_include_keys(include_keys, item):
queue.append(obj[item])
elif (item in exclude_keys) or _matches_exclude_keys(
exclude_keys,
item,
):
del obj[item]
else:
q.append(o[i])
queue.append(obj[item])

return event
22 changes: 11 additions & 11 deletions extensions/eda/plugins/event_source/aws_cloudtrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def _get_events(events: list[dict], last_event_ids: list) -> list:
event_time = None
event_ids = []
result = []
for e in events:
for event in events:
# skip last event
if last_event_ids and e["EventId"] in last_event_ids:
if last_event_ids and event["EventId"] in last_event_ids:
continue
if event_time is None or event_time < e["EventTime"]:
event_time = e["EventTime"]
event_ids = [e["EventId"]]
elif event_time == e["EventTime"]:
event_ids.append(e["EventId"])
result.append(e)
if event_time is None or event_time < event["EventTime"]:
event_time = event["EventTime"]
event_ids = [event["EventId"]]
elif event_time == event["EventTime"]:
event_ids.append(event["EventId"])
result.append(event)
return result, event_time, event_ids


Expand All @@ -81,9 +81,9 @@ async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:

session = get_session()
params = {}
for k, v in ARGS_MAPPING.items():
if args.get(k) is not None:
params[v] = args.get(k)
for key, value in ARGS_MAPPING.items():
if args.get(key) is not None:
params[value] = args.get(key)

params["StartTime"] = datetime.utcnow() # noqa: DTZ003

Expand Down
5 changes: 3 additions & 2 deletions extensions/eda/plugins/event_source/aws_sqs_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:
err.response["Error"]["Code"]
== "AWS.SimpleQueueService.NonExistentQueue"
):
raise ValueError("Queue %s does not exist" % queue_name) from None
msg = f"Queue {queue_name} does not exist"
raise ValueError(msg) from None
raise

queue_url = response["QueueUrl"]
Expand Down Expand Up @@ -103,7 +104,7 @@ def connection_args(args: dict[str, Any]) -> dict[str, Any]:


if __name__ == "__main__":
"""MockQueue if running directly."""
# MockQueue if running directly

class MockQueue:
"""A fake queue."""
Expand Down
7 changes: 5 additions & 2 deletions extensions/eda/plugins/event_source/azure_service_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
def receive_events(
loop: asyncio.events.AbstractEventLoop,
queue: asyncio.Queue,
args: dict[str, Any],
args: dict[str, Any], # pylint: disable=W0621
) -> None:
"""Receive events from service bus."""
servicebus_client = ServiceBusClient.from_connection_string(
Expand All @@ -52,7 +52,10 @@ def receive_events(
receiver.complete_message(msg)


async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:
async def main(
queue: asyncio.Queue,
args: dict[str, Any], # pylint: disable=W0621
) -> None:
"""Receive events from service bus in a loop."""
loop = asyncio.get_running_loop()

Expand Down
6 changes: 4 additions & 2 deletions extensions/eda/plugins/event_source/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

def send_facts(queue, filename: str) -> None: # noqa: ANN001
"""Send facts to the queue."""
with pathlib.Path(filename).open() as f:
data = yaml.safe_load(f.read())
with pathlib.Path(filename).open(encoding="utf-8") as file:
data = yaml.safe_load(file.read())
if data is None:
return
if isinstance(data, dict):
Expand Down Expand Up @@ -58,6 +58,8 @@ def main(queue, args: dict) -> None: # noqa: ANN001

def _observe_files(queue, files: list[str]) -> None: # noqa: ANN001
class Handler(RegexMatchingEventHandler):
"""A handler for file events."""

def __init__(self: "Handler", **kwargs) -> None: # noqa: ANN003
RegexMatchingEventHandler.__init__(self, **kwargs)

Expand Down
2 changes: 2 additions & 0 deletions extensions/eda/plugins/event_source/file_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def watch(
root_path = args["path"]

class Handler(RegexMatchingEventHandler):
"""A handler for file system events."""

def __init__(self: "Handler", **kwargs: dict) -> None:
RegexMatchingEventHandler.__init__(self, **kwargs)

Expand Down
5 changes: 4 additions & 1 deletion extensions/eda/plugins/event_source/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
from typing import Any


async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:
async def main( # pylint: disable=R0914
queue: asyncio.Queue,
args: dict[str, Any],
) -> None:
"""Insert event data into the queue."""
payload = args.get("payload")
randomize = args.get("randomize", False)
Expand Down
5 changes: 4 additions & 1 deletion extensions/eda/plugins/event_source/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
from aiokafka.helpers import create_ssl_context


async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:
async def main( # pylint: disable=R0914
queue: asyncio.Queue,
args: dict[str, Any],
) -> None:
"""Receive events via a kafka topic."""
logger = logging.getLogger()

Expand Down
2 changes: 1 addition & 1 deletion extensions/eda/plugins/event_source/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:


if __name__ == "__main__":
"""MockQueue if running directly."""
# MockQueue if running directly

class MockQueue:
"""A fake queue."""
Expand Down
4 changes: 2 additions & 2 deletions extensions/eda/plugins/event_source/url_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None:
},
)

except aiohttp.ClientError as e:
client_error = str(e)
except aiohttp.ClientError as exc:
client_error = str(exc)
await queue.put(
{
"url_check": {
Expand Down
6 changes: 3 additions & 3 deletions extensions/eda/plugins/event_source/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ async def webhook(request: web.Request) -> web.Response:
"""Return response to webhook request."""
try:
payload = await request.json()
except json.JSONDecodeError as e:
logger.warning("Wrong body request: failed to decode JSON payload: %s", e)
except json.JSONDecodeError as exc:
logger.warning("Wrong body request: failed to decode JSON payload: %s", exc)
raise web.HTTPBadRequest(text="Invalid JSON payload") from None
endpoint = request.match_info["endpoint"]
headers = dict(request.headers)
Expand All @@ -60,7 +60,7 @@ def _parse_token(request: web.Request) -> (str, str):
async def bearer_auth(request: web.Request, handler: Callable) -> web.StreamResponse:
"""Verify authorization is Bearer type."""
try:
scheme, token = _parse_token(request)
_parse_token(request)
except KeyError:
raise web.HTTPUnauthorized(reason="Missing authorization token") from None
except ValueError:
Expand Down
5 changes: 0 additions & 5 deletions pyproject.toml

This file was deleted.

Loading

0 comments on commit a31b892

Please sign in to comment.