Skip to content

Commit

Permalink
boardwalk (client, server): utcnow deprecations; ruff formatting, lin…
Browse files Browse the repository at this point in the history
…ting (#72)

* boardwalk (client, server): utcnow deprecations; ruff formatting, linting

- Changes out invocations of `datetime.utcnow()` for `datetime.now(UTC)`.
  - boardwalkd's ui_method_sort_events_by_date _would_ have made this a backwards
  incompatible change, but we just forcibly update the tzinfo on the datetime object
  to be UTC, so that existing workspace state can be preserved, and so that prior
  versions of the client can connect. (This might be revisited down the line.)
- Adds W, UP, and DTZ rulesets into the ruff configuration (pycodestyle warnings,
  pyupgrade, flake8-datetimez) and formats accordingly.

Minimum Python version is now 3.11.

Resolves Backblaze SVRENG-315.
Supersedes PR #71.
  • Loading branch information
asullivan-blze authored May 10, 2024
1 parent 6c1352a commit 79e9f4b
Show file tree
Hide file tree
Showing 17 changed files with 414 additions and 565 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/make-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.10'
python-version: '3.11'

#----------------------------------------------
# Install and configure Poetry, or restore from cache
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM python:3.10 AS build
FROM python:3.11 AS build
WORKDIR /build
COPY . .
RUN python3 -m pip install --user pipx \
&& PATH=PATH:/root/.local/bin pipx install poetry \
&& PATH=PATH:/root/.local/bin poetry build

FROM python:3.10-slim
FROM python:3.11-slim
COPY --from=build /build/dist ./dist
ENV DEBIAN_FRONTEND=noninteractive
RUN groupadd -g 1000 not_root && useradd -u 1000 -g 1000 not_root \
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ clean:
src/boardwalkd/__pycache__ \
.boardwalk \
.boardwalkd \
test/boardwalk/__pycache__ \
test/server-client/.boardwalk \
test/server-client/__pycache__ \
.pytest_cache \
.ruff_cache \
|| :
Expand Down
569 changes: 283 additions & 286 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[project]
requires-python = ">=3.11"

[tool.poetry]
name = "boardwalk"
version = "0.8.17"
version = "0.8.18"
description = "Boardwalk is a linear Ansible workflow engine"
readme = "README.md"
authors = [
Expand Down Expand Up @@ -34,7 +37,7 @@ include = [
"Issues" = "https://github.com/Backblaze/boardwalk/issues"

[tool.poetry.dependencies]
python = ">=3.10,<4"
python = ">=3.11,<4"
ansible-runner = ">=2.3.0"
click = ">=8.1.3"
cryptography = ">=38.0.3"
Expand All @@ -53,11 +56,16 @@ boardwalk = "boardwalk.cli:cli"
boardwalkd = "boardwalkd.cli:cli"

[tool.ruff]
line-length = 120

extend-exclude = [
"typings/*",
]
lint.extend-select = [
"I",
"I", # isort (import sorting)
"W", # pycodestyle warnings
"UP", # pyupgrade
"DTZ", # flake8-datetimez
]

[tool.pyright]
Expand Down
18 changes: 6 additions & 12 deletions src/boardwalk/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from boardwalk.app_exceptions import BoardwalkException

if TYPE_CHECKING:
from typing import Any, Optional, TypedDict
from typing import Any, TypedDict

from ansible_runner import Runner

Expand All @@ -37,10 +37,10 @@ class InventoryMetaData(TypedDict):

class RunnerKwargs(TypedDict, total=False):
cancel_callback: partial[bool]
cmdline: Optional[str]
cmdline: str | None
envvars: RunnerKwargsEnvvars
fact_cache_type: str
limit: Optional[str]
limit: str | None
passwords: dict[str, str | None]
project_dir: str
quiet: bool
Expand Down Expand Up @@ -86,19 +86,15 @@ def ansible_runner_errors_to_output(runner: Runner, include_msg: bool = True) ->
or event["event"] == "runner_item_on_failed"
or event["event"] == "runner_on_async_failed"
or event["event"] == "runner_on_unreachable"
) and not (
"ignore_errors" in event["event_data"]
and event["event_data"]["ignore_errors"]
):
) and not ("ignore_errors" in event["event_data"] and event["event_data"]["ignore_errors"]):
msg: list[str] = [
event["event"],
event["event_data"]["task"],
event["event_data"]["task_action"],
event["event_data"]["host"],
]
if include_msg and not (
"_ansible_no_log" in event["event_data"]["res"]
and event["event_data"]["res"]["_ansible_no_log"]
"_ansible_no_log" in event["event_data"]["res"] and event["event_data"]["res"]["_ansible_no_log"]
):
try:
msg.append(event["event_data"]["res"]["msg"])
Expand Down Expand Up @@ -134,9 +130,7 @@ def ansible_runner_run_tasks(
"cancel_callback": partial(ansible_runner_cancel_callback, workspace),
"envvars": {
"ANSIBLE_BECOME_ASK_PASS": "False" if become_password is None else "True",
"ANSIBLE_CACHE_PLUGIN_CONNECTION": str(
workspace.path.joinpath("fact_cache")
),
"ANSIBLE_CACHE_PLUGIN_CONNECTION": str(workspace.path.joinpath("fact_cache")),
"ANSIBLE_CACHE_PLUGIN": "community.general.pickle",
"ANSIBLE_NOCOLOR": "True",
},
Expand Down
4 changes: 1 addition & 3 deletions src/boardwalk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def cli(ctx: click.Context, debug: bool | Literal[0, 1]):
except KeyError:
pass
except ValueError:
raise BoardwalkException(
"BOARDWALK_DEBUG env variable has an invalid boolean value"
)
raise BoardwalkException("BOARDWALK_DEBUG env variable has an invalid boolean value")

if debug:
loglevel = logging.DEBUG
Expand Down
9 changes: 2 additions & 7 deletions src/boardwalk/cli_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ def add_gathered_facts_to_state(event: RunnerEvent, ws: Workspace):
if event["event"] == "runner_on_ok" and event["event_data"]["task"] == "setup":
# If the host is already in the statefile, just update record
if event["event_data"]["host"] in ws.state.hosts:
ws.state.hosts[event["event_data"]["host"]].ansible_facts = event[
"event_data"
]["res"]["ansible_facts"]
ws.state.hosts[event["event_data"]["host"]].ansible_facts = event["event_data"]["res"]["ansible_facts"]
# Otherwise, add the host to the state as a new host
else:
ws.state.hosts[event["event_data"]["host"]] = Host(
Expand All @@ -165,10 +163,7 @@ def handle_failed_init_hosts(event: RunnerEvent, retry_file_path: Path):
"""Processes runner events to find failed hosts during init. Saves any failed
or unreachable hosts to a retry file and writes warnings and errors to stdout"""
# Save any unreachable/failed hosts to the retry file
if (
event["event"] == "runner_on_unreachable"
or event["event"] == "runner_on_failed"
):
if event["event"] == "runner_on_unreachable" or event["event"] == "runner_on_failed":
logger.warn(event["stdout"])
with open(retry_file_path, "a") as file:
file.write(f"{event['event_data']['host']}\n")
Expand Down
Loading

0 comments on commit 79e9f4b

Please sign in to comment.