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

107 lines
2.6 KiB
Python
Raw Normal View History

2021-12-31 12:08:03 +01:00
"""
Invoke management tasks for the project.
The current implementation with type annotations is not compatible
with invoke 1.6.0 and requires manual patching.
See https://github.com/pyinvoke/invoke/pull/458/files
"""
2021-12-28 23:45:35 +01:00
import time
from pathlib import Path
2021-12-28 23:45:35 +01:00
import requests
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}
@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):
ctx.run("pre-commit run --all-files", pty=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])
2021-12-31 12:08:03 +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(
"docker-compose build django", pty=True, echo=True, env=COMPOSE_BUILD_ENV
)
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(
"docker-compose push django", pty=True, echo=True, env=COMPOSE_BUILD_ENV
)
@task
2021-12-31 12:08:03 +01:00
def deploy(ctx: Context) -> None:
ctx.run("ssh ubuntu /home/gaugendre/blog/update", pty=True, echo=True)
2021-12-27 11:47:36 +01:00
2021-12-28 23:45:35 +01:00
@task
2021-12-31 12:08:03 +01:00
def check_alive(ctx: Context) -> None:
2022-02-24 21:14:44 +01:00
exception = None
2021-12-28 23:45:35 +01:00
for _ in range(5):
try:
res = requests.get("https://gabnotes.org")
res.raise_for_status()
2022-02-24 21:14:44 +01:00
print("Server is up & running")
return
except requests.exceptions.HTTPError as e:
time.sleep(2)
exception = e
raise RuntimeError("Failed to reach the server") from exception
2021-12-28 23:45:35 +01:00
@task(pre=[check, build, publish, deploy], post=[check_alive])
2021-12-31 12:08:03 +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):
2021-12-28 12:26:17 +01:00
ctx.run("scp ubuntu:/home/gaugendre/blog/db/db.sqlite3 ./db/db.sqlite3")
ctx.run("rm -rf src/media/")
ctx.run("scp -r ubuntu:/home/gaugendre/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)