Resolves merge request 6

This commit is contained in:
Lutchy Horace 2021-02-17 11:24:46 -05:00
parent c0a0c8c1ff
commit cb97128fb0
2 changed files with 98 additions and 38 deletions

View file

@ -6,13 +6,15 @@ DEBUG=0
set -e
function usage
{
function usage
{
echo "Usage: ${0}"
echo " --domain domain.tld"
echo " Domain to use when creating vhost"
echo " --proxypass x.x.x.x"
echo " IP of the backend server to pass traffic to"
echo " --root /var/www/html"
echo " Root directory of this vhost"
echo " --backend http://127.0.0.1:80"
echo " Hostname of the backend server to pass traffic to"
echo " --listenip x.x.x.x"
echo " IP to bind to when listening"
echo " --desc x.x.x.x"
@ -42,14 +44,14 @@ _cwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_bootstrap=${_cwd}/bootstrap.sh
# Init script
if test -f $_bootstrap; then
source $_bootstrap 2> /dev/null
else
echo "Unable to parse BOOTSTRAP: $_bootstrap"
exit 1
if test -f $_bootstrap; then
source $_bootstrap 2> /dev/null
else
echo "Unable to parse BOOTSTRAP: $_bootstrap"
exit 1
fi
OPTS=$(getopt -o h -l domain:,proxypass:,listenip:,desc: -n 'createVhosts' -- "$@")
OPTS=$(getopt -o h -l domain:,root:,backend:,listenip:,desc: -n 'createVhosts' -- "$@")
if [ "$?" -gt '0' ]; then
echo 'Failed to set command line arguments'
exit 1;
@ -58,15 +60,19 @@ fi
eval set -- "$OPTS"
_domain=false
_proxyip=false
_listenip=false
_root=""
_backend=""
_listenip=""
while true; do
case "$1" in
--domain )
_domain=$2
shift ;;
--proxypass )
_proxyip=$2
--root )
_root=$2
shift ;;
--backend )
_backend=$2
shift ;;
--listenip )
_listenip=$2
@ -84,15 +90,38 @@ if [[ $_domain = false ]]; then
err "You must set domain"
fi
if [[ $_proxyip = false ]]; then
err "You must set the proxy pass IP"
if test -n "$_root"; then
if ! test -d $_root; then
err "Path doesn't exists! $_root"
fi
_rootpath="root $_root;"
fi
if [[ $_listenip = false ]]; then
err "You must set listen ip"
if test -n "$_backend"; then
if ! validate_host $_backend; then
err "Invalid hostname: $_backend. Not resolvable!"
fi
_locationblock=$(cat <<- EOF
proxy_pass $_backend;
include proxy_params;
EOF
)
fi
echo "Creating Nginx Vhosts..."
if test -n "$_listenip"; then
if ! validate_ip $_listenip; then
err "Invalid IP: $_listenip"
fi
_listenip="$_listenip:"
else
warn "No listen ip specified, listing on all interfaces."
fi
if test -z "$_root" -a -z "$_backend"; then
err "You must specify either --root or --backend!"
fi
echo "Creating Nginx configuration..."
_vhost_conf_file=/etc/nginx/conf.d/${_domain}.conf
cat << EOF > $_vhost_conf_file
@ -101,43 +130,43 @@ cat << EOF > $_vhost_conf_file
## VHost: $_domain
## $_desc
server {
listen ${_listenip}:80;
listen ${_listenip}80;
server_name $_domain;
location /.well-known {
autoindex on;
}
location / {
return 302 https://${_domain}\$request_uri;
}
}
server {
listen ${_listenip}:443 http2 ssl;
listen ${_listenip}443 http2 ssl;
server_name $_domain;
$_rootpath
error_log /var/log/nginx/${_domain}.error.log;
access_log /var/log/nginx/${_domain}.access.log main;
ssl_certificate ssl/${_domain}-fullchain.crt;
ssl_certificate_key ssl/${_domain}.key;
location / {
proxy_pass https://${_proxyip}:443;
include proxy_params;
${_locationblock}
}
}
EOF
echo "Setting permissions on conf file..."
setfacl -m user:sysadmin:rw $_vhost_conf_file
#setfacl -m user:sysadmin:rw $_vhost_conf_file
echo "Stopping Nginx..."
stop_nginx
#stop_nginx
echo "Retrieving Let's Encrypt Certificate..."
get_cert
#get_cert
echo "Starting Nginx..."
start_nginx
#start_nginx