diff --git a/bin/container/addDomainCtl.sh b/bin/container/addDomainCtl.sh deleted file mode 100755 index b826bd3..0000000 --- a/bin/container/addDomainCtl.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash - -domain=${1//\./\\.} -#replace . with \. -#so example.com becomes example\.com, this is because in regex . is used for matching any character -#so the regex would match on example.com and example7com, because . matches on "7" -#httpd_conf=`cat httpd_config.conf` -check=$(perl -0777 -ne 'print $1 if /(^ member '$domain' \{.*[^}]*})/m' httpd_config.conf) -#check=$(perl -0777 -le "print $1 if /(^ member $domain \{.*[^}]*})/m" httpd_config.conf) -if [ ! -z "$check" ]; then - echo "# It appears the domain already exist, therefor we won\'t add it. Check the httpd_config.conf if you believe this is a mistake" - exit 0 -else - echo "# Domain has been added" -fi - -perl -0777 -p -i -e 's/(vhTemplate centralConfigLog \{[^}]+)\}*(^.*listeners.*$)/\1$2 - member '$1' { - vhDomain '$1' - }/gmi' httpd_config.conf - -#perl -0777 -p -i.bak -e "s/(vhTemplate centralConfigLog \{[^}]+)\}*(^$)/\1 -# member $1 { -# vhDomain $1 -# }/gmi" httpd_config.conf diff --git a/bin/container/addDomainCtl.sh.bak b/bin/container/addDomainCtl.sh.bak deleted file mode 100755 index 3bd1f73..0000000 --- a/bin/container/addDomainCtl.sh.bak +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash -perl -0777 -p -i.bak -e "s/(vhTemplate centralConfigLog \{[^}]+)\}*(^$)/\1 - member $1 { - vhDomain $1 - }/gmi" httpd_config.conf diff --git a/bin/container/domainctl.sh b/bin/container/domainctl.sh new file mode 100644 index 0000000..7860040 --- /dev/null +++ b/bin/container/domainctl.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +CK_RESULT='' +HTTPD_CONF='httpd_config.conf' + +help_message(){ + echo 'Command [-add|-del] [domain_name]' + echo 'Example 1: domain-ctl.sh -add example.com' + echo 'Example 2: domain-ctl.sh -del example.com' +} + +dot_escape(){ + ESCAPE=$(echo ${1} | sed 's/\./\\./g') +} + +check_duplicate(){ + CK_RESULT=$(grep -E "${1}" ${2}) +} + +fst_match_line(){ + FIRST_LINE_NUM=$(grep -n -m 1 ${1} ${2} | awk -F ':' '{print $1}') +} +fst_match_after(){ + FIRST_NUM_AFTER=$(tail -n +${1} ${2} | grep -n -m 1 ${3} | awk -F ':' '{print $1}') +} +lst_match_line(){ + fst_match_after ${1} ${2} '}' + LAST_LINE_NUM=$((${FIRST_LINE_NUM}+${FIRST_NUM_AFTER}-1)) +} + +add_domain(){ + dot_escape ${1} + DOMAIN=${ESCAPE} + check_duplicate "member.*${DOMAIN}" ${HTTPD_CONF} + if [ "${CK_RESULT}" != '' ]; then + echo "# It appears the domain already exist! Check the ${HTTPD_CONF} if you believe this is a mistake!" + exit 1 + else + perl -0777 -p -i -e 's/(vhTemplate centralConfigLog \{[^}]+)\}*(^.*listeners.*$)/\1$2 + member '${1}' { + vhDomain '${1}' + }/gmi' ${HTTPD_CONF} + fi +} + +del_domain(){ + dot_escape ${1} + DOMAIN=${ESCAPE} + check_duplicate "member.*${DOMAIN}" ${HTTPD_CONF} + if [ "${CK_RESULT}" = '' ]; then + echo "# We couldn't find the domain you wanted to remove! Check the ${HTTPD_CONF} if you believe this is a mistake!" + exit 1 + else + fst_match_line ${1} ${HTTPD_CONF} + lst_match_line ${FIRST_LINE_NUM} ${HTTPD_CONF} + sed -i "${FIRST_LINE_NUM},${LAST_LINE_NUM}d" ${HTTPD_CONF} + fi +} + +while [ ! -z "${1}" ]; do + case ${1} in + -[hH] | -help | --help) + help_message + ;; + -add | -a | -A) shift + add_domain ${1} + ;; + -del | -d | -D | -delete) shift + del_domain ${1} + ;; + *) + help_message + ;; + esac + shift +done \ No newline at end of file diff --git a/bin/container/downloadWPCtl.sh b/bin/container/downloadWPCtl.sh deleted file mode 100755 index 7908634..0000000 --- a/bin/container/downloadWPCtl.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash -if [ -z "$1" ] -then - echo "Missing arguments, exit" - exit 0 -fi - -cd /var/www/vhosts/$1/html -if [ ! -f "./wp-config.php" ]; then - # su -s /bin/bash www-data -c - COUNTER=0 - until [ "$(curl -v mysql:3306 2>&1 | grep native)" ]; - do - echo "Counter: ${COUNTER}" - COUNTER=$((COUNTER+1)) - if [ ${COUNTER} = 10 ]; then - echo '--- MySQL is starting, please wait... ---' - elif [ ${COUNTER} = 100 ]; then - echo '--- MySQL is timeout, exit! ---' - exit 1 - fi - sleep 1 - done - wp core download \ - --allow-root \ - --force - first_www_uid=$(stat -c "%u" /var/www/vhosts/$1) - first_www_gid=$(stat -c "%g" /var/www/vhosts/$1) - chown $first_www_uid:$first_www_gid /var/www/vhosts/$1 -R - -fi - - -www_uid=$(stat -c "%u" /var/www/vhosts/$1) -if [ ${www_uid} -eq 0 ]; then - #echo "./sites/localhost is owned by root, auto changing ownership of ./sites/localhost to uid 1000" - chown 1000:1000 /var/www/vhosts/localhost -R -fi - -#echo "WordPress installation finished." -#exec "$@" - diff --git a/bin/container/downloadctl.sh b/bin/container/downloadctl.sh new file mode 100644 index 0000000..8173ba1 --- /dev/null +++ b/bin/container/downloadctl.sh @@ -0,0 +1,102 @@ +#!/bin/bash +DEFAULT_VH_ROOT='/var/www/vhosts' +VH_DOC_ROOT='' +APP_NAME='' +DOMAIN='' +WWW_UID='' +WWW_GID='' + +help_message(){ + echo 'Command [-app app_name] [-domain domain_name]' + echo 'Example: download.sh -app wordpress -d example.com' +} + +check_input(){ + if [ -z "${1}" ]; then + help_message + exit 1 + fi +} + +get_owner(){ + WWW_UID=$(stat -c "%u" ${DEFAULT_VH_ROOT}/${1}) + WWW_GID=$(stat -c "%g" ${DEFAULT_VH_ROOT}/${1}) + if [ ${WWW_UID} -eq 0 ] || [ ${WWW_GID} -eq 0 ]; then + echo "Found ${WWW_UID}:${WWW_GID} has root, will auto fix to 1000" + WWW_UID=1000 + WWW_GID=1000 + fi +} + +set_vh_docroot(){ + if [ -d ${DEFAULT_VH_ROOT}/${1}/html ]; then + VH_DOC_ROOT="${DEFAULT_VH_ROOT}/${1}/html" + else + echo "${DEFAULT_VH_ROOT}/${1}/html does not exist, please add domain first! Abort!" + exit 1 + fi +} + +check_sql_native(){ + local COUNTER=0 + local LIMIT_NUM=100 + until [ "$(curl -v mysql:3306 2>&1 | grep native)" ]; + do + echo "Counter: ${COUNTER}/${LIMIT_NUM}" + COUNTER=$((COUNTER+1)) + if [ ${COUNTER} = 10 ]; then + echo '--- MySQL is starting, please wait... ---' + elif [ ${COUNTER} = ${LIMIT_NUM} ]; then + echo '--- MySQL is timeout, exit! ---' + exit 1 + fi + sleep 1 + done +} + +app_wordpress_dl(){ + if [ ! -f "${VH_DOC_ROOT}/wp-config.php" ] && [ ! -f "${VH_DOC_ROOT}/wp-config-sample.php" ]; then + wp core download \ + --allow-root \ + --force + chown -R ${WWW_UID}:${WWW_GID} ${DEFAULT_VH_ROOT}/${DOMAIN} + else + echo 'wp-config*.php already exist, abort!' + exit 1 + fi +} + +main(){ + get_owner + cd ${VH_DOC_ROOT} + if [ "${APP_NAME}" = 'wordpress' ] || [ "${APP_NAME}" = 'wp' ]; then + check_sql_native + app_wordpress_dl + exit 0 + else + echo "APP: ${APP_NAME} not support, exit!" + exit 1 + fi +} + +while [ ! -z "${1}" ]; do + case ${1} in + -[hH] | -help | --help) + help_message + ;; + -app | -a | -A) shift + check_input "${1}" + APP_NAME="${1}" + ;; + -d | -D | -domain) shift + check_input "${1}" + DOMAIN="${1}" + set_vh_docroot ${DOMAIN} + ;; + *) + help_message + ;; + esac + shift +done +main diff --git a/bin/container/rmDomainCtl.sh b/bin/container/rmDomainCtl.sh deleted file mode 100755 index d6a4574..0000000 --- a/bin/container/rmDomainCtl.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -domain=${1//\./\\.} -#replace . with \. -#so example.com becomes example\.com, this is because in regex . is used for matching any character -#so the regex would match on example.com and example7com, because . matches on "7" - -check=$(perl -0777 -ne 'print $1 if /(^ member '$domain' \{.*[^}]*})/m' httpd_config.conf) -if [ ! -z "$check" ]; then - echo "# Domain has been removed" -else - echo "# We couldn't find the domain you wanted to remove, is it already removed?. Check the httpd_config.conf if you believe this is a mistake" - exit 0; -fi - -perl -0777 -p -i.bak -e "s/(^ member $domain \{.*[^}]*})/#thislinewillbedeletedj98311/gmi" httpd_config.conf -perl -i -ne '/#thislinewillbedeletedj98311/ or print' httpd_config.conf -#aboves replaces the matched group with a string and random numbers, then second command searches for that string and deletes the line -#if anyone can figure out how to do above in oneline, feel free to let us know diff --git a/bin/domain.sh b/bin/domain.sh index f38c5bc..2bc340d 100644 --- a/bin/domain.sh +++ b/bin/domain.sh @@ -16,7 +16,7 @@ check_input(){ add_domain(){ check_input ${1} - docker-compose exec ${CONT_NAME} su -s /bin/bash lsadm -c "cd /usr/local/lsws/conf && addDomainCtl.sh ${1}" + docker-compose exec ${CONT_NAME} su -s /bin/bash lsadm -c "cd /usr/local/lsws/conf && domainctl.sh -add ${1}" if [ ! -d "./sites/${1}" ]; then mkdir -p ./sites/${1}/{html,logs} fi @@ -24,7 +24,7 @@ add_domain(){ del_domain(){ check_input ${1} - docker-compose exec ${CONT_NAME} su -s /bin/bash lsadm -c "cd /usr/local/lsws/conf && rmDomainCtl.sh ${1}" + docker-compose exec ${CONT_NAME} su -s /bin/bash lsadm -c "cd /usr/local/lsws/conf && domainctl.sh -del ${1}" } check_input ${1} diff --git a/bin/download-wp.sh b/bin/download-wp.sh deleted file mode 100644 index 2a421aa..0000000 --- a/bin/download-wp.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -docker-compose exec litespeed su -c "downloadWPCtl.sh $1" - diff --git a/bin/download.sh b/bin/download.sh new file mode 100644 index 0000000..ed0df42 --- /dev/null +++ b/bin/download.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +APP_NAME='' +DOMAIN='' + +help_message(){ + echo 'Command [-app app_name] [-domain domain_name]' + echo 'Example: download.sh -app wordpress -d example.com' +} + +check_input(){ + if [ -z "${1}" ]; then + help_message + exit 1 + fi +} + +app_download(){ + docker-compose exec litespeed su -c "downloadctl.sh -app ${1} -domain ${2}" + exit 0 +} + +while [ ! -z "${1}" ]; do + case ${1} in + -[hH] | -help | --help) + help_message + ;; + -app | -a | -A) shift + check_input "${1}" + APP_NAME="${1}" + ;; + -d | -D | -domain) shift + check_input "${1}" + DOMAIN="${1}" + ;; + *) + help_message + ;; + esac + shift +done + +app_download ${APP_NAME} ${DOMAIN} \ No newline at end of file