Skip to content

Commit

Permalink
Going full into pyproject.toml
Browse files Browse the repository at this point in the history
Switch all relevant config files to pyproject.toml
Also added in pre-commit checks for black and ruff
Bumped out python version 3.9
Added in dependabot config for actions, since others are largely unpinned
  • Loading branch information
JacobCallahan committed Jul 18, 2023
1 parent 155383c commit 6f1bdee
Show file tree
Hide file tree
Showing 30 changed files with 232 additions and 384 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This is the configuration file for Dependabot. You can find configuration information below.
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# Note: Dependabot has a configurable max open PR limit of 5

version: 2
updates:

# Maintain dependencies for our GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
labels:
- "dependencies"
14 changes: 6 additions & 8 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.10", "3.11"]

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand All @@ -35,14 +35,12 @@ jobs:
uses: github/codeql-action/analyze@v2

- name: Setup Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Run Ruff Checks
uses: chartboost/ruff-action@v1
with:
src: ./broker
- name: Pre Commit Checks
uses: pre-commit/action@v3.0.0

- name: Setup Temp Directory
run: mkdir broker_dir
Expand All @@ -52,7 +50,7 @@ jobs:
BROKER_DIRECTORY: "${{ github.workspace }}/broker_dir"
run: |
pip install -U pip
pip install -U .[test,docker]
pip install -U .[dev,docker]
ls -l "$BROKER_DIRECTORY"
broker --version
pytest -v tests/ --ignore tests/functional
4 changes: 2 additions & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: PythonPackage

on:
push:
tags:
tags:
- "*"

jobs:
Expand All @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
# build/push in lowest support python version
python-version: [ 3.9 ]
python-version: [ 3.10 ]

steps:
- uses: actions/checkout@v2
Expand Down
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# configuration for pre-commit git hooks
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.277
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

20 changes: 5 additions & 15 deletions broker/binds/beaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def _exec_command(self, *cmd_args, **cmd_kwargs):
)
if result.status != 0 and raise_on_error:
raise BeakerBindError(
f"Beaker command failed:\n"
f"Command={' '.join(exec_cmd)}\n"
f"Result={result}",
f"Beaker command failed:\nCommand={' '.join(exec_cmd)}\nResult={result}",
)
logger.debug(f"Beaker command result: {result.stdout}")
return result
Expand All @@ -120,9 +118,7 @@ def job_watch(self, job_id):
def job_results(self, job_id, format="beaker-results-xml", pretty=False):
"""Get the results of a job in the specified format."""
job_id = f"J:{job_id}" if not job_id.startswith("J:") else job_id
return self._exec_command(
"job-results", job_id, format=format, prettyxml=pretty
)
return self._exec_command("job-results", job_id, format=format, prettyxml=pretty)

def job_clone(self, job_id, wait=False, **kwargs):
"""Clone a job by the specified job id."""
Expand Down Expand Up @@ -171,9 +167,7 @@ def system_list(self, **kwargs):
"""
# convert the flags passed in kwargs to arguments
args = [
f"--{key}"
for key in ("available", "free", "removed", "mine")
if kwargs.pop(key, False)
f"--{key}" for key in ("available", "free", "removed", "mine") if kwargs.pop(key, False)
]
return self._exec_command("system-list", *args, **kwargs)

Expand Down Expand Up @@ -207,9 +201,7 @@ def execute_job(self, job, max_wait="24h"):
time.sleep(60)
result = self.job_results(job_id, pretty=True)
if 'result="Pass"' in result.stdout:
return _curate_job_info(
_elementree_to_dict(ET.fromstring(result.stdout))
)
return _curate_job_info(_elementree_to_dict(ET.fromstring(result.stdout)))
elif 'result="Fail"' in result.stdout or "Exception: " in result.stdout:
raise BeakerBindError(f"Job {job_id} failed:\n{result}")
elif 'result="Warn"' in result.stdout:
Expand Down Expand Up @@ -249,8 +241,6 @@ def jobid_from_system(self, system_hostname):
"""Return the job id for the current reservation on the system."""
for job_id in json.loads(self.job_list(mine=True).stdout):
job_result = self.job_results(job_id, pretty=True)
job_detail = _curate_job_info(
_elementree_to_dict(ET.fromstring(job_result.stdout))
)
job_detail = _curate_job_info(_elementree_to_dict(ET.fromstring(job_result.stdout)))
if job_detail["hostname"] == system_hostname:
return job_id
14 changes: 3 additions & 11 deletions broker/binds/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def image_info(self, name):
"id": image.short_id,
"tags": image.tags,
"size": image.attrs["Size"],
"config": {
k: v for k, v in image.attrs["Config"].items() if k != "Env"
},
"config": {k: v for k, v in image.attrs["Config"].items() if k != "Env"},
}

def create_container(self, image, command=None, **kwargs):
Expand All @@ -51,9 +49,7 @@ def create_container(self, image, command=None, **kwargs):

def execute(self, image, command=None, remove=True, **kwargs):
"""Run a container and return the raw result."""
return self.client.containers.run(
image, command=command, remove=remove, **kwargs
).decode()
return self.client.containers.run(image, command=command, remove=remove, **kwargs).decode()

def remove_container(self, container=None):
"""Remove a container from the container host."""
Expand Down Expand Up @@ -102,11 +98,7 @@ def __init__(self, **kwargs):
if self.host == "localhost":
self.uri = "unix:///run/user/1000/podman/podman.sock"
else:
self.uri = (
"http+ssh://{username}@{host}:{port}/run/podman/podman.sock".format(
**kwargs
)
)
self.uri = "http+ssh://{username}@{host}:{port}/run/podman/podman.sock".format(**kwargs)

def _sanitize_create_args(self, kwargs):
from podman.domain.containers_create import CreateMixin
Expand Down
31 changes: 8 additions & 23 deletions broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def mp_split(*args, **kwargs):
max_workers_count = self.MAX_WORKERS or count
with self.EXECUTOR(max_workers=max_workers_count) as workers:
completed_futures = as_completed(
workers.submit(self.func, instance, *args, **kwargs)
for _ in range(count)
workers.submit(self.func, instance, *args, **kwargs) for _ in range(count)
)
for f in completed_futures:
results.extend(f.result())
Expand Down Expand Up @@ -122,9 +121,7 @@ def _act(self, provider, method, checkout=False):
}
)
method_obj = getattr(provider_inst, method)
logger.debug(
f"On {provider_inst=} executing {method_obj=} with params {self._kwargs=}."
)
logger.debug(f"On {provider_inst=} executing {method_obj=} with params {self._kwargs=}.")
result = method_obj(**self._kwargs)
logger.debug(f"Action {result=}")
if result and checkout:
Expand Down Expand Up @@ -238,9 +235,7 @@ def checkin(self, sequential=False, host=None, in_context=False):
hosts = [hosts]
if in_context:
hosts = [
host
for host in hosts
if not getattr(host, "_skip_context_checkin", False)
host for host in hosts if not getattr(host, "_skip_context_checkin", False)
]
if not hosts:
logger.debug("Checkin called with no hosts, taking no action")
Expand All @@ -255,9 +250,7 @@ def checkin(self, sequential=False, host=None, in_context=False):
for completed in completed_checkins:
_host = completed.result()
self._hosts = [h for h in self._hosts if h.to_dict() != _host.to_dict()]
logger.debug(
f"Completed checkin process for {_host.hostname or _host.name}"
)
logger.debug(f"Completed checkin process for {_host.hostname or _host.name}")
helpers.update_inventory(remove=[h.hostname for h in hosts])

def _extend(self, host):
Expand Down Expand Up @@ -297,9 +290,7 @@ def extend(self, sequential=False, host=None):
return

with ThreadPoolExecutor(max_workers=1 if sequential else len(hosts)) as workers:
completed_extends = as_completed(
workers.submit(self._extend, _host) for _host in hosts
)
completed_extends = as_completed(workers.submit(self._extend, _host) for _host in hosts)
for completed in completed_extends:
_host = completed.result()
logger.info(f"Completed extend for {_host.hostname or _host.name}")
Expand All @@ -320,9 +311,7 @@ def sync_inventory(provider):
prov_inventory = PROVIDERS[provider](**instance).get_inventory(additional_arg)
curr_inventory = [
hostname if (hostname := host.get("hostname")) else host.get("name")
for host in helpers.load_inventory(
filter=f'@inv._broker_provider == "{provider}"'
)
for host in helpers.load_inventory(filter=f'@inv._broker_provider == "{provider}"')
]
helpers.update_inventory(add=prov_inventory, remove=curr_inventory)

Expand Down Expand Up @@ -390,18 +379,14 @@ def multi_manager(cls, **multi_dict):
all_hosts.extend(broker_inst._hosts)
# run setup on all hosts in parallel
with ThreadPoolExecutor(max_workers=len(all_hosts)) as workers:
completed_setups = as_completed(
workers.submit(host.setup) for host in all_hosts
)
completed_setups = as_completed(workers.submit(host.setup) for host in all_hosts)
for completed in completed_setups:
completed.result()
# yield control to the user
yield {name: broker._hosts for name, broker in broker_instances.items()}
# teardown all hosts in parallel
with ThreadPoolExecutor(max_workers=len(all_hosts)) as workers:
completed_teardowns = as_completed(
workers.submit(host.teardown) for host in all_hosts
)
completed_teardowns = as_completed(workers.submit(host.teardown) for host in all_hosts)
for completed in completed_teardowns:
completed.result()
# checkin all hosts in parallel
Expand Down
49 changes: 13 additions & 36 deletions broker/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ def cli(version):
import requests

latest_version = Version(
requests.get("https://pypi.org/pypi/broker/json", timeout=60).json()[
"info"
]["version"]
requests.get("https://pypi.org/pypi/broker/json", timeout=60).json()["info"][
"version"
]
)
if latest_version > Version(broker_version):
click.secho(
Expand All @@ -187,9 +187,7 @@ def cli(version):
@loggedcli(context_settings={"allow_extra_args": True, "ignore_unknown_options": True})
@click.option("-b", "--background", is_flag=True, help="Run checkout in the background")
@click.option("-n", "--nick", type=str, help="Use a nickname defined in your settings")
@click.option(
"-c", "--count", type=int, help="Number of times broker repeats the checkout"
)
@click.option("-c", "--count", type=int, help="Number of times broker repeats the checkout")
@click.option(
"--args-file",
type=click.Path(exists=True),
Expand Down Expand Up @@ -238,9 +236,7 @@ def providers():
@click.option("-b", "--background", is_flag=True, help="Run checkin in the background")
@click.option("--all", "all_", is_flag=True, help="Select all VMs")
@click.option("--sequential", is_flag=True, help="Run checkins sequentially")
@click.option(
"--filter", type=str, help="Checkin only what matches the specified filter"
)
@click.option("--filter", type=str, help="Checkin only what matches the specified filter")
def checkin(vm, background, all_, sequential, filter):
"""Checkin or "remove" a VM or series of VM broker instances.
Expand All @@ -251,12 +247,7 @@ def checkin(vm, background, all_, sequential, filter):
inventory = helpers.load_inventory(filter=filter)
to_remove = []
for num, host in enumerate(inventory):
if (
str(num) in vm
or host.get("hostname") in vm
or host.get("name") in vm
or all_
):
if str(num) in vm or host.get("hostname") in vm or host.get("name") in vm or all_:
to_remove.append(Broker().reconstruct_host(host))
Broker(hosts=to_remove).checkin(sequential=sequential)

Expand All @@ -268,9 +259,7 @@ def checkin(vm, background, all_, sequential, filter):
type=str,
help="Class-style name of a supported broker provider. (AnsibleTower)",
)
@click.option(
"--filter", type=str, help="Display only what matches the specified filter"
)
@click.option("--filter", type=str, help="Display only what matches the specified filter")
def inventory(details, sync, filter):
"""Get a list of all VMs you've checked out showing hostname and local id.
Expand All @@ -297,9 +286,7 @@ def inventory(details, sync, filter):
@click.option("-b", "--background", is_flag=True, help="Run extend in the background")
@click.option("--all", "all_", is_flag=True, help="Select all VMs")
@click.option("--sequential", is_flag=True, help="Run extends sequentially")
@click.option(
"--filter", type=str, help="Extend only what matches the specified filter"
)
@click.option("--filter", type=str, help="Extend only what matches the specified filter")
@provider_options
def extend(vm, background, all_, sequential, filter, **kwargs):
"""Extend a host's lease time.
Expand All @@ -319,16 +306,10 @@ def extend(vm, background, all_, sequential, filter, **kwargs):

@loggedcli()
@click.argument("vm", type=str, nargs=-1)
@click.option(
"-b", "--background", is_flag=True, help="Run duplicate in the background"
)
@click.option(
"-c", "--count", type=int, help="Number of times broker repeats the duplicate"
)
@click.option("-b", "--background", is_flag=True, help="Run duplicate in the background")
@click.option("-c", "--count", type=int, help="Number of times broker repeats the duplicate")
@click.option("--all", "all_", is_flag=True, help="Select all VMs")
@click.option(
"--filter", type=str, help="Duplicate only what matches the specified filter"
)
@click.option("--filter", type=str, help="Duplicate only what matches the specified filter")
def duplicate(vm, background, count, all_, filter):
"""Duplicate a broker-procured vm.
Expand All @@ -347,17 +328,13 @@ def duplicate(vm, background, count, all_, filter):
broker_inst = Broker(**broker_args)
broker_inst.checkout()
else:
logger.warning(
f"Unable to duplicate {host['hostname']}, no _broker_args found"
)
logger.warning(f"Unable to duplicate {host['hostname']}, no _broker_args found")


@loggedcli(context_settings={"allow_extra_args": True, "ignore_unknown_options": True})
@click.option("-b", "--background", is_flag=True, help="Run execute in the background")
@click.option("--nick", type=str, help="Use a nickname defined in your settings")
@click.option(
"--output-format", "-o", type=click.Choice(["log", "raw", "yaml"]), default="log"
)
@click.option("--output-format", "-o", type=click.Choice(["log", "raw", "yaml"]), default="log")
@click.option(
"--artifacts",
type=click.Choice(["merge", "last"]),
Expand Down
Loading

0 comments on commit 6f1bdee

Please sign in to comment.