-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknossos-aam
62 lines (56 loc) · 2.54 KB
/
knossos-aam
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "knossos_aam.settings")
from django.core import management
if sys.argv[1] == "init":
import django
import getpass
import json
from psycopg2 import connect
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
# host and port
host = raw_input("Enter server hostname (default localhost):")
if host == "": host = "localhost"
port = raw_input("Enter port number (default: 5432):")
if port == "": port = "5432"
# db name
dbname = raw_input("Choose a database name (default: knossos_aam):")
if dbname == "": dbname = "knossos_aam"
# db user
print("Create a database user:")
username = raw_input("• Enter username (default: aam):")
if username == "": username = "aam"
email = raw_input("• Enter email address:")
passprompt = lambda: (getpass.getpass("• Choose password:"), getpass.getpass("• Retype password:"))
p1, p2 = passprompt()
while p1 != p2:
print("Passwords do not match. Try again.")
p1, p2 = passprompt()
# setup database
with connect(dbname="postgres", user="postgres") as connection:
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
with connection.cursor() as cursor:
cursor.execute("CREATE USER " + username + " WITH SUPERUSER CREATEDB CREATEROLE PASSWORD '" + p1 + "'")
cursor.execute("CREATE DATABASE " + dbname + " WITH OWNER " + username + " ENCODING 'utf8'")
# setup django
defaultdb = {}
defaultdb["ENGINE"] = "django.db.backends.postgresql_psycopg2"
defaultdb["NAME"] = dbname
defaultdb["USER"] = username
defaultdb["PASSWORD"] = p1
defaultdb["HOST"] = host
defaultdb["PORT"] = port
with open("defaultdb.txt", "w") as dbfile:
json.dump(defaultdb, dbfile)
django.setup() # setup after defaultdb has been specified
management.call_command("makemigrations", verbosity=1)
management.call_command("migrate", verbosity=1)
management.call_command("makemigrations", "knossos_aam_backend", verbosity=1)
management.call_command("migrate", verbosity=1)
from django.contrib.auth.models import User
User.objects.create_superuser(username, email, p1)
else:
management.execute_from_command_line(sys.argv)