Skip to content

Docker friendly scripts: init_dbconn_webapp.sh

Ryan Fischbach edited this page Oct 23, 2017 · 2 revisions
#!/bin/bash

function processCLIArgs
{
    while [[ $# -gt 0 ]] ; do
        case $1 in
            --table-prefix)
                export DBCONN_WEBAPP_TABLE_PREFIX=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dns-scheme)
                export DBCONN_WEBAPP_DNS_SCHEME=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dbtype)
                export DBCONN_WEBAPP_DBTYPE=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dbhost)
                export DBCONN_WEBAPP_DBHOST=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dbport)
                export DBCONN_WEBAPP_DBPORT=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dbname)
                export DBCONN_WEBAPP_DBNAME=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dbuser)
                export DBCONN_WEBAPP_DBUSER=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            --dbpswd|--dbpw)
                export DBCONN_WEBAPP_DBPSWD=$2
                shift # extra beyond the "shift" at the end of the block
                ;;
            *)
                echo -e "\033[0;93mIgnoring unrecognized option [$1].\033[0m"
                ;;
        esac
        shift # to advance the loop
    done
}

###############################################################################
# MAIN SCRIPT BODY

processCLIArgs $@

if [[ -z ${DBCONN_WEBAPP_TABLE_PREFIX} ]] ; then
  DBCONN_WEBAPP_TABLE_PREFIX="webapp_"
fi
if [[ -z ${DBCONN_WEBAPP_DNS_SCHEME} ]] ; then
  DBCONN_WEBAPP_DNS_SCHEME="ini"
fi
if [[ -z ${DBCONN_WEBAPP_DBTYPE} ]] ; then
  DBCONN_WEBAPP_DBTYPE=${DBCONN_DBTYPE}
  if [[ -z ${DBCONN_WEBAPP_DBTYPE} ]] ; then
    DBCONN_WEBAPP_DBTYPE="mysql"
  fi
fi
if [[ -z ${DBCONN_WEBAPP_DBHOST} ]] ; then
  DBCONN_WEBAPP_DBHOST=${DBCONN_DBHOST}
fi
if [[ -z ${DBCONN_WEBAPP_DBPORT} ]] ; then
  DBCONN_WEBAPP_DBPORT=${DBCONN_DBPORT}
fi
if [[ -z ${DBCONN_WEBAPP_DBNAME} ]] ; then
  DBCONN_WEBAPP_DBNAME=${DBCONN_DBNAME}
fi
if [[ -z ${DBCONN_WEBAPP_DBUSER} ]] ; then
  DBCONN_WEBAPP_DBUSER=${DBCONN_DBUSER}
fi
if [[ -z ${DBCONN_WEBAPP_DBPSWD} ]] ; then
  DBCONN_WEBAPP_DBPSWD=${DBCONN_DBPSWD}
fi

if [[ -z ${DBCONN_WEBAPP_DBHOST} || -z ${DBCONN_WEBAPP_DBNAME} || -z ${DBCONN_WEBAPP_DBUSER} || -z ${DBCONN_WEBAPP_DBPSWD} ]] ; then
  exit 1
fi

if [[ "${DBCONN_WEBAPP_DBHOST}" -eq "FIND-DOCKER-MAGIC-HOST" ]] ; then
  echo "Attempting to discover the proper magic host value to use..."
  FIND_MY_MAGIC_HOST="${SITE_PATH}/app/cli/actionDiscoverDockerDbHostTunnelMagic.php"
  FIND_MY_MAGIC_HOST+=" --dbname=${DBCONN_WEBAPP_DBNAME}"
  FIND_MY_MAGIC_HOST+=" --dbuser=${DBCONN_WEBAPP_DBUSER}"
  FIND_MY_MAGIC_HOST+=" --dbpswd=${DBCONN_WEBAPP_DBPSWD}"
  if [[ -n ${DBCONN_WEBAPP_DBPORT} ]] ; then
    FIND_MY_MAGIC_HOST+=" --dbport=${DBCONN_WEBAPP_DBPORT}"
  fi
  if [[ -n ${DBCONN_DBHOST_TRY_THESE_TOO} ]] ; then
    FIND_MY_MAGIC_HOST+=" --hostlist=${DBCONN_DBHOST_TRY_THESE_TOO}"
  fi
  DBCONN_WEBAPP_DBHOST=$( ${FIND_MY_MAGIC_HOST} )
  if [[ -n ${DBCONN_WEBAPP_DBHOST} ]] ; then
    echo "Using DBCONN_WEBAPP_DBHOST=${DBCONN_WEBAPP_DBHOST}"
    if [[ "${DBCONN_DBHOST}" -eq "FIND-DOCKER-MAGIC-HOST" ]] ; then
      export DBCONN_DBHOST=${DBCONN_WEBAPP_DBHOST}
    fi
  else
    echo "DBHOST value not discovered. :("
    exit 1
  fi
fi

TPL_SRC_FILE="${SITE_PATH}/res/templates/dbconn-webapp.tpl"
PHP_DST_FILE="${SITE_PATH}/configs/anyhost/dbconn-webapp.ini"

if [ ! -f ${PHP_DST_FILE} ] ; then
  SED_PATTERN="s/%table_prefix%/${DBCONN_WEBAPP_TABLE_PREFIX}/"
  SED_PATTERN+=";s/%dns_scheme%/${DBCONN_WEBAPP_DNS_SCHEME}/"
  SED_PATTERN+=";s/%dns_alias%/${DBCONN_WEBAPP_DNS_ALIAS}/"
  SED_PATTERN+=";s/%dns_uri%/${DBCONN_WEBAPP_DNS_URI}/"
  SED_PATTERN+=";s/%dns_custom%/${DBCONN_WEBAPP_DNS_CUSTOM}/"
  SED_PATTERN+=";s/%dbtype%/${DBCONN_WEBAPP_DBTYPE}/"
  SED_PATTERN+=";s/%dbhost%/${DBCONN_WEBAPP_DBHOST}/"
  if [[ -n ${DBCONN_WEBAPP_DBPORT} ]] ; then
    SED_PATTERN+=";s/;port = 3306/port=${DBCONN_WEBAPP_DBPORT}/"
  fi
  SED_PATTERN+=";s/%dbname%/${DBCONN_WEBAPP_DBNAME}/"
  SED_PATTERN+=";s/%dbuser%/${DBCONN_WEBAPP_DBUSER}/"
  SED_PATTERN+=";s/%dbpwrd%/${DBCONN_WEBAPP_DBPSWD}/"
  sed ${SED_PATTERN} ${TPL_SRC_FILE} > ${PHP_DST_FILE}
fi

Clone this wiki locally