From 01366f02a6eac5ecde068216c4517f1514d9428b Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 10 Jan 2024 23:35:52 +0300 Subject: [PATCH] cleanup + dependency checking... Signed-off-by: Alex A. Naanou --- .pct-helpers | 61 +++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 10 +++----- nextcloud/make.sh | 6 ----- wireguard/make.sh | 19 +++++++++++++-- 4 files changed, 81 insertions(+), 15 deletions(-) diff --git a/.pct-helpers b/.pct-helpers index f1d8870..4a08ac1 100644 --- a/.pct-helpers +++ b/.pct-helpers @@ -27,6 +27,28 @@ ECHO_PREFIX="### " +#---------------------------------------------------------------------- + +# +# check MSG COMMAND .. +# +check(){ + local MSG=$1 + shift + for cmd in "$@" ; do + which $cmd > /dev/null 2>&1 \ + || eval "echo \"$MSG\"" >&2 + done +} + +need(){ + check 'ERROR: "$cmd": needed by this script but not in path.' "$@" +} +would-like(){ + check 'WARNING: "$cmd": is not in path.' "$@" +} + + #---------------------------------------------------------------------- # Fill section... # @@ -172,12 +194,17 @@ getLatestTemplate(){ # xread [-n] MSG VAR # # This saves all user input variables to the $XREAD_VARS array. +# +# XXX add support for keywords like SKIP and DISABLE xread(){ local non_empty= if [[ $1 == '-n' ]] ; then shift local non_empty=1 fi + if [[ "${!2}" == "SKIP" ]] ; then + return + fi if [ -z ${!2} ] ; then eval 'read -ep "'$1'" -i "$DFL_'$2'" '${2}'' XREAD_VARS+=(${2}) @@ -189,6 +216,40 @@ xread(){ && echo "$2=${!2}" } +# +# xreadYes MSG VAR +# +xreadYes(){ + if [[ "${!2}" == "SKIP" ]] ; then + return + fi + if [ -z ${!2} ] ; then + if [ -z $(eval "echo \$DFL_${2}") ] ; then + local yes=y + local no=N + local dfl= + else + local yes=Y + local no=n + local dfl=1 + fi + eval 'read -ep "'$1' ('$yes'/'$no') " '${2}'' + XREAD_VARS+=(${2}) + # normalize... + eval "${2}=${!2,,}" + if [[ "${!2}" == 'y' ]] ; then + eval "${2}=1" + elif [[ ${!2} == 'n' ]] ; then + eval "${2}=" + # set default if empty... + else + eval "${2}=\${${2}:-$dfl}" + fi + fi + [ $SCRIPTING ] \ + && echo "$2=${!2}" +} + # # xreadpass VAR # diff --git a/Makefile b/Makefile index be27e12..1c6db2f 100644 --- a/Makefile +++ b/Makefile @@ -18,13 +18,9 @@ # - the rest of the CT's are created in order of importance, strting # from CT's needed for access and ending with services. CTs := \ - gate \ - ns \ - ssh \ - wireguard \ - syncthing \ - nextcloud \ - gitea + gate ns \ + ssh wireguard syncthing \ + nextcloud gitea diff --git a/nextcloud/make.sh b/nextcloud/make.sh index 54d0e91..50c73f5 100755 --- a/nextcloud/make.sh +++ b/nextcloud/make.sh @@ -72,12 +72,6 @@ buildAssets echo "# Creating CT..." pctCreateTurnkey 'nextcloud' $ID "$OPTS_STAGE_1" "$PASS" -#getLatestTemplate '.*-turnkey-nextcloud' TEMPLATE -#pctCreate $ID "$TEMPLATE" "$OPTS_STAGE_1" "$PASS" -#sleep ${TIMEOUT:=5} -# -#tklWaitForSetup $ID - echo "# Starting TKL UI..." # XXX might be a good idea to reaaad stuff from config... @ lxc-attach $ID -- bash -c "\ diff --git a/wireguard/make.sh b/wireguard/make.sh index b698635..f3679a1 100755 --- a/wireguard/make.sh +++ b/wireguard/make.sh @@ -13,11 +13,14 @@ source ../.pct-helpers #---------------------------------------------------------------------- -readConfig +# check dependencies... +would-like dig qrencode #---------------------------------------------------------------------- +readConfig + DFL_ID=${DFL_ID:=103} DFL_CTHOSTNAME=${DFL_CTHOSTNAME:=wireguard} @@ -36,7 +39,11 @@ LAN_GATE=- REBOOT=${REBOOT:=1} # Wireguard config... -DFL_ENDPOINT=${DFL_ENDPOINT:=$(dig +short ${DOMAIN:-$DFL_DOMAIN} | tail -1)} +DFL_ENDPOINT=${DFL_ENDPOINT:=$(\ + which dig > /dev/null 2>&1 \ + && (dig +short ${DOMAIN:-$DFL_DOMAIN} \ + | tail -1) \ + || echo "${DOMAIN:-$DFL_DOMAIN}")} xread "Wireguard endpoint: " ENDPOINT DFL_ENDPOINT_PORT=${DFL_ENDPOINT_PORT:=51820} @@ -45,6 +52,7 @@ xread "Wireguard endpoint port: " ENDPOINT_PORT CLIENT_IPS=${CLIENT_IPS:-10.42.0.0/16} ALLOWED_IPS=${ALLOWED_IPS:-0.0.0.0/0,${CLIENT_IPS}} +xreadYes "Show profile as QRcode when done?" QRCODE readVars @@ -99,6 +107,13 @@ echo "# Setup: wireguard default profile..." echo "# client config:" @ mkdir -p clients @ pct pull $ID /etc/wireguard/clients/default.conf clients/default.conf +# show the profile as a qrcode... +if [ "$QRCODE" ] \ + && which qrencode > /dev/null 2>&1 ; then + echo "# default progile:" + cat clients/default.conf \ + | qrencode -f UTF8 +fi #echo "# Setup: bridge device..." @ lxc-attach $ID wg-quick up wg0