From 0bffa3ca8d30795147b08db0813c128491ae448c Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Thu, 16 Sep 2021 16:21:35 -0700 Subject: [PATCH] adding help() and associated changes --- bin/vhost-add.sh | 58 ++++++++++++---- bin/vhost-del.sh | 21 +++++- bin/vhost-deploy.sh | 54 +++++++++----- bin/vhost-destroy.sh | 59 ++++++++++++---- bin/vhost-disable.sh | 18 ++++- bin/vhost-enable.sh | 112 +++++++++++++++++++----------- bin/vhost-fix-perms.sh | 41 +++++++++-- bin/vhost-mysql-db-add.sh | 42 ++++++++++- bin/vhost-mysql-db-del.sh | 82 ++++++++++------------ bin/vhost-user-add.sh | 62 +++++++++++++---- bin/vhost-user-del.sh | 18 ++++- bin/vhost-user-jail-reset.sh | 18 ++++- bin/vhost-user-jail.sh | 18 ++++- bin/vhost-user-jails-cp.sh | 18 ++++- bin/vhost-user-jails-reset.sh | 17 +++++ bin/vhost-user-jails-update.sh | 25 +++++++ bin/vhost-varnish-disable.sh | 31 +++++++-- bin/vhost-varnish-enable.sh | 22 +++++- bin/vhost-varnish-update-sites.sh | 26 +++++++ bin/vhost.sh | 5 ++ 20 files changed, 580 insertions(+), 167 deletions(-) diff --git a/bin/vhost-add.sh b/bin/vhost-add.sh index 1f4ce38..b575139 100755 --- a/bin/vhost-add.sh +++ b/bin/vhost-add.sh @@ -7,25 +7,53 @@ # load include file source $(dirname $0)/vhost.sh -# check for and set virtualhost +help() +{ + thisfilename=$(basename -- "$0") + echo "Add virtualhost to this server." + echo "" + echo "usage: $thisfilename virtualhost username [OPTIONS]" + echo "" + echo " -h Print this help." + echo "" + echo " Username must already exist. If need be run vhost-user-add.sh first." + exit +} + +# check for and set virtualhost & username if [ -n "$1" ]; then - if vhost::validate_domain $1; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help else - echo "ERROR: $1 is not a valid domain name." - exit 1 + # virtualhost + if vhost::validate_domain $1; then + virtualhost="${1,,}" + shift + else + echo "ERROR: $1 is not a valid domain name." + exit 1 + fi + # username + if [ -n "$2" ]; then + if [ $2 == "-h" ]; then + help + else + username="${2,,}" + shift + fi + else + echo "username not set" + exit 1 + fi + # last check for -h + if [ -n "$3" ]; then + if [ $3 == "-h" ]; then + help + fi + fi fi else - echo "virtualhost not set" - exit 1 -fi - -# check for and set username -if [ -n "$2" ]; then - username=$2 -else - echo "username not set" - exit 1 + help fi if [ ! -d /home/$username ]; then diff --git a/bin/vhost-del.sh b/bin/vhost-del.sh index 6b0c81f..57070c1 100755 --- a/bin/vhost-del.sh +++ b/bin/vhost-del.sh @@ -7,12 +7,27 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Removes virtualhost from server." + echo "" + echo "usage: $thisfilename virtualhost [OPTIONS]" + echo "" + echo " -h Print this help." + exit +} + # check for and set virtualhost if [ -n "$1" ]; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help + else + virtualhost="${1,,}" + shift + fi else - echo "virtualhost not set" - exit 1 + help fi # remove virtualhost dir diff --git a/bin/vhost-deploy.sh b/bin/vhost-deploy.sh index 6337f57..41135f5 100755 --- a/bin/vhost-deploy.sh +++ b/bin/vhost-deploy.sh @@ -7,16 +7,40 @@ # load include file source $(dirname $0)/vhost.sh -# -v virtualhost (required) -# -u username (optional) -# -p password (optional) -# -j jail (true or false, optional) +help() +{ + thisfilename=$(basename -- "$0") + echo "Add virtualhost to this server, including shell user and MySQL database." + echo "" + echo "usage: $thisfilename virtualhost [OPTIONS]" + echo "" + echo " -h Print this help." + echo " -u USERNAME Username to use for this virtualhost. Optional, defaults to first 8 characters of virtualhost." + echo " -p PASSWORD Password for username. Optional, random password generated if none specified." + echo " -j Whether or not to jail the user. Optional, default is to not jail user." + exit +} -while getopts ":v:u:p:j:" opt; do +# check for and set virtualhost +if [ -n "$1" ]; then + if [ $1 == "-h" ]; then + help + elif vhost::validate_domain $1; then + virtualhost="${1,,}" + shift + else + echo "ERROR: Invalid virtualhost: $1" + exit 1 + fi +else + help +fi + +while getopts "hu:p:j" opt; do case "${opt}" in - v ) - virtualhost=${OPTARG} - ;; + h ) + help + exit;; u ) username=${OPTARG} ;; @@ -24,7 +48,7 @@ while getopts ":v:u:p:j:" opt; do password=${OPTARG} ;; j ) - jail=${OPTARG} + jail=true ;; \? ) echo "Invalid option: $OPTARG" @@ -43,11 +67,6 @@ if [ ! -n "$virtualhost" ]; then exit 1 fi -if ! vhost::validate_domain $virtualhost; then - echo "ERROR: $virtualhost is not a valid domain name." - exit 1 -fi - if [ -d /srv/www/$virtualhost ] || [ -f /etc/apache2/sites-available/$virtualhost.conf ]; then echo "virtualhost for $virtualhost already installed" exit 1 @@ -64,10 +83,8 @@ if ! grep -q "^$username:" /etc/passwd; then if [ ! -n "$password" ]; then password=`/usr/bin/pwgen 12 1` fi - # set userid - userid=`awk -F: '{uid[$3]=1}END{for(x=1000; x<=65534; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/passwd` # add user - /usr/local/bin/vhost-user-add.sh $username "$password" $userid + /usr/local/bin/vhost-user-add.sh $username -p "$password" # if jail option is set then jail user if [[ $jail = true ]]; then /usr/local/bin/vhost-user-jail.sh $username > /dev/null 2>&1 @@ -76,3 +93,6 @@ fi # add virtualhost /usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1 + +# add mysql database +/usr/local/bin/vhost-mysql-db-add.sh $virtualhost > /dev/null 2>&1 diff --git a/bin/vhost-destroy.sh b/bin/vhost-destroy.sh index 3ea5798..04766bf 100755 --- a/bin/vhost-destroy.sh +++ b/bin/vhost-destroy.sh @@ -7,30 +7,59 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Remove virtualhost and associated user & database & db user from this server." + echo "" + echo "usage: $thisfilename virtualhost" + echo "" + echo " -h Print this help." + exit +} + # check for and set virtualhost if [ -n "$1" ]; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help + elif [ ! -d /srv/www/$1 ]; then + echo "virtualhost dir does not exist" + exit 1 + else + virtualhost="${1,,}" + shift + fi else - echo "virtualhost not set" - exit 1 + help fi -# First check for virtualhost. Redundant -# as vhost-del.sh run later does this same check -# but prevents connecting to all nodes unnecessarily -if [ ! -d /srv/www/$virtualhost ]; then - echo "virtualhost dir does not exist" - exit 1 +# check for database and delete if it exists +database=${virtualhost//./dot} +database=${database//-/dash} +if [ -d /var/lib/mysql/$database ]; then + /usr/local/bin/vhost-mysql-db-del.sh $virtualhost fi -# check for userdel option and set username if enabled -if [ -n "$2" ] && [ $2 = "userdel" ]; then - username=$(stat -c '%U' /srv/www/$virtualhost) -fi +# get & set username for this virtualhost +username=$(stat -c '%U' /srv/www/$virtualhost) +# check for a delete varnish config + +# del virtualhost files & configs /usr/local/bin/vhost-del.sh $virtualhost -# check for userdel option -if [ -n "$2" ] && [ $2 = "userdel" ] && [ -n $username ]; then +# check for any remaining virtualhosts before deleting user +# same check is done in vhost-user-del.sh +# but doing this here avoids generating any errors +existingvirtualhosts=false +vhost::set-virtualhostArray +for v in "${virtualhostArray[@]}" +do + if [ $(stat -c '%U' /srv/www/$v) = $username ]; then + existingvirtualhosts=true + fi +done + +if [[ $existingvirtualhosts = false ]]; then /usr/local/bin/vhost-user-del.sh $username fi diff --git a/bin/vhost-disable.sh b/bin/vhost-disable.sh index 0bf144c..6c59240 100755 --- a/bin/vhost-disable.sh +++ b/bin/vhost-disable.sh @@ -7,9 +7,25 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Disable Apache config for specified virtualhost." + echo "" + echo "usage: $thisfilename virtualhost" + echo "" + echo " -h Print this help." + exit +} + # check for and set virtualhost if [ -n "$1" ]; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help + else + virtualhost="${1,,}" + shift + fi else echo "virtualhost not set" exit 1 diff --git a/bin/vhost-enable.sh b/bin/vhost-enable.sh index 68b5ba5..63f348d 100755 --- a/bin/vhost-enable.sh +++ b/bin/vhost-enable.sh @@ -7,54 +7,86 @@ # load include file source $(dirname $0)/vhost.sh -# vhost-enable.sh macro_name vhost [subdomain|alias] -# vhost-enable.sh VHostHTTPS example.com -# vhost-enable.sh VHostSubdomainHTTPS example.com staging -# vhost-enable.sh VHostAliasHTTPS example.com existingsite -# vhost-enable.sh VMailHTTPS mail.example.com -# vhost-enable.sh RedirectHTTPS example.com https://my.newsite.com/path/page.html -# -# mod_macro config will look like: -# Use VHostHTTP $vhost $username -# Use VHostHTTPS $vhost $username -# Use VHostHTTPSVarnish $vhost $username -# Use VHostSubdomainHTTP $vhost $username $subdomain -# Use VHostSubdomainHTTPS $vhost $username $subdomain -# Use VHostSubdomainHTTPSVarnish $vhost $username $subdomain -# Use VHostAliasHTTP $vhost $username $alias -# Use VHostAliasHTTPS $vhost $username $alias -# Use VHostAliasHTTPSVarnish $vhost $username $alias -# Use VMailHTTPS $vhost -# Use RedirectHTTP $vhost $redirect -# Use RedirectHTTPS $vhost $redirect -# -# $username is autodetected from vhost dir ownership +help() +{ + thisfilename=$(basename -- "$0") + echo "Enable Apache config for virtualhost." + echo "" + echo "usage: $thisfilename macro_name vhost [subdomain|alias]" + echo "" + echo " -h Print this help." + echo "" + echo " Available Apache Macros:" + echo "" +# working on building autodetected macros +# needs work - remove $username, set other vars +# default_ifs="$IFS" +# IFS="" +# macro_help_array=($(grep Macro /etc/apache2/mods-available/macro.conf|cut -d ' ' -f 2-|cut -d \> -f 1|grep -v Macro)) +# for each_macro in ${macro_help_array[*]} +# do +# echo "${each_macro}" +# done +# IFS="$default_ifs" + echo "" + echo " Usage examples:" + echo " vhost-enable.sh VHostHTTPS example.com" + echo " vhost-enable.sh VHostSubdomainHTTPS example.com staging" + echo " vhost-enable.sh VHostAliasHTTPS example.com existingsite" + echo " vhost-enable.sh VMailHTTPS mail.example.com" + echo " vhost-enable.sh RedirectHTTPS example.com https://my.newsite.com/path/page.html" + echo "" + echo " Apache mod_macro config will look like:" + echo ' Use VHostHTTP $vhost $username' + echo ' Use VHostHTTPS $vhost $username' + echo ' Use VHostHTTPSVarnish $vhost $username' + echo ' Use VHostSubdomainHTTP $vhost $username $subdomain' + echo ' Use VHostSubdomainHTTPS $vhost $username $subdomain' + echo ' Use VHostSubdomainHTTPSVarnish $vhost $username $subdomain' + echo ' Use VHostAliasHTTP $vhost $username $alias' + echo ' Use VHostAliasHTTPS $vhost $username $alias' + echo ' Use VHostAliasHTTPSVarnish $vhost $username $alias' + echo ' Use VMailHTTPS $vhost' + echo ' Use RedirectHTTP $vhost $redirect' + echo ' Use RedirectHTTPS $vhost $redirect' + echo '' + echo ' $username is autodetected from vhost dir ownership' + echo " See /etc/apache2/mods-available/macro.conf for macro details." + echo "" + exit +} macro_array=($(grep Macro /etc/apache2/mods-available/macro.conf |cut -d ' ' -f 2|grep -v Macro)) macro_vhost_line="Use" -# check for and set mode +# check for and set macro & vhost if [ -n "$1" ]; then - macro_name=$1 - if [[ " ${macro_array[@]} " =~ " ${macro_name} " ]]; then - macro_vhost_line="$macro_vhost_line $macro_name" + if [ $1 == "-h" ]; then + help else - echo "invalid macro name" - exit 1 + # check for and set macro + macro_name=$1 + shift + if [[ " ${macro_array[@]} " =~ " ${macro_name} " ]]; then + macro_vhost_line="$macro_vhost_line $macro_name" + else + echo "invalid macro name" + exit 1 + fi + # check for and set vhost + if [ -n "$2" ]; then + vhost=$2 + shift + macro_vhost_line="$macro_vhost_line $vhost" + vhost_conf="$vhost.conf" + else + echo "vhost not set" + echo + help + fi fi else - echo "macro name not set" - exit 1 -fi - -# check for and set vhost -if [ -n "$2" ]; then - vhost=$2 - macro_vhost_line="$macro_vhost_line $vhost" - vhost_conf="$vhost.conf" -else - echo "vhost not set" - exit 1 + help fi # set username for all VHost macros diff --git a/bin/vhost-fix-perms.sh b/bin/vhost-fix-perms.sh index 9c4aeba..367df69 100755 --- a/bin/vhost-fix-perms.sh +++ b/bin/vhost-fix-perms.sh @@ -7,13 +7,40 @@ # load include file source $(dirname $0)/vhost.sh -if [ -n "$1" ]; then - if [ "$1" = '--verbose' ] || [ "$1" = '-v' ]; then - mode=verbose - elif [ "$1" = '--dry-run' ] || [ "$1" = '-n' ]; then - mode=dry-run - fi -fi +help() +{ + thisfilename=$(basename -- "$0") + echo "Make sure all home (/home/...) and virtualhost (/srv/www/...) files are owned by correct users." + echo "" + echo "usage: $thisfilename [OPTIONS]" + echo "" + echo " -h Print this help." + echo " -n dry-run - List all files that need modification, but don't actually do anything." + echo " -v verbose - List all files that are being modified." + exit +} + +while getopts "hvn" opt; do + case "${opt}" in + h ) + help + ;; + v ) + mode=verbose + ;; + n ) + mode=dry-run + ;; + \? ) + echo "Invalid option: $OPTARG" + exit 1 + ;; + : ) + echo "Invalid option: $OPTARG requires an argument" + exit 1 + ;; + esac +done for VHOST in /srv/www/*/; { diff --git a/bin/vhost-mysql-db-add.sh b/bin/vhost-mysql-db-add.sh index ecf7772..955cc51 100755 --- a/bin/vhost-mysql-db-add.sh +++ b/bin/vhost-mysql-db-add.sh @@ -7,9 +7,35 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Creates default MySQL database and db user for specified virtualhost." + echo "" + echo "usage: $thisfilename virtualhost. [OPTIONS]" + echo "" + echo " -h Print this help." + echo " -u USERNAME Username for accessing the database. Optional, autogenerated if none specified." + echo " -p PASSWORD Password for username. Optional, random password generated if none specified." + echo "" + echo " MySQL database names is based on virtualhost, with . replaced by the word 'dot'" + echo " and - replaced by the word 'dash'. If username is autogenerated it is based on" + echo " the shell username & the virtualhost name." + echo " e.g. for virtualost example.com the db name will be 'exampledotcom' and the" + echo " username will be examplec@example.com." + echo " db info stored in a file named /srv/www/$virtualhost/db_info.txt" + exit +} + + # check for and set virtualhost if [ -n "$1" ]; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help + else + virtualhost="${1,,}" + shift + fi else echo "virtualhost not set" exit 1 @@ -31,11 +57,14 @@ if [ -d /var/lib/mysql/$database ]; then exit 1 fi +# get & set username of virtualhost +vhost_username=$(stat -c '%U' /srv/www/$virtualhost) + # check for and set mysql username if [ -n "$2" ]; then username=$2 else - username=$(stat -c '%U' /srv/www/$virtualhost)@$virtualhost + username=$vhost_username@$virtualhost fi # check for and set mysql password @@ -50,4 +79,13 @@ mysql -e "CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';" mysql -e "GRANT ALL PRIVILEGES ON $database.* TO '$username'@'localhost';" mysqladmin flush-privileges +# save mysql db info to file +touch /srv/www/$virtualhost/db_info.txt +chown $vhost_username:$vhost_username /srv/www/$virtualhost/db_info.txt +chmod 640 /srv/www/$virtualhost/db_info.txt +echo "hostname=127.0.0.1" > /srv/www/$virtualhost/db_info.txt +echo "database=$database" >> /srv/www/$virtualhost/db_info.txt +echo "username=$username" >> /srv/www/$virtualhost/db_info.txt +echo "password=$password" >> /srv/www/$virtualhost/db_info.txt + echo "database '$database' created with username '$username' and password '$password'" diff --git a/bin/vhost-mysql-db-del.sh b/bin/vhost-mysql-db-del.sh index fa55d2f..2e91fee 100755 --- a/bin/vhost-mysql-db-del.sh +++ b/bin/vhost-mysql-db-del.sh @@ -7,47 +7,41 @@ # load include file source $(dirname $0)/vhost.sh -# # check for and set virtualhost -# if [ -n "$1" ]; then -# virtualhost=$1 -# else -# echo "virtualhost not set" -# exit 1 -# fi -# -# # make sure virtualhost exists -# if [ ! -d /srv/www/$virtualhost ]; then -# echo "virtualhost $virtualhost does not exist" -# exit 1 -# fi -# -# # set database name -# database=${virtualhost//./dot} -# database=${database//-/dash} -# -# # make sure database doesn't already exist -# if [ -d /var/lib/mysql/$database ]; then -# echo "database $database already exists" -# exit 1 -# fi -# -# # check for and set mysql username -# if [ -n "$2" ]; then -# username=$2 -# else -# username=$(stat -c '%U' /srv/www/$virtualhost)@$virtualhost -# fi -# -# # check for and set mysql password -# if [ -n "$3" ]; then -# password=$3 -# else -# password=`/usr/bin/pwgen 16 1` -# fi -# -# mysqladmin create $database -# mysql -e "CREATE USER '$username'@'localhost' IDENTIFIED BY '$password';" -# mysql -e "GRANT ALL PRIVILEGES ON $database.* TO '$username'@'localhost';" -# mysqladmin flush-privileges -# -# echo "database '$database' created with username '$username' and password '$password'" +help() +{ + thisfilename=$(basename -- "$0") + echo "Remove MySQL database and default db user for the specified virtualhost." + echo "" + echo "usage: $thisfilename virtualhost." + echo "" + echo " -h Print this help." + exit +} + +# check for and set virtualhost. +if [ -n "$1" ]; then + if [ $1 == "-h" ]; then + help + else + virtualhost="${1,,}" + shift + fi +else + echo "virtualhost not set" + exit 1 +fi + +# set database name +database=${virtualhost//./dot} +database=${database//-/dash} + +# drop database +mysql -e "DROP DATABASE IF EXISTS $database;" + +# set default username and attempt to drop user +if [ -d /srv/www/$virtualhost ]; then + vhost_username=$(stat -c '%U' /srv/www/$virtualhost) + username=$vhost_username@$virtualhost + mysql -e "DROP USER IF EXISTS '$username'@'localhost';" + mysqladmin flush-privileges +fi diff --git a/bin/vhost-user-add.sh b/bin/vhost-user-add.sh index d154bc7..c014341 100755 --- a/bin/vhost-user-add.sh +++ b/bin/vhost-user-add.sh @@ -7,30 +7,66 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Add system user to server." + echo "" + echo "usage: $thisfilename username [OPTIONS]" + echo "" + echo " -h Print this help." + echo " -p PASSWORD Password for username. Optional, random password generated if none specified." + echo " -u UID Numberic User ID to assign to user. Optional, next available uid set if none specified." + exit +} + # check for and set username if [ -n "$1" ]; then - username=$1 + if [ $1 == "-h" ]; then + help + else + username="${1,,}" + shift + fi else echo "username not set" exit 1 fi -# check for and set password -if [ -n "$2" ]; then - password=$2 -else - echo "password not set" - exit 1 +while getopts "hp:u:" opt; do + case "${opt}" in + h ) + help + p ) + password=${OPTARG} + ;; + u ) + uid=${OPTARG} + ;; + \? ) + echo "Invalid option: $OPTARG" + exit 1 + ;; + : ) + echo "Invalid option: $OPTARG requires an argument" + exit 1 + ;; + esac +done + +# generate password if none specified +if [ ! -n "$password" ]; then + password=`/usr/bin/pwgen 12 1` fi -# check for and set userid -if [ -n "$3" ]; then - userid=$3 -else - echo "userid not set" - exit 1 +# get next UID if none specified +if [ ! -n "$uid" ]; then + userid=`awk -F: '{uid[$3]=1}END{for(x=1000; x<=65534; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/passwd` fi +# user & related files are only added if they don't already exist +# in this way it's safe to repeatedly try to add the same user + if ! /bin/grep -q "^$username:" /etc/passwd; then newusers="$username:$password:$userid:$userid::/home/$username:/bin/bash" echo "$newusers"|newusers diff --git a/bin/vhost-user-del.sh b/bin/vhost-user-del.sh index a4b33aa..766bb4e 100755 --- a/bin/vhost-user-del.sh +++ b/bin/vhost-user-del.sh @@ -7,9 +7,25 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Remove user from this server." + echo "" + echo "usage: $thisfilename username" + echo "" + echo " -h Print this help." + exit +} + # check for and set username if [ -n "$1" ]; then - username=$1 + if [ $1 == "-h" ]; then + help + else + username="${1,,}" + shift + fi else echo "username not set" exit 1 diff --git a/bin/vhost-user-jail-reset.sh b/bin/vhost-user-jail-reset.sh index b342e8b..5c10695 100755 --- a/bin/vhost-user-jail-reset.sh +++ b/bin/vhost-user-jail-reset.sh @@ -7,9 +7,25 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Rebuild jail for specified user." + echo "" + echo "usage: $thisfilename username [OPTIONS]" + echo "" + echo " -h Print this help." + exit +} + # check for and set username if [ -n "$1" ]; then - username=$1 + if [ $1 == "-h" ]; then + help + else + username="${1,,}" + shift + fi else echo "username not set" exit 1 diff --git a/bin/vhost-user-jail.sh b/bin/vhost-user-jail.sh index 111c144..eb4a1d2 100755 --- a/bin/vhost-user-jail.sh +++ b/bin/vhost-user-jail.sh @@ -7,9 +7,25 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Jail specified user." + echo "" + echo "usage: $thisfilename username" + echo "" + echo " -h Print this help." + exit +} + # check for and set username if [ -n "$1" ]; then - username=$1 + if [ $1 == "-h" ]; then + help + else + username="${1,,}" + shift + fi else echo "username not set" exit 1 diff --git a/bin/vhost-user-jails-cp.sh b/bin/vhost-user-jails-cp.sh index 03a3883..b908dff 100755 --- a/bin/vhost-user-jails-cp.sh +++ b/bin/vhost-user-jails-cp.sh @@ -7,9 +7,25 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Add file to all existing jails." + echo "" + echo "usage: $thisfilename pathtofile" + echo "" + echo " -h Print this help." + exit +} + # check for and set file to be copied in to jails if [ -n "$1" ]; then - cpfile=$1 + if [ $1 == "-h" ]; then + help + else + cpfile=$1 + shift + fi else echo "file to copy in to jails not set" exit 1 diff --git a/bin/vhost-user-jails-reset.sh b/bin/vhost-user-jails-reset.sh index 0b551a0..9dd8c2a 100755 --- a/bin/vhost-user-jails-reset.sh +++ b/bin/vhost-user-jails-reset.sh @@ -7,6 +7,23 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Rebuilds all existing jails by running vhost-user-jail-reset.sh for each jailed user." + echo "" + echo "usage: $thisfilename" + echo "" + echo " -h Print this help." + exit +} + +if [ -n "$1" ]; then + if [ $1 == "-h" ]; then + help + fi +fi + # make sure jails dir exists if [[ ! -e /usr/jails/ ]]; then echo "/usr/jails does not exist" diff --git a/bin/vhost-user-jails-update.sh b/bin/vhost-user-jails-update.sh index 81213c2..8f19e53 100755 --- a/bin/vhost-user-jails-update.sh +++ b/bin/vhost-user-jails-update.sh @@ -7,6 +7,31 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Updates all existing jails." + echo "" + echo "usage: $thisfilename" + echo "" + echo " -h Print this help." + echo "" + echo " Cycles through all existing jails and runs jk_update" + echo " for each one. This updates all software installed in" + echo " the jails to match the latest versions installed on" + echo " the host server. e.g. if php-cli gets update on the" + echo " host this syncs that update to all the jails." + exit +} + +# check for -h +if [ -n "$1" ]; then + if [ $1 == "-h" ]; then + help + exit + fi +fi + # make sure jails dir exists if [[ ! -e /usr/jails/ ]]; then echo "/usr/jails does not exist" diff --git a/bin/vhost-varnish-disable.sh b/bin/vhost-varnish-disable.sh index e226083..8d80092 100755 --- a/bin/vhost-varnish-disable.sh +++ b/bin/vhost-varnish-disable.sh @@ -7,11 +7,28 @@ # load include file source $(dirname $0)/vhost.sh -# varnish-disable.sh virtualhost +help() +{ + thisfilename=$(basename -- "$0") + echo "Disables Varnish config for specified virtualhost." + echo "" + echo "usage: $thisfilename virtualhost" + echo "" + echo " -h Print this help." + echo "" + echo " Varnish is proxied through Apache. This disables the" + echo " Apache proxy to Varnish and removes the varnish config." + exit +} # check for and set virtualhost if [ -n "$1" ]; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help + else + virtualhost="${1,,}" + shift + fi else echo "virtualhost not set" exit 1 @@ -41,7 +58,11 @@ if [[ "$macro_name" == *"Subdomain"* ]] || [[ "$macro_name" == *"Alias"* ]]; the vhost_enable="$vhost_enable $macro_var" fi -# uncomment to flush varnish cache -# systemctl is-active --quiet varnish && systemctl reload --quiet varnish +/usr/local/bin/vhost-enable.sh $vhost_enable -echo /usr/local/bin/vhost-enable.sh $vhost_enable +if [ -f /etc/varnish/sites.d/$virtualhost ]; then + rm /etc/varnish/sites.d/$virtualhost + /usr/local/bin/vhost-varnish-update-sites.sh $vhost_enable + # uncomment to flush varnish cache + # systemctl is-active --quiet varnish && systemctl reload --quiet varnish +fi diff --git a/bin/vhost-varnish-enable.sh b/bin/vhost-varnish-enable.sh index 3343841..d956863 100755 --- a/bin/vhost-varnish-enable.sh +++ b/bin/vhost-varnish-enable.sh @@ -7,9 +7,29 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Enables Varnish for specified virtualhost." + echo "" + echo "usage: $thisfilename virtualhost. [OPTIONS]" + echo "" + echo " -h Print this help." + echo "" + echo " Creates Varnish config, loads it in Varnish and then" + echo " enables Apache proxy to Varnish for virtualhost." + exit +} + # check for and set virtualhost +# check for and set username if [ -n "$1" ]; then - virtualhost=$1 + if [ $1 == "-h" ]; then + help + else + virtualhost="${1,,}" + shift + fi else echo "virtualhost not set" exit 1 diff --git a/bin/vhost-varnish-update-sites.sh b/bin/vhost-varnish-update-sites.sh index e8cd209..52ac391 100755 --- a/bin/vhost-varnish-update-sites.sh +++ b/bin/vhost-varnish-update-sites.sh @@ -7,6 +7,32 @@ # load include file source $(dirname $0)/vhost.sh +help() +{ + thisfilename=$(basename -- "$0") + echo "Makes sure all existing varnish configs are loaded." + echo "" + echo "usage: $thisfilename" + echo "" + echo " -h Print this help." + echo "" + echo " The varnish config is broken down in to individual" + echo " configurations for each virtualhost. Each config has" + echo " to be included by the master sites.vcl. This tool makes" + echo " sure all existing virtualhost varnish configs are" + echo " properly included in sites.vcl, and removes includes" + echo " for varnish virtualhost configs that have been deleted." + exit +} + +# check for -h +if [ -n "$1" ]; then + if [ $1 == "-h" ]; then + help + exit + fi +fi + if [ ! -f /etc/varnish/sites.d/example.com.vcl ]; then echo "ERROR: example.vlc does not exist" diff --git a/bin/vhost.sh b/bin/vhost.sh index 74f7f2d..4a9788b 100755 --- a/bin/vhost.sh +++ b/bin/vhost.sh @@ -36,3 +36,8 @@ function vhost::validate_domain () { return 1 fi } + +# check for local config, which can be used to override any of the above +if [[ -f /usr/local/etc/vhost.conf ]]; then + source /usr/local/etc/vhost.conf +fi