vhost-stack/bin/vhost.sh

97 lines
2.2 KiB
Bash
Raw Normal View History

2021-04-04 14:15:16 -07:00
#!/bin/bash
#
2021-04-04 13:28:22 -07:00
# vhost configs
# any script that includes this conf file will force user to be root
if [ "$USER" != "root" ]; then
#exec sudo -u root $0 $@
exec sudo $0 $@
fi
# constants
# functions
function vhost::set-virtualhostArray () {
cd /srv/www
virtualhostArray=(`ls -1|grep -v ^html$`)
}
function vhost::set-phpVersion () {
PHP_MAJOR_VERSION=`php -r "echo PHP_MAJOR_VERSION;"`
PHP_MINOR_VERSION=`php -r "echo PHP_MINOR_VERSION;"`
phpVersion=$PHP_MAJOR_VERSION.$PHP_MINOR_VERSION
}
# crude but good enough domain name format validation
function vhost::validate_domain () {
local my_domain=$1
2021-10-02 14:54:58 -07:00
if [[ $my_domain =~ ^(([a-zA-Z0-9](-?[a-zA-Z0-9])*)\.)+[a-zA-Z]{2,}$ ]] ; then
2021-04-04 13:28:22 -07:00
return 0
else
return 1
fi
}
2021-09-16 16:21:35 -07:00
2021-10-05 11:33:24 -07:00
function vhost:getoptions () {
local OPTIND
while getopts "d:i:o:p:u:jhnvw" opt ; do
case "${opt}" in
h ) # display help and exit
help
exit
;;
d ) # domain name (virtualhost) to act on
domain=${OPTARG,,}
if ! vhost::validate_domain $domain; then
echo "ERROR: $domain is not a valid domain name."
exit
fi
;;
i ) # User ID (UID) for new user
uid=${OPTARG}
;;
i ) # option - usually applied to previously specified variable
# e.g. could be subdomain or alias depending on the macro defined
option=${OPTARG}
;;
p ) # password
password=${OPTARG}
;;
u ) # username
username=${OPTARG,,}
;;
j ) # jail - if enabled user will be jailed
jail=true
;;
n ) # dry-run
dryrun=true
;;
v ) # verbose
verbose=true
;;
w ) # write - store data in file
write=true
;;
\? )
echo "Invalid option: $OPTARG"
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument"
exit 1
;;
esac
done
shift $((OPTIND-1))
}
2021-09-16 16:21:35 -07:00
# 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