From 5a658e416bb8b59ec66ee7e0cfe9b888886e520c Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sun, 8 May 2022 20:58:31 +0200 Subject: [PATCH] refactor: Use shell version of URL parser --- Dockerfile | 2 +- entrypoint.sh | 120 +++++++++++++++++++++++++------------------------- parseurl.py | 63 -------------------------- parseurl.sh | 35 +++++++++++++++ 4 files changed, 95 insertions(+), 125 deletions(-) mode change 100644 => 100755 entrypoint.sh delete mode 100755 parseurl.py create mode 100755 parseurl.sh diff --git a/Dockerfile b/Dockerfile index 9bbdbac..b0c96e6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ ENV ETHERPAD_VERSION 1.8.13 # RUN = docker run ... + docker commit RUN apt-get update \ - && apt-get install -y curl unzip mariadb-client python netcat \ + && apt-get install -y curl unzip mariadb-client netcat \ && apt-get clean \ && rm -r /var/lib/apt/lists/* diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 index 3d16a7b..e780209 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,27 +1,25 @@ #!/bin/sh set -e -chmod +x /parseurl.py -/parseurl.py ETHERPAD_DB_ -eval "$(/parseurl.py ETHERPAD_DB_)" +eval "$(sh /parseurl.sh "$DATABASE_URL" ETHERPAD_DB)" -# ETHERPAD_DB_PASSWORD is mandatory in mysql container, so we're not offering +# ETHERPAD_DB_PASS 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_USERNAME" = 'root' ]; then -# : ${ETHERPAD_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD} +# if [ "$ETHERPAD_DB_USER" = 'root' ]; then +# : ${ETHERPAD_DB_PASS:=$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 +if [ -z "$ETHERPAD_DB_USER" ]; then + echo >&2 'error: missing required ETHERPAD_DB_USER environment variable' + echo >&2 ' Did you forget to -e ETHERPAD_DB_USER=... ?' + 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=... ?' - exit 1 +if [ -z "$ETHERPAD_DB_PASS" ]; then + echo >&2 'error: missing required ETHERPAD_DB_PASS environment variable' + echo >&2 ' Did you forget to -e ETHERPAD_DB_PASS=... ?' + exit 1 fi RANDOM_STRING="$(node -p "require('crypto').randomBytes(32).toString('hex')")}" @@ -47,62 +45,62 @@ sleep 1 # Check if database already exists RESULT="$(mysql \ - "-u${ETHERPAD_DB_USERNAME}" \ - "-p${ETHERPAD_DB_PASSWORD}" \ - "-P${ETHERPAD_DB_PORT}" \ - "-h${ETHERPAD_DB_HOST}" \ - --skip-column-names \ - -e "SHOW DATABASES LIKE '${ETHERPAD_DB_NAME}'")" + "-u${ETHERPAD_DB_USER}" \ + "-p${ETHERPAD_DB_PASS}" \ + "-P${ETHERPAD_DB_PORT}" \ + "-h${ETHERPAD_DB_HOST}" \ + --skip-column-names \ + -e "SHOW DATABASES LIKE '${ETHERPAD_DB_NAME}'")" if [ "$RESULT" != "$ETHERPAD_DB_NAME" ]; then - # mysql database does not exist, create it - echo "Creating database ${ETHERPAD_DB_NAME}" + # mysql database does not exist, create it + echo "Creating database ${ETHERPAD_DB_NAME}" - mysql "-u${ETHERPAD_DB_USERNAME}" \ - "-p${ETHERPAD_DB_PASSWORD}" \ - "-P${ETHERPAD_DB_PORT}" \ - "-h${ETHERPAD_DB_HOST}" \ - -e "create database ${ETHERPAD_DB_NAME}" + mysql "-u${ETHERPAD_DB_USER}" \ + "-p${ETHERPAD_DB_PASS}" \ + "-P${ETHERPAD_DB_PORT}" \ + "-h${ETHERPAD_DB_HOST}" \ + -e "create database ${ETHERPAD_DB_NAME}" fi if ! [ -f settings.json ]; then - echo "Creating database configuration" - cat <<- EOF > settings.json - { - "title": "${ETHERPAD_TITLE}", - "ip": "0.0.0.0", - "port" :${ETHERPAD_PORT}, - "skinName": "colibris", - "sessionKey" : "${ETHERPAD_SESSION_KEY}", - "trustProxy" : false, - "minify" : true, - "defaultPadText" : "Welcome to Etherpad!\n\nThis pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!\n\nIMPORTANT: this pad will be deleted after 30 days. Please don't consider it as document storage.", - "dbType" : "mysql", - "dbSettings" : { - "user" : "${ETHERPAD_DB_USERNAME}", - "host" : "${ETHERPAD_DB_HOST}", - "password": "${ETHERPAD_DB_PASSWORD}", - "database": "${ETHERPAD_DB_NAME}" - }, - EOF + echo "Creating database configuration" + cat <<- EOF > settings.json + { + "title": "${ETHERPAD_TITLE}", + "ip": "0.0.0.0", + "port" :${ETHERPAD_PORT}, + "skinName": "colibris", + "sessionKey" : "${ETHERPAD_SESSION_KEY}", + "trustProxy" : false, + "minify" : true, + "defaultPadText" : "Welcome to Etherpad!\n\nThis pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!\n\nIMPORTANT: this pad will be deleted after 30 days. Please don't consider it as document storage.", + "dbType" : "mysql", + "dbSettings" : { + "user" : "${ETHERPAD_DB_USER}", + "host" : "${ETHERPAD_DB_HOST}", + "password": "${ETHERPAD_DB_PASS}", + "database": "${ETHERPAD_DB_NAME}" + }, +EOF - if [ -n "$ETHERPAD_ADMIN_PASSWORD" ]; then - ETHERPAD_ADMIN_USER="${ETHERPAD_ADMIN_USER:-admin}" - echo "Creating admin user configuration for $ETHERPAD_ADMIN_USER/$ETHERPAD_ADMIN_PASSWORD" + if [ -n "$ETHERPAD_ADMIN_PASSWORD" ]; then + ETHERPAD_ADMIN_USER="${ETHERPAD_ADMIN_USER:-admin}" + echo "Creating admin user configuration for $ETHERPAD_ADMIN_USER/$ETHERPAD_ADMIN_PASSWORD" - cat <<- EOF >> settings.json - "users": { - "${ETHERPAD_ADMIN_USER}": { - "password": "${ETHERPAD_ADMIN_PASSWORD}", - "is_admin": true - } - }, - EOF - fi + cat <<-EOF >> settings.json + "users": { + "${ETHERPAD_ADMIN_USER}": { + "password": "${ETHERPAD_ADMIN_PASSWORD}", + "is_admin": true + } + }, +EOF + fi - cat <<- EOF >> settings.json - } - EOF + cat <<- EOF >> settings.json + } +EOF fi if ! grep -q '/* GLENUX BEGIN */' src/static/css/pad.css ; then diff --git a/parseurl.py b/parseurl.py deleted file mode 100755 index 4a7030e..0000000 --- a/parseurl.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python - -import sys -import os -from urlparse import urlparse - -prefix = sys.argv[1] -# print os.environ.keys -uri_str = os.environ['DATABASE_URL'] -uri = urlparse(uri_str) - -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_arr1 = db_netloc.split('@') - db_credential = tmp_arr1[0] - db_host = tmp_arr1[1] - -if db_credential and db_credential.find(':') >= 0 : - tmp_arr2 = db_credential.split(':') - db_username = tmp_arr2[0] - db_password = tmp_arr2[1] - -if db_host and db_host.find(':') >= 0 : - tmp_arr3 = db_host.split(':') - db_host = tmp_arr3[0] - db_port = tmp_arr3[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 + 'NAME=' + db_path[1:]) - -if db_query : - print(prefix + 'QUERY=' + db_query) - -if db_fragment : - print(prefix + 'FRAGMENT=' + db_fragment) - diff --git a/parseurl.sh b/parseurl.sh new file mode 100755 index 0000000..b0e87a0 --- /dev/null +++ b/parseurl.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Referenced and tweaked from http://stackoverflow.com/questions/6174220/parse-url-in-shell-script#6174447 + +url="$1" +prefix="$2" +proto="$(echo "$url" | grep :// | sed -e 's,^\(.*\)://.*,\1,g')" +# remove the protocol +workurl="$(echo "$url" |sed -e "s,^$proto://,,")" +# extract the user (if any) +userpass="$(echo "$workurl" | grep @ | cut -d@ -f1)" +pass="$(echo "$userpass"| grep : | cut -d: -f2)" +if [ -n "$pass" ]; then + user="$(echo "$userpass" | grep : | cut -d: -f1)" +else + user="$userpass" +fi + +# extract the host +hostport="$(echo "$workurl" |sed -e "s,$userpass@,," | cut -d/ -f1)" +# by request - try to extract the port +port="$(echo "$hostport" | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')" +host="$(echo "$hostport" | cut -d: -f1)" +# extract the path (if any) +path="/$(echo "$workurl" | grep / | cut -d/ -f2-)" +name="$(echo "$workurl" | grep / | cut -d/ -f2-)" + +echo "${prefix}_URL=\"$url\"" +echo "${prefix}_PROTO=\"$proto\"" +echo "${prefix}_USER=\"$user\"" +echo "${prefix}_PASS=\"$pass\"" +echo "${prefix}_HOST=\"$host\"" +echo "${prefix}_PORT=\"$port\"" +echo "${prefix}_PATH=\"$path\"" +echo "${prefix}_NAME=\"$name\"" +