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`
HOSTNAME=`hostname`
ARGLINE="$@"
echo $ARGLINE
function debug
{
@ -17,7 +15,7 @@ function debug
function err
{
echo -e "FATAL ERROR: $1"
echo -e "FATAL ERROR: $@"
exit 1
}
@ -72,16 +70,30 @@ function send_notification
function become
{
local tempfile
if test -z "$BECOME"; then
echo $BASH_SOURCE
exit
chmod 0755 $tempfile
sudo su - --command=$tempfile
rm $tempfile
exit
local _bbfile
_bbfile=/tmp/bb_become.$_bb_myname
if test -z "$_bb_myname" -o -z "$_bb_mypath"; then
err "\$_bb_myname and/or \$_bb_mypath must bet set to user become function!"
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

View File

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