-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
71 lines (62 loc) · 2.18 KB
/
init.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
import os
import sys
import django
import psycopg2
from django.core.management import call_command
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
sys.path.insert(0, os.path.abspath("."))
django.setup()
from water.models import Device as WaterDevice
from light.models import ConfigType
from django.contrib.auth.models import User
POSTGRES_DB = os.getenv("POSTGRES_DB", "postgres")
POSTGRES_USER = os.getenv("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", "postgres")
POSTGRES_HOST = os.getenv("POSTGRES_HOST", "localhost")
POSTGRES_PORT = os.getenv("POSTGRES_PORT", "5432")
DJANGO_ADMIN_USERNAME = os.getenv("DJANGO_ADMIN_USERNAME")
DJANGO_ADMIN_PASSWORD = os.getenv("DJANGO_ADMIN_PASSWORD")
con = psycopg2.connect(
host=POSTGRES_HOST,
port=POSTGRES_PORT,
database="postgres",
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = con.cursor()
try:
cursor.execute(f"create database {POSTGRES_DB};")
print(f"[OK] Database {POSTGRES_DB} created")
except psycopg2.errors.DuplicateDatabase:
print(f"[OK] Database {POSTGRES_DB} already exist")
print("[INFO] Executing django db migrations ...")
call_command("makemigrations", interactive=False, verbosity=2)
call_command("makemigrations", "glbl", interactive=False, verbosity=2)
call_command("makemigrations", "sprinkler", interactive=False, verbosity=2)
call_command("makemigrations", "water", interactive=False, verbosity=2)
call_command("makemigrations", "light", interactive=False, verbosity=2)
call_command("migrate", interactive=False, verbosity=2)
# Water devices
# Creating default device
try:
WaterDevice(tag="tap-water").save()
except django.db.IntegrityError:
pass
# Light default configuration creation
try:
ConfigType(name="daily").save()
except django.db.IntegrityError:
pass
try:
ConfigType(name="planner").save()
except django.db.IntegrityError:
pass
# Admin user creation
try:
User.objects.create_superuser(
DJANGO_ADMIN_USERNAME, "cre@ted-by-helm-chart.com", DJANGO_ADMIN_PASSWORD
)
except django.db.IntegrityError:
pass