déplacer la logique de apacheconfig dans le module apache.tools

This commit is contained in:
Jephté Clain 2016-08-18 17:01:49 +04:00
parent 9fa8e3c19b
commit e52d6c6558
2 changed files with 170 additions and 125 deletions

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
urequire DEFAULTS template apache.tools
apacheconfig_initvars
function display_help() {
uecho "$scriptname: Gérer la configuration d'un serveur web apache
@ -65,95 +66,6 @@ OPTIONS
tests."
}
# toujours placer une variable dépendante AVANT la variable maitre
TEMPLATE_STATIC_VARS=(
hostname aliases host
certsdir caname
)
TEMPLATE_DYNAMIC_VARS=(
ips_namevirtualhosts ips_listens ips
dhostname daliases dhost
admin configdir
)
TEMPLATE_NOWRITE_VARS=(hostname aliases dhostname daliases configdir)
__TEMPLATE_DEFAULTF_host=__template_defaultf_host
__TEMPLATE_UPDATEF_host=__template_updatef_host
function __template_defaultf_host() {
myhost
}
function __template_updatef_host() {
if [ "$host" == "@@dhost@@" ]; then
__template_set_var hostname @@dhostname@@
__template_set_var aliases @@daliases@@
else
local update_aliases
[ "$aliases" == "$hostname" ] && update_aliases=1
__template_set_var hostname "${host%%.*}"
[ -n "$update_aliases" ] && __template_set_var aliases "$hostname"
[ -n "$ips" ] || __template_set_var ips ""
fi
}
__TEMPLATE_DEFAULTF_ips=__template_defaultf_ips
__TEMPLATE_UPDATEF_ips=__template_updatef_ips
function __template_defaultf_ips() {
local -a ips
if [ "$host" != "@@dhost@@" ]; then
resolv_ips ips "$host"
else
resolv_ips ips "$dhost"
fi
echo "${ips[*]}"
}
function __template_updatef_ips() {
local -a ipports; local ipport ip port
local nvhs ls
nvhs=
ls=
array_split ipports "$ips" " "
for ipport in "${ipports[@]}"; do
splitpair "$ipport" ip port
[ -n "$port" ] || port=443
[ -n "$nvhs" ] || nvhs="#ips_namevirtualhosts"
nvhs="$nvhs
NameVirtualHost $ip:$port"
[ -n "$ls" ] || ls="#ips_listens"
ls="$ls
Listen $ip:$port"
done
[ -n "$nvhs" ] && __template_set_var ips_namevirtualhosts "$nvhs"
[ -n "$ls" ] && __template_set_var ips_listens "$ls"
}
__TEMPLATE_DEFAULTF_dhost=__template_defaultf_dhost
__TEMPLATE_UPDATEF_dhost=__template_updatef_dhost
function __template_defaultf_dhost() {
myhost
}
function __template_updatef_dhost() {
local update_daliases
[ "$daliases" == "$dhostname" ] && update_daliases=1
__template_set_var dhostname "${dhost%%.*}"
[ -n "$update_daliases" ] && __template_set_var daliases "$dhostname"
[ -n "$ips" ] || __template_set_var ips ""
}
DEFAULT_ADMIN=supervision-gdrsi@listes.univ-reunion.fr
DEFAULT_CERTSDIR=1507-renater
DEFAULT_CANAME=1507-DigiCertCA.crt
set_defaults apacheconfig
TEMPLATECTL_NAME=apacheconfig
TEMPLATECTL_SRCDIRS=(apacheconfig)
TEMPLATECTL_CONFIG="$TEMPLATECTL_NAME"
TEMPLATECTL_DEFAULTS=(
admin="$DEFAULT_ADMIN"
certsdir="$DEFAULT_CERTSDIR"
caname="$DEFAULT_CANAME"
)
TEMPLATECTL_VARS=()
action=
destdir=
nohideconfig=
@ -192,25 +104,8 @@ args=(
)
parse_args "$@"; set -- "${args[@]}"
__template_set_destdir destdir autocreate "$TEMPLATECTL_NAME" || die
setx config=templatectl_config "$destdir" ${nohideconfig:+nohideconfig}
modified=
templatectl_loadvars "$config" && modified=1
if [ -n "$sysname" -o -n "$sysdist" -o -n "$sysver" ]; then
ensure_sysinfos sysname sysdist sysver
custom_sysinfos=1
else
sysname="$MYSYSNAME"
sysdist="$MYSYSDIST"
sysver="$MYSYSVER"
bits="$MYBITS"
custom_sysinfos=
fi
__template_set_var sysname "$sysname"
__template_set_var sysdist "$sysdist"
__template_set_var sysver "$sysver"
#check_sysinfos --vars sysname sysdist sysver bits "${templatectl_suffix[@]}
apacheconfig_loadconf "$destdir" || die
apacheconfig_sysinfos "$sysname" "$sysdist" "$sysver" "$bits"
################################################################################
if [ "$action" == create ]; then
@ -262,24 +157,14 @@ elif [ "$action" == deploy -o "$action" == localhosts ]; then
if [ "$action" == deploy ]; then
etitle "Mise à jour du système"
args=(apache_autoconf
--ignore "$(basename -- "$config")"
${oneconf:+--one-conf "$(basename -- "$oneconf")"}
${onemodule:+--one-module "$(basename -- "$onemodule")"}
)
# sur une ligne séparée parce que $bits peut être vide
[ -n "$custom_sysinfos" ] && array_add args --sysname "$sysname" --sysdist "$sysdist" --sysver "$sysver" --bits "$bits"
apacheconfig_deploy \
"$destdir" "$aac_certsdir" \
"$config" "$oneconf" "$onemodule" "$onesite" \
"$custom_sysinfos" "$sysname" "$sysdist" "$sysver" "$bits" || die
eend
elif [ "$action" == localhosts ]; then
etitle "Mise à jour de /etc/hosts"
args=(apache_autoconf_localhosts)
apacheconfig_deploy_localhosts "$destdir" "$aac_certsdir" "$onesite" || die
eend
fi
args=("${args[@]}"
"$destdir" "$aac_certsdir"
${onesite:+--one-site "$(basename -- "$onesite")"}
)
for __name in "${TEMPLATE_DYNAMIC_VARS[@]}"; do
array_add args "$__name=${!__name}"
done
"${args[@]}" "$@" || die
eend
fi

View File

@ -652,3 +652,163 @@ $1 == "ServerAlias" { for (i = 2; i <= NF; i++) { if ($i ~ /\.local$/) print $i
echo "$ip $host" >>/etc/hosts
done
}
function __template_defaultf_host() {
myhost
}
function __template_updatef_host() {
if [ "$host" == "@@dhost@@" ]; then
__template_set_var hostname @@dhostname@@
__template_set_var aliases @@daliases@@
else
local update_aliases
[ "$aliases" == "$hostname" ] && update_aliases=1
__template_set_var hostname "${host%%.*}"
[ -n "$update_aliases" ] && __template_set_var aliases "$hostname"
[ -n "$ips" ] || __template_set_var ips ""
fi
}
function __template_defaultf_ips() {
local -a ips
if [ "$host" != "@@dhost@@" ]; then
resolv_ips ips "$host"
else
resolv_ips ips "$dhost"
fi
echo "${ips[*]}"
}
function __template_updatef_ips() {
local -a ipports; local ipport ip port
local nvhs ls
nvhs=
ls=
array_split ipports "$ips" " "
for ipport in "${ipports[@]}"; do
splitpair "$ipport" ip port
[ -n "$port" ] || port=443
[ -n "$nvhs" ] || nvhs="#ips_namevirtualhosts"
nvhs="$nvhs
NameVirtualHost $ip:$port"
[ -n "$ls" ] || ls="#ips_listens"
ls="$ls
Listen $ip:$port"
done
[ -n "$nvhs" ] && __template_set_var ips_namevirtualhosts "$nvhs"
[ -n "$ls" ] && __template_set_var ips_listens "$ls"
}
function __template_defaultf_dhost() {
myhost
}
function __template_updatef_dhost() {
local update_daliases
[ "$daliases" == "$dhostname" ] && update_daliases=1
__template_set_var dhostname "${dhost%%.*}"
[ -n "$update_daliases" ] && __template_set_var daliases "$dhostname"
[ -n "$ips" ] || __template_set_var ips ""
}
# toujours placer une variable dépendante AVANT la variable maitre
APACHECONFIG_TEMPLATE_STATIC_VARS=(
hostname aliases host
certsdir caname
)
APACHECONFIG_TEMPLATE_DYNAMIC_VARS=(
ips_namevirtualhosts ips_listens ips
dhostname daliases dhost
admin configdir
)
APACHECONFIG_TEMPLATE_NOWRITE_VARS=(hostname aliases dhostname daliases configdir)
function apacheconfig_initvars() {
DEFAULT_ADMIN=supervision-gdrsi@listes.univ-reunion.fr
DEFAULT_CERTSDIR=1507-renater
DEFAULT_CANAME=1507-DigiCertCA.crt
set_defaults apacheconfig
TEMPLATE_STATIC_VARS=("${APACHECONFIG_TEMPLATE_STATIC_VARS[@]}")
TEMPLATE_DYNAMIC_VARS=("${APACHECONFIG_TEMPLATE_DYNAMIC_VARS[@]}")
TEMPLATE_NOWRITE_VARS=("${APACHECONFIG_TEMPLATE_NOWRITE_VARS[@]}")
__TEMPLATE_DEFAULTF_host=__template_defaultf_host
__TEMPLATE_UPDATEF_host=__template_updatef_host
__TEMPLATE_DEFAULTF_ips=__template_defaultf_ips
__TEMPLATE_UPDATEF_ips=__template_updatef_ips
__TEMPLATE_DEFAULTF_dhost=__template_defaultf_dhost
__TEMPLATE_UPDATEF_dhost=__template_updatef_dhost
TEMPLATECTL_NAME=apacheconfig
TEMPLATECTL_SRCDIRS=(apacheconfig)
TEMPLATECTL_CONFIG="$TEMPLATECTL_NAME"
TEMPLATECTL_DEFAULTS=(
admin="$DEFAULT_ADMIN"
certsdir="$DEFAULT_CERTSDIR"
caname="$DEFAULT_CANAME"
)
TEMPLATECTL_VARS=()
}
function apacheconfig_loadconf() {
local config modified
local destdir="$1"
__template_set_destdir destdir autocreate "$TEMPLATECTL_NAME" || return 1
setx config=templatectl_config "$destdir"
modified=
templatectl_loadvars "$config" && modified=1
upvar config "$config"
}
function apacheconfig_sysinfos() {
local sysname="$1" sysdist="$2" sysver="$3" bits="$4" custom_sysinfos
if [ -n "$sysname" -o -n "$sysdist" -o -n "$sysver" ]; then
ensure_sysinfos sysname sysdist sysver
custom_sysinfos=1
else
sysname="$MYSYSNAME"
sysdist="$MYSYSDIST"
sysver="$MYSYSVER"
bits="$MYBITS"
custom_sysinfos=
fi
__template_set_var sysname "$sysname"
__template_set_var sysdist "$sysdist"
__template_set_var sysver "$sysver"
#check_sysinfos --vars sysname sysdist sysver bits "${templatectl_suffix[@]}
upvars sysname "$sysname" sysdist "$sysdist" sysver "$sysver" bits "$bits" \
custom_sysinfos "$custom_sysinfos"
}
function apacheconfig_deploy() {
local destdir="$1" certsdir="$2"; shift; shift
local config="$1" oneconf="$2" onemodule="$3"; onesite="$4"; shift; shift; shift; shift
local custom_sysinfos="$1" sysname="$2" sysdist="$3" sysver="$4" bits="$5"; shift; shift; shift; shift; shift
local -a args
args=(--ignore "$(basename -- "$config")")
[ -n "$oneconf" ] && array_add args --one-conf "$(basename -- "$oneconf")"
[ -n "$onemodule" ] && array_add args --one-module "$(basename -- "$onemodule")"
[ -n "$onesite" ] && array_add args --one-site "$(basename -- "$onesite")"
[ -n "$custom_sysinfos" ] && array_add args --sysname "$sysname" --sysdist "$sysdist" --sysver "$sysver" --bits "$bits"
array_add args "$destdir" "$certsdir"
for __name in "${TEMPLATE_DYNAMIC_VARS[@]}"; do
array_add args "$__name=${!__name}"
done
apache_autoconf "${args[@]}" "$@"
}
function apacheconfig_localhosts() {
local destdir="$1" certsdir="$2"; shift; shift
local onesite="$1"; shift
local -a args
[ -n "$onesite" ] && array_add args --one-site "$(basename -- "$onesite")"
array_add args "$destdir" "$certsdir"
for __name in "${TEMPLATE_DYNAMIC_VARS[@]}"; do
array_add args "$__name=${!__name}"
done
apache_autoconf_localhosts "${args[@]}" "$@"
}