-
Notifications
You must be signed in to change notification settings - Fork 9
/
tasks.py
223 lines (176 loc) · 5.93 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import os
import re
from pathlib import Path
from typing import Any
from decouple import config
from invoke import task
@task
def django_settings(ctx: Any) -> Any:
"""Get django settings."""
os.environ["DJANGO_SETTINGS_MODULE"] = "demo.settings.development"
import django
django.setup()
from django.conf import settings
return settings
@task
def build(ctx: Any) -> None:
"""Build the project."""
delete_database(ctx)
delete_media(ctx)
delete_migrations(ctx)
create_database(ctx)
create_user(ctx)
populate_database(ctx)
@task
def create_database(ctx: Any) -> None:
"""Create the database."""
ctx.run("python manage.py makemigrations")
ctx.run("python manage.py migrate")
@task
def create_user(ctx: Any) -> None:
"""Create a superuser."""
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.create(username="admin", is_superuser=True, is_staff=True)
user.set_password("semantic")
user.save()
@task
def populate_database(ctx: Any) -> None:
"""Populate the database."""
django_settings(ctx)
import datetime
import os
import random
from demo_app.factory import PersonFactory
from demo_app.models import Favorite, Picture
from django.conf import settings
from django.core.files import File
from django.utils.text import slugify
from faker import Faker
fake = Faker()
COFFEE_DIR = settings.BASE_DIR / "coffee"
coffees = [c for c in os.listdir(COFFEE_DIR) if Path(c).suffix == ".jpeg"]
people = []
for index in range(int(len(coffees) / 2)):
first_name = fake.first_name()
last_name = fake.first_name()
name = f"{first_name} {last_name}"
slug = slugify(name)
domain = fake.safe_domain_name()
dotted_name = slug.replace("-", ".")
email = f"{dotted_name}@{domain}"
person = PersonFactory(name=name, slug=slug, url=domain, email=email)
people.append(person)
for person in people:
total_friends = int(random.random() * 3)
can_be_friends = [p for p in people if person != p]
friends = random.sample(can_be_friends, total_friends)
person.friends.add(*friends)
pictures = []
random.shuffle(coffees)
coffee_people = people + random.choices(people, k=len(coffees) - len(people))
for coffee, coffee_person in zip(coffees, coffee_people):
date_and_time = fake.past_datetime().replace(tzinfo=datetime.timezone.utc)
picture = Picture(person=coffee_person, date_and_time=date_and_time)
path = COFFEE_DIR / coffee
with open(path, "rb") as f:
picture.picture.save(coffee, File(f))
tags = fake.bs().split(" ")
picture.tags.add(*tags)
pictures.append(picture)
for person in people:
total_favorites = 1 + int(random.random() * 5)
for index in range(total_favorites):
picture = random.choice(pictures)
Favorite.objects.get_or_create(person=person, picture=picture)
@task
def delete_database(ctx: any) -> None:
"""Delete the database."""
django_settings(ctx)
from django.conf import settings
db = settings.BASE_DIR / "db.sqlite3"
if db.exists():
ctx.run(f"rm {db}")
@task
def delete_media(ctx: Any) -> None:
"""Delete media."""
django_settings(ctx)
from django.conf import settings
if settings.MEDIA_ROOT.exists():
ctx.run(f"rm -r {settings.MEDIA_ROOT}")
@task
def delete_migrations(ctx: Any) -> None:
"""Delete migrations."""
import os
from django.conf import settings
MIGRATIONS_DIR = settings.BASE_DIR / "demo_app/migrations/"
migrations = [
MIGRATIONS_DIR / migration
for migration in os.listdir(MIGRATIONS_DIR)
if Path(migration).stem != "__init__" and Path(migration).suffix == ".py"
]
for migration in migrations:
ctx.run(f"rm {migration}")
@task
def get_container_name(ctx: Any, region: str = "asia-northeast1") -> str:
"""Get container name."""
project_id = ctx.run("gcloud config get-value project").stdout.strip()
name = "django-semantic-admin"
return f"{region}-docker.pkg.dev/{project_id}/{name}/{name}"
def docker_secrets() -> str:
"""Get docker secrets."""
build_args = [
f'{secret}="{config(secret)}"' for secret in ("SECRET_KEY", "SENTRY_DSN")
]
return " ".join([f"--build-arg {build_arg}" for build_arg in build_args])
def build_semantic_admin(ctx: Any) -> str:
"""Build semantic admin."""
result = ctx.run("poetry build").stdout
return re.search(r"django_semantic_admin-.*\.whl", result).group()
@task
def build_container(ctx: Any, region: str = "asia-northeast1") -> None:
"""Build container."""
wheel = build_semantic_admin(ctx)
ctx.run("echo yes | python manage.py collectstatic")
name = get_container_name(ctx, region=region)
# Requirements
requirements = [
"django-filter",
"django-taggit",
"gunicorn",
"pillow",
"python-decouple",
"whitenoise",
]
# Versions
reqs = " ".join(
[
req.split(";")[0]
for req in ctx.run("poetry export --dev --without-hashes").stdout.split(
"\n"
)
if req.split("==")[0] in requirements
]
)
# Build
build_args = {"WHEEL": wheel, "POETRY_EXPORT": reqs}
build_args = " ".join(
[f'--build-arg {key}="{value}"' for key, value in build_args.items()]
)
with ctx.cd(".."):
cmd = " ".join(
[
"docker build",
build_args,
docker_secrets(),
f"--no-cache --file=Dockerfile --tag={name} .",
]
)
ctx.run(cmd)
@task
def push_container(ctx: Any, region: str = "asia-northeast1") -> None:
"""Push container."""
name = get_container_name(ctx, region=region)
# Push
cmd = f"docker push {name}"
ctx.run(cmd)