vhost-stack/bin/vhost-deploy.sh
Matthew Saunders Brown b2bd9f7875 closing if tag
2021-08-24 14:59:23 -07:00

79 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load include file
source $(dirname $0)/vhost.sh
# -v virtualhost (required)
# -u username (optional)
# -p password (optional)
# -j jail (true or false, optional)
while getopts ":v:u:p:j:" opt; do
case "${opt}" in
v )
virtualhost=${OPTARG}
;;
u )
username=${OPTARG}
;;
p )
password=${OPTARG}
;;
j )
jail=${OPTARG}
;;
\? )
echo "Invalid option: $OPTARG"
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument"
exit 1
;;
esac
done
# check virtualhost
if [ ! -n "$virtualhost" ]; then
echo "virtualhost not set"
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
fi
# check for and set username
if [ ! -n "$username" ]; then
username=`echo $virtualhost | sed 's|\.||'`
username=`echo ${username:0:8}`
fi
if ! grep -q "^$username:" /etc/passwd; then
# check for and set password
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/user-add.sh $username "$password" $userid
# if jail option is set then jail user
if [[ $jail = true ]]; then
/usr/local/bin/user-jail.sh $username > /dev/null 2>&1
fi
fi
# add virtualhost
/usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1