From 3f98b279fefdecc297a254be6cd0a904d08c2396 Mon Sep 17 00:00:00 2001 From: oupala Date: Thu, 9 May 2019 15:58:29 +0200 Subject: [PATCH] refactor: large rewrite of install script --- apaxy-configure.sh | 229 +++++++++++++++++++++++++++++++++++---------- apaxy.config | 77 ++++++++------- 2 files changed, 218 insertions(+), 88 deletions(-) mode change 100755 => 100644 apaxy-configure.sh diff --git a/apaxy-configure.sh b/apaxy-configure.sh old mode 100755 new mode 100644 index a1712a8..95cefe2 --- a/apaxy-configure.sh +++ b/apaxy-configure.sh @@ -1,69 +1,200 @@ #!/bin/bash -echo "Apaxy Configurator - by Jordan Bancino" -echo "Checking configuration..." +# +# apaxy configurator +# v0.2 +# configure apaxy according to your local paths and configuration +# author : Jordan Bancino and Ploc +# contact : jordan [@] bancino.net +# licence : GPLv3 -CONFIG="apaxy.config" +# enabling strict mode +# -e - exit immediatly on error (disable with "+e" when an error can happens, then enable it again with "-e") +# -u - undefined variables are forbidden (enable this option after getting parameters from $1 $2...) see below +# -o pipefail - find error return code inside piped commands +# IFS - set strong internal field separator +set -eo pipefail +IFS=$'\n\t' -if [ -f "$CONFIG" ]; then - . "$CONFIG" -else - echo "Apaxy configuration not found! Please restore or create the configuration file: $CONFIG" - exit 1 -fi +# default config +defaultLogLevel=2 +defaultLogFile="$(basename "${0}" .sh).log" +defaultApacheWebRootPath="/var/www/html" +defaultInstallWebPath="" -if [ -v INSTALL_DIRECTORY ] && [ -v WEB_ROOT ]; then - echo "- Configuring Apaxy for use in directory: $INSTALL_DIRECTORY (Web root: $WEB_ROOT)" - mkdir -p "$WEB_ROOT$INSTALL_DIRECTORY" - if [ ! -w "$WEB_ROOT$INSTALL_DIRECTORY" ] || [ ! -d "$WEB_ROOT$INSTALL_DIRECTORY" ]; then - echo "Directory does not exist or is not writable by the current user: $WEB_ROOT$INSTALL_DIRECTORY" - exit 1 +# functions + +### + # display help + ## +displayHelp () { + cat <> "${logFile}" + fi +} + +# getting parameters value from config file (can be overloaded by cli values) +workingDirectory=$(dirname "$0") + +if [ -f "${workingDirectory}/apaxy.config" ]; then + # shellcheck source=apaxy.config + source "${workingDirectory}/apaxy.config" else - echo "No directory specified! Please define the INSTALL_DIRECTORY and WEB_ROOT variables in $CONFIG" + log 1 "ERROR - apaxy configuration not found, please restore or create the configuration file apaxy.config" exit 1 fi -echo "Copying files to web root..." -cp -r . "$WEB_ROOT$INSTALL_DIRECTORY" -cd "$WEB_ROOT$INSTALL_DIRECTORY" +# getting parameters value from cli (can overload config file values) +while [ "$#" -ge 1 ] ; do + case "${1}" in + -h|--help) # display help + displayHelp + exit 0 + ;; + -d) # set path/to/dir/ directory where apaxy will be available on the httpd server + shiftStep=2 + apacheWebRootPath="${2}" + ;; + -w) # set path/to/dir/ directory where apaxy will be installed on the filesystem + shiftStep=2 + installWebPath="${2}" + ;; + -ll) # set the log level + shiftStep=2 + logLevel="${2}" + ;; + -lf) # set the log file + shiftStep=2 + logFile="${2}" + ;; + *) + displayUsage + exit 2 + ;; + esac -if [ -v HTACCESS ]; then - if [ -f "$HTACCESS" ]; then - echo "- Using template: $HTACCESS to generate configuration" + if [ "$#" -ge "${shiftStep}" ] + then + shift "${shiftStep}" else - echo "Configuration template does not exist! Please specify an existing configuration template in $CONFIG" - exit 1 + log 1 "ERROR - invalid number of arguments" + exit 3 fi +done + +# setting parameters value +if [ -z "${apacheWebRootPath}" ] +then + apacheWebRootPath="${defaultApacheWebRootPath}" +fi + +if [ -z "${installWebPath}" ] +then + installWebPath="${defaultInstallWebPath}" +fi + +if [ -n "${apacheWebRootPath}" ] && [ -z "${installWebPath}" ] +then + installDir="${apacheWebRootPath}" else - echo "No configuration template specified. Please define the HTACCESS variable in $CONFIG" - exit 1 + installDir="${apacheWebRootPath}${installWebPath}" fi -if [ ! -v TEMPLATE_VAR_FOLDERNAME ]; then - echo "No foldername variable defined. Please define the TEMPLATE_VAR_FOLDERNAME variable in $CONFIG" - exit +if [ -z "${logLevel}" ] +then + logLevel="${defaultLogLevel}" fi -echo "Configuring..." -echo "- Generating .htaccess..." -sed "s|$TEMPLATE_VAR_FOLDERNAME|$INSTALL_DIRECTORY/apaxy/|g" <"$HTACCESS" >"$HTACCESS_OUTPUT" +if [ -z "${logFile}" ] +then + logFile="${workingDirectory}/${defaultLogFile}" +fi -echo "- Setting variables in documents..." -# find all the HTML files and replace the variable in them. -# This will automatically take care of the error pages, headers and -# footers. -FILES=$(find -name "*.html") +# enabling strict mode +# -u - undefined variables are forbidden (enable this option after getting parameters from $1 $2...) +set -u + +# checking parameters value +if [ ! -d "$(dirname "${logFile}")" ] +then + log 1 "ERROR - $(dirname "${logFile}") does not exist" + exit 4 +fi + +# script +log 1 "- creating install directory ${installDir}" +mkdir -p "${installDir}" +if [ ! -d "${installDir}" ] || [ ! -w "${installDir}" ]; then + log 1 "ERROR - install directory ${installDir} does not exist or is not writable by the current user" + exit 5 +fi + +log 1 "- copying apaxy in install directory" +cp -r apaxy/* "${installDir}/" + +log 1 "- configuring apaxy in install directory" + +log 2 "- generating htaccess" +sed "s|{FOLDERNAME}|${installWebPath}|g" < "${installDir}/htaccess.txt" > "${installDir}/.htaccess" +rm "${installDir}/htaccess.txt" + +# find all the html files and replace the variable in them +# this will automatically take care of the error pages, headers and footers +log 2 "- setting path in html files" +files=$(find ${installDir} -name "*.html") while read -r file; do - sed -i "s|$TEMPLATE_VAR_FOLDERNAME|$INSTALL_DIRECTORY/apaxy/|g" "$file" -done <<< "$FILES" + sed -i "s|{FOLDERNAME}|${installWebPath}|g" "${file}" +done <<< "${files}" -if [ -v THEME_HTACCESS_IN ] && [ -v THEME_HTACCESS_OUT ]; then - echo "- Activating theme configuration..." - mv "$THEME_HTACCESS_IN" "$THEME_HTACCESS_OUT" -fi - -# TODO: -# - Implement any other options we want here - -echo "Done." -exit 0 +log 2 "- syncing filesystem" +sync +log 1 "- filesystem has been synced and is now consistent" +log 1 "- apaxy has been successfully configured and installed in ${installDir}" diff --git a/apaxy.config b/apaxy.config index 0aa6176..2238029 100755 --- a/apaxy.config +++ b/apaxy.config @@ -1,45 +1,44 @@ -# Apaxy Configuration - by Jordan Bancino -# (C) 2018 Jordan Bancino +#!/bin/bash # -# Use "apaxy-configure.sh" to configure Apaxy after adjusting the needed values. +# apaxy configurator +# v0.2 +# use "apaxy-configure.sh" to configure apaxy after adjusting the needed values in this file +# author : Jordan Bancino and Ploc +# contact : jordan [@] bancino.net +# licence : GPLv3 + +# default log level on the console (the lower the number is, the less verbose it is) +#defaultLogLevel=2 + +# all log messages are written to log file +#defaultLogFile="apaxy.log" + +# the apache web root path where the server serves documents # -# NOTICE: Directories can be absolute or relative, but make sure they start with either "/", "../", or "./", -# and DO NOT leave a trailing slash. -# Good: "/var/www/html" <- Starts with "/", does not have trailing slash -# Bad : "var/www/html" <- Does not start with "/", "./", or "../" -# Good: "./var/www/html" <- Starts with "./", does not have trailing slash -# Bad : "/var/www/html/" <- Has trailing slash - +# default for apache is /var/www/html # -# GENERAL CONFIGURATION +# note that path should be absolute, so make sure it starts with a "/" +# and DO NOT leave a trailing slash # - -# The directory to configure Apaxy for use in. This is relative to the web server root, and contains the "theme" folder. -# For instance: "http://website.com/share" would make this value "/share", where "/share/apaxy/theme" is the theme directory -INSTALL_DIRECTORY="/dev/apaxy-deploy" - -# The web root where the server serves documents. Default for Apache is /var/www/html -WEB_ROOT="/var/www/html" - -# The output .htaccess file. -# Place the file in the root of the directory in which you are applying apaxy. -HTACCESS_OUTPUT="$WEB_ROOT$INSTALL_DIRECTORY/.htaccess" - +# good: "/var/www/html" <- starts with "/", does not have trailing "/" +# bad : "var/www/html/" <- does not start with "/", have a trailing "/" # -# ADVANCED CONFIGURATION -# Normal users: Do not modify below this line. +apacheWebRootPath="/var/www/html" + +# the directory where apaxy will be used (this is relative to the web server root, and contains the "theme" folder) # - -# The htaccess template to run the configuration on. -# Do not change this value for a regular installation of Apaxy. -HTACCESS="apaxy/htaccess.txt" - -# The theme config for Apaxy. IN will be renamed to OUT. -# There should be no reason to change this in most cases. -THEME_HTACCESS_IN="apaxy/theme/htaccess.txt" -THEME_HTACCESS_OUT="apaxy/theme/.htaccess" - - -# The variable in the htaccess and footer templates. -# Do not change this value unless you use a different template with a different variable. -TEMPLATE_VAR_FOLDERNAME="/{FOLDERNAME}/" +# for instance: "http://example.org/share" would make this value "/share", where "/share/theme" is the theme directory +# +# note that path should be absolute, so make sure it starts with a "/" +# and DO NOT leave a trailing slash +# +# good: "/share" <- starts with "/", does not have trailing "/" +# bad : "share/" <- does not start with "/", has a trailing "/" +# +# by default, apaxy is configured to be installed at the root level of your web server +# +# if you want apaxy to be installed anywhere else, you can set it in the installWebPath variable +# +# installWebPath="/share" +# +installWebPath=""