Skip to content

Commit

Permalink
Simplify pyinvoke tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Apr 26, 2024
1 parent 169ceb4 commit b236028
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import requests
from dotenv import set_key
from invoke.context import Context
from invoke.runners import Result
from invoke.tasks import task
from tqdm import tqdm

Expand Down Expand Up @@ -120,17 +119,6 @@ def download_with_progress_bar(url: str, filepath: Path):
raise RuntimeError("Could not download file")


def run_cmd(ctx: Context, cmd: str, silent=False) -> Result:
if not silent:
print(f"Running: {cmd}")

hide = True if silent else None

result = ctx.run(cmd, pty=True, hide=hide)
assert result and result.ok
return result


###
# Tasks
###
Expand All @@ -140,7 +128,7 @@ def run_cmd(ctx: Context, cmd: str, silent=False) -> Result:
def compose_build(ctx: Context, env: Environments = "dev"):
"""Build RADIS image for specified environment"""
cmd = f"{build_compose_cmd(env)} build"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -156,7 +144,7 @@ def compose_up(
cmd = f"{build_compose_cmd(env)} --profile {profile} up {build_opt} --detach"
if service:
cmd += f" {service}"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -173,7 +161,7 @@ def compose_down(
cmd += " --remove-orphans --volumes"
if service:
cmd += f" {service}"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -182,7 +170,7 @@ def compose_restart(ctx: Context, env: Environments = "dev", service: str | None
cmd = f"{build_compose_cmd(env)} restart"
if service:
cmd += f" {service}"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -207,7 +195,7 @@ def compose_logs(
cmd += f" --until {until}"
if tail:
cmd += f" --tail {tail}"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -226,40 +214,40 @@ def stack_deploy(ctx: Context, env: Environments = "prod", build: bool = False):
raise ValueError(f"Unknown environment: {env}")

cmd = f"docker stack deploy {suffix}"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
def stack_rm(ctx: Context, env: Environments = "prod"):
"""Remove the stack from Docker Swarm (prod by default!)."""
stack_name = get_stack_name(env)
cmd = f"docker stack rm {stack_name}"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
def format(ctx: Context):
"""Format the source code with ruff and djlint"""
# Format Python code
format_code_cmd = "poetry run ruff format ."
run_cmd(ctx, format_code_cmd)
ctx.run(format_code_cmd, pty=True)
# Sort Python imports
sort_imports_cmd = "poetry run ruff check . --fix --select I"
run_cmd(ctx, sort_imports_cmd)
ctx.run(sort_imports_cmd, pty=True)
# Format Django templates
format_templates_cmd = "poetry run djlint . --reformat"
run_cmd(ctx, format_templates_cmd)
ctx.run(format_templates_cmd, pty=True)


@task
def lint(ctx: Context):
"""Lint the source code (ruff, djlint, pyright)"""
cmd_ruff = "poetry run ruff check ."
run_cmd(ctx, cmd_ruff)
ctx.run(cmd_ruff, pty=True)
cmd_djlint = "poetry run djlint . --lint"
run_cmd(ctx, cmd_djlint)
ctx.run(cmd_djlint, pty=True)
cmd_pyright = "poetry run pyright"
run_cmd(ctx, cmd_pyright)
ctx.run(cmd_pyright, pty=True)


@task
Expand Down Expand Up @@ -299,7 +287,7 @@ def test(
cmd += "-x "
if path:
cmd += path
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -314,22 +302,22 @@ def reset_dev(ctx: Context):
"""Reset dev container environment"""
# Wipe the database
flush_cmd = f"{build_compose_cmd('dev')} exec web python manage.py flush --noinput"
run_cmd(ctx, flush_cmd)
ctx.run(flush_cmd, pty=True)
# Re-populate the database with users and groups
populate_cmd = f"{build_compose_cmd('dev')} exec web python manage.py populate_users_and_groups"
populate_cmd += " --users 20 --groups 3"
run_cmd(ctx, populate_cmd)
ctx.run(populate_cmd, pty=True)
# Re-populate the database with example reports
populate_cmd = f"{build_compose_cmd('dev')} exec web python manage.py populate_reports"
populate_cmd += " --report-language de"
run_cmd(ctx, populate_cmd)
ctx.run(populate_cmd, pty=True)


@task
def radis_web_shell(ctx: Context, env: Environments = "dev"):
"""Open Python shell in RADIS web container of specified environment"""
cmd = f"{build_compose_cmd(env)} exec web python manage.py shell_plus"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand Down Expand Up @@ -359,7 +347,7 @@ def modify_env_file(domain: str | None = None):
modify_env_file(codespaces_url)
elif environ.get("GITPOD_WORKSPACE_ID"):
# Inside Gitpod
result = run_cmd(ctx, "gp url 8000", silent=True)
result = ctx.run("gp url 8000", silent=True, pty=True)
assert result and result.ok
gitpod_url = result.stdout.strip().removeprefix("https://")
modify_env_file(gitpod_url)
Expand All @@ -373,18 +361,19 @@ def show_outdated(ctx: Context):
"""Show outdated dependencies"""
print("### Outdated Python dependencies ###")
poetry_cmd = "poetry show --outdated --top-level"
result = run_cmd(ctx, poetry_cmd)
result = ctx.run(poetry_cmd, pty=True)
assert result and result.ok
print(result.stderr.strip())

print("### Outdated NPM dependencies ###")
npm_cmd = "npm outdated"
run_cmd(ctx, npm_cmd)
ctx.run(npm_cmd, pty=True)


@task
def upgrade(ctx: Context):
"""Upgrade Python and JS packages"""
run_cmd(ctx, "poetry update")
ctx.run("poetry update", pty=True)


@task
Expand All @@ -393,12 +382,12 @@ def try_github_actions(ctx: Context):
act_path = project_dir / "bin" / "act"
if not act_path.exists():
print("Installing act...")
run_cmd(
ctx,
ctx.run(
"curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash",
silent=True,
pty=True,
)
run_cmd(ctx, f"{act_path} -P ubuntu-latest=catthehacker/ubuntu:act-latest")
ctx.run(f"{act_path} -P ubuntu-latest=catthehacker/ubuntu:act-latest", pty=True)


@task
Expand All @@ -412,7 +401,7 @@ def purge_celery(
cmd = f"{build_compose_cmd(env)} exec web celery -A radis purge -Q {queues}"
if force:
cmd += " -f"
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -429,7 +418,7 @@ def backup_db(ctx: Context, env: Environments = "prod"):
f"docker exec --env DJANGO_SETTINGS_MODULE={settings} "
f"{web_container_id} ./manage.py dbbackup --clean -v 2"
)
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -441,7 +430,7 @@ def restore_db(ctx: Context, env: Environments = "prod"):
f"docker exec --env DJANGO_SETTINGS_MODULE={settings} "
f"{web_container_id} ./manage.py dbrestore"
)
run_cmd(ctx, cmd)
ctx.run(cmd, pty=True)


@task
Expand All @@ -452,10 +441,10 @@ def upgrade_postgresql(ctx: Context, env: Environments = "dev", version: str = "
print("Starting docker container that upgrades the database files.")
print("Watch the output if everything went fine or if any further steps are necessary.")
volume = get_postgres_volume(env)
run_cmd(
ctx,
ctx.run(
f"docker run -e POSTGRES_PASSWORD=postgres -v {volume}:/var/lib/postgresql/data "
f"pgautoupgrade/pgautoupgrade:{version}",
pty=True,
)
else:
print("Cancelled")
Expand Down

0 comments on commit b236028

Please sign in to comment.