refactoring...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2024-01-20 17:58:46 +03:00
parent 153e0a9c82
commit 3963fdd97e
8 changed files with 129 additions and 69 deletions

View File

@ -164,18 +164,17 @@ normpath(){
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# #
# getLatestTemplate PATTERN [VAR] # pveGetLatestTemplate PATTERN [VAR]
# #
# see: # see:
# https://pve.proxmox.com/wiki/Linux_Container # https://pve.proxmox.com/wiki/Linux_Container
getLatestTemplate(){ pveGetLatestTemplate(){
if [ $DRY_RUN ] ; then if [ $DRY_RUN ] ; then
[ -z $2 ] \ [ -z $2 ] \
|| eval "$2=${CT_TEMPLATE:-\\\$CT_TEMPLATE}" || eval "$2=${CT_TEMPLATE:-\\\$CT_TEMPLATE}"
return return
fi fi
#IFS=$'\n'
#@ pveam update #@ pveam update
local templates=($(pveam available | grep -o ''${1}'.*$')) local templates=($(pveam available | grep -o ''${1}'.*$'))
@ -489,6 +488,99 @@ readVars(){
} }
#
# makeTemplateSEDPatterns VAR ...
#
makeTemplateSEDPatterns(){
local var
for var in "$@" ; do
local val=${!var}
if [[ $val == SKIP ]] ; then
val=
fi
echo "-e 's/\\\${${var}}/${val//\//\\/}/g'"
done
}
# same as makeTemplateSEDPatterns but adds default vars + generates *_IPn vars...
PCT_TEMPLATE_VARS=(
EMAIL
DOMAIN
CTHOSTNAME
GATE_HOSTNAME
NS_HOSTNAME
GATE_LAN_IP
GATE_ADMIN_IP
NS_LAN_IP
NS_ADMIN_IP
WAN_IP
WAN_GATE
LAN_IP
LAN_GATE
ADMIN_IP
ADMIN_GATE
)
makePCTTemplateSEDPatterns(){
# strip ips and save to *_IPn var...
local ip_vars=()
local var
local val
for var in ${PCT_TEMPLATE_VARS[@]} ; do
if [[ $var =~ .*_IP ]] ; then
local val=${!var}
if [[ $val == SKIP ]] ; then
val=
fi
ip_vars+=("${var}n")
eval "local ${var}n=\"${val/\/*}\""
fi
done
makeTemplateSEDPatterns "${PCT_TEMPLATE_VARS[@]}" "${ip_vars[@]}" "$@"
}
#
# expandTemplate PATH VAR ...
# .. | expandTemplate VAR ...
#
PCT_TEMPLATE_PATTERNS=
expandTemplate(){
if [ -t 0 ] ; then
local input=$1
shift
else
local input=/dev/stdin
fi
if [ -z "$PCT_TEMPLATE_PATTERNS" ] ; then
local patterns=($(makeTemplateSEDPatterns "$@"))
else
local patterns=("${PCT_TEMPLATE_PATTERNS[@]}")
fi
cat "${input}" \
| eval "sed ${patterns[@]}"
}
#
# expandTemplate PATH [VAR ...]
# .. | expandTemplate [VAR ...]
#
expandPCTTemplate(){
local input=
if [ -t 0 ] ; then
input=$1
shift
fi
local PCT_TEMPLATE_PATTERNS=($(makePCTTemplateSEDPatterns "$@"))
expandTemplate "${input}"
}
# #
# buildAssets [VAR ..] # buildAssets [VAR ..]
# #
@ -497,62 +589,17 @@ NOTES=NOTES.md
buildAssets(){ buildAssets(){
local template_dir=${TEMPLATE_DIR:-templates} local template_dir=${TEMPLATE_DIR:-templates}
local assets_dir=${ASSETS_DIR:-assets} local assets_dir=${ASSETS_DIR:-assets}
local staging_dir=${STAGING_DIR:-staging}
if ! [ -e $template_dir ] ; then if ! [ -e $template_dir ] ; then
return return
fi fi
local PATTERNS=() local PCT_TEMPLATE_PATTERNS=($(makePCTTemplateSEDPatterns "$@"))
local DFL_VARS=(
EMAIL
DOMAIN
CTHOSTNAME
GATE_HOSTNAME
NS_HOSTNAME
GATE_LAN_IP
GATE_ADMIN_IP
NS_LAN_IP
NS_ADMIN_IP
WAN_IP
WAN_GATE
LAN_IP
LAN_GATE
ADMIN_IP
ADMIN_GATE
)
for var in ${DFL_VARS[@]} ; do
local val=${!var}
if [[ $val == SKIP ]] ; then
val=
fi
PATTERNS+=("-e 's/\\\${${var}}/${val//\//\\/}/g'")
done
local IP_VARS=(
GATE_LAN_IPn
GATE_ADMIN_IPn
NS_LAN_IPn
NS_ADMIN_IPn
WAN_IPn
LAN_IPn
ADMIN_IPn
)
for var in ${IP_VARS[@]} ; do
var=${var%n}
local val=${!var}
if [[ $val == SKIP ]] ; then
val=
fi
PATTERNS+=("-e 's/\\\${${var}n}/${val/\/*}/g'")
done
# args...
for var in $@ ; do
local val=${!var}
if [[ $val == SKIP ]] ; then
val=
fi
PATTERNS+=("-e 's/\\\${${var}}/${val//\//\\/}/g'")
done
# assets...
cp -R "${assets_dir}"/* "${staging_dir}"
# template dir...
local TEMPLATES=($(find "$template_dir" -type f)) local TEMPLATES=($(find "$template_dir" -type f))
for file in "${TEMPLATES[@]}" ; do for file in "${TEMPLATES[@]}" ; do
file=${file#${template_dir}} file=${file#${template_dir}}
@ -560,20 +607,28 @@ buildAssets(){
[ $DRY_RUN ] \ [ $DRY_RUN ] \
&& continue && continue
# ensure the directory exists... # ensure the directory exists...
mkdir -p "$(dirname "${assets_dir}/${file}")" mkdir -p "$(dirname "${staging_dir}/${file}")"
cat "${template_dir}/${file}" \ cat "${template_dir}/${file}" \
| eval "sed ${PATTERNS[@]}" \ | expandTemplate \
> "${assets_dir}/${file}" > "${staging_dir}/${file}"
done done
# special case: NOTES.md... # special case: NOTES.md...
if [ -z "$DESCRIPTION" ] && [ -e "$NOTES" ] ; then if [ -z "$DESCRIPTION" ] && [ -e "$NOTES" ] ; then
DESCRIPTION="$(\ DESCRIPTION="$(\
cat ${NOTES} \ cat ${NOTES} \
| eval "sed ${PATTERNS[@]}")" | expandTemplate)"
fi fi
} }
#
# pctPushAssets ID
#
pctPushAssets(){
@ pct-push-r $1 "${STAGING_DIR:-./staging}" /
}
# #
# pctCreate ID TEMPLATE ARGS [PASS] # pctCreate ID TEMPLATE ARGS [PASS]
# #
@ -598,7 +653,7 @@ pctCreate(){
# #
pctCreateAlpine(){ pctCreateAlpine(){
local TEMPLATE local TEMPLATE
getLatestTemplate alpine TEMPLATE pveGetLatestTemplate alpine TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3" pctCreate $1 "$TEMPLATE" "$2" "$3"
@ -609,7 +664,7 @@ pctCreateAlpine(){
} }
pctCreateDebian(){ pctCreateDebian(){
local TEMPLATE local TEMPLATE
getLatestTemplate 'debian-12-standard' TEMPLATE pveGetLatestTemplate 'debian-12-standard' TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3" pctCreate $1 "$TEMPLATE" "$2" "$3"
@ -620,7 +675,7 @@ pctCreateDebian(){
} }
pctCreateUbuntu(){ pctCreateUbuntu(){
local TEMPLATE local TEMPLATE
getLatestTemplate ubuntu TEMPLATE pveGetLatestTemplate ubuntu TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3" pctCreate $1 "$TEMPLATE" "$2" "$3"
@ -637,7 +692,7 @@ pctCreateTurnkey(){
local app=$1 local app=$1
shift shift
local TEMPLATE local TEMPLATE
getLatestTemplate '.*-turnkey-'$app TEMPLATE pveGetLatestTemplate '.*-turnkey-'$app TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3" pctCreate $1 "$TEMPLATE" "$2" "$3"

View File

@ -33,17 +33,18 @@ LAN_BRIDGE=
ADMIN_BRIDGE= ADMIN_BRIDGE=
# NOTE: it is simpler to statically assign these than to configure dhcp
# plus port forewarding to the dynamically assigned IP.
DFL_WAN_IP=192.168.1.101/24 DFL_WAN_IP=192.168.1.101/24
DFL_WAN_GATE=192.168.1.252 DFL_WAN_GATE=192.168.1.252
DFL_WAN_SSH_IP:192.168.1.102/24 DFL_WAN_SSH_IP:192.168.1.102/24
# Doman and email configuration # Domain and email configuration
# #
EMAIL=user@example.com
DOMAIN=example.com DOMAIN=example.com
EMAIL=user@example.com
# Web app/service domain configuration # Web app/service domain configuration

View File

@ -75,7 +75,7 @@ echo "# Installing dependencies..."
@ lxc-attach $ID apk add bash bridge iptables traefik logrotate @ lxc-attach $ID apk add bash bridge iptables traefik logrotate
echo "# Copying assets..." echo "# Copying assets..."
@ pct-push-r $ID ./assets / pctPushAssets $ID
echo "# Setup: traefik..." echo "# Setup: traefik..."
@ lxc-attach $ID rc-update add traefik @ lxc-attach $ID rc-update add traefik

View File

@ -72,7 +72,7 @@ echo "# Starting TKL UI..."
@ lxc-attach $ID -- bash -c "HUB_APIKEY=SKIP SEC_UPDATES=SKIP /usr/sbin/turnkey-init" @ lxc-attach $ID -- bash -c "HUB_APIKEY=SKIP SEC_UPDATES=SKIP /usr/sbin/turnkey-init"
echo "# Copying assets..." echo "# Copying assets..."
@ pct-push-r $ID ./assets / pctPushAssets $ID
echo "# Disabling fail2ban..." echo "# Disabling fail2ban..."
# NOTE: we do not need this as we'll be running from behind a reverse proxy... # NOTE: we do not need this as we'll be running from behind a reverse proxy...

View File

@ -145,7 +145,8 @@ done
@ lxc-attach $ID -- turnkey-occ maintenance:update:htaccess @ lxc-attach $ID -- turnkey-occ maintenance:update:htaccess
echo "# Copying assets..." echo "# Copying assets..."
@ pct-push-r $ID ./assets / pctPushAssets $ID
# XXX need to push proxy config to gate...
echo "# Disabling fail2ban..." echo "# Disabling fail2ban..."
# NOTE: we do not need this as we'll be running from behind a reverse proxy... # NOTE: we do not need this as we'll be running from behind a reverse proxy...

View File

@ -75,7 +75,7 @@ echo "# Installing dependencies..."
@ lxc-attach $ID apk add bash dnsmasq logrotate @ lxc-attach $ID apk add bash dnsmasq logrotate
echo "# Copying assets..." echo "# Copying assets..."
@ pct-push-r $ID ./assets / pctPushAssets $ID
echo "# Setup: dnsmasq..." echo "# Setup: dnsmasq..."
@ lxc-attach $ID rc-update add dnsmasq @ lxc-attach $ID rc-update add dnsmasq

View File

@ -60,6 +60,9 @@ OPTS_STAGE_2="\
#---------------------------------------------------------------------- #----------------------------------------------------------------------
echo "# Building config..."
buildAssets
echo "# Creating CT..." echo "# Creating CT..."
pctCreateAlpine $ID "${OPTS_STAGE_1}" "$PASS" pctCreateAlpine $ID "${OPTS_STAGE_1}" "$PASS"

View File

@ -100,7 +100,7 @@ echo "# Installing dependencies..."
iptables wireguard-tools-wg-quick make bind-tools libqrencode logrotate iptables wireguard-tools-wg-quick make bind-tools libqrencode logrotate
echo "# Copying assets..." echo "# Copying assets..."
@ pct-push-r $ID ./assets / pctPushAssets $ID
@ lxc-attach $ID -- chmod +x /root/getFreeClientIP @ lxc-attach $ID -- chmod +x /root/getFreeClientIP
echo "# Setup: wireguard server and client profile..." echo "# Setup: wireguard server and client profile..."