Skip to content

Commit d4552bd

Browse files
Allow database credentials to be mounted as volume from secret created by service broker.
1 parent 1055979 commit d4552bd

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

katacoda/settings.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,44 @@
7979

8080
import dj_database_url
8181

82-
if os.environ.get('DATABASE_URL'):
82+
if os.path.isdir('/opt/app-root/secrets/database'):
83+
# Note that as we set what is read from the file as the default
84+
# only, it can still be overridden by the 'DATABASE_URL' environment
85+
# variable if that is also set.
86+
87+
def database_url():
88+
try:
89+
from urllib.parse import urlparse
90+
except ImportError:
91+
from urlparse import urlparse
92+
93+
with open('/opt/app-root/secrets/database/database_name') as fp:
94+
database_name = fp.read().strip()
95+
96+
with open('/opt/app-root/secrets/database/uri') as fp:
97+
uri = fp.read().strip()
98+
99+
with open('/opt/app-root/secrets/database/username') as fp:
100+
username = fp.read().strip()
101+
102+
with open('/opt/app-root/secrets/database/password') as fp:
103+
password = fp.read().strip()
104+
105+
address = urlparse(uri)
106+
107+
scheme = address.scheme
108+
hostname = address.hostname
109+
port = address.port
110+
111+
return '%s://%s:%s@%s:%s/%s' % (scheme, username, password,
112+
hostname, port, database_name)
113+
114+
DATABASES = {
115+
'default': dj_database_url.config(default=database_url(),
116+
conn_max_age=600)
117+
}
118+
119+
elif os.environ.get('DATABASE_URL'):
83120
DATABASES = {
84121
'default': dj_database_url.config(conn_max_age=600)
85122
}

0 commit comments

Comments
 (0)