Skip to content

Commit

Permalink
update codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
zagy committed Apr 24, 2023
1 parent 46986df commit 7819bdc
Show file tree
Hide file tree
Showing 32 changed files with 722 additions and 479 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: pre-commit

on:
pull_request:
push:
branches: [master]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: pre-commit/[email protected]
47 changes: 27 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
exclude: ^secrets/|^appenv$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.10.0
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/pre-commit/mirrors-yapf
rev: 'v0.31.0' # Use the sha / tag you want to point at
hooks:
- id: yapf
args: [-i, -p]
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
- hooks:
- exclude: "(?x)^(\n secrets/|environments/.*/secret.*|\n .*\\.patch\n)$\n"
id: trailing-whitespace
- exclude: "(?x)^(\n environments/.*/secret.*|\n .*\\.patch\n)$\n"
id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-json
- id: check-xml
- id: check-toml
- id: detect-private-key
repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
- hooks:
- args:
- --profile
- black
- --filter-files
id: isort
name: isort (python)
repo: https://github.com/pycqa/isort
rev: 5.12.0
- hooks:
- id: black
repo: https://github.com/psf/black
rev: 22.3.0
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tool.black]
line-length = 80
target-version = ['py36', 'py37', 'py38']

[tool.isort]
profile = "black"
line_length = 80
28 changes: 20 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ def project_path(*names):
"batou >= 2.3b4",
"pyaml",
"setuptools",
"six", ],
"six",
],
extras_require={
"test": [
"boto3",
"passlib>=1.7",
"pytest",
"pytest-mock", ], },
"pytest-mock",
],
},
author="Christian Theune <[email protected]>",
author_email="[email protected]",
license="BSD (2-clause)",
Expand All @@ -39,20 +42,29 @@ def project_path(*names):
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3 :: Only
"""[:-1].split("\n"),
"""[
:-1
].split(
"\n"
),
description=__doc__.strip(),
long_description="\n\n".join(
open(project_path(name)).read() for name in (
open(project_path(name)).read()
for name in (
"README.md",
"CHANGES.txt",
"HACKING.txt",
)),
)
),
packages=find_packages("src"),
package_dir={"": "src"},
include_package_data=True,
data_files=[("", glob.glob(project_path("*.txt")))],
entry_points=dict(console_scripts=[
"jenkins = batou_ext.jenkins:main",
"fcio = batou_ext.fcio:main", ]),
entry_points=dict(
console_scripts=[
"jenkins = batou_ext.jenkins:main",
"fcio = batou_ext.fcio:main",
]
),
zip_safe=False,
)
9 changes: 5 additions & 4 deletions src/batou_ext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class RegexPatch(batou.component.Component):
"""

_required_params_ = {
"pattern": "pattern", }
"pattern": "pattern",
}
path = None
namevar = "path"

Expand Down Expand Up @@ -82,14 +83,14 @@ def verify(self):

m = self.pattern.search(self._source_data)
assert m, "Could not configure, no match for pattern: {}".format(
self.pattern.pattern)
self.pattern.pattern
)
if self._target_data != self._patch():
raise batou.UpdateNeeded()

def update(self):
if not os.path.exists(self.source):
raise IOError("The file to be patched does not exist:",
self.source)
raise IOError("The file to be patched does not exist:", self.source)

if self._source_data is None:
raise RuntimeError(
Expand Down
31 changes: 20 additions & 11 deletions src/batou_ext/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,21 @@ class CronJob(batou.component.Component):
args = "" # to satisfy sorting

def format(self):
return self.expand("""\
return self.expand(
"""\
{{component.timing}} \
timeout {{component.timeout}} \
{{component.wrapped_command}} \
>> {{component.log_file}} 2>&1""")
>> {{component.log_file}} 2>&1"""
)

def configure(self):
self.provide(batou.lib.cron.CronJob.key, self)

if self.timing is None:
raise ValueError(
"Required timing value missing from cron job %r." %
self.command)
"Required timing value missing from cron job %r." % self.command
)

self += batou.lib.file.File("logs", ensure="directory")
self.log_file = self.map(self.expand("logs/{{component.tag}}.log"))
Expand All @@ -85,8 +87,9 @@ def configure(self):

self += batou.lib.file.File(
self.expand("{{component.tag}}.sh"),
content=pkg_resources.resource_string(__name__,
"resources/cron-wrapper.sh"),
content=pkg_resources.resource_string(
__name__, "resources/cron-wrapper.sh"
),
mode=0o755,
)
self.wrapped_command = self._.path
Expand Down Expand Up @@ -140,7 +143,8 @@ def configure(self):
self += batou.lib.file.File(
self.expand("{{component.tag}}.sh"),
mode=0o755,
content=dedent(f"""\
content=dedent(
f"""\
#!/bin/sh
# This file is generated by batou and run by systemd
Expand All @@ -149,13 +153,16 @@ def configure(self):
source /etc/profile
set -exo pipefail
""") + self.command,
"""
)
+ self.command,
)
self.wrapped_command = self._.path

self += batou.lib.file.File(
f"/etc/local/nixos/timer-{self.tag}.nix",
content=dedent("""\
content=dedent(
"""\
{ ... }:
{
systemd.timers."{{component.tag}}" = {
Expand All @@ -176,13 +183,15 @@ def configure(self):
};
};
}
"""),
"""
),
)

self += batou.lib.file.File(
self.expand("check_systemd_unit.py"),
content=pkg_resources.resource_string(
__name__, "resources/check_systemd_unit.py"),
__name__, "resources/check_systemd_unit.py"
),
mode=0o755,
)
self.check_command = self._.path
Expand Down
64 changes: 37 additions & 27 deletions src/batou_ext/fcio.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,31 @@ def _compute_calls(self):
def _call(self):
api = xmlrpc.client.ServerProxy(
"https://{s.project}:{s.api_key}@api.flyingcircus.io/v1".format(
s=self))
s=self
)
)
api.apply(self.calls)

def _add_calls(self, hostname, interface, aliases_str):
if not aliases_str:
return
aliases = aliases_str.split()
aliases.sort()
self.calls.append({
"__type__": "virtualmachine",
"name": hostname + self.postfix,
"aliases_" + interface: aliases, })
self.calls.append(
{
"__type__": "virtualmachine",
"name": hostname + self.postfix,
"aliases_" + interface: aliases,
}
)
self.aliases.extend(aliases)

def _wait_for_aliases(self):
if not self.wait_for_aliases:
return
batou.output.line("Waiting up to %s seconds for aliases." %
self.wait_for_aliases)
batou.output.line(
"Waiting up to %s seconds for aliases." % self.wait_for_aliases
)
started = time.time()
error = True
while started + self.wait_for_aliases > time.time():
Expand All @@ -119,15 +125,15 @@ def _check_aliases(self):
for alias in self.aliases:
fqdn = "{}{}.{}.fcio.net".format(alias, self.postfix, self.project)
try:
addrs = socket.getaddrinfo(fqdn, None, 0, 0,
socket.IPPROTO_TCP)
addrs = socket.getaddrinfo(fqdn, None, 0, 0, socket.IPPROTO_TCP)
except socket.gaierror as e:
result = str(e)
error = True
else:
result = ", ".join(sockaddr[0]
for (family, type, proto, canonname,
sockaddr) in addrs)
result = ", ".join(
sockaddr[0]
for (family, type, proto, canonname, sockaddr) in addrs
)
results.append("{}: {}".format(fqdn, result))
return error, results

Expand Down Expand Up @@ -158,8 +164,7 @@ def load_env(self):
environment.load_secrets()
if environment.exceptions:
# Yeah, this is awkward.
batou.output = batou._output.Output(
batou._output.TerminalBackend())
batou.output = batou._output.Output(batou._output.TerminalBackend())
for exc in environment.exceptions:
exc.report()
sys.exit(1)
Expand All @@ -172,7 +177,8 @@ def get_api(self):
if not api_url:
api_url = self.api_url
api = xmlrpc.client.ServerProxy(
api_url.format(project=rg_name, api_key=api_key))
api_url.format(project=rg_name, api_key=api_key)
)
return api

def get_currently_provisioned_vms(self):
Expand All @@ -198,7 +204,8 @@ def config(name):

if d.get("environment", config("vm_environment")) is None:
raise ValueError(
"'environment' for {} must be set.".format(name))
"'environment' for {} must be set.".format(name)
)

call = dict(
__type__="virtualmachine",
Expand All @@ -209,8 +216,9 @@ def config(name):
name=host.name,
classes=classes,
resource_group=rg_name,
environment_class=d.get("environment_class",
config("vm_environment_class")),
environment_class=d.get(
"environment_class", config("vm_environment_class")
),
environment=d.get("environment", config("vm_environment")),
location=config("location"),
rbd_pool=d.get("rbdpool", "rbd.hdd"),
Expand All @@ -235,15 +243,17 @@ def alias(interface):
diff = self.get_diff(self.get_currently_provisioned_vms(), vms)
print(
"Applying the configuration to {env} would yield the following"
" changes:\n".format(env=self.env_name))
" changes:\n".format(env=self.env_name)
)

for vm, changes in sorted(diff.items()):
if changes:
print(vm)
for key, (old, new) in sorted(changes.items()):
if old or new:
print(" {key:20}: {old} → {new}".format(
**locals()))
print(
" {key:20}: {old} → {new}".format(**locals())
)
else:
print(" {key}".format(**locals()))
else:
Expand Down Expand Up @@ -271,8 +281,7 @@ def get_diff(self, old, new):
result[vm_name] = changes = {}
new_vm = new.get(vm_name)
if not new_vm:
changes["VM exists and is unknown to deployment"] = (None,
None)
changes["VM exists and is unknown to deployment"] = (None, None)
continue
# starting with new because that only includes the data we
# can set. We ignore all the other keys.
Expand All @@ -290,9 +299,9 @@ def get_diff(self, old, new):
if vm_name in result:
continue
result[vm_name] = {"CREATE VM": (None, None)}
result[vm_name].update({
key: (None, value)
for key, value in list(new_vm.items())})
result[vm_name].update(
{key: (None, value) for key, value in list(new_vm.items())}
)
return result


Expand All @@ -304,7 +313,8 @@ def main():
p.add_argument("env_name", help="Environment")
p.add_argument("-n", "--dry-run", help="Dry run", action="store_true")
p.add_argument(
"-d", "--diff", help="Show changes in resources", action="store_true")
"-d", "--diff", help="Show changes in resources", action="store_true"
)
p.set_defaults(func=lambda **kw: Provision(**kw).apply())

args = parser.parse_args()
Expand Down
Loading

0 comments on commit 7819bdc

Please sign in to comment.