Finalize python script to parse DATABASE_URL var

This commit is contained in:
Glenn Y. Rolland 2017-07-18 13:55:37 +02:00
parent ce668be090
commit 79430ea69e
4 changed files with 84 additions and 28 deletions

View file

@ -22,6 +22,7 @@ WORKDIR etherpad-lite
RUN bin/installDeps.sh && rm settings.json
COPY entrypoint.sh /entrypoint.sh
COPY parseurl.py /parseurl.py
RUN chmod +x /entrypoint.sh
RUN sed -i 's/^node/exec\ node/' bin/run.sh

15
Makefile Normal file
View file

@ -0,0 +1,15 @@
all: build run
# --build-arg "BUNDLE_BITBUCKET__ORG=$(BUNDLE_BITBUCKET__ORG)" \
build:
docker build -t dt-service-pad .
run:
docker run --rm -p 9001:9001 \
-e DATABASE_URL="mysql://user:foo@databasehost/database?options" \
-t dt-service-pad \
#

View file

@ -1,26 +1,26 @@
#!/bin/bash
set -e
chmod +x parseurl.py
eval $(./parseurl.py)
: ${ETHERPAD_DB_HOST:=mysql}
: ${ETHERPAD_DB_USER:=root}
: ${ETHERPAD_DB_NAME:=etherpad}
ETHERPAD_DB_NAME=$( echo $ETHERPAD_DB_NAME | sed 's/\./_/g' )
chmod +x /parseurl.py
# /parseurl.py ETHERPAD_DB_
eval $(/parseurl.py ETHERPAD_DB_)
# ETHERPAD_DB_PASSWORD is mandatory in mysql container, so we're not offering
# any default. If we're linked to MySQL through legacy link, then we can try
# using the password from the env variable MYSQL_ENV_MYSQL_ROOT_PASSWORD
if [ "$ETHERPAD_DB_USER" = 'root' ]; then
: ${ETHERPAD_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
# if [ "$ETHERPAD_DB_USERNAME" = 'root' ]; then
# : ${ETHERPAD_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
# fi
if [ -z "$ETHERPAD_DB_USERNAME" ]; then
echo >&2 'error: missing required ETHERPAD_DB_USERNAME environment variable'
echo >&2 ' Did you forget to -e ETHERPAD_DB_USERNAME=... ?'
exit 1
fi
if [ -z "$ETHERPAD_DB_PASSWORD" ]; then
echo >&2 'error: missing required ETHERPAD_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e ETHERPAD_DB_PASSWORD=... ?'
echo >&2
echo >&2 ' (Also of interest might be ETHERPAD_DB_USER and ETHERPAD_DB_NAME.)'
exit 1
fi
@ -30,7 +30,7 @@ fi
node -p "require('crypto').randomBytes(32).toString('hex')")}
# Check if database already exists
RESULT=`mysql -u${ETHERPAD_DB_USER} -p${ETHERPAD_DB_PASSWORD} \
RESULT=`mysql -u${ETHERPAD_DB_USERNAME} -p${ETHERPAD_DB_PASSWORD} \
-h${ETHERPAD_DB_HOST} --skip-column-names \
-e "SHOW DATABASES LIKE '${ETHERPAD_DB_NAME}'"`
@ -38,7 +38,7 @@ if [ "$RESULT" != $ETHERPAD_DB_NAME ]; then
# mysql database does not exist, create it
echo "Creating database ${ETHERPAD_DB_NAME}"
mysql -u${ETHERPAD_DB_USER} -p${ETHERPAD_DB_PASSWORD} -h${ETHERPAD_DB_HOST} \
mysql -u${ETHERPAD_DB_USERNAME} -p${ETHERPAD_DB_PASSWORD} -h${ETHERPAD_DB_HOST} \
-e "create database ${ETHERPAD_DB_NAME}"
fi
@ -52,7 +52,7 @@ if [ ! -f settings.json ]; then
"sessionKey" : "${ETHERPAD_SESSION_KEY}",
"dbType" : "mysql",
"dbSettings" : {
"user" : "${ETHERPAD_DB_USER}",
"user" : "${ETHERPAD_DB_USERNAME}",
"host" : "${ETHERPAD_DB_HOST}",
"password": "${ETHERPAD_DB_PASSWORD}",
"database": "${ETHERPAD_DB_NAME}"

View file

@ -5,19 +5,59 @@ import os
from urlparse import urlparse
prefix = sys.argv[1]
uri = os.environ['DATABASE_URL']
result = urlparse(uri)
credential, machine = result.netloc.split('@')
username, password = credential.split(':')
host, port = machine.split(':')
path = result.path
# print os.environ.keys
uri_str = os.environ['DATABASE_URL']
uri = urlparse(uri_str)
print(prefix, 'SCHEME=', result.scheme)
print(prefix, 'USERNAME=', username)
print(prefix, 'PASSWORD=', password)
print(prefix, 'HOST=', host)
print(prefix, 'PORT=', port)
print(prefix, 'PATH=', path)
print(prefix, 'QUERY=', result.query)
print(prefix, 'FRAGMENT=', result.fragment)
db_netloc = uri.netloc
db_credential = None
db_username = None
db_password = None
db_host = None
db_port = None
if db_netloc.find("@") >= 0 :
tmp_arr = db_netloc.split('@')
db_credential = tmp_arr[0]
db_host = tmp_arr[1]
if db_credential and db_credential.find(':') >= 0 :
tmp_arr = db_credential.split(':')
db_username = tmp_arr[0]
db_password = tmp_arr[1]
if db_host and db_host.find(':') >= 0 :
tmp_arr = db_credential.split(':')
db_host = tmp_arr[0]
db_port = tmp_arr[1]
db_path = uri.path
db_scheme = uri.scheme
db_query = uri.query
db_fragment = uri.fragment
if db_scheme :
print(prefix + 'SCHEME=' + db_scheme)
if db_username :
print(prefix + 'USERNAME=' + db_username)
if db_password :
print(prefix + 'PASSWORD=' + db_password)
if db_host :
print(prefix + 'HOST=' + db_host)
if db_port :
print(prefix + 'PORT=' + db_port)
if db_path :
print(prefix + 'PATH=' + db_path)
if db_query :
print(prefix + 'QUERY=' + db_query)
if db_fragment :
print(prefix + 'FRAGMENT=' + db_fragment)