2019-10-17 17:13:57 -06:00
#!/bin/sh
#
2021-01-07 15:22:36 -07:00
# start ab-nginx container using params file variables
2019-10-17 17:13:57 -06:00
#
2021-07-27 14:41:32 -06:00
# TODO: add stop & stop and remove commands
2019-10-17 17:13:57 -06:00
# text formatting presets
2021-01-06 00:57:00 -07:00
if command -v tput >/dev/null; then
2021-07-26 01:30:35 -06:00
cyan = $( tput bold) $( tput setaf 6)
err = $( tput bold) $( tput setaf 1)
magenta = $( tput sgr0) $( tput setaf 5)
norm = $( tput sgr0)
yellow = $( tput sgr0) $( tput setaf 3)
width = $( tput cols)
2021-01-06 00:19:54 -07:00
else
2021-07-26 01:30:35 -06:00
cyan = ''
err = ''
magenta = ''
norm = ''
yellow = ''
width = 80
2021-01-06 00:19:54 -07:00
fi
2019-10-17 17:13:57 -06:00
### parameter defaults
2021-07-26 00:54:27 -06:00
doShell = false
2021-07-27 15:26:53 -06:00
doStatus = false
2019-11-16 15:36:13 -07:00
container_name = "ab-nginx"
NETWORK = 'nginx_network'
SUBNET = '172.31.254.0/24'
2019-10-17 21:43:28 -06:00
HTTP_PORT = 80
HTTPS_PORT = 443
2021-07-27 14:59:16 -06:00
CONFIG_DIR = ""
SERVERS_DIR = ""
WEBROOT_DIR = ""
volumeMounts = ""
2019-10-17 17:13:57 -06:00
2019-11-16 16:10:55 -07:00
### functions
2021-01-06 00:57:00 -07:00
checkExist( ) {
2021-07-26 01:30:35 -06:00
if [ " $1 " = 'file' ] ; then
if [ ! -f " $2 " ] ; then
printf " %s\nCannot find file: ' $2 '. Exiting.\n%s " " $err " " $norm "
2021-07-27 15:39:30 -06:00
exit 1
2021-07-26 01:30:35 -06:00
fi
elif [ " $1 " = 'dir' ] ; then
if [ ! -d " $2 " ] ; then
printf " %s\nCannot find directory: ' $2 '. Exiting.\n $%s " " $err " " $norm "
2021-07-27 15:39:30 -06:00
exit 1
2021-07-26 01:30:35 -06:00
fi
2021-01-06 00:57:00 -07:00
fi
2021-07-26 01:30:35 -06:00
return 0
2019-11-16 16:10:55 -07:00
}
2021-01-06 00:57:00 -07:00
scriptHelp( ) {
2021-07-26 01:30:35 -06:00
printf "\n%s" " $magenta "
printf '%.0s-' $( seq " $width " )
printf "\n%s" " $norm "
2021-07-27 14:37:57 -06:00
textBlock "This is a simple helper script so you can avoid typing lengthy commands when working with the ab-nginx container."
textBlock "The script reads the contents of 'ab-nginx.params' and constructs various 'docker run' commands based on that file. The biggest time-saver is working with certificates. If they are specified in the params file, the script will automatically bind-mount them so nginx serves content via SSL by default."
2021-07-26 01:30:35 -06:00
newline
2021-07-27 14:37:57 -06:00
textBlock "If you run the script with no parameters, it will execute the container 'normally': Run in detached mode with nginx automatically launched. If you specified certificates, nginx will serve over SSL by default."
textBlock "Note: Containers (except shell) are always set to restart 'unless-stopped'. You must remove them manually if desired."
2021-07-26 01:30:35 -06:00
printf "%s" " $magenta "
newline
2021-07-27 14:37:57 -06:00
textBlock "The script has the following (optional) parameters:"
textBlockParam 'parameter in cyan' 'default in yellow'
2021-07-26 01:30:35 -06:00
newline
2021-07-27 14:37:57 -06:00
textBlockParam '-n|--name' 'ab-nginx'
textBlock "Set the name of the container, otherwise the default will be used."
2021-07-26 01:30:35 -06:00
newline
2021-07-27 15:26:53 -06:00
textBlockParam '-s|--shell' 'off: run in detached mode'
2021-07-27 14:37:57 -06:00
textBlock "Enter the container using an interactive ASH/BusyBox shell. This happens after startup operations but *before* nginx is actually started. This is a great way to see configuration changes possibly stopping nginx from starting normally."
2021-07-27 15:26:53 -06:00
newline
textBlockParam '--status'
textBlock "Run a search for all AB-NGINX containers and display their name and status."
2021-07-26 01:30:35 -06:00
printf "%s" " $yellow "
newline
2021-07-27 14:37:57 -06:00
textBlock"More information can be found at: https://git.asifbacchus.dev/ab-docker/ab-nginx/wiki"
2021-07-26 01:30:35 -06:00
printf "\n%s" " $magenta "
printf '%.0s-' $( seq " $width " )
printf "\n%s" " $norm "
exit 0
2019-10-17 17:13:57 -06:00
}
2021-01-06 00:57:00 -07:00
newline( ) {
2021-07-26 01:30:35 -06:00
printf "\n"
2021-01-06 00:19:54 -07:00
}
2021-07-27 14:37:57 -06:00
textBlock( ) {
2021-07-26 01:30:35 -06:00
printf "%s\n" " $1 " | fold -w " $width " -s
2021-01-06 00:19:54 -07:00
}
2021-07-27 14:37:57 -06:00
textBlockParam( ) {
2021-07-26 01:30:35 -06:00
if [ -z " $2 " ] ; then
# no default
printf "%s%s%s\n" " $cyan " " $1 " " $norm "
else
# default param provided
printf "%s%s %s(%s)%s\n" " $cyan " " $1 " " $yellow " " $2 " " $norm "
fi
2021-01-06 00:19:54 -07:00
}
2019-10-17 17:13:57 -06:00
### pre-requisite checks
2021-01-06 02:35:29 -07:00
# is docker installed?
2021-07-26 01:30:35 -06:00
if ! command -v docker >/dev/null; then
printf "%s\nCannot find docker... is it installed?\n%s" " $err " " $norm "
exit 2
2021-01-06 02:35:29 -07:00
fi
2019-10-17 17:13:57 -06:00
# is user root or in the docker group?
2021-01-06 00:57:00 -07:00
if [ ! " $( id -u) " -eq 0 ] ; then
2021-07-26 01:30:35 -06:00
if ! id -Gn | grep docker >/dev/null; then
printf " %s\nYou must either be root or in the 'docker' group to run this script since you must be able to actually start the container! Exiting.\n $%s " " $err " " $norm "
2021-07-27 15:39:30 -06:00
exit 3
2021-07-26 01:30:35 -06:00
fi
2019-10-17 17:13:57 -06:00
fi
# does the params file exist?
2019-11-16 16:10:55 -07:00
checkExist 'file' './ab-nginx.params'
2019-10-17 17:13:57 -06:00
2019-10-17 17:57:03 -06:00
# read .params file
2021-01-06 00:57:00 -07:00
. "./ab-nginx.params"
2019-10-17 17:57:03 -06:00
2019-11-16 17:22:16 -07:00
# fix case of TLS13_ONLY var
if [ " $TLS13_ONLY " ] ; then
2021-07-26 01:30:35 -06:00
TLS13_ONLY = $( printf "%s" " $TLS13_ONLY " | tr "[:lower:]" "[:upper:]" )
2019-11-16 17:22:16 -07:00
fi
2019-10-17 17:13:57 -06:00
# check for certs if using SSL
2019-11-18 05:20:04 -07:00
if [ " $SSL_CERT " ] ; then checkExist 'file' " $SSL_CERT " ; fi
if [ " $SSL_KEY " ] ; then checkExist 'file' " $SSL_KEY " ; fi
if [ " $SSL_CHAIN " ] ; then checkExist 'file' " $SSL_CHAIN " ; fi
2019-10-17 17:13:57 -06:00
2019-10-17 18:23:07 -06:00
# check if specified config directory exists
2019-11-16 16:10:55 -07:00
if [ " $CONFIG_DIR " ] ; then
2021-07-26 01:30:35 -06:00
checkExist 'dir' " $CONFIG_DIR "
2019-10-17 18:23:07 -06:00
fi
2019-10-18 00:16:12 -06:00
# check if specified server-block directory exists
2019-11-16 16:10:55 -07:00
if [ " $SERVERS_DIR " ] ; then
2021-07-26 01:30:35 -06:00
checkExist 'dir' " $SERVERS_DIR "
2019-10-18 00:16:12 -06:00
fi
2019-10-17 18:23:07 -06:00
# check if specified webroot directory exists
2019-11-16 16:10:55 -07:00
if [ " $WEBROOT_DIR " ] ; then
2021-07-26 01:30:35 -06:00
checkExist 'dir' " $WEBROOT_DIR "
2019-10-17 18:23:07 -06:00
fi
2019-10-17 17:13:57 -06:00
2019-11-16 16:52:44 -07:00
# set up volume mounts
if [ " $CONFIG_DIR " ] ; then
2021-07-27 14:58:01 -06:00
volumeMounts = " ${ volumeMounts } -v $CONFIG_DIR :/etc/nginx/config "
2019-11-16 16:52:44 -07:00
fi
if [ " $SERVERS_DIR " ] ; then
2021-07-27 14:58:01 -06:00
volumeMounts = " ${ volumeMounts } -v $SERVERS_DIR :/etc/nginx/sites "
2019-11-16 16:52:44 -07:00
fi
2019-11-16 17:18:24 -07:00
if [ " $SNIPPETS_DIR " ] ; then
2021-07-27 14:58:01 -06:00
volumeMounts = " ${ volumeMounts } -v $SNIPPETS_DIR :/etc/nginx/snippets "
2019-11-16 17:18:24 -07:00
fi
2019-11-16 16:52:44 -07:00
if [ " $WEBROOT_DIR " ] ; then
2021-07-27 14:58:01 -06:00
volumeMounts = " ${ volumeMounts } -v $WEBROOT_DIR :/usr/share/nginx/html "
2019-10-17 18:53:22 -06:00
fi
2019-11-16 16:52:44 -07:00
# trim leading whitespace
2021-07-27 14:58:01 -06:00
volumeMounts = ${ volumeMounts ##[[ : space : ]] }
2019-10-17 18:53:22 -06:00
2021-01-06 01:19:37 -07:00
# handle null HOSTNAMES
if [ -z " $HOSTNAMES " ] ; then HOSTNAMES = "_" ; fi
2019-10-17 17:13:57 -06:00
# process startup parameters
while [ $# -gt 0 ] ; do
2021-07-26 01:30:35 -06:00
case " $1 " in
-h | -\? | --help)
# display help
scriptHelp
exit 0
; ;
-s | --shell)
# start shell instead of default CMD
doShell = true
; ;
-n | --name)
# container name
if [ -z " $2 " ] ; then
printf "%s\nNo container name specified. Exiting.\n%s" " $err " " $norm "
exit 1
fi
container_name = " $2 "
shift
; ;
2021-07-27 15:26:53 -06:00
--status)
# find containers and check their status
doStatus = true
; ;
2021-07-26 01:30:35 -06:00
*)
printf "%s\nUnknown option: %s\n" " $err " " $1 "
printf "Use '--help' for valid options.\n\n%s" " $norm "
exit 1
; ;
esac
2019-10-17 17:13:57 -06:00
shift
done
2021-07-27 15:26:53 -06:00
#
# status check
if [ " $doStatus " = "true" ] ; then
printf "\nFound the following AB-NGINX containers:\n"
docker ps -a --filter "label=dev.asifbacchus.docker.internalName=ab-nginx"
printf "\n"
exit 0
fi
2019-11-16 15:36:13 -07:00
# create network if it doesn't already exist
2021-01-06 00:57:00 -07:00
docker network inspect ${ NETWORK } >/dev/null 2>& 1 ||
2021-07-26 01:30:35 -06:00
docker network create \
--attachable \
--driver= bridge \
--subnet= ${ SUBNET } \
${ NETWORK }
2019-10-17 17:13:57 -06:00
# run without TLS
2021-01-06 00:57:00 -07:00
if [ -z " $SSL_CERT " ] ; then
2021-07-26 01:30:35 -06:00
if [ " $doShell " = 'true' ] ; then
# exec shell
printf "%s\nRunning SHELL on %s...%s\n" " $cyan " " $container_name " " $norm "
# shellcheck disable=SC2086
docker run --rm -it --name " ${ container_name } " \
--env-file ab-nginx.params \
2021-07-26 01:36:41 -06:00
--user= " ${ NGINX_UID :- 8080 } : ${ NGINX_GID :- 8080 } " \
2021-07-26 01:30:35 -06:00
-e SERVER_NAMES = " $HOSTNAMES " \
2021-07-27 14:58:01 -06:00
${ volumeMounts } \
2021-07-26 01:30:35 -06:00
--network= ${ NETWORK } \
-p ${ HTTP_PORT } :80 \
docker.asifbacchus.dev/nginx/ab-nginx:latest /bin/sh
else
# exec normally
printf "%s\nRunning NGINX on %s...%s\n" " $cyan " " $container_name " " $norm "
# shellcheck disable=SC2086
docker run -d --name " ${ container_name } " \
--env-file ab-nginx.params \
2021-07-26 01:36:41 -06:00
--user= " ${ NGINX_UID :- 8080 } : ${ NGINX_GID :- 8080 } " \
2021-07-26 01:30:35 -06:00
-e SERVER_NAMES = " $HOSTNAMES " \
2021-07-27 14:58:01 -06:00
${ volumeMounts } \
2021-07-26 01:30:35 -06:00
--network= ${ NETWORK } \
-p ${ HTTP_PORT } :80 \
--restart unless-stopped \
docker.asifbacchus.dev/nginx/ab-nginx:${ TAG :- latest }
fi
2021-07-26 00:49:51 -06:00
# run with TLS
else
2021-07-26 00:54:27 -06:00
if [ " $doShell " = 'true' ] ; then
2021-07-26 00:49:51 -06:00
if [ " $TLS13_ONLY " = 'FALSE' ] ; then
printf "%s\nRunning SHELL on %s (TLS 1.2)...%s\n" " $cyan " " $container_name " " $norm "
else
printf "%s\nRunning SHELL on %s (TLS 1.3)...%s\n" " $cyan " " $container_name " " $norm "
fi
# shellcheck disable=SC2086
docker run --rm -it --name " ${ container_name } " \
2021-07-26 01:30:35 -06:00
--env-file ab-nginx.params \
2021-07-26 01:36:41 -06:00
--user= " ${ NGINX_UID :- 8080 } : ${ NGINX_GID :- 8080 } " \
2021-07-26 01:30:35 -06:00
-e SERVER_NAMES = " $HOSTNAMES " \
2021-07-27 14:58:01 -06:00
${ volumeMounts } \
2021-07-26 01:30:35 -06:00
--network= ${ NETWORK } \
-v " $SSL_CERT " :/certs/fullchain.pem:ro \
-v " $SSL_KEY " :/certs/privkey.pem:ro \
-v " $SSL_CHAIN " :/certs/chain.pem:ro \
-p ${ HTTP_PORT } :80 -p ${ HTTPS_PORT } :443 \
docker.asifbacchus.dev/nginx/ab-nginx:${ TAG :- latest } /bin/sh
2021-07-26 00:49:51 -06:00
else
if [ " $TLS13_ONLY " = 'FALSE' ] ; then
printf "%s\nRunning NGINX on %s (TLS 1.2)...%s\n" " $cyan " " $container_name " " $norm "
else
printf "%s\nRunning NGINX on %s (TLS 1.3)...%s\n" " $cyan " " $container_name " " $norm "
fi
# shellcheck disable=SC2086
docker run -d --name " ${ container_name } " \
2021-07-26 01:30:35 -06:00
--env-file ab-nginx.params \
2021-07-26 01:36:41 -06:00
--user= " ${ NGINX_UID :- 8080 } : ${ NGINX_GID :- 8080 } " \
2021-07-26 01:30:35 -06:00
-e SERVER_NAMES = " $HOSTNAMES " \
2021-07-27 14:58:01 -06:00
${ volumeMounts } \
2021-07-26 01:30:35 -06:00
--network= ${ NETWORK } \
-v " $SSL_CERT " :/certs/fullchain.pem:ro \
-v " $SSL_KEY " :/certs/privkey.pem:ro \
-v " $SSL_CHAIN " :/certs/chain.pem:ro \
-p ${ HTTP_PORT } :80 -p ${ HTTPS_PORT } :443 \
--restart unless-stopped \
docker.asifbacchus.dev/nginx/ab-nginx:${ TAG :- latest }
2021-07-26 00:49:51 -06:00
fi
2019-10-17 17:13:57 -06:00
fi
### exit gracefully
2021-07-26 00:29:35 -06:00
exit 0
2021-07-27 15:39:30 -06:00
#
# exit return codes
# 0: normal exit, no errors
# 1: missing or invalid parameter
# 2: cannot find docker
# 3: incorrect permissions to access docker
# 1x: operation errors
# 11 no container found with specified name
#EOF