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 RUN bin/installDeps.sh && rm settings.json
COPY entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
COPY parseurl.py /parseurl.py COPY parseurl.py /parseurl.py
RUN chmod +x /entrypoint.sh
RUN sed -i 's/^node/exec\ node/' bin/run.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 #!/bin/bash
set -e set -e
chmod +x parseurl.py chmod +x /parseurl.py
eval $(./parseurl.py) # /parseurl.py ETHERPAD_DB_
eval $(/parseurl.py ETHERPAD_DB_)
: ${ETHERPAD_DB_HOST:=mysql}
: ${ETHERPAD_DB_USER:=root}
: ${ETHERPAD_DB_NAME:=etherpad}
ETHERPAD_DB_NAME=$( echo $ETHERPAD_DB_NAME | sed 's/\./_/g' )
# ETHERPAD_DB_PASSWORD is mandatory in mysql container, so we're not offering # 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 # 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 # using the password from the env variable MYSQL_ENV_MYSQL_ROOT_PASSWORD
if [ "$ETHERPAD_DB_USER" = 'root' ]; then # if [ "$ETHERPAD_DB_USERNAME" = 'root' ]; then
: ${ETHERPAD_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD} # : ${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 fi
if [ -z "$ETHERPAD_DB_PASSWORD" ]; then if [ -z "$ETHERPAD_DB_PASSWORD" ]; then
echo >&2 'error: missing required ETHERPAD_DB_PASSWORD environment variable' echo >&2 'error: missing required ETHERPAD_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e ETHERPAD_DB_PASSWORD=... ?' 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 exit 1
fi fi
@ -30,7 +30,7 @@ fi
node -p "require('crypto').randomBytes(32).toString('hex')")} node -p "require('crypto').randomBytes(32).toString('hex')")}
# Check if database already exists # 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 \ -h${ETHERPAD_DB_HOST} --skip-column-names \
-e "SHOW DATABASES LIKE '${ETHERPAD_DB_NAME}'"` -e "SHOW DATABASES LIKE '${ETHERPAD_DB_NAME}'"`
@ -38,7 +38,7 @@ if [ "$RESULT" != $ETHERPAD_DB_NAME ]; then
# mysql database does not exist, create it # mysql database does not exist, create it
echo "Creating database ${ETHERPAD_DB_NAME}" 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}" -e "create database ${ETHERPAD_DB_NAME}"
fi fi
@ -52,7 +52,7 @@ if [ ! -f settings.json ]; then
"sessionKey" : "${ETHERPAD_SESSION_KEY}", "sessionKey" : "${ETHERPAD_SESSION_KEY}",
"dbType" : "mysql", "dbType" : "mysql",
"dbSettings" : { "dbSettings" : {
"user" : "${ETHERPAD_DB_USER}", "user" : "${ETHERPAD_DB_USERNAME}",
"host" : "${ETHERPAD_DB_HOST}", "host" : "${ETHERPAD_DB_HOST}",
"password": "${ETHERPAD_DB_PASSWORD}", "password": "${ETHERPAD_DB_PASSWORD}",
"database": "${ETHERPAD_DB_NAME}" "database": "${ETHERPAD_DB_NAME}"

View file

@ -5,19 +5,59 @@ import os
from urlparse import urlparse from urlparse import urlparse
prefix = sys.argv[1] prefix = sys.argv[1]
uri = os.environ['DATABASE_URL'] # print os.environ.keys
result = urlparse(uri) uri_str = os.environ['DATABASE_URL']
credential, machine = result.netloc.split('@') uri = urlparse(uri_str)
username, password = credential.split(':')
host, port = machine.split(':')
path = result.path
print(prefix, 'SCHEME=', result.scheme) db_netloc = uri.netloc
print(prefix, 'USERNAME=', username)
print(prefix, 'PASSWORD=', password) db_credential = None
print(prefix, 'HOST=', host) db_username = None
print(prefix, 'PORT=', port) db_password = None
print(prefix, 'PATH=', path) db_host = None
print(prefix, 'QUERY=', result.query) db_port = None
print(prefix, 'FRAGMENT=', result.fragment)
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)