add command params

This commit is contained in:
Simon Vieille 2021-02-21 16:17:45 +01:00
parent 94dea99a5f
commit 2bc84967d4
4 changed files with 163 additions and 44 deletions

View File

@ -12,33 +12,105 @@ cd "$SCRIPT_PATH/../"
. ./etc/config
exit_if_empty() {
if [ -z "$1" ]; then
if [ -n "$2" ]; then
printf "%s\\n" "$2"
error() {
if [ -n "$1" ]; then
printf "%s\\n" "$1" >/dev/stderr
fi
exit 1
}
exit_if_empty() {
if [ -z "$1" ]; then
error "$2"
fi
}
usage() {
cat << EOF
USAGE:
$0 [options]
OPTIONS:
-d The domain name
-a Aliase(s)
-m value Enable *http* or *https*
-r Force redirection of http to https
-p value Enable PHP with the given version (eg: 8.0)
-u value Username in the system
-i Enable script interactivity
-h Show this help
EOF
}
#######################
# Injection of params #
#######################
ERROR=0
INTERACTIVE=1
SYSTEM_USER_USERNAME="web"
PHP_ENABLED="yes"
if [ -n "$1" ]; then
INTERACTIVE=0
WEB_HTTP="no"
WEB_HTTPS="no"
WEB_HTTPS_FORCE="no"
PHP_ENABLED="no"
fi
while getopts "hid:a:u:p:m:r" option; do
case "$option" in
d) DOMAIN="$OPTARG";;
a) DOMAIN_ALIASES="$OPTARG";;
u) SYSTEM_USER_USERNAME="web$(echo $OPTARG | sed 's/^web//')";;
p)
PHP_VERSION="$OPTARG"
PHP_ENABLED="yes"
;;
r) WEB_HTTPS_FORCE="yes";;
m)
if [ "$OPTARG" = "http" ]; then
WEB_HTTP="yes"
elif [ "$OPTARG" = "https" ]; then
WEB_HTTPS="yes"
fi
;;
i) INTERACTIVE=1;;
h) usage; exit 0;;
:) ERROR=1;;
?) ERROR=1;;
esac
done
if [ $ERROR -eq 1 ]; then
error "Invalid parameter\\n$(usage)"
fi
export INTERACTIVE
##########################
# Collect of information #
##########################
TITLE="VHOST-MANAGER - NEW WEBSITE"
DOMAIN="$(form_input -t "$TITLE" -l "Domain" -r)"
DOMAIN="$(form_input -t "$TITLE" -l "Domain" -r -d "$DOMAIN")"
exit_if_empty "$DOMAIN" "Aborded! (DOMAIN_EMPTY)"
DOMAIN_ALIASES="$(form_input -t "$TITLE" -l "Aliase(s)")"
DOMAIN_ALIASES="$(form_input -t "$TITLE" -l "Aliase(s)" -d "$DOMAIN_ALIASES")"
WEB_HTTP="$(form_yes_no -t "$TITLE" -l "Support of HTTP (recommended)")"
WEB_HTTPS="$(form_yes_no -t "$TITLE" -l "Support of HTTPS (recommended)")"
WEB_HTTP="$(form_yes_no -t "$TITLE" -l "Support of HTTP (recommended)" -d "$WEB_HTTP")"
WEB_HTTPS="$(form_yes_no -t "$TITLE" -l "Support of HTTPS (recommended)" -d "$WEB_HTTPS")"
if [ "$WEB_HTTP" = "yes" ] && [ "$WEB_HTTPS" = "yes" ]; then
WEB_HTTPS_FORCE="$(form_yes_no -t "$TITLE" -l "Redirect HTTP to HTTPS (recommended)")"
WEB_HTTPS_FORCE="$(form_yes_no -t "$TITLE" -l "Redirect HTTP to HTTPS (recommended)" -d "$WEB_HTTPS_FORCE")"
else
WEB_HTTPS_FORCE="no"
fi
@ -46,17 +118,23 @@ fi
DOCUMENT_ROOT="$(form_input -t "$TITLE" -l "Document root\n$WEB_ROOT_PATH/$DOMAIN/" -d "web/")"
DOCUMENT_ROOT="$WEB_ROOT_PATH/$DOMAIN/$DOCUMENT_ROOT"
PHP_ENABLED="$(form_yes_no -t "$TITLE" -l "Support of PHP")"
PHP_ENABLED="$(form_yes_no -t "$TITLE" -l "Support of PHP" -d "$PHP_ENABLED")"
if [ "$PHP_ENABLED" = "yes" ]; then
COMMAND="form_choices -t \"$TITLE\" -l \"Version of PHP\""
COMMAND="form_choices -t \"$TITLE\""
for PHP_VERSION in $PHP_VERSIONS; do
PHP_VERSION_NORMALISED="$(echo "$PHP_VERSION" | tr -dc 1234567890)"
if [ -n "$PHP_VERSION" ]; then
COMMAND="$COMMAND -d \"$PHP_VERSION\""
fi
eval "PHP_LABEL=\$PHP_${PHP_VERSION_NORMALISED}_LABEL"
COMMAND="$COMMAND -l \"Version of PHP\""
COMMAND="$COMMAND \"$PHP_VERSION\" \"$PHP_LABEL\""
for VERSION in $PHP_VERSIONS; do
VERSION_NORMALISED="$(echo "$VERSION" | tr -dc 1234567890)"
eval "LABEL=\$PHP_${VERSION_NORMALISED}_LABEL"
COMMAND="$COMMAND \"$VERSION\" \"$LABEL\""
done
PHP_VERSION="$(eval "$COMMAND")"
@ -66,7 +144,7 @@ fi
USER_PASSWORD="$(tr -dc "0123456789!@#$%()[]*@<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" < /dev/urandom | head -c 20)"
SYSTEM_USER_USERNAME="$(form_input -t "$TITLE" -l "System username" -d "web" -r)"
SYSTEM_USER_USERNAME="$(form_input -t "$TITLE" -l "System username" -d "$SYSTEM_USER_USERNAME" -r)"
SYSTEM_USER_PASSWORD=$(
PASSWORD="$USER_PASSWORD"
@ -101,6 +179,9 @@ export WEB_HTTPS_DEFAULT_CERT_PRIVKEY
export WEB_HTTPS_DEFAULT_CERT_CHAIN
export WEB_BIND_ADDRESS
if [ $INTERACTIVE -eq 0 ]; then
CONTINUE="yes"
else
TMP_FILE="/tmp/vhost-add-$(tr -dc "qwertQWERTasdfgASDFGzxcvbZXCVB" < /dev/urandom | head -c 16)"
touch "$TMP_FILE"
chmod 600 "$TMP_FILE"
@ -109,6 +190,7 @@ whiptail --textbox "$TMP_FILE" --title "$TITLE - PREVIEW" 25 80
rm "$TMP_FILE"
CONTINUE="$(form_yes_no -t "$TITLE" -l "Do you confirm?" -n)"
fi
if [ "$CONTINUE" = "no" ]; then
printf "Aborded!\\n"
@ -179,6 +261,10 @@ TMP_FILE="/tmp/vhost-add-$(tr -dc "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS
touch "$TMP_FILE"
chmod 600 "$TMP_FILE"
template result > "$TMP_FILE"
if [ $INTERACTIVE -eq 1 ]; then
whiptail --textbox "$TMP_FILE" --title "$TITLE - FINISH" 25 80
fi
cat "$TMP_FILE"
rm "$TMP_FILE"

View File

@ -6,6 +6,7 @@ ERROR=0
HEIGHT=25
MENU_HEIGHT=16
WIDTH=50
DEFAULT=
while getopts "t:l:d:h:w:m:r" option; do
case "$option" in
@ -14,6 +15,10 @@ while getopts "t:l:d:h:w:m:r" option; do
h) HEIGHT="$OPTARG";;
m) MENU_HEIGHT="$OPTARG";;
w) WIDTH="$OPTARG";;
d)
DEFAULT="$OPTARG"
shift 2
;;
:) ERROR=1;;
?) ERROR=1;;
esac
@ -23,6 +28,15 @@ if [ $ERROR -eq 1 ]; then
exit 1
fi
if [ $INTERACTIVE -eq 0 ]; then
printf "$DEFAULT"
else
shift $((OPTIND-1))
whiptail --title "$TITLE" --menu "$LABEL" $HEIGHT $WIDTH $MENU_HEIGHT "$@" 3>&1 1>&2 2>&3
if [ -n "$DEFAULT" ]; then
DEFAULT="--default-item $DEFAULT"
fi
whiptail --title "$TITLE" --menu "$LABEL" $HEIGHT $WIDTH $MENU_HEIGHT $DEFAULT "$@" 3>&1 1>&2 2>&3
fi

View File

@ -25,6 +25,9 @@ if [ $ERROR -eq 1 ]; then
exit 1
fi
if [ $INTERACTIVE -eq 0 ]; then
printf "%s" "$DEFAULT"
else
VALUE=
DO_ASK=1
@ -43,4 +46,5 @@ while [ $DO_ASK -eq 1 ]; do
fi
done
echo "$VALUE"
printf "%s" "$VALUE"
fi

View File

@ -6,6 +6,7 @@ ERROR=0
HEIGHT=8
WIDTH=50
PARAMS=
DEFAULT=
while getopts "t:l:d:h:w:rn" option; do
case "$option" in
@ -14,6 +15,16 @@ while getopts "t:l:d:h:w:rn" option; do
h) HEIGHT="$OPTARG";;
w) WIDTH="$OPTARG";;
n) PARAMS="$PARAMS --defaultno";;
d)
if [ -n "$OPTARG" ]; then
if [ "$OPTARG" = "no" ]; then
PARAMS="$PARAMS --defaultno"
DEFAULT="no"
elif [ "$OPTARG" = "yes" ]; then
DEFAULT="yes"
fi
fi
;;
:) ERROR=1;;
?) ERROR=1;;
esac
@ -23,8 +34,12 @@ if [ $ERROR -eq 1 ]; then
exit 1
fi
if [ $INTERACTIVE -eq 0 ]; then
printf "$DEFAULT"
else
if (whiptail --title "$TITLE" --yesno "$LABEL" $PARAMS $HEIGHT $WIDTH 3>&1 1>&2 2>&3) then
printf "yes"
else
printf "no"
fi
fi