replace existence checks with function

This commit is contained in:
Asif Bacchus 2019-11-16 16:10:55 -07:00
parent a14985ed8a
commit bb9690f282
2 changed files with 47 additions and 31 deletions

View File

@ -1,3 +1,19 @@
{ {
"bookmarks": [] "bookmarks": [
{
"fsPath": "$ROOTPATH$/ab-nginx.sh",
"bookmarks": [
-1,
29,
86,
-1,
-1,
-1,
-1,
-1,
-1,
-1
]
}
]
} }

View File

@ -25,6 +25,23 @@ unset WEBROOT_DIR
unset vmount unset vmount
### functions
checkExist () {
if [ "$1" = "file" ]; then
if [ ! -f "$2" ]; then
printf "${err}\nCannot find file: '$2'. Exiting.\n${norm}"
exit 3
fi
elif [ "$1" = "dir" ]; then
if [ ! -d "$2" ]; then
printf "${err}\nCannot find directory: '$2'. Exiting.\n${norm}"
exit 3
fi
fi
return 0
}
scriptHelp () { scriptHelp () {
printf "\n${magenta}%80s\n" | tr " " "-" printf "\n${magenta}%80s\n" | tr " " "-"
printf "${norm}This is a simple helper script so you can avoid lengthy typing when working\n" printf "${norm}This is a simple helper script so you can avoid lengthy typing when working\n"
@ -56,6 +73,7 @@ scriptHelp () {
exit 0 exit 0
} }
### pre-requisite checks ### pre-requisite checks
# is user root or in the docker group? # is user root or in the docker group?
@ -67,57 +85,39 @@ if [ ! "$( id -u )" -eq 0 ]; then
fi fi
# does the params file exist? # does the params file exist?
if [ ! -f "./ab-nginx.params" ]; then checkExist 'file' './ab-nginx.params'
printf "${err}\nCannot find 'ab-nginx.params' file in the same directory as this script. Exiting.\n${norm}"
exit 3
fi
# read .params file # read .params file
. ./ab-nginx.params . ./ab-nginx.params
# check for certs if using SSL # check for certs if using SSL
if [ "$SSL_CERT" ]; then checkExist 'file' "$SSL_CERT"
if [ ! -f "$SSL_CERT" ]; then checkExist 'file' "$SSL_KEY"
printf "${err}\nCannot find specified SSL certificate file. Exiting.${norm}\n" checkExist 'file' "$SSL_CHAIN"
exit 5
fi
if [ ! -f "$SSL_KEY" ]; then
printf "${err}\nCannot find specified SSL private key file. Exiting.${norm}\n"
exit 5
fi
if [ ! -f "$SSL_CHAIN" ]; then
printf "${err}\nCannot find specified SSL certificate chain file. Exiting.${norm}\n"
exit 5
fi
fi
# check for DHparam if using TLS1.2 # check for DHparam if using TLS1.2
if [ "$TLS13_ONLY" = FALSE ]; then if [ "$TLS13_ONLY" = FALSE ]; then
if [ -z "$DH" ]; then if [ -z "$DH" ]; then
printf "${err}\nA DHparam file must be specified when using TLS 1.2. Exiting.${norm}\n" printf "${err}\nA DHparam file must be specified when using TLS 1.2. Exiting.${norm}\n"
exit 5 exit 5
elif [ ! -f "$DH" ]; then else
printf "${err}\nCannot find specified DHparam file. Exiting.${norm}\n" checkExist 'file' "$DH"
exit 5
fi fi
fi fi
# check if specified config directory exists # check if specified config directory exists
if [ "$CONFIG_DIR" ] && [ ! -d "$CONFIG_DIR" ]; then if [ "$CONFIG_DIR" ]; then
printf "${err}\nCannot find specified configuration file directory. Exiting.${norm}\n" checkExist 'dir' "$CONFIG_DIR"
exit 4
fi fi
# check if specified server-block directory exists # check if specified server-block directory exists
if [ "$SERVERS_DIR" ] && [ ! -d "$SERVERS_DIR" ]; then if [ "$SERVERS_DIR" ]; then
printf "${err}\nCannot find specified server-block file directory. Exiting.${norm}\n" checkExist 'dir' "$SERVERS_DIR"
exit 4
fi fi
# check if specified webroot directory exists # check if specified webroot directory exists
if [ "$WEBROOT_DIR" ] && [ ! -d "$WEBROOT_DIR" ]; then if [ "$WEBROOT_DIR" ]; then
printf "${err}\nCannot find specified webroot directory. Exiting.${norm}\n" checkExist 'dir' "$WEBROOT_DIR"
exit 4
fi fi
# set up volume mounts for config, servers, webroot # set up volume mounts for config, servers, webroot