From e0fff5e52903f66a47c402f6956c170b37f6fff9 Mon Sep 17 00:00:00 2001 From: szaimen Date: Wed, 27 May 2020 18:32:57 +0200 Subject: [PATCH] update lib.sh to change variables to functions (#1247) --- apps/collabora.sh | 13 ++--- apps/fail2ban.sh | 6 +- apps/fulltextsearch.sh | 10 ++-- apps/onlyoffice.sh | 6 +- apps/talk.sh | 14 +++-- lets-encrypt/activate-tls.sh | 8 +-- lib.sh | 91 +++++++++++++++++++------------ nextcloud-startup-script.sh | 25 +++++---- nextcloud_install_production.sh | 7 +-- nextcloud_update.sh | 16 +++--- static/change_db_pass.sh | 6 +- static/nextcloud_configuration.sh | 6 +- static/static_ip.sh | 14 +++-- static/updatenotification.sh | 6 +- 14 files changed, 132 insertions(+), 96 deletions(-) diff --git a/apps/collabora.sh b/apps/collabora.sh index cd193428ea..0f7cc7d260 100644 --- a/apps/collabora.sh +++ b/apps/collabora.sh @@ -71,13 +71,13 @@ then count=$((count+1)) fi done - + msg_box "Collabora was successfully uninstalled." exit ;; "Reinstall Collabora") print_text_in_color "$ICyan" "Reinstalling Collabora..." - + # Check if Collabora is previously installed # If yes, then stop and prune the docker container docker_prune_this 'collabora/code' @@ -138,12 +138,9 @@ then occ_command app:remove onlyoffice fi -# shellcheck disable=2034,2059 -true -# shellcheck source=lib.sh -NC_UPDATE=1 && COLLABORA_INSTALL=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE -unset COLLABORA_INSTALL +# Get needed variables +nc_update +collabora_install # Notification msg_box "Before you start, please make sure that port 80+443 is directly forwarded to this machine!" diff --git a/apps/fail2ban.sh b/apps/fail2ban.sh index b4b93b7925..78148077bf 100644 --- a/apps/fail2ban.sh +++ b/apps/fail2ban.sh @@ -6,8 +6,10 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +nc_update # Check for errors + debug code and abort if something isn't right # 1 = ON diff --git a/apps/fulltextsearch.sh b/apps/fulltextsearch.sh index 30d66e9ca6..e7fcc6bd2c 100644 --- a/apps/fulltextsearch.sh +++ b/apps/fulltextsearch.sh @@ -6,10 +6,12 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NCDB=1 && NC_UPDATE=1 && ES_INSTALL=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE -unset ES_INSTALL -unset NCDB +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +ncdb +nc_update +es_install # Check for errors + debug code and abort if something isn't right # 1 = ON diff --git a/apps/onlyoffice.sh b/apps/onlyoffice.sh index 74fdb52e58..28b4796de1 100644 --- a/apps/onlyoffice.sh +++ b/apps/onlyoffice.sh @@ -5,8 +5,10 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +nc_update # Check for errors + debug code and abort if something isn't right # 1 = ON diff --git a/apps/talk.sh b/apps/talk.sh index 83833f86af..eef5c2f901 100644 --- a/apps/talk.sh +++ b/apps/talk.sh @@ -5,9 +5,11 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NC_UPDATE=1 && TURN_INSTALL=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE -unset TURN_INSTALL +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +nc_update +turn_install # Check for errors + debug code and abort if something isn't right # 1 = ON @@ -37,7 +39,7 @@ then choice=$(whiptail --radiolist "It seems like 'Nextcloud Talk' is already installed.\nChoose what you want to do.\nSelect by pressing the spacebar and ENTER" "$WT_HEIGHT" "$WT_WIDTH" 4 \ "Uninstall Nextcloud Talk" "" OFF \ "Reinstall Nextcloud Talk" "" ON 3>&1 1>&2 2>&3) - + case "$choice" in "Uninstall Nextcloud Talk") print_text_in_color "$ICyan" "Uninstalling Nextcloud Talk and resetting all settings..." @@ -85,8 +87,8 @@ NONO_PORTS=(22 25 53 80 443 3306 5432 7983 8983 10000) msg_box "The default port for Talk used in this script is port $TURN_PORT. You can read more about that port here: https://www.speedguide.net/port.php?port=$TURN_PORT -You will now be given the option to change this port to something of your own. -Please keep in mind NOT to use the following ports as they are likley to be in use already: +You will now be given the option to change this port to something of your own. +Please keep in mind NOT to use the following ports as they are likley to be in use already: ${NONO_PORTS[*]}" if [[ "yes" == $(ask_yes_or_no "Do you want to change port?") ]] diff --git a/lets-encrypt/activate-tls.sh b/lets-encrypt/activate-tls.sh index 20d2665489..dddbbaf8c3 100644 --- a/lets-encrypt/activate-tls.sh +++ b/lets-encrypt/activate-tls.sh @@ -62,12 +62,8 @@ fi while true do -print_text_in_color "$ICyan" "Updating lib.sh..." -# shellcheck disable=2034,2059 -true -# shellcheck source=lib.sh -TLS_INSTALL=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset TLS_INSTALL +# Get needed variables +tls_install # Ask for domain name echo diff --git a/lib.sh b/lib.sh index e10881b41e..958381674d 100644 --- a/lib.sh +++ b/lib.sh @@ -35,13 +35,19 @@ gen_passwd() { DISTRO=$(lsb_release -sr) KEYBOARD_LAYOUT=$(localectl status | grep "Layout" | awk '{print $3}') # Network -[ -n "$FIRST_IFACE" ] && IFACE=$(lshw -c network | grep "logical name" | awk '{print $3; exit}') +first_iface() { +IFACE=$(lshw -c network | grep "logical name" | awk '{print $3; exit}') +} +[ -n "$FIRST_IFACE" ] && first_iface && unset FIRST_IFACE IFACE2=$(ip -o link show | awk '{print $2,$9}' | grep 'UP' | cut -d ':' -f 1) REPO=$(grep deb-src /etc/apt/sources.list | grep http | awk '{print $3}' | head -1) ADDRESS=$(hostname -I | cut -d ' ' -f 1) # WANIP4=$(dig +short myip.opendns.com @resolver1.opendns.com) # as an alternative WANIP4=$(curl -s -k -m 5 https://ipv4bot.whatismyipaddress.com) -[ -n "$LOAD_IP6" ] && WANIP6=$(curl -s -k -m 5 https://ipv6bot.whatismyipaddress.com) +load_ip6() { +WANIP6=$(curl -s -k -m 5 https://ipv6bot.whatismyipaddress.com) +} +[ -n "$LOAD_IP6" ] && load_ip6 && unset LOAD_IP6 INTERFACES="/etc/netplan/01-netcfg.yaml" GATEWAY=$(ip route | grep default | awk '{print $3}') DNS1="9.9.9.9" @@ -63,26 +69,41 @@ ROOT_PROFILE="/root/.bash_profile" SHUF=$(shuf -i 25-29 -n 1) PGDB_PASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=") NEWPGPASS=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=") -[ -n "$NCDB" ] && NCCONFIGDB=$(grep "dbname" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g") -[ -n "$NCDBPASS" ] && NCCONFIGDBPASS=$(grep "dbpassword" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g") +ncdb() { +NCCONFIGDB=$(grep "dbname" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g") +} +[ -n "$NCDB" ] && ncdb && unset NCDB +ncdbpass() { +NCCONFIGDBPASS=$(grep "dbpassword" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g") +} +[ -n "$NCDBPASS" ] && ncdbpass && unset NCDBPASS # Path to specific files SECURE="$SCRIPTS/setup_secure_permissions_nextcloud.sh" # Nextcloud version -[ -n "$NC_UPDATE" ] && CURRENTVERSION=$(sudo -u www-data php $NCPATH/occ status | grep "versionstring" | awk '{print $3}') -[ -n "$NC_UPDATE" ] && NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | tail -1) -[ -n "$NC_UPDATE" ] && STABLEVERSION="nextcloud-$NCVERSION" -[ -n "$NC_UPDATE" ] && NCMAJOR="${NCVERSION%%.*}" -[ -n "$NC_UPDATE" ] && NCBAD=$((NCMAJOR-2)) +nc_update() { +CURRENTVERSION=$(sudo -u www-data php $NCPATH/occ status | grep "versionstring" | awk '{print $3}') +NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | tail -1) +STABLEVERSION="nextcloud-$NCVERSION" +NCMAJOR="${NCVERSION%%.*}" +NCBAD=$((NCMAJOR-2)) +} +[ -n "$NC_UPDATE" ] && nc_update && unset NC_UPDATE # Set the hour for automatic updates. This would be 18:00 as only the hour is configurable. AUT_UPDATES_TIME="18" # Keys OpenPGP_fingerprint='28806A878AE423A28372792ED75899B9A724937A' # Collabora Docker URL (collabora.sh -[ -n "$COLLABORA_INSTALL" ] && SUBDOMAIN=$(whiptail --title "T&M Hansson IT - Collabora" --inputbox "Collabora subdomain eg: office.yourdomain.com\n\nNOTE: This domain must be different than your Nextcloud domain. They can however be hosted on the same server, but would require seperate DNS entries." "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) +collabora_install() { +SUBDOMAIN=$(whiptail --title "T&M Hansson IT - Collabora" --inputbox "Collabora subdomain eg: office.yourdomain.com\n\nNOTE: This domain must be different than your Nextcloud domain. They can however be hosted on the same server, but would require seperate DNS entries." "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3) # Nextcloud Main Domain (collabora.sh) -[ -n "$COLLABORA_INSTALL" ] && NCDOMAIN=$(whiptail --title "T&M Hansson IT - Collabora" --inputbox "Nextcloud domain, make sure it looks like this: cloud\\.yourdomain\\.com" "$WT_HEIGHT" "$WT_WIDTH" cloud\\.yourdomain\\.com 3>&1 1>&2 2>&3) +NCDOMAIN=$(whiptail --title "T&M Hansson IT - Collabora" --inputbox "Nextcloud domain, make sure it looks like this: cloud\\.yourdomain\\.com" "$WT_HEIGHT" "$WT_WIDTH" cloud\\.yourdomain\\.com 3>&1 1>&2 2>&3) +} +[ -n "$COLLABORA_INSTALL" ] && collabora_install && unset COLLABORA_INSTALL # Nextcloud Main Domain (activate-tls.sh) -[ -n "$TLS_INSTALL" ] && TLSDOMAIN=$(whiptail --title "T&M Hansson IT - Let's Encrypt" --inputbox "Please enter the domain name you will use for Nextcloud.\n\nMake sure it looks like this:\nyourdomain.com, or cloud.yourdomain.com" "$WT_HEIGHT" "$WT_WIDTH" cloud.yourdomain.com 3>&1 1>&2 2>&3) +tls_install() { +TLSDOMAIN=$(whiptail --title "T&M Hansson IT - Let's Encrypt" --inputbox "Please enter the domain name you will use for Nextcloud.\n\nMake sure it looks like this:\nyourdomain.com, or cloud.yourdomain.com" "$WT_HEIGHT" "$WT_WIDTH" cloud.yourdomain.com 3>&1 1>&2 2>&3) +} +[ -n "$TLS_INSTALL" ] && tls_install && unset TLS_INSTALL # Letsencrypt SITES_AVAILABLE="/etc/apache2/sites-available" LETSENCRYPTPATH="/etc/letsencrypt" @@ -112,17 +133,22 @@ SPAMHAUS=/etc/spamhaus.wl ENVASIVE=/etc/apache2/mods-available/mod-evasive.load APACHE2=/etc/apache2/apache2.conf # Full text Search -[ -n "$ES_INSTALL" ] && INDEX_USER=$(gen_passwd "$SHUF" '[:lower:]') -[ -n "$ES_INSTALL" ] && ROREST=$(gen_passwd "$SHUF" "A-Za-z0-9") -[ -n "$ES_INSTALL" ] && nc_fts="ark74/nc_fts" -[ -n "$ES_INSTALL" ] && fts_es_name="fts_esror" +es_install() { +INDEX_USER=$(gen_passwd "$SHUF" '[:lower:]') +ROREST=$(gen_passwd "$SHUF" "A-Za-z0-9") +nc_fts="ark74/nc_fts" +fts_es_name="fts_esror" +} +[ -n "$ES_INSTALL" ] && es_install && unset ES_INSTALL # Talk -[ -n "$TURN_INSTALL" ] && TURN_CONF="/etc/turnserver.conf" -[ -n "$TURN_INSTALL" ] && TURN_PORT=5349 -[ -n "$TURN_INSTALL" ] && TURN_DOMAIN=$(sudo -u www-data /var/www/nextcloud/occ config:system:get overwrite.cli.url | sed 's#https://##;s#/##') -[ -n "$TURN_INSTALL" ] && SHUF=$(shuf -i 25-29 -n 1) -[ -n "$TURN_INSTALL" ] && TURN_SECRET=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=") - +turn_install() { +TURN_CONF="/etc/turnserver.conf" +TURN_PORT=5349 +TURN_DOMAIN=$(sudo -u www-data /var/www/nextcloud/occ config:system:get overwrite.cli.url | sed 's#https://##;s#/##') +SHUF=$(shuf -i 25-29 -n 1) +TURN_SECRET=$(gen_passwd "$SHUF" "a-zA-Z0-9@#*=") +} +[ -n "$TURN_INSTALL" ] && turn_install && unset TURN_INSTALL ## FUNCTIONS @@ -790,8 +816,7 @@ download_verify_nextcloud_stable() { while [ -z "$NCVERSION" ] do print_text_in_color "$ICyan" "Fetching the latest Nextcloud version..." - NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | tail -1) - STABLEVERSION="nextcloud-$NCVERSION" + nc_update print_text_in_color "$IGreen" "$NCVERSION" done @@ -940,11 +965,9 @@ any_key() { } lowest_compatible_nc() { -if [ -z "$NC_UPDATE" ] +if [ -z "$CURRENTVERSION" ] then -# shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL $GITHUB_REPO/lib.sh) -unset NC_UPDATE + nc_update fi if [ "${CURRENTVERSION%%.*}" -lt "$1" ] then @@ -980,11 +1003,9 @@ fi # Check new version # shellcheck source=lib.sh -if [ -z "$NC_UPDATE" ] +if [ -z "$CURRENTVERSION" ] then -# shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL $GITHUB_REPO/lib.sh) -unset NC_UPDATE + nc_update fi if [ "${CURRENTVERSION%%.*}" -ge "$1" ] then @@ -1123,11 +1144,9 @@ printf "%b%s%b\n" "$1" "$2" "$Color_Off" # 2 = repository # Nextcloud version git_apply_patch() { -if [ -z "$NC_UPDATE" ] +if [ -z "$CURRENTVERSION" ] then -# shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL $GITHUB_REPO/lib.sh) -unset NC_UPDATE + nc_update fi if [[ "$CURRENTVERSION" = "$3" ]] then diff --git a/nextcloud-startup-script.sh b/nextcloud-startup-script.sh index 18b46d31f6..fb1353781f 100644 --- a/nextcloud-startup-script.sh +++ b/nextcloud-startup-script.sh @@ -42,17 +42,23 @@ then # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NCDB=1 && FIRST_IFACE=1 source /var/scripts/lib.sh -unset NCDB -unset FIRST_IFACE +source /var/scripts/lib.sh + +# Get needed variables +ncdb +first_iface + # If we have internet, then use the latest variables from the lib remote file elif print_text_in_color "$ICyan" "Testing internet connection..." && ping github.com -c 2 then true # shellcheck source=lib.sh -NCDB=1 && FIRST_IFACE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset FIRST_IFACE -unset NCDB +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +ncdb +first_iface + else print_text_in_color "$IRed" "You don't seem to have a working internet connection, and /var/scripts/lib.sh is missing so you can't run this script." print_text_in_color "$ICyan" "Please report this to https://github.com/nextcloud/vm/issues/" @@ -131,10 +137,9 @@ Please also post this issue on: https://github.com/nextcloud/vm/issues" exit 1 fi -# shellcheck source=lib.sh -NCDB=1 && NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE -unset NCDB +# Get needed variables +ncdb +nc_update # Check for errors + debug code and abort if something isn't right # 1 = ON diff --git a/nextcloud_install_production.sh b/nextcloud_install_production.sh index 76fd090c7b..51496278f4 100644 --- a/nextcloud_install_production.sh +++ b/nextcloud_install_production.sh @@ -50,11 +50,8 @@ else apt install whiptail -y fi -# shellcheck disable=2034,2059 -true -# shellcheck source=lib.sh -FIRST_IFACE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset FIRST_IFACE +# Get needed variables +first_iface # Check for errors + debug code and abort if something isn't right # 1 = ON diff --git a/nextcloud_update.sh b/nextcloud_update.sh index 5277211d98..6f73427066 100644 --- a/nextcloud_update.sh +++ b/nextcloud_update.sh @@ -7,9 +7,11 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NCDB=1 && NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE -unset NCDB +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +ncdb +nc_update # T&M Hansson IT AB © - 2020, https://www.hanssonit.se/ @@ -110,7 +112,7 @@ then yes no | pecl upgrade redis systemctl restart redis-server.service fi - + # Double check if redis.so is enabled if ! grep -qFx extension=redis.so "$PHP_INI" then @@ -413,8 +415,8 @@ then sed -i "s|date --utc|date|g" /usr/sbin/zfs-auto-snapshot check_command zfs-auto-snapshot -r ncdata fi -fi - +fi + # Backup data for folders in config apps do @@ -457,7 +459,7 @@ fi if [ -d $BACKUP/apps/ ] then print_text_in_color "$ICyan" "$BACKUP/apps/ exists" - echo + echo print_text_in_color "$IGreen" "All files are backed up." occ_command maintenance:mode --on countdown "Removing old Nextcloud instance in 5 seconds..." "5" diff --git a/static/change_db_pass.sh b/static/change_db_pass.sh index c68f66bea6..2cde2c04f6 100644 --- a/static/change_db_pass.sh +++ b/static/change_db_pass.sh @@ -2,8 +2,10 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NCDBPASS=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NCDBPASS +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +ncdbpass # T&M Hansson IT AB © - 2020, https://www.hanssonit.se/ diff --git a/static/nextcloud_configuration.sh b/static/nextcloud_configuration.sh index a255606f24..25bb4abfdd 100644 --- a/static/nextcloud_configuration.sh +++ b/static/nextcloud_configuration.sh @@ -5,8 +5,10 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +nc_update # Check for errors + debug code and abort if something isn't right # 1 = ON diff --git a/static/static_ip.sh b/static/static_ip.sh index 9f27b44245..610fafac86 100644 --- a/static/static_ip.sh +++ b/static/static_ip.sh @@ -15,16 +15,22 @@ then # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -FIRST_IFACE=1 source /var/scripts/lib.sh -unset FIRST_IFACE +source /var/scripts/lib.sh + +# Get needed variables +first_iface + # If we have internet, then use the latest variables from the lib remote file elif print_text_in_color "$ICyan" "Testing internet connection..." && ping github.com -c 2 then # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -FIRST_IFACE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset FIRST_IFACE +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +first_iface + else print_text_in_color "$IRed" "You don't seem to have a working internet connection, and /var/scripts/lib.sh is missing so you can't run this script." print_text_in_color "$ICyan" "Please report this to https://github.com/nextcloud/vm/issues/" diff --git a/static/updatenotification.sh b/static/updatenotification.sh index 0acda2c766..734c081f7c 100644 --- a/static/updatenotification.sh +++ b/static/updatenotification.sh @@ -5,8 +5,10 @@ # shellcheck disable=2034,2059 true # shellcheck source=lib.sh -NC_UPDATE=1 . <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) -unset NC_UPDATE +. <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh) + +# Get needed variables +nc_update print_text_in_color "$ICyan" "Checking for new Nextcloud version..."