This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/tasks.py

147 lines
3.5 KiB
Python
Raw Normal View History

2021-12-28 23:45:35 +01:00
import time
from pathlib import Path
2021-12-31 12:08:03 +01:00
from invoke import Context, task
BASE_DIR = Path(__file__).parent.resolve(strict=True)
2021-12-19 21:41:20 +01:00
SRC_DIR = BASE_DIR / "src"
2022-02-24 21:11:58 +01:00
COMPOSE_BUILD_FILE = BASE_DIR / "docker-compose-build.yaml"
COMPOSE_BUILD_ENV = {"COMPOSE_FILE": COMPOSE_BUILD_FILE}
2022-12-19 22:12:47 +01:00
@task
2023-01-30 20:21:32 +01:00
def update_dependencies(ctx: Context, *, sync: bool = True) -> None:
return compile_dependencies(ctx, update=True, sync=sync)
@task
def compile_dependencies(
2023-03-02 13:10:25 +01:00
ctx: Context,
*,
update: bool = False,
sync: bool = False,
2023-01-30 20:21:32 +01:00
) -> None:
common_args = "-q --allow-unsafe --resolver=backtracking"
if update:
common_args += " --upgrade"
2022-12-19 22:12:47 +01:00
with ctx.cd(BASE_DIR):
ctx.run(
f"pip-compile {common_args} requirements.in",
2022-12-19 22:12:47 +01:00
pty=True,
echo=True,
)
ctx.run(
f"pip-compile {common_args} --strip-extras -o constraints.txt requirements.in",
pty=True,
echo=True,
)
ctx.run(
f"pip-compile {common_args} requirements-dev.in",
2022-12-19 22:12:47 +01:00
pty=True,
echo=True,
)
2023-01-30 20:21:32 +01:00
if sync:
sync_dependencies(ctx)
@task
def sync_dependencies(ctx: Context) -> None:
with ctx.cd(BASE_DIR):
2022-12-19 22:12:47 +01:00
ctx.run("pip-sync requirements.txt requirements-dev.txt", pty=True, echo=True)
@task
2021-12-31 12:08:03 +01:00
def test(ctx: Context) -> None:
2021-12-19 21:41:20 +01:00
with ctx.cd(SRC_DIR):
ctx.run("pytest", pty=True, echo=True)
@task
2021-12-31 12:08:03 +01:00
def test_cov(ctx: Context) -> None:
2021-12-19 21:41:20 +01:00
with ctx.cd(SRC_DIR):
ctx.run(
"pytest --cov=. --cov-branch --cov-report term-missing:skip-covered",
pty=True,
echo=True,
env={"COVERAGE_FILE": BASE_DIR / ".coverage"},
)
2021-12-28 23:33:50 +01:00
@task
2021-12-31 12:08:03 +01:00
def pre_commit(ctx: Context) -> None:
2021-12-28 22:31:53 +01:00
with ctx.cd(BASE_DIR):
2022-05-25 19:20:23 +02:00
ctx.run("pre-commit run --all-files", pty=True, echo=True)
2021-12-28 23:33:50 +01:00
@task
2021-12-31 12:08:03 +01:00
def mypy(ctx: Context) -> None:
2021-12-28 23:33:50 +01:00
with ctx.cd(BASE_DIR):
2021-12-29 16:19:31 +01:00
ctx.run("pre-commit run --all-files mypy", pty=True)
2021-12-28 22:31:53 +01:00
2021-12-29 16:19:31 +01:00
@task(pre=[pre_commit, test_cov])
2023-01-30 20:58:18 +01:00
def check(_ctx: Context) -> None:
2021-12-28 23:33:50 +01:00
pass
@task
2021-12-31 12:08:03 +01:00
def build(ctx: Context) -> None:
with ctx.cd(BASE_DIR):
2022-02-24 21:11:58 +01:00
ctx.run(
2023-03-02 13:10:25 +01:00
"docker-compose build django",
pty=True,
echo=True,
env=COMPOSE_BUILD_ENV,
2022-02-24 21:11:58 +01:00
)
2021-12-19 21:41:20 +01:00
@task
2021-12-31 12:08:03 +01:00
def publish(ctx: Context) -> None:
2021-12-19 21:41:20 +01:00
with ctx.cd(BASE_DIR):
2022-02-24 21:11:58 +01:00
ctx.run(
2023-03-02 13:10:25 +01:00
"docker-compose push django",
pty=True,
echo=True,
env=COMPOSE_BUILD_ENV,
2022-02-24 21:11:58 +01:00
)
@task
2021-12-31 12:08:03 +01:00
def deploy(ctx: Context) -> None:
2022-07-06 00:05:51 +02:00
ctx.run("ssh ubuntu /mnt/data/blog/update", pty=True, echo=True)
2021-12-27 11:47:36 +01:00
2021-12-28 23:45:35 +01:00
@task
2023-01-30 20:58:18 +01:00
def check_alive(_ctx: Context) -> None:
2022-12-19 22:12:47 +01:00
import requests
2022-02-24 21:14:44 +01:00
exception = None
2021-12-28 23:45:35 +01:00
for _ in range(5):
try:
2023-01-30 20:58:18 +01:00
res = requests.get("https://gabnotes.org", timeout=5)
2021-12-28 23:45:35 +01:00
res.raise_for_status()
2022-02-24 21:14:44 +01:00
except requests.exceptions.HTTPError as e:
time.sleep(2)
exception = e
2023-01-30 20:58:18 +01:00
else:
print("Server is up & running") # noqa: T201
return
2023-03-02 13:10:25 +01:00
msg = "Failed to reach the server"
raise RuntimeError(msg) from exception
2021-12-28 23:45:35 +01:00
@task(pre=[check, build, publish, deploy], post=[check_alive])
2023-01-30 20:58:18 +01:00
def beam(_ctx: Context) -> None:
pass
2021-12-27 11:47:36 +01:00
@task
2021-12-31 12:08:03 +01:00
def download_db(ctx: Context) -> None:
2021-12-27 11:47:36 +01:00
with ctx.cd(BASE_DIR):
2022-07-06 00:05:51 +02:00
ctx.run("scp ubuntu:/mnt/data/blog/db/db.sqlite3 ./db/db.sqlite3")
2021-12-28 12:26:17 +01:00
ctx.run("rm -rf src/media/")
2022-07-06 00:05:51 +02:00
ctx.run("scp -r ubuntu:/mnt/data/blog/media/ ./src/media")
with ctx.cd(SRC_DIR):
ctx.run("./manage.py two_factor_disable gaugendre", pty=True)
ctx.run("./manage.py changepassword gaugendre", pty=True)