Files @ babda105c9cf
Branch filter:

Location: majic-ansible-roles/roles/web_server/files/nginx_verify_site.sh - annotation

branko
MAR-16: Added option to common role for setting-up caching proxy for apt.
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
be92dd65fc60
#!/bin/bash
#
# nginx_verify_site.sh
#
# Copyright (C) 2015, Branko Majic <branko@majic.rs>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

program="nginx_verify_site.sh"
version="0.1"

function usage() {
    cat <<EOF
$program $version, a non-interactive utility for testing a new nginx site configuration file

Usage: $program [OPTIONS] site_configuration_file

$program is a non-interactive utility for testing a new nginx site configuration
file. The utility works by copying the existing nginx configuration to a
temporary path, deploying the passed-in site configuration file to the temporary
location, and running the nginx -t command on temporary configuration
copy. During this process the utility will also fix the paths in temporary
configuration files on the fly.

A single positional argument is accepted:

site_configuration_file
  Path to the (new) file containing the site configuration that should be
  tested.

$program accepts the following options:

    -n name   Name to use for site configuration file. Useful when combined with
              Ansible's validate command (modules copy/template) which does not
              preserve filenames when executed. Defaults to original filename
              for site_configuration_file.

    -v        show script version and licensing information
    -h        show usage help


Please report bugs and send feature requests to <branko@majic.rs>.
EOF
}

function version() {
        cat <<EOF
$program, version $version

+-----------------------------------------------------------------------+
| Copyright (C) 2015, Branko Majic <branko@majic.rs>                    |
|                                                                       |
| This program is free software: you can redistribute it and/or modify  |
| it under the terms of the GNU General Public License as published by  |
| the Free Software Foundation, either version 3 of the License, or     |
| (at your option) any later version.                                   |
|                                                                       |
| This program is distributed in the hope that it will be useful,       |
| but WITHOUT ANY WARRANTY; without even the implied warranty of        |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
| GNU General Public License for more details.                          |
|                                                                       |
| You should have received a copy of the GNU General Public License     |
| along with this program.  If not, see <http://www.gnu.org/licenses/>. |
+-----------------------------------------------------------------------+

EOF
}

# Set-up colours for message printing if we're not piping and terminal is
# capable of outputting the colors.
_color_terminal=$(tput colors 2>&1)
if [[ -t 1 ]] && (( ${_color_terminal} > 0 )); then
    _text_bold=$(tput bold)
    _text_white=$(tput setaf 7)
    _text_blue=$(tput setaf 6)
    _text_green=$(tput setaf 2)
    _text_yellow=$(tput setaf 3)
    _text_red=$(tput setaf 1)
    _text_reset=$(tput sgr0)
else
    _text_bold=""
    _text_white=""
    _text_blue=""
    _text_green=""
    _text_yellow=""
    _text_red=""
    _text_reset=""
fi

# Set-up functions for printing coloured messages.
function debug() {
    echo "${_text_bold}${_text_blue}[DEBUG]${_text_reset}" "$@"
}

function info() {
    echo "${_text_bold}${_text_white}[INFO] ${_text_reset}" "$@"
}

function success() {
    echo "${_text_bold}${_text_green}[OK]   ${_text_reset}" "$@"
}

function warning() {
    echo "${_text_bold}${_text_yellow}[WARN] ${_text_reset}" "$@"
}

function error() {
    echo "${_text_bold}${_text_red}[ERROR]${_text_reset}" "$@" >&2
}

# Helper function for removing temporary directory.
function remove_temp_config() {
    [[ -e $temp_config ]] && rm -rf "$temp_config"
}

# If no arguments were given, just show usage help.
if [[ -z $1 ]]; then
    usage
    exit 0
fi

# Parse the arguments
while getopts "n:vh" opt; do
    case "$opt" in
        n) name="$OPTARG";;
        v) version
           exit 0;;
        h) usage
           exit 0;;
        *) usage
           exit 1;;
    esac
done
i=$OPTIND
shift $(($i-1))

# Read the site configuration.
site_configuration_file="$1"

# Verify positional arguments.
if [[ ! -f $site_configuration_file ]]; then
    error "Could not open site configuration file: $site_configuration_file"
    exit 1
fi

# Determine what the file should be called like in temporary directory. Sanitise
# the name if it was passed in, just in case.
if [[ -z $name ]]; then
    name=$(basename "$site_configuration_file")
else
    name=$(basename "$name")
fi

# Set-up paths where original and temporary configurations can be found.
original_config="/etc/nginx"
temp_config=$(mktemp -d)

# Remove the temporary directory on exit.
trap remove_temp_config EXIT

# Prepare temporary copy for verification.
cp -a "$original_config" "$temp_config/"
find "$temp_config" -type f -print0 | xargs -0 sed -i -e "s#$original_config#$temp_config/nginx#g"

# Enable the new site configuration in temporary copy.
rm -f "$temp_config/nginx/sites-enabled/$name"
cp "$site_configuration_file" "$temp_config/nginx/sites-enabled/$name"

# Finally, run the test.
nginx -t -c "$temp_config/nginx/nginx.conf"