Browse Source

add command params

master
Simon Vieille 4 months ago
parent
commit
2bc84967d4
  1. 138
      bin/vhost-add
  2. 18
      share/bin/form_choices
  3. 32
      share/bin/form_input
  4. 21
      share/bin/form_yes_no

138
bin/vhost-add

@ -12,33 +12,105 @@ cd "$SCRIPT_PATH/../"
. ./etc/config
error() {
if [ -n "$1" ]; then
printf "%s\\n" "$1" >/dev/stderr
fi
exit 1
}
exit_if_empty() {
if [ -z "$1" ]; then
if [ -n "$2" ]; then
printf "%s\\n" "$2"
fi
exit 1
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\""
if [ -n "$PHP_VERSION" ]; then
COMMAND="$COMMAND -d \"$PHP_VERSION\""
fi
COMMAND="$COMMAND -l \"Version of PHP\""
for PHP_VERSION in $PHP_VERSIONS; do
PHP_VERSION_NORMALISED="$(echo "$PHP_VERSION" | tr -dc 1234567890)"
for VERSION in $PHP_VERSIONS; do
VERSION_NORMALISED="$(echo "$VERSION" | tr -dc 1234567890)"
eval "PHP_LABEL=\$PHP_${PHP_VERSION_NORMALISED}_LABEL"
eval "LABEL=\$PHP_${VERSION_NORMALISED}_LABEL"
COMMAND="$COMMAND \"$PHP_VERSION\" \"$PHP_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,14 +179,18 @@ export WEB_HTTPS_DEFAULT_CERT_PRIVKEY
export WEB_HTTPS_DEFAULT_CERT_CHAIN
export WEB_BIND_ADDRESS
TMP_FILE="/tmp/vhost-add-$(tr -dc "qwertQWERTasdfgASDFGzxcvbZXCVB" < /dev/urandom | head -c 16)"
touch "$TMP_FILE"
chmod 600 "$TMP_FILE"
template summary > "$TMP_FILE"
whiptail --textbox "$TMP_FILE" --title "$TITLE - PREVIEW" 25 80
rm "$TMP_FILE"
CONTINUE="$(form_yes_no -t "$TITLE" -l "Do you confirm?" -n)"
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"
template summary > "$TMP_FILE"
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"
whiptail --textbox "$TMP_FILE" --title "$TITLE - FINISH" 25 80
if [ $INTERACTIVE -eq 1 ]; then
whiptail --textbox "$TMP_FILE" --title "$TITLE - FINISH" 25 80
fi
cat "$TMP_FILE"
rm "$TMP_FILE"

18
share/bin/form_choices

@ -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
shift $((OPTIND-1))
if [ $INTERACTIVE -eq 0 ]; then
printf "$DEFAULT"
else
shift $((OPTIND-1))
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
whiptail --title "$TITLE" --menu "$LABEL" $HEIGHT $WIDTH $MENU_HEIGHT "$@" 3>&1 1>&2 2>&3

32
share/bin/form_input

@ -25,22 +25,26 @@ if [ $ERROR -eq 1 ]; then
exit 1
fi
VALUE=
DO_ASK=1
if [ $INTERACTIVE -eq 0 ]; then
printf "%s" "$DEFAULT"
else
VALUE=
DO_ASK=1
while [ $DO_ASK -eq 1 ]; do
VALUE="$(whiptail --title "$TITLE" --inputbox "$LABEL" $HEIGHT $WIDTH "$DEFAULT" 3>&1 1>&2 2>&3)"
STATUS=$?
while [ $DO_ASK -eq 1 ]; do
VALUE="$(whiptail --title "$TITLE" --inputbox "$LABEL" $HEIGHT $WIDTH "$DEFAULT" 3>&1 1>&2 2>&3)"
STATUS=$?
if [ $STATUS -eq 0 ]; then
if [ $REQUIRED -eq 1 ] && [ -z "$VALUE" ]; then
DO_ASK=1
if [ $STATUS -eq 0 ]; then
if [ $REQUIRED -eq 1 ] && [ -z "$VALUE" ]; then
DO_ASK=1
else
DO_ASK=0
fi
else
DO_ASK=0
exit $STATUS
fi
else
exit $STATUS
fi
done
done
echo "$VALUE"
printf "%s" "$VALUE"
fi

21
share/bin/form_yes_no

@ -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 (whiptail --title "$TITLE" --yesno "$LABEL" $PARAMS $HEIGHT $WIDTH 3>&1 1>&2 2>&3) then
printf "yes"
if [ $INTERACTIVE -eq 0 ]; then
printf "$DEFAULT"
else
printf "no"
if (whiptail --title "$TITLE" --yesno "$LABEL" $PARAMS $HEIGHT $WIDTH 3>&1 1>&2 2>&3) then
printf "yes"
else
printf "no"
fi
fi
Loading…
Cancel
Save