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:
# https://pve.proxmox.com/wiki/Linux_Container
getLatestTemplate(){
pveGetLatestTemplate(){
if [ $DRY_RUN ] ; then
[ -z $2 ] \
|| eval "$2=${CT_TEMPLATE:-\\\$CT_TEMPLATE}"
return
fi
#IFS=$'\n'
#@ pveam update
local templates=($(pveam available | grep -o ''${1}'.*$'))
@ -490,20 +489,21 @@ readVars(){
#
# buildAssets [VAR ..]
# makeTemplateSEDPatterns VAR ...
#
# XXX revise: SKIP handling...
NOTES=NOTES.md
buildAssets(){
local template_dir=${TEMPLATE_DIR:-templates}
local assets_dir=${ASSETS_DIR:-assets}
if ! [ -e $template_dir ] ; then
return
makeTemplateSEDPatterns(){
local var
for var in "$@" ; do
local val=${!var}
if [[ $val == SKIP ]] ; then
val=
fi
echo "-e 's/\\\${${var}}/${val//\//\\/}/g'"
done
}
local PATTERNS=()
local DFL_VARS=(
# same as makeTemplateSEDPatterns but adds default vars + generates *_IPn vars...
PCT_TEMPLATE_VARS=(
EMAIL
DOMAIN
CTHOSTNAME
@ -520,39 +520,86 @@ buildAssets(){
ADMIN_IP
ADMIN_GATE
)
for var in ${DFL_VARS[@]} ; do
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
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=
ip_vars+=("${var}n")
eval "local ${var}n=\"${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
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 ..]
#
# XXX revise: SKIP handling...
NOTES=NOTES.md
buildAssets(){
local template_dir=${TEMPLATE_DIR:-templates}
local assets_dir=${ASSETS_DIR:-assets}
local staging_dir=${STAGING_DIR:-staging}
if ! [ -e $template_dir ] ; then
return
fi
local PCT_TEMPLATE_PATTERNS=($(makePCTTemplateSEDPatterns "$@"))
# assets...
cp -R "${assets_dir}"/* "${staging_dir}"
# template dir...
local TEMPLATES=($(find "$template_dir" -type f))
for file in "${TEMPLATES[@]}" ; do
file=${file#${template_dir}}
@ -560,20 +607,28 @@ buildAssets(){
[ $DRY_RUN ] \
&& continue
# ensure the directory exists...
mkdir -p "$(dirname "${assets_dir}/${file}")"
mkdir -p "$(dirname "${staging_dir}/${file}")"
cat "${template_dir}/${file}" \
| eval "sed ${PATTERNS[@]}" \
> "${assets_dir}/${file}"
| expandTemplate \
> "${staging_dir}/${file}"
done
# special case: NOTES.md...
if [ -z "$DESCRIPTION" ] && [ -e "$NOTES" ] ; then
DESCRIPTION="$(\
cat ${NOTES} \
| eval "sed ${PATTERNS[@]}")"
| expandTemplate)"
fi
}
#
# pctPushAssets ID
#
pctPushAssets(){
@ pct-push-r $1 "${STAGING_DIR:-./staging}" /
}
#
# pctCreate ID TEMPLATE ARGS [PASS]
#
@ -598,7 +653,7 @@ pctCreate(){
#
pctCreateAlpine(){
local TEMPLATE
getLatestTemplate alpine TEMPLATE
pveGetLatestTemplate alpine TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3"
@ -609,7 +664,7 @@ pctCreateAlpine(){
}
pctCreateDebian(){
local TEMPLATE
getLatestTemplate 'debian-12-standard' TEMPLATE
pveGetLatestTemplate 'debian-12-standard' TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3"
@ -620,7 +675,7 @@ pctCreateDebian(){
}
pctCreateUbuntu(){
local TEMPLATE
getLatestTemplate ubuntu TEMPLATE
pveGetLatestTemplate ubuntu TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3"
@ -637,7 +692,7 @@ pctCreateTurnkey(){
local app=$1
shift
local TEMPLATE
getLatestTemplate '.*-turnkey-'$app TEMPLATE
pveGetLatestTemplate '.*-turnkey-'$app TEMPLATE
pctCreate $1 "$TEMPLATE" "$2" "$3"

View File

@ -33,17 +33,18 @@ LAN_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_GATE=192.168.1.252
DFL_WAN_SSH_IP:192.168.1.102/24
# Doman and email configuration
# Domain and email configuration
#
EMAIL=user@example.com
DOMAIN=example.com
EMAIL=user@example.com
# Web app/service domain configuration

View File

@ -75,7 +75,7 @@ echo "# Installing dependencies..."
@ lxc-attach $ID apk add bash bridge iptables traefik logrotate
echo "# Copying assets..."
@ pct-push-r $ID ./assets /
pctPushAssets $ID
echo "# Setup: 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"
echo "# Copying assets..."
@ pct-push-r $ID ./assets /
pctPushAssets $ID
echo "# Disabling fail2ban..."
# 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
echo "# Copying assets..."
@ pct-push-r $ID ./assets /
pctPushAssets $ID
# XXX need to push proxy config to gate...
echo "# Disabling fail2ban..."
# 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
echo "# Copying assets..."
@ pct-push-r $ID ./assets /
pctPushAssets $ID
echo "# Setup: dnsmasq..."
@ lxc-attach $ID rc-update add dnsmasq

View File

@ -60,6 +60,9 @@ OPTS_STAGE_2="\
#----------------------------------------------------------------------
echo "# Building config..."
buildAssets
echo "# Creating CT..."
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
echo "# Copying assets..."
@ pct-push-r $ID ./assets /
pctPushAssets $ID
@ lxc-attach $ID -- chmod +x /root/getFreeClientIP
echo "# Setup: wireguard server and client profile..."