From 95e16287c877dc45058c123079ff6d57e0321f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lecour?= Date: Wed, 18 Oct 2017 22:48:22 +0200 Subject: [PATCH] Extract hook scripts for Apache and Nginx --- evoacme/files/apahe_update_and_reload.sh | 47 ++++++++++++++++++++++++ evoacme/files/nginx_update_and_reload.sh | 47 ++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 evoacme/files/apahe_update_and_reload.sh create mode 100644 evoacme/files/nginx_update_and_reload.sh diff --git a/evoacme/files/apahe_update_and_reload.sh b/evoacme/files/apahe_update_and_reload.sh new file mode 100644 index 00000000..66b73df1 --- /dev/null +++ b/evoacme/files/apahe_update_and_reload.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +error() { + >&2 echo "${PROGNAME}: $1" + exit 1 +} +debug() { + if [ "${VERBOSE}" = "1" ] && [ "${QUIET}" != "1" ]; then + >&2 echo "${PROGNAME}: $1" + fi +} + +readonly PROGNAME=$(basename "$0") + +readonly VERBOSE=${VERBOSE:-"0"} + +if [ -z "${EVOACME_VHOST_PATH}"]; then + error "Missing EVOACME_VHOST_PATH environment variable" +fi +if [ -z "${EVOACME_CERT_PATH}"]; then + error "Missing EVOACME_CERT_PATH environment variable" +fi + +readonly APACHE2CTL_BIN=$(command -v apache2ctl) || error "apache2ctl command not installed" + +[ -r "${EVOACME_VHOST_PATH}"] || error "File ${EVOACME_VHOST_PATH} is not readable" + +local search="^SSLCertificateFile.*$" +local replace="SSLCertificateFile ${EVOACME_VHOST_PATH}" + +if ! $(grep -qE "${search}" "${EVOACME_VHOST_PATH}"); then + [ -w "${EVOACME_VHOST_PATH}" ] || error "File ${EVOACME_VHOST_PATH} is not writable" + + sed -i "s~${search}~${replace}~" "${EVOACME_VHOST_PATH}" + debug "Config in ${EVOACME_VHOST_PATH} has been updated" +fi + +if [ -n "$(pidof apache2)" ]; then + if $(${APACHE2CTL_BIN} -t 2> /dev/null); then + debug "Apache detected... reloading" + service apache2 reload + else + error "Apache config is broken, you must fix it !" + fi +fi + +exit 0 diff --git a/evoacme/files/nginx_update_and_reload.sh b/evoacme/files/nginx_update_and_reload.sh new file mode 100644 index 00000000..988c7389 --- /dev/null +++ b/evoacme/files/nginx_update_and_reload.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +error() { + >&2 echo "${PROGNAME}: $1" + exit 1 +} +debug() { + if [ "${VERBOSE}" = "1" ] && [ "${QUIET}" != "1" ]; then + >&2 echo "${PROGNAME}: $1" + fi +} + +readonly PROGNAME=$(basename "$0") + +readonly VERBOSE=${VERBOSE:-"0"} + +if [ -z "${EVOACME_VHOST_PATH}"]; then + error "Missing EVOACME_VHOST_PATH environment variable" +fi +if [ -z "${EVOACME_CERT_PATH}"]; then + error "Missing EVOACME_CERT_PATH environment variable" +fi + +readonly NGINX_BIN=$(command -v nginx) || error "nginx command not installed" + +[ -r "${EVOACME_VHOST_PATH}"] || error "File ${EVOACME_VHOST_PATH} is not readable" + +readonly search="^ssl_certificate[^_].*$" +readonly replace="ssl_certificate ${EVOACME_CERT_PATH};" + +if ! $(grep -qE "${search}" "${EVOACME_VHOST_PATH}"); then + [ -w "${EVOACME_VHOST_PATH}" ] || error "File ${EVOACME_VHOST_PATH} is not writable" + + sed -i "s~${search}~${replace}~" "${EVOACME_VHOST_PATH}" + debug "Config in ${EVOACME_VHOST_PATH} has been updated" +fi + +if [ -n "$(pidof nginx)" ]; then + if $(${NGINX_BIN} -t 2> /dev/null); then + debug "Nginx detected... reloading" + service nginx reload + else + error "Nginx config is broken, you must fix it !" + fi +fi + +exit 0