scripts/bootstrap.sh

164 lines
3.2 KiB
Bash
Raw Normal View History

2021-01-21 16:58:32 -05:00
#!/usr/bin/env bash
#
# Collection of reusable functions and variables
2021-02-17 11:24:46 -05:00
#
2021-01-21 16:58:32 -05:00
TODAY=`date`
HOSTNAME=`hostname`
function debug
{
if [ "${DEBUG}" = '1' ]; then
echo -e "DEBUG: $1"
fi
}
function err
{
2021-03-17 16:10:32 -04:00
echo -e "FATAL ERROR: $@"
2021-01-21 16:58:32 -05:00
exit 1
}
2021-02-17 11:24:46 -05:00
function warn
{
echo -e "WARNING: $1"
}
function validate_host
{
2021-03-17 17:47:19 -04:00
local _ret=0
echo "$1" | grep -P '^(http|https):\/\/(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])(:[0-9]+)?$' > /dev/null 2>&1
2021-02-17 11:24:46 -05:00
if [ $? -eq 0 ]; then
debug "Checking host is resolvable: $1"
2021-03-17 17:47:19 -04:00
# Add --insecure becase remote servers may sometimes have self-signed certs
2021-03-17 18:25:33 -04:00
if ! curl --insecure --max-time 5 $1 > /dev/null 2>&1; then
2021-03-17 17:47:19 -04:00
_ret=1
debug "Host '$1' is not resolvable!"
fi
2021-02-17 11:24:46 -05:00
fi
2021-03-17 17:47:19 -04:00
return $_ret
2021-02-17 11:24:46 -05:00
}
function validate_ip
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
2021-01-21 16:58:32 -05:00
function send_notification
{
opt=""
if [ -n "$FROM" ]; then
local mailcmd=$(readlink -f `which mail`)
if [ "${mailcmd}" = "/usr/bin/bsd-mailx" ]; then
opt="-a 'From: ${FROM}'"
2021-02-17 11:24:46 -05:00
else
2021-01-21 16:58:32 -05:00
opt="-r $FROM"
fi
fi
echo "Sending notification."
echo -e "$2" | mail $opt -s "$1" ${EMAIL}
}
2021-02-17 12:47:48 -05:00
function become
{
2021-03-17 16:10:32 -04:00
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."
2021-03-17 13:52:01 -04:00
fi
2021-03-17 16:10:32 -04:00
2021-03-17 17:56:09 -04:00
debug "Escalating priviledge!"
2021-03-17 16:10:32 -04:00
touch $_bbfile
sudo bash --login $_bb_mypath "$@"
rm $_bbfile
exit
2021-02-17 12:47:48 -05:00
}
2021-01-21 16:58:32 -05:00
function check_values
{
2021-02-17 11:24:46 -05:00
2021-01-21 16:58:32 -05:00
a_name=$1[@]
a_array=("${!a_name}")
if [ -z ${!a_name+x} ]; then
return
fi
2021-02-17 11:24:46 -05:00
2021-01-21 16:58:32 -05:00
if [ ${a_array[$2]} == true ]; then
if [ "x${4}" = 'x' ]; then
echo $3
exit 1
fi
fi
2021-02-17 11:24:46 -05:00
2021-01-21 16:58:32 -05:00
}
function run_cmd {
debug "Executing: '$1 $2'"
if [ ! -e $LOG ]; then
touch $LOG
fi
## Check if command exists on system
if ! cmd_exists $1; then
2021-01-21 16:58:32 -05:00
err "$1: command not found"
fi
## Check if debug is set and send command output to shell
## Some commands send animated text that can corrupt the log
if test -n "${DEBUG}"; then
2021-02-17 11:24:46 -05:00
debug "DEBUG variable set, redirecting command output to console"
2021-01-21 16:58:32 -05:00
$1 $2
if [ $? -gt 0 ]; then
err "Failed running command '$1 $2'"
fi
else
$1 $2 2>> $LOG
# FIXME: This logic might need re-work?
if [ $? -gt 0 ]; then
echo "ERROR: Failed running command '$1 $2'."
echo "Displaying last 20 lines of ${LOG}..."
echo
tail $LOG
echo
exit 1
fi
fi
2021-02-17 11:24:46 -05:00
2021-01-21 16:58:32 -05:00
}
function cmd_exists
{
if ! command -v $1 > /dev/null 2>&1; then
return 1
fi
return 0
}