This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
106 lines (88 loc) · 3.52 KB
/
main.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
"""Start AiiDA REST API for default profile."""
import os
from aiida.restapi import api
from aiida.restapi.run_api import run_api
from aiida.manage.configuration import load_profile, load_config
from aiida.manage.configuration.profile import Profile
from aiida.manage.manager import get_manager
import aiida.restapi
from aiida.manage.external.postgres import Postgres
import traceback
config = load_config(create=True)
profile_name = os.getenv("AIIDA_PROFILE")
#if profile_name in config.profile_names:
profile = Profile(
profile_name, {
"database_hostname":
os.getenv("AIIDADB_HOST"),
"database_port":
os.getenv("AIIDADB_PORT"),
"database_engine":
os.getenv("AIIDADB_ENGINE"),
"database_name":
os.getenv("AIIDADB_NAME"),
"database_username":
os.getenv("AIIDADB_USER"),
"database_password":
os.getenv("AIIDADB_PASS"),
"database_backend":
os.getenv("AIIDADB_BACKEND"),
"default_user":
os.getenv("default_user_email"),
"repository_uri":
"file://{}/.aiida/repository/{}".format(os.getenv("AIIDA_PATH"),
profile_name),
})
config.add_profile(profile)
config.set_default_profile(profile_name)
config.store()
def create_db(profile):
"""Create PostgreSQL database, if missing."""
dbinfo_su = {
'host': profile.database_hostname,
'port': profile.database_port,
'user': os.getenv("AIIDADB_SUPER_USER"),
'password': os.getenv("AIIDADB_SUPER_PASS"),
'database': 'template1',
}
postgres = Postgres(interactive=False, quiet=False, dbinfo=dbinfo_su) #, determine_setup=False)
if not postgres.is_connected:
raise ConnectionError("Unable to connect as super user")
try:
if not postgres.dbuser_exists(dbuser=profile.database_username):
postgres.create_dbuser(dbuser=profile.database_username, dbpass=profile.database_password)
if not postgres.db_exists(dbname=profile.database_name):
postgres.create_db(profile.database_username, profile.database_name)
# Fill DB with vanilla content
load_profile(profile.name)
backend = get_manager()._load_backend(schema_check=False) # pylint: disable=protected-access
try:
backend.migrate()
except Exception as exception: # pylint: disable=broad-except
print(
'database migration failed, probably because connection details are incorrect:\n{}'.format(exception)
)
# Create the user if it does not yet exist
created, user = orm.User.objects.get_or_create(
email=profile.default_user, first_name='AiiDA', last_name='EXPLORER', institution='WEBSERVICE'
)
if created:
user.store()
profile.default_user = user.email
except:
print(traceback.format_exc())
if os.getenv("AIIDADB_SUPER_USER"):
create_db(profile)
config.update_profile(profile)
load_profile(profile_name)
CONFIG_DIR = os.path.join(
os.path.split(os.path.abspath(aiida.restapi.__file__))[0], 'common')
(app, api) = run_api(api.App,
api.AiidaApi,
hostname="localhost",
port=5000,
config=CONFIG_DIR,
debug=False,
wsgi_profile=False,
hookup=False,
catch_internal_server=False)