vhost-stack/bin/vhost-deploy.sh

99 lines
2.5 KiB
Bash
Raw Normal View History

2021-04-04 13:28:22 -07:00
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
source /etc/vhost.conf || echo "ERROR: Either you do not have vhost user permissions or the config file is missing." && exit
# -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
elif [ -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`
# cylce thru each node and add user
vhost::set-clusterNodes all
for n in "${clusterNodes[@]}"
do
if [ $n = `hostname -s` ]; then
/usr/local/bin/user-add.sh $username "$password" $userid
else
ssh $n.lan "/usr/local/bin/user-add.sh $username \"$password\" $userid"
fi
done
# if jail option is set cylce thru each node and jail user
if [[ $jail = true ]]; then
vhost::set-clusterNodes nds
for n in "${clusterNodes[@]}"
do
if [ $n = `hostname -s` ]; then
/usr/local/bin/user-jail.sh $username > /dev/null 2>&1 &
else
ssh $n.lan "/usr/local/bin/user-jail.sh $username > /dev/null 2>&1 &" &
fi
done
# Wait for jails to be created. As of now they take about 30 seconds.
vhost::countdown 60
fi
fi
# cylce thru each node and add virtualhost
vhost::set-clusterNodes all
for n in "${clusterNodes[@]}"
do
if [ $n = `hostname -s` ]; then
/usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1 &
else
ssh $n.lan "/usr/local/bin/vhost-add.sh $virtualhost $username > /dev/null 2>&1 &"
fi
done