Advertisement
onejdc

nginx_modsite

Jul 4th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.95 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##
  4. #  File:
  5. #    nginx_modsite
  6. #  Description:
  7. #    Provides a basic script to automate enabling and disabling websites found
  8. #    in the default configuration directories:
  9. #      /etc/nginx/sites-available and /etc/nginx/sites-enabled
  10. #    For easy access to this script, copy it into the directory:
  11. #      /usr/local/sbin
  12. #   or /usr/bin/ninx_modsite; chmod+x
  13. #    Run this script without any arguments or with -h or --help to see a basic
  14. #    help dialog displaying all options.
  15. ##
  16.  
  17. # Copyright (C) 2010 Michael Lustfield <mtecknology@ubuntu.com>
  18.  
  19. # Redistribution and use in source and binary forms, with or without
  20. # modification, are permitted provided that the following conditions
  21. # are met:
  22. # 1. Redistributions of source code must retain the above copyright
  23. #    notice, this list of conditions and the following disclaimer.
  24. # 2. Redistributions in binary form must reproduce the above copyright
  25. #    notice, this list of conditions and the following disclaimer in the
  26. #    documentation and/or other materials provided with the distribution.
  27. #
  28. # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  29. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. # ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  32. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. # SUCH DAMAGE.
  39.  
  40. ##
  41. # Default Settings
  42. ##
  43.  
  44. NGINX_CONF_FILE="$(awk -F= -v RS=' ' '/conf-path/ {print $2}' <<< $(nginx -V 2>&1))"
  45. NGINX_CONF_DIR="${NGINX_CONF_FILE%/*}"
  46. NGINX_SITES_AVAILABLE="$NGINX_CONF_DIR/sites-available"
  47. NGINX_SITES_ENABLED="$NGINX_CONF_DIR/sites-enabled"
  48. SELECTED_SITE="$2"
  49.  
  50. ##
  51. # Script Functions
  52. ##
  53.  
  54. ngx_enable_site() {
  55.     [[ ! "$SELECTED_SITE" ]] &&
  56.         ngx_select_site "not_enabled"
  57.  
  58.     [[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
  59.         ngx_error "Site does not appear to exist."
  60.     [[ -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
  61.         ngx_error "Site appears to already be enabled"
  62.  
  63.     ln -sf "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" -T "$NGINX_SITES_ENABLED/$SELECTED_SITE"
  64.     ngx_reload
  65. }
  66.  
  67. ngx_disable_site() {
  68.     [[ ! "$SELECTED_SITE" ]] &&
  69.         ngx_select_site "is_enabled"
  70.  
  71.     [[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
  72.         ngx_error "Site does not appear to be \'available\'. - Not Removing"
  73.     [[ ! -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
  74.         ngx_error "Site does not appear to be enabled."
  75.  
  76.     rm -f "$NGINX_SITES_ENABLED/$SELECTED_SITE"
  77.     ngx_reload
  78. }
  79.  
  80. ngx_list_site() {
  81.     echo "Available sites:"
  82.     ngx_sites "available"
  83.     echo "Enabled Sites"
  84.     ngx_sites "enabled"
  85. }
  86.  
  87. ##
  88. # Helper Functions
  89. ##
  90.  
  91. ngx_select_site() {
  92.     sites_avail=($NGINX_SITES_AVAILABLE/*)
  93.     sa="${sites_avail[@]##*/}"
  94.     sites_en=($NGINX_SITES_ENABLED/*)
  95.     se="${sites_en[@]##*/}"
  96.  
  97.     case "$1" in
  98.         not_enabled) sites=$(comm -13 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
  99.         is_enabled) sites=$(comm -12 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
  100.     esac
  101.  
  102.     ngx_prompt "$sites"
  103. }
  104.  
  105. ngx_prompt() {
  106.     sites=($1)
  107.     i=0
  108.  
  109.     echo "SELECT A WEBSITE:"
  110.     for site in ${sites[@]}; do
  111.         echo -e "$i:\t${sites[$i]}"
  112.         ((i++))
  113.     done
  114.  
  115.     read -p "Enter number for website: " i
  116.     SELECTED_SITE="${sites[$i]}"
  117. }
  118.  
  119. ngx_sites() {
  120.     case "$1" in
  121.         available) dir="$NGINX_SITES_AVAILABLE";;
  122.         enabled) dir="$NGINX_SITES_ENABLED";;
  123.     esac
  124.  
  125.     for file in $dir/*; do
  126.         echo -e "\t${file#*$dir/}"
  127.     done
  128. }
  129.  
  130. ngx_reload() {
  131.     read -p "Would you like to reload the Nginx configuration now? (Y/n) " reload
  132.     [[ "$reload" != "n" && "$reload" != "N" ]] && invoke-rc.d nginx reload
  133. }
  134.  
  135. ngx_error() {
  136.     echo -e "${0##*/}: ERROR: $1"
  137.     [[ "$2" ]] && ngx_help
  138.     exit 1
  139. }
  140.  
  141. ngx_help() {
  142.     echo "Usage: ${0##*/} [options]"
  143.     echo "Options:"
  144.     echo -e "\t<-e|--enable> <site>\tEnable site"
  145.     echo -e "\t<-d|--disable> <site>\tDisable site"
  146.     echo -e "\t<-l|--list>\t\tList sites"
  147.     echo -e "\t<-h|--help>\t\tDisplay help"
  148.     echo -e "\n\tIf <site> is left out a selection of options will be presented."
  149.     echo -e "\tIt is assumed you are using the default sites-enabled and"
  150.     echo -e "\tsites-disabled located at $NGINX_CONF_DIR."
  151. }
  152.  
  153. ##
  154. # Core Piece
  155. ##
  156.  
  157. case "$1" in
  158.     -e|--enable)    ngx_enable_site;;
  159.     -d|--disable)   ngx_disable_site;;
  160.     -l|--list)  ngx_list_site;;
  161.     -h|--help)  ngx_help;;
  162.     *)      ngx_error "No Options Selected" 1; ngx_help;;
  163. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement