vhost-stack/bin/vhost-deploy.sh
Matthew Saunders Brown e8570e1a82 reworked getopts
2021-10-05 11:33:24 -07:00

101 lines
3.3 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
help()
{
thisfilename=$(basename -- "$0")
echo "Add virtualhost to this server, including shell user and MySQL database."
echo ""
echo "usage: $thisfilename -d <domain> [OPTIONS]"
echo ""
echo " -h Print this help."
echo " -d <domain> Domain name of VirtualHost to remove."
echo " -u <username> Username to use for this virtualhost. Optional, defaults to first 8 alphanumeric 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
}
vhost:getoptions "$@"
# check for domain (virtualhost)
if [[ -z $domain ]]; then
echo "domain is required"
exit
fi
if [[ -d /srv/www/$domain ]] || [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
echo "virtualhost for $domain already installed"
exit 1
fi
# check for and set username
if [[ -z "$username" ]]; then
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:8}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:7}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:6}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:5}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:9}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:10}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:11}`
if grep -q "^$username:" /etc/passwd; then
# username already exists, try another
username=`echo $domain | sed 's|\.||'`
username=`echo ${username:0:12}`
if grep -q "^$username:" /etc/passwd; then
echo "trouble setting unique username, specify '-u USERNAME' to use an existing username"
exit 1
fi
fi
fi
fi
fi
fi
fi
fi
fi
if ! grep -q "^$username:" /etc/passwd; then
# check for and set password
if [[ -z "$password" ]]; then
password=`/usr/bin/pwgen 12 1`
fi
# add user
/usr/local/bin/vhost-user-add.sh $username -p "$password"
# if jail option is set then jail user
if [[ -n $jail ]]; then
/usr/local/bin/vhost-user-jail.sh $username > /dev/null 2>&1
fi
fi
# add virtualhost
/usr/local/bin/vhost-add.sh $domain $username > /dev/null 2>&1
# add mysql database
/usr/local/bin/vhost-mysql-db-add.sh $domain > /dev/null 2>&1