Refactored and fixed become

This commit is contained in:
Lutchy Horace 2021-03-17 16:10:32 -04:00
parent 2e865bfa02
commit 185d813c0e
2 changed files with 38 additions and 20 deletions

View File

@ -5,8 +5,6 @@
TODAY=`date` TODAY=`date`
HOSTNAME=`hostname` HOSTNAME=`hostname`
ARGLINE="$@"
echo $ARGLINE
function debug function debug
{ {
@ -17,7 +15,7 @@ function debug
function err function err
{ {
echo -e "FATAL ERROR: $1" echo -e "FATAL ERROR: $@"
exit 1 exit 1
} }
@ -72,16 +70,30 @@ function send_notification
function become function become
{ {
local tempfile local _bbfile
if test -z "$BECOME"; then _bbfile=/tmp/bb_become.$_bb_myname
echo $BASH_SOURCE
exit if test -z "$_bb_myname" -o -z "$_bb_mypath"; then
chmod 0755 $tempfile err "\$_bb_myname and/or \$_bb_mypath must bet set to user become function!"
sudo su - --command=$tempfile
rm $tempfile
exit
fi fi
if test -f "$_bbfile"; then
if [ $(id -u) = 0 ]; then
# Check if it's a login shell
if shopt -q login_shell; then
return 0
fi
fi
err "Unable to become: $_bbfile exists." \
"\nThis may happen if the script was interrupted." \
"\nIf this is the case, please remove '$_bbfile' and run this script '$_bb_mypath' again."
fi
touch $_bbfile
sudo bash --login $_bb_mypath "$@"
rm $_bbfile
exit
} }
function check_values function check_values

View File

@ -1,10 +1,10 @@
#!/bin/bash -x #!/bin/bash
# #
# Create Vhosts on VPS3 # Create Vhosts on VPS3
# #
DEBUG=0 DEBUG=0
set -e #set -e
function usage function usage
{ {
@ -37,6 +37,8 @@ function reload_nginx
_cwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" _cwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_bootstrap=${_cwd}/bootstrap.sh _bootstrap=${_cwd}/bootstrap.sh
_bb_myname=$(basename "$0")
_bb_mypath=$(realpath $BASH_SOURCE)
# Init script # Init script
if test -f $_bootstrap; then if test -f $_bootstrap; then
@ -46,6 +48,9 @@ else
exit 1 exit 1
fi fi
# gain priviledges
become "$@"
OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc: -n 'createVhosts' -- "$@") OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc: -n 'createVhosts' -- "$@")
if [ "$?" -gt '0' ]; then if [ "$?" -gt '0' ]; then
echo 'Failed to set command line arguments' echo 'Failed to set command line arguments'
@ -85,9 +90,13 @@ if [[ $_domain = false ]]; then
err "You must set domain" err "You must set domain"
fi fi
echo -n "Checking if $_root exists?"
if test -n "$_root"; then if test -n "$_root"; then
if ! test -d $_root; then if ! test -d $_root; then
err "Path doesn't exists! $_root" echo " Creating..."
mkdir -p $_root
else
echo " Yes!"
fi fi
_rootpath="root $_root;" _rootpath="root $_root;"
fi fi
@ -110,16 +119,13 @@ if test -n "$_listenip"; then
fi fi
_listenip="$_listenip:" _listenip="$_listenip:"
else else
warn "No listen ip specified, listing on all interfaces." warn "Listen ip not specified, listening on all interfaces."
fi fi
if test -z "$_root" -a -z "$_backend"; then if test -z "$_root" -a -z "$_backend"; then
err "You must specify either --root or --backend!" err "You must specify either --root or --backend!"
fi fi
# gain priviledges
become $@
echo -n "Checking if /srv/http-content-combined/ exists?" echo -n "Checking if /srv/http-content-combined/ exists?"
if ! test -d /srv/http-content-combined; then if ! test -d /srv/http-content-combined; then
echo " Creating..." echo " Creating..."
@ -128,7 +134,6 @@ else
echo " Yes!" echo " Yes!"
fi fi
echo "Creating Nginx configuration..."
_vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf _vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf
echo -n "Checking if $_vhost_conf_file exists? " echo -n "Checking if $_vhost_conf_file exists? "
@ -139,6 +144,7 @@ else
echo "No!" echo "No!"
fi fi
echo "Creating Nginx configuration..."
cat << EOF > $_vhost_conf_file cat << EOF > $_vhost_conf_file
#### Description #### Description
## Type: HTTP ## Type: HTTP