From 3117719472c603c64f4eb3c3d2f7e56964595e07 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sun, 2 Jun 2024 22:12:26 +0300 Subject: [PATCH 01/40] feat: created script file --- bin/ctrlhelper.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 bin/ctrlhelper.sh diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh new file mode 100644 index 000000000..25d3c2478 --- /dev/null +++ b/bin/ctrlhelper.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "In development" \ No newline at end of file From cd1c4aaa753fa12740e09b97ab1555d2ece2b0a1 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 15:50:20 +0000 Subject: [PATCH 02/40] feat: Specifying CPGG directory if it differs from the default one using CLI-GUI --- bin/ctrlhelper.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 25d3c2478..7b0055d51 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -1,3 +1,55 @@ #!/bin/bash -echo "In development" \ No newline at end of file +# System vars | DO NOT TOUCH!!! +SCRIPT_VER="0.0.1" +DEFAULT_DIR="/var/www/controlpanel/" +CPGG_DIR="" +DIR_NULL="" + +# Logo for CLI-GUI +logo() { + clear + echo " ________ ______ __ " + echo " / ____/ /______/ / __ \____ _____ ___ / /____ _____ _" + echo " / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/" + echo " / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / " + echo " \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / " + echo " /____//____/ " + echo "" +} + +# Set root CtrlPanel directory using CLI-GUI +## If $DEFAULT_DIR doesn't exists and $CPGG_DIR var isn't specified +if [ ! -d "$DEFAULT_DIR" ] && [ -z "$CPGG_DIR" ]; then + while true; do + # If $CPGG_DIR var isn't specified, show "Default not exists" + if [ -z "$CPGG_DIR" ]; then + logo + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" + fi + # If $DIR_NULL is true, show "Cannot be empty" + if [ "$DIR_NULL" == "true" ]; then + logo + echo " You have not specified a directory, it cannot be empty!" + fi + # Reading directory specified by the user + read -rp " > " CPGG_DIR + + # If $CPGG_DIR var isn't specified set $DIR_NULL to true + if [ -z "$CPGG_DIR" ]; then + DIR_NULL="true" + continue + fi + + # If $CPGG_DIR exists set $DIR_NULL to null and continue script + if [ -d "$CPGG_DIR" ]; then + DIR_NULL="" + break + # If $CPGG_DIR doesn't exists, show logo with "Directory does not exist" message + else + logo + echo " $CPGG_DIR directory does not exist. Try again" + DIR_NULL="" + fi + done +fi \ No newline at end of file From 9344041944b1eda592309766b58db9c7d57555d3 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 15:52:38 +0000 Subject: [PATCH 03/40] feat: Specifying CPGG directory using --cpgg-dir parameter --- bin/ctrlhelper.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 7b0055d51..9b80a51fd 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -18,6 +18,29 @@ logo() { echo "" } +# Setting root CtrlPanel directory using the --cpgg-dir=/path/to/folder parameter +while [[ $# -gt 0 ]]; do + case "$1" in + --cpgg-dir=*) + CPGG_DIR="${1#*=}" + shift + + # Check if directory exists + if [ ! -d "$CPGG_DIR" ]; then + CPGGDIR_SET_ERR="echo $CPGG_DIR directory does not exist. Try again" + restore_terminal "$CPGGDIR_SET_ERR" + fi + if [ ! -f "$CPGG_DIR/config/app.php" ]; then + NOT_CPGG_ROOT_ERR="echo $CPGG_DIR is not a root CtrlPanel directory. Try again" + restore_terminal "$NOT_CPGG_ROOT_ERR" + fi + ;; + *) + shift + ;; + esac +done + # Set root CtrlPanel directory using CLI-GUI ## If $DEFAULT_DIR doesn't exists and $CPGG_DIR var isn't specified if [ ! -d "$DEFAULT_DIR" ] && [ -z "$CPGG_DIR" ]; then From 46cf09afa31393f3e05e0d9d16b46489d498ce20 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 15:57:36 +0000 Subject: [PATCH 04/40] feat: Saving and restoring terminal contents to execute a script in a "clean" terminal --- bin/ctrlhelper.sh | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 9b80a51fd..0e4d323b3 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -18,6 +18,21 @@ logo() { echo "" } +# Function for restoring terminal content after the script is completed or closed +restore_terminal() { + tput rmcup + if [[ -n $1 ]]; then + $1 + fi + exit +} + +# Restoring terminal content after ^C +trap restore_terminal SIGINT + +# Save terminal +tput smcup + # Setting root CtrlPanel directory using the --cpgg-dir=/path/to/folder parameter while [[ $# -gt 0 ]]; do case "$1" in @@ -75,4 +90,7 @@ if [ ! -d "$DEFAULT_DIR" ] && [ -z "$CPGG_DIR" ]; then DIR_NULL="" fi done -fi \ No newline at end of file +fi + +# Restoring terminal after succes +restore_terminal \ No newline at end of file From bc972dbabe8662065d347bb0bbe6ee1a960aeba1 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 16:01:23 +0000 Subject: [PATCH 05/40] feat: Checking if the panel needs to be updated --- bin/ctrlhelper.sh | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 0e4d323b3..876000d29 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -92,5 +92,69 @@ if [ ! -d "$DEFAULT_DIR" ] && [ -z "$CPGG_DIR" ]; then done fi +# Getting curent CtrlPanel version +PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${CPGG_DIR:-$DEFAULT_DIR}/config/app.php") +# Getting latest CtrlPanel version +PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') + +# Comparing current and latest versions +## -1 => Version above the latest one is installed +## 0 => Latest version is installed +## 1 => Update available +version_compare() { + local current_version="$1" + local latest_version="$2" + + # Break down versions into components + IFS='.' read -r -a current_parts <<<"$current_version" + IFS='.' read -r -a latest_parts <<<"$latest_version" + + # Add zeros to the shorter version (e.g. 0.10 => 0.10.0) + while ((${#current_parts[@]} < ${#latest_parts[@]})); do + current_parts+=("0") + done + + # Compare components one by one + for ((i = 0; i < ${#current_parts[@]}; i++)); do + if ((${current_parts[i]} < ${latest_parts[i]})); then + echo "1" + return 1 # Update needed + elif ((${current_parts[i]} > ${latest_parts[i]})); then + echo "-1" + return -1 # A newer version is installed + fi + done + + echo "0" + return 0 # Latest version is installed +} + +UPDATE_NEEDED=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") + +# Logo with versions for CLI-GUI +logo_version() { + clear + echo " ________ ______ __ " + echo " / ____/ /______/ / __ \____ _____ ___ / /____ _____ _" + echo " / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/" + echo " / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / " + echo " \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / " + echo " /____//____/ " + echo " Script version: $SCRIPT_VER" + echo " CtrlPanel version: $PANEL_VER" + echo "" +} + +# Message about available Update +logo_version_message() { + if [[ $UPDATE_NEEDED == 1 ]]; then + echo " New version available! You can update right now by selecting \"Update\" option." + echo "" + elif [[ $UPDATE_NEEDED == -1 ]]; then + echo " You are using a newer version! Most likely you have a development branch installed." + echo "" + fi +} + # Restoring terminal after succes restore_terminal \ No newline at end of file From 72b433653bcd6c7e188d2c5979ef29a30b06efb2 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 16:03:33 +0000 Subject: [PATCH 06/40] feat: main menu and submenus template --- bin/ctrlhelper.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 876000d29..0453ec9c0 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -156,5 +156,70 @@ logo_version_message() { fi } +# Main menu +main_menu() { + logo_version + logo_version_message + echo " Select an option:" + echo " 1. Install dependencies" + echo " 2. Update" + echo " 3. Uninstall" + echo " 4. Info & Help" + echo " 0. Exit" + echo "" + read -rp " > " MAIN_MENU_CHOICE + + case $MAIN_MENU_CHOICE in + 1) + menu_1 + ;; + 2) + menu_2 + ;; + 3) + menu_3 + ;; + 4) + menu_4 + ;; + 0) + restore_terminal + ;; + *) + main_menu + ;; + esac +} + +menu_1() { + logo + echo " In dev" + sleep 3 + main_menu +} + +menu_2() { + logo + echo " In dev" + sleep 3 + main_menu +} + +menu_3() { + logo + echo " In dev" + sleep 3 + main_menu +} + +menu_4() { + logo + echo " In dev" + sleep 3 + main_menu +} + +main_menu + # Restoring terminal after succes restore_terminal \ No newline at end of file From 5db0887ee4478cee18f905785b4fd2b6898b1fc0 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 16:38:10 +0000 Subject: [PATCH 07/40] =?UTF-8?q?fix:=20=D0=A1ase=20of=20letters=20in=20va?= =?UTF-8?q?riables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/ctrlhelper.sh | 58 +++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 0453ec9c0..398b2b57a 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -3,8 +3,8 @@ # System vars | DO NOT TOUCH!!! SCRIPT_VER="0.0.1" DEFAULT_DIR="/var/www/controlpanel/" -CPGG_DIR="" -DIR_NULL="" +cpgg_dir="" +dir_null="" # Logo for CLI-GUI logo() { @@ -37,16 +37,16 @@ tput smcup while [[ $# -gt 0 ]]; do case "$1" in --cpgg-dir=*) - CPGG_DIR="${1#*=}" + cpgg_dir="${1#*=}" shift # Check if directory exists - if [ ! -d "$CPGG_DIR" ]; then - CPGGDIR_SET_ERR="echo $CPGG_DIR directory does not exist. Try again" + if [ ! -d "$cpgg_dir" ]; then + CPGGDIR_SET_ERR="echo $cpgg_dir directory does not exist. Try again" restore_terminal "$CPGGDIR_SET_ERR" fi - if [ ! -f "$CPGG_DIR/config/app.php" ]; then - NOT_CPGG_ROOT_ERR="echo $CPGG_DIR is not a root CtrlPanel directory. Try again" + if [ ! -f "$cpgg_dir/config/app.php" ]; then + NOT_CPGG_ROOT_ERR="echo $cpgg_dir is not a root CtrlPanel directory. Try again" restore_terminal "$NOT_CPGG_ROOT_ERR" fi ;; @@ -57,43 +57,43 @@ while [[ $# -gt 0 ]]; do done # Set root CtrlPanel directory using CLI-GUI -## If $DEFAULT_DIR doesn't exists and $CPGG_DIR var isn't specified -if [ ! -d "$DEFAULT_DIR" ] && [ -z "$CPGG_DIR" ]; then +## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified +if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then while true; do - # If $CPGG_DIR var isn't specified, show "Default not exists" - if [ -z "$CPGG_DIR" ]; then + # If $cpgg_dir var isn't specified, show "Default not exists" + if [ -z "$cpgg_dir" ]; then logo echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" fi - # If $DIR_NULL is true, show "Cannot be empty" - if [ "$DIR_NULL" == "true" ]; then + # If $dir_null is true, show "Cannot be empty" + if [ "$dir_null" == "true" ]; then logo echo " You have not specified a directory, it cannot be empty!" fi # Reading directory specified by the user - read -rp " > " CPGG_DIR + read -rp " > " cpgg_dir - # If $CPGG_DIR var isn't specified set $DIR_NULL to true - if [ -z "$CPGG_DIR" ]; then - DIR_NULL="true" + # If $cpgg_dir var isn't specified set $dir_null to true + if [ -z "$cpgg_dir" ]; then + dir_null="true" continue fi - # If $CPGG_DIR exists set $DIR_NULL to null and continue script - if [ -d "$CPGG_DIR" ]; then - DIR_NULL="" + # If $cpgg_dir exists set $dir_null to null and continue script + if [ -d "$cpgg_dir" ]; then + dir_null="" break - # If $CPGG_DIR doesn't exists, show logo with "Directory does not exist" message + # If $cpgg_dir doesn't exists, show logo with "Directory does not exist" message else logo - echo " $CPGG_DIR directory does not exist. Try again" - DIR_NULL="" + echo " $cpgg_dir directory does not exist. Try again" + dir_null="" fi done fi # Getting curent CtrlPanel version -PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${CPGG_DIR:-$DEFAULT_DIR}/config/app.php") +PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") # Getting latest CtrlPanel version PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') @@ -129,7 +129,7 @@ version_compare() { return 0 # Latest version is installed } -UPDATE_NEEDED=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") +is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") # Logo with versions for CLI-GUI logo_version() { @@ -147,10 +147,10 @@ logo_version() { # Message about available Update logo_version_message() { - if [[ $UPDATE_NEEDED == 1 ]]; then + if [[ $is_update_needed == 1 ]]; then echo " New version available! You can update right now by selecting \"Update\" option." echo "" - elif [[ $UPDATE_NEEDED == -1 ]]; then + elif [[ $is_update_needed == -1 ]]; then echo " You are using a newer version! Most likely you have a development branch installed." echo "" fi @@ -167,9 +167,9 @@ main_menu() { echo " 4. Info & Help" echo " 0. Exit" echo "" - read -rp " > " MAIN_MENU_CHOICE + read -rp " > " main_menu_choice - case $MAIN_MENU_CHOICE in + case $main_menu_choice in 1) menu_1 ;; From 5f67a7fceca988ddb7a9223698cfcb326e16d280 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 16:46:08 +0000 Subject: [PATCH 08/40] refactor: logo_version now outputs only versions without logo --- bin/ctrlhelper.sh | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 398b2b57a..6e30057c7 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -133,20 +133,13 @@ is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") # Logo with versions for CLI-GUI logo_version() { - clear - echo " ________ ______ __ " - echo " / ____/ /______/ / __ \____ _____ ___ / /____ _____ _" - echo " / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/" - echo " / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / " - echo " \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / " - echo " /____//____/ " echo " Script version: $SCRIPT_VER" echo " CtrlPanel version: $PANEL_VER" echo "" } # Message about available Update -logo_version_message() { +logo_message() { if [[ $is_update_needed == 1 ]]; then echo " New version available! You can update right now by selecting \"Update\" option." echo "" @@ -158,8 +151,9 @@ logo_version_message() { # Main menu main_menu() { + logo logo_version - logo_version_message + logo_message echo " Select an option:" echo " 1. Install dependencies" echo " 2. Update" From c3d9c092e63dc75205033b0da3b6cc9d53378ac6 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 16:51:48 +0000 Subject: [PATCH 09/40] fix: make some variables readonly to prevent future errors --- bin/ctrlhelper.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 6e30057c7..4c4f39aee 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -1,8 +1,8 @@ #!/bin/bash # System vars | DO NOT TOUCH!!! -SCRIPT_VER="0.0.1" -DEFAULT_DIR="/var/www/controlpanel/" +readonly SCRIPT_VER="0.0.1" +readonly DEFAULT_DIR="/var/www/controlpanel/" cpgg_dir="" dir_null="" @@ -93,9 +93,9 @@ if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then fi # Getting curent CtrlPanel version -PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") +readonly PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") # Getting latest CtrlPanel version -PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') +readonly PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') # Comparing current and latest versions ## -1 => Version above the latest one is installed @@ -129,7 +129,7 @@ version_compare() { return 0 # Latest version is installed } -is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") +readonly is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") # Logo with versions for CLI-GUI logo_version() { From b8bee35101dcbb68be6b0ce8d812bcd51225e336 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 18:15:07 +0000 Subject: [PATCH 10/40] feat: CLI mode using argument --- bin/ctrlhelper.sh | 220 +++++++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 100 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 4c4f39aee..da6e98e70 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -5,6 +5,7 @@ readonly SCRIPT_VER="0.0.1" readonly DEFAULT_DIR="/var/www/controlpanel/" cpgg_dir="" dir_null="" +cli_mode="false" # Logo for CLI-GUI logo() { @@ -27,15 +28,13 @@ restore_terminal() { exit } -# Restoring terminal content after ^C -trap restore_terminal SIGINT - -# Save terminal -tput smcup - # Setting root CtrlPanel directory using the --cpgg-dir=/path/to/folder parameter while [[ $# -gt 0 ]]; do case "$1" in + --cli) + cli_mode="true" + shift + ;; --cpgg-dir=*) cpgg_dir="${1#*=}" shift @@ -56,40 +55,58 @@ while [[ $# -gt 0 ]]; do esac done -# Set root CtrlPanel directory using CLI-GUI -## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified -if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then - while true; do - # If $cpgg_dir var isn't specified, show "Default not exists" - if [ -z "$cpgg_dir" ]; then - logo - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" - fi - # If $dir_null is true, show "Cannot be empty" - if [ "$dir_null" == "true" ]; then - logo - echo " You have not specified a directory, it cannot be empty!" - fi - # Reading directory specified by the user - read -rp " > " cpgg_dir +# Do terminal actions only if $cli_mode = false +if [ "$cli_mode" == "false" ]; then + # Restoring terminal content after ^C + trap restore_terminal SIGINT - # If $cpgg_dir var isn't specified set $dir_null to true - if [ -z "$cpgg_dir" ]; then - dir_null="true" - continue - fi + # Save terminal + tput smcup +fi - # If $cpgg_dir exists set $dir_null to null and continue script - if [ -d "$cpgg_dir" ]; then - dir_null="" - break - # If $cpgg_dir doesn't exists, show logo with "Directory does not exist" message - else - logo - echo " $cpgg_dir directory does not exist. Try again" - dir_null="" - fi - done +if [ "$cli_mode" == "false" ]; then + # Set root CtrlPanel directory using CLI-GUI + ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified + if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then + while true; do + # If $cpgg_dir var isn't specified, show "Default not exists" + if [ -z "$cpgg_dir" ]; then + logo + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" + fi + # If $dir_null is true, show "Cannot be empty" + if [ "$dir_null" == "true" ]; then + logo + echo " You have not specified a directory, it cannot be empty!" + fi + # Reading directory specified by the user + read -rp " > " cpgg_dir + + # If $cpgg_dir var isn't specified set $dir_null to true + if [ -z "$cpgg_dir" ]; then + dir_null="true" + continue + fi + + # If $cpgg_dir exists set $dir_null to null and continue script + if [ -d "$cpgg_dir" ]; then + dir_null="" + break + # If $cpgg_dir doesn't exists, show logo with "Directory does not exist" message + else + logo + echo " $cpgg_dir directory does not exist. Try again" + dir_null="" + fi + done + fi +else + # Set root CtrlPanel directory using in CLI mode + ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified + if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir=/path/to/cpgg argument" + exit 1 + fi fi # Getting curent CtrlPanel version @@ -149,71 +166,74 @@ logo_message() { fi } -# Main menu -main_menu() { - logo - logo_version - logo_message - echo " Select an option:" - echo " 1. Install dependencies" - echo " 2. Update" - echo " 3. Uninstall" - echo " 4. Info & Help" - echo " 0. Exit" - echo "" - read -rp " > " main_menu_choice - - case $main_menu_choice in - 1) - menu_1 - ;; - 2) - menu_2 - ;; - 3) - menu_3 - ;; - 4) - menu_4 - ;; - 0) - restore_terminal - ;; - *) +if [ "$cli_mode" == "false" ]; then + # Main menu + main_menu() { + logo + logo_version + logo_message + echo " Select an option:" + echo " 1. Install dependencies" + echo " 2. Update" + echo " 3. Uninstall" + echo " 4. Info & Help" + echo " 0. Exit" + echo "" + read -rp " > " main_menu_choice + + case $main_menu_choice in + 1) + menu_1 + ;; + 2) + menu_2 + ;; + 3) + menu_3 + ;; + 4) + menu_4 + ;; + 0) + restore_terminal + ;; + *) + main_menu + ;; + esac + } + + menu_1() { + logo + echo " In dev" + sleep 3 main_menu - ;; - esac -} + } -menu_1() { - logo - echo " In dev" - sleep 3 - main_menu -} + menu_2() { + logo + echo " In dev" + sleep 3 + main_menu + } -menu_2() { - logo - echo " In dev" - sleep 3 - main_menu -} + menu_3() { + logo + echo " In dev" + sleep 3 + main_menu + } -menu_3() { - logo - echo " In dev" - sleep 3 - main_menu -} + menu_4() { + logo + echo " In dev" + sleep 3 + main_menu + } -menu_4() { - logo - echo " In dev" - sleep 3 main_menu -} - -main_menu - -# Restoring terminal after succes -restore_terminal \ No newline at end of file +fi +# Restoring terminal after succes if $cli_mode = false +if [ "$cli_mode" == "false" ]; then + restore_terminal +fi \ No newline at end of file From 3fd7a44c58f6ec516405dde9f4a468cbac446b80 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 18:17:56 +0000 Subject: [PATCH 11/40] fix: Check if argument "--cpgg-dir" not empty --- bin/ctrlhelper.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index da6e98e70..4c0f13ae3 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -39,6 +39,11 @@ while [[ $# -gt 0 ]]; do cpgg_dir="${1#*=}" shift + # Check if --cpgg-dir not empty + if [ -z "$cpgg_dir" ]; then + echo " Argument --cpgg-dir can't be empty!" + exit 1 + fi # Check if directory exists if [ ! -d "$cpgg_dir" ]; then CPGGDIR_SET_ERR="echo $cpgg_dir directory does not exist. Try again" From b0908c1659bfce972ba57a232e5c6df61c5111bd Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 18:22:00 +0000 Subject: [PATCH 12/40] fix: better error handling when using "--dir-cpgg" argument --- bin/ctrlhelper.sh | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 4c0f13ae3..f08c662cb 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -22,9 +22,6 @@ logo() { # Function for restoring terminal content after the script is completed or closed restore_terminal() { tput rmcup - if [[ -n $1 ]]; then - $1 - fi exit } @@ -46,12 +43,12 @@ while [[ $# -gt 0 ]]; do fi # Check if directory exists if [ ! -d "$cpgg_dir" ]; then - CPGGDIR_SET_ERR="echo $cpgg_dir directory does not exist. Try again" - restore_terminal "$CPGGDIR_SET_ERR" + echo " $cpgg_dir directory does not exist. Try again" + exit 1 fi if [ ! -f "$cpgg_dir/config/app.php" ]; then - NOT_CPGG_ROOT_ERR="echo $cpgg_dir is not a root CtrlPanel directory. Try again" - restore_terminal "$NOT_CPGG_ROOT_ERR" + echo " $cpgg_dir is not a root CtrlPanel directory. Try again" + exit 1 fi ;; *) From eeaa5e7ad5ba7866d17418af4b85c2008079cd19 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 18:22:49 +0000 Subject: [PATCH 13/40] fix: readonly variables --- bin/ctrlhelper.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index f08c662cb..f511c928c 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -112,9 +112,11 @@ else fi # Getting curent CtrlPanel version -readonly PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") +PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") +readonly PANEL_VER # Getting latest CtrlPanel version -readonly PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') +PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') +readonly PANEL_LATEST_VER # Comparing current and latest versions ## -1 => Version above the latest one is installed @@ -148,7 +150,8 @@ version_compare() { return 0 # Latest version is installed } -readonly is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") +is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") +readonly is_update_needed # Logo with versions for CLI-GUI logo_version() { From 4d9194a19a97205701eacefdd93c8a43d7e0eb38 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 18:26:06 +0000 Subject: [PATCH 14/40] fix: displaying message about default directory when suvmit null value --- bin/ctrlhelper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index f511c928c..5b7d8f6c6 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -72,7 +72,7 @@ if [ "$cli_mode" == "false" ]; then if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then while true; do # If $cpgg_dir var isn't specified, show "Default not exists" - if [ -z "$cpgg_dir" ]; then + if [ -z "$cpgg_dir" ] && [ ! "$dir_null" == "true" ]; then logo echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" fi From 31c841d970b7d9c8dc5374347be1f5fb38eee3cb Mon Sep 17 00:00:00 2001 From: MrWeez Date: Tue, 4 Jun 2024 23:50:19 +0000 Subject: [PATCH 15/40] refactor: Improved checks when specifying a directory --- bin/ctrlhelper.sh | 97 ++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 5b7d8f6c6..2825c5310 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -4,7 +4,6 @@ readonly SCRIPT_VER="0.0.1" readonly DEFAULT_DIR="/var/www/controlpanel/" cpgg_dir="" -dir_null="" cli_mode="false" # Logo for CLI-GUI @@ -66,50 +65,60 @@ if [ "$cli_mode" == "false" ]; then tput smcup fi -if [ "$cli_mode" == "false" ]; then - # Set root CtrlPanel directory using CLI-GUI - ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified - if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then - while true; do - # If $cpgg_dir var isn't specified, show "Default not exists" - if [ -z "$cpgg_dir" ] && [ ! "$dir_null" == "true" ]; then - logo - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" - fi - # If $dir_null is true, show "Cannot be empty" - if [ "$dir_null" == "true" ]; then - logo - echo " You have not specified a directory, it cannot be empty!" - fi - # Reading directory specified by the user - read -rp " > " cpgg_dir - - # If $cpgg_dir var isn't specified set $dir_null to true - if [ -z "$cpgg_dir" ]; then - dir_null="true" - continue - fi - - # If $cpgg_dir exists set $dir_null to null and continue script - if [ -d "$cpgg_dir" ]; then - dir_null="" - break - # If $cpgg_dir doesn't exists, show logo with "Directory does not exist" message - else +set_cpgg_dir() { + + local is_exists="" + local is_cpgg_root="" + local is_null="" + + if [ "$cli_mode" == "false" ]; then + # Set root CtrlPanel directory using CLI-GUI + ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified + if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then + while true; do logo - echo " $cpgg_dir directory does not exist. Try again" - dir_null="" - fi - done - fi -else - # Set root CtrlPanel directory using in CLI mode - ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified - if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir=/path/to/cpgg argument" - exit 1 + if [ -z "$is_exists" ] && [ -z "$is_cpgg_root" ] || [ "$is_null" == "true" ]; then + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" + elif [[ $is_exists == false ]]; then + echo " $cpgg_dir directory does not exist. Try again" + elif [[ $is_cpgg_root == false ]]; then + echo " $cpgg_dir is not a root CtrlPanel directory. Try again" + fi + read -rp " > " cpgg_dir + + is_null="" + is_exists="" + is_cpgg_root="" + + if [ "$cpgg_dir" != "" ]; then + if [ ! -d "$cpgg_dir" ]; then + is_null="" + is_exists="false" + else + if [ ! -f "$cpgg_dir/config/app.php" ]; then + is_null="" + is_cpgg_root="false" + else + break + fi + fi + else + is_null="true" + fi + + done + fi + else + # Set root CtrlPanel directory using in CLI mode + ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified + if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir=/path/to/cpgg argument" + exit 1 + fi fi -fi +} + +set_cpgg_dir # Getting curent CtrlPanel version PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") @@ -241,4 +250,4 @@ fi # Restoring terminal after succes if $cli_mode = false if [ "$cli_mode" == "false" ]; then restore_terminal -fi \ No newline at end of file +fi From 08c0934ab3e14819217a46addf39dafb469c1a5c Mon Sep 17 00:00:00 2001 From: MrWeez Date: Wed, 5 Jun 2024 00:04:18 +0000 Subject: [PATCH 16/40] refactor: comments and grammar improvements --- bin/ctrlhelper.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 2825c5310..f8a845a4d 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -42,7 +42,7 @@ while [[ $# -gt 0 ]]; do fi # Check if directory exists if [ ! -d "$cpgg_dir" ]; then - echo " $cpgg_dir directory does not exist. Try again" + echo " $cpgg_dir directory doesn't exist. Try again" exit 1 fi if [ ! -f "$cpgg_dir/config/app.php" ]; then @@ -77,15 +77,21 @@ set_cpgg_dir() { if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then while true; do logo + # If $is_exists and $is_cpgg_root = null or $is_null = true show "Default wasn't found" if [ -z "$is_exists" ] && [ -z "$is_cpgg_root" ] || [ "$is_null" == "true" ]; then echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" + # If $is_exists = false show "Directory not exist" elif [[ $is_exists == false ]]; then - echo " $cpgg_dir directory does not exist. Try again" + echo " $cpgg_dir directory doesn't exist. Try again" + # If $is_cpgg_root = false show "Not a root CtrlPanel" elif [[ $is_cpgg_root == false ]]; then echo " $cpgg_dir is not a root CtrlPanel directory. Try again" fi + + # Read specified directory read -rp " > " cpgg_dir + # Set all validation vars to null is_null="" is_exists="" is_cpgg_root="" From f5f5cc1a88eccc766784afe926cb75b6b45e2589 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Wed, 5 Jun 2024 00:05:48 +0000 Subject: [PATCH 17/40] fix: do not return negative values --- bin/ctrlhelper.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index f8a845a4d..b2c8e8b07 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -134,9 +134,9 @@ PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags readonly PANEL_LATEST_VER # Comparing current and latest versions -## -1 => Version above the latest one is installed -## 0 => Latest version is installed -## 1 => Update available +## 0 => Latest version is installed +## 1 => Update available +## 2 => Version above the latest one is installed version_compare() { local current_version="$1" local latest_version="$2" @@ -156,8 +156,8 @@ version_compare() { echo "1" return 1 # Update needed elif ((${current_parts[i]} > ${latest_parts[i]})); then - echo "-1" - return -1 # A newer version is installed + echo "2" + return 2 # A newer version is installed fi done @@ -180,7 +180,7 @@ logo_message() { if [[ $is_update_needed == 1 ]]; then echo " New version available! You can update right now by selecting \"Update\" option." echo "" - elif [[ $is_update_needed == -1 ]]; then + elif [[ $is_update_needed == 2 ]]; then echo " You are using a newer version! Most likely you have a development branch installed." echo "" fi From 64eb268b932c1260edb87ed13a2a9e94e4311d6b Mon Sep 17 00:00:00 2001 From: MrWeez Date: Wed, 5 Jun 2024 20:33:22 +0000 Subject: [PATCH 18/40] refactor: Fix Shellcheck warnings (SC2004) --- bin/ctrlhelper.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index b2c8e8b07..6073d6924 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -152,10 +152,10 @@ version_compare() { # Compare components one by one for ((i = 0; i < ${#current_parts[@]}; i++)); do - if ((${current_parts[i]} < ${latest_parts[i]})); then + if ((current_parts[i] < latest_parts[i])); then echo "1" return 1 # Update needed - elif ((${current_parts[i]} > ${latest_parts[i]})); then + elif ((current_parts[i] > latest_parts[i])); then echo "2" return 2 # A newer version is installed fi From 08f5380ad2c413b8c476f4eb885503b1632ec347 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Wed, 5 Jun 2024 23:52:00 +0000 Subject: [PATCH 19/40] fix: Change return codes to correct ones --- bin/ctrlhelper.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 6073d6924..8d08b63b4 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -153,16 +153,16 @@ version_compare() { # Compare components one by one for ((i = 0; i < ${#current_parts[@]}; i++)); do if ((current_parts[i] < latest_parts[i])); then - echo "1" - return 1 # Update needed + echo "1" # Update needed + return 0 elif ((current_parts[i] > latest_parts[i])); then - echo "2" - return 2 # A newer version is installed + echo "2" # A newer version is installed + return 0 fi done - echo "0" - return 0 # Latest version is installed + echo "0" # Latest version is installed + return 0 } is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") From a1d2377c35983d2e6edb448a9ae060b109a82318 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Wed, 5 Jun 2024 23:53:57 +0000 Subject: [PATCH 20/40] refactor: Added and corrected comments and code optimization --- bin/ctrlhelper.sh | 180 +++++++++++++++++++++++++++------------------- 1 file changed, 106 insertions(+), 74 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 8d08b63b4..54d8aae31 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -6,7 +6,7 @@ readonly DEFAULT_DIR="/var/www/controlpanel/" cpgg_dir="" cli_mode="false" -# Logo for CLI-GUI +# CtrlPanel logo logo() { clear echo " ________ ______ __ " @@ -18,84 +18,50 @@ logo() { echo "" } -# Function for restoring terminal content after the script is completed or closed -restore_terminal() { - tput rmcup - exit -} - -# Setting root CtrlPanel directory using the --cpgg-dir=/path/to/folder parameter -while [[ $# -gt 0 ]]; do - case "$1" in - --cli) - cli_mode="true" - shift - ;; - --cpgg-dir=*) - cpgg_dir="${1#*=}" - shift - - # Check if --cpgg-dir not empty - if [ -z "$cpgg_dir" ]; then - echo " Argument --cpgg-dir can't be empty!" - exit 1 - fi - # Check if directory exists - if [ ! -d "$cpgg_dir" ]; then - echo " $cpgg_dir directory doesn't exist. Try again" - exit 1 - fi - if [ ! -f "$cpgg_dir/config/app.php" ]; then - echo " $cpgg_dir is not a root CtrlPanel directory. Try again" - exit 1 - fi - ;; - *) - shift - ;; - esac -done - -# Do terminal actions only if $cli_mode = false -if [ "$cli_mode" == "false" ]; then +# Saving terminal content +save_terminal() { # Restoring terminal content after ^C trap restore_terminal SIGINT # Save terminal tput smcup -fi +} -set_cpgg_dir() { +# Restoring terminal content after the script is completed or closed +restore_terminal() { + tput rmcup + exit +} +# Specifying CtrlPanel directory if it differs from the default one +set_cpgg_dir() { local is_exists="" local is_cpgg_root="" local is_null="" if [ "$cli_mode" == "false" ]; then - # Set root CtrlPanel directory using CLI-GUI - ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then while true; do logo - # If $is_exists and $is_cpgg_root = null or $is_null = true show "Default wasn't found" + + # Different message depending on the validation response if [ -z "$is_exists" ] && [ -z "$is_cpgg_root" ] || [ "$is_null" == "true" ]; then echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" - # If $is_exists = false show "Directory not exist" elif [[ $is_exists == false ]]; then - echo " $cpgg_dir directory doesn't exist. Try again" - # If $is_cpgg_root = false show "Not a root CtrlPanel" + echo " Directory $cpgg_dir doesn't exist. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" elif [[ $is_cpgg_root == false ]]; then - echo " $cpgg_dir is not a root CtrlPanel directory. Try again" + echo " $cpgg_dir is not a root CtrlPanel directory. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" fi - # Read specified directory - read -rp " > " cpgg_dir + # Reading specified directory + read -rep " > " cpgg_dir # Set all validation vars to null is_null="" is_exists="" is_cpgg_root="" + # Validation of the entered directory if [ "$cpgg_dir" != "" ]; then if [ ! -d "$cpgg_dir" ]; then is_null="" @@ -115,23 +81,21 @@ set_cpgg_dir() { done fi else - # Set root CtrlPanel directory using in CLI mode - ## If $DEFAULT_DIR doesn't exists and $cpgg_dir var isn't specified - if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir=/path/to/cpgg argument" - exit 1 - fi + # If user uses --cli flag, default directory doesn't exists and user did not specify the directory using --cpgg-dir then return an error and stop script + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" + exit 1 fi } -set_cpgg_dir - -# Getting curent CtrlPanel version -PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") -readonly PANEL_VER -# Getting latest CtrlPanel version -PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') -readonly PANEL_LATEST_VER +# Getting current and latest version of CtrlPanel +get_version() { + # Curent CtrlPanel version + PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") + readonly PANEL_VER + # Latest CtrlPanel version + PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') + readonly PANEL_LATEST_VER +} # Comparing current and latest versions ## 0 => Latest version is installed @@ -165,19 +129,28 @@ version_compare() { return 0 } -is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") -readonly is_update_needed +# Checking if an update is needed +## 0 => Latest version is installed +## 1 => Update available +## 2 => Version above the latest one is installed +update_needed_checker() { + is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") + readonly is_update_needed +} -# Logo with versions for CLI-GUI +# Version block for logo logo_version() { echo " Script version: $SCRIPT_VER" echo " CtrlPanel version: $PANEL_VER" echo "" } -# Message about available Update +# Message about update status for logo logo_message() { - if [[ $is_update_needed == 1 ]]; then + if [[ $is_update_needed == 0 ]]; then + echo " You are using the latest version! No update required." + echo "" + elif [[ $is_update_needed == 1 ]]; then echo " New version available! You can update right now by selecting \"Update\" option." echo "" elif [[ $is_update_needed == 2 ]]; then @@ -186,7 +159,59 @@ logo_message() { fi } +# Handling arguments +while [[ $# -gt 0 ]]; do + case "$1" in + --cli) + cli_mode="true" + shift + ;; + --cpgg-dir=*) + cpgg_dir="${1#*=}" + shift + + # Validation of specified directory + if [ "$cpgg_dir" != "" ]; then + if [ ! -d "$cpgg_dir" ]; then + echo " ERROR: Directory $cpgg_dir doesn't exist." + exit 1 + else + if [ ! -f "$cpgg_dir/config/app.php" ]; then + echo " ERROR: $cpgg_dir is not a root CtrlPanel directory." + exit 1 + else + continue + fi + fi + else + echo " ERROR: Argument --cpgg-dir can't be empty!" + exit 1 + fi + ;; + *) + echo "ERROR: Argument $1 not exists. Use --help to display help menu" + exit 1 + ;; + esac +done + +# Save terminal only if $cli_mode = false +if [ "$cli_mode" == "false" ]; then + save_terminal +fi + +# Calling function to specify a directory +set_cpgg_dir + +# Moving to the CtrlPanel directory +cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, inform the developers!"; exit 1; } + +# Main functions if [ "$cli_mode" == "false" ]; then + + get_version + update_needed_checker + # Main menu main_menu() { logo @@ -223,6 +248,7 @@ if [ "$cli_mode" == "false" ]; then esac } + # Install dependencies menu menu_1() { logo echo " In dev" @@ -230,6 +256,7 @@ if [ "$cli_mode" == "false" ]; then main_menu } + # Update menu menu_2() { logo echo " In dev" @@ -237,6 +264,7 @@ if [ "$cli_mode" == "false" ]; then main_menu } + # Uninstall menu menu_3() { logo echo " In dev" @@ -244,6 +272,7 @@ if [ "$cli_mode" == "false" ]; then main_menu } + # Info & Help menu menu_4() { logo echo " In dev" @@ -251,9 +280,12 @@ if [ "$cli_mode" == "false" ]; then main_menu } + # Launch main munu main_menu -fi -# Restoring terminal after succes if $cli_mode = false -if [ "$cli_mode" == "false" ]; then + + # Restoring terminal after script end restore_terminal +else + # Temporary function. In the future, all necessary actions in CLI mode will be performed here + echo " CLI mode commands" fi From 33eb066a9b6641d68974214cc7c535f44b7efd7b Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 01:11:09 +0000 Subject: [PATCH 21/40] fix: --cpgg-dir argument returned an error even if it was specified --- bin/ctrlhelper.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 54d8aae31..d937a7c0e 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -81,9 +81,11 @@ set_cpgg_dir() { done fi else - # If user uses --cli flag, default directory doesn't exists and user did not specify the directory using --cpgg-dir then return an error and stop script - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" - exit 1 + if [ -z "$cpgg_dir" ]; then + # If user uses --cli flag, default directory doesn't exists and user did not specify the directory using --cpgg-dir then return an error and stop script + echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" + exit 1 + fi fi } From 68b91d95d0b980f1d2e90c2b38a4e17767cf281e Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 01:14:56 +0000 Subject: [PATCH 22/40] refactor: better error message --- .gitignore | 1 + bin/ctrlhelper.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f72249a83..6954e2a3a 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,5 @@ Homestead.yaml # Ignore installation logs and locks public/install/logs.txt install.lock +/bin/.CHdeps.lock public/install/logs/installer.log diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index d937a7c0e..5273ab848 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -206,7 +206,7 @@ fi set_cpgg_dir # Moving to the CtrlPanel directory -cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, inform the developers!"; exit 1; } +cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } # Main functions if [ "$cli_mode" == "false" ]; then From 796f8f019d10202ec79f966d2f157daa3a67d963 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 01:17:00 +0000 Subject: [PATCH 23/40] feat: Install script dependencies on first script run --- bin/ctrlhelper.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 5273ab848..bd752996d 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -3,6 +3,7 @@ # System vars | DO NOT TOUCH!!! readonly SCRIPT_VER="0.0.1" readonly DEFAULT_DIR="/var/www/controlpanel/" +readonly DEPENDENCIES_INSTALLED_FILE="/bin/.CHdeps.lock" cpgg_dir="" cli_mode="false" @@ -208,6 +209,12 @@ set_cpgg_dir # Moving to the CtrlPanel directory cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } +# Install script dependencies on first run +if [ ! -f "${cpgg_dir:-$DEFAULT_DIR}$DEPENDENCIES_INSTALLED_FILE" ]; then + sudo apt-get install -y curl jq grep > /dev/null 2>&1 || { echo " ERROR: An error occurred while trying to install script dependencies. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } + touch "${cpgg_dir:-$DEFAULT_DIR}$DEPENDENCIES_INSTALLED_FILE" +fi + # Main functions if [ "$cli_mode" == "false" ]; then From 6dc5d961387b3039d0da3c0e4c48d88ed3572a28 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 18:15:01 +0000 Subject: [PATCH 24/40] revert "feat: Install script dependencies on first script run" This reverts commit 796f8f019d10202ec79f966d2f157daa3a67d963. --- bin/ctrlhelper.sh | 7 ------- 1 file changed, 7 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index bd752996d..5273ab848 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -3,7 +3,6 @@ # System vars | DO NOT TOUCH!!! readonly SCRIPT_VER="0.0.1" readonly DEFAULT_DIR="/var/www/controlpanel/" -readonly DEPENDENCIES_INSTALLED_FILE="/bin/.CHdeps.lock" cpgg_dir="" cli_mode="false" @@ -209,12 +208,6 @@ set_cpgg_dir # Moving to the CtrlPanel directory cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } -# Install script dependencies on first run -if [ ! -f "${cpgg_dir:-$DEFAULT_DIR}$DEPENDENCIES_INSTALLED_FILE" ]; then - sudo apt-get install -y curl jq grep > /dev/null 2>&1 || { echo " ERROR: An error occurred while trying to install script dependencies. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } - touch "${cpgg_dir:-$DEFAULT_DIR}$DEPENDENCIES_INSTALLED_FILE" -fi - # Main functions if [ "$cli_mode" == "false" ]; then From 123470c06f09a5ee414f027c0bb71593fcf077f3 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 18:17:33 +0000 Subject: [PATCH 25/40] revert "refactor: better error message" This reverts commit 68b91d95d0b980f1d2e90c2b38a4e17767cf281e. --- .gitignore | 1 - bin/ctrlhelper.sh | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6954e2a3a..f72249a83 100644 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,4 @@ Homestead.yaml # Ignore installation logs and locks public/install/logs.txt install.lock -/bin/.CHdeps.lock public/install/logs/installer.log diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 5273ab848..d937a7c0e 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -206,7 +206,7 @@ fi set_cpgg_dir # Moving to the CtrlPanel directory -cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } +cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, inform the developers!"; exit 1; } # Main functions if [ "$cli_mode" == "false" ]; then From a4ac66713c20bc7505486e3016d6df09ff0aa190 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 18:19:37 +0000 Subject: [PATCH 26/40] refactor: better error message if script can't cd to directory --- bin/ctrlhelper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index d937a7c0e..5273ab848 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -206,7 +206,7 @@ fi set_cpgg_dir # Moving to the CtrlPanel directory -cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, inform the developers!"; exit 1; } +cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } # Main functions if [ "$cli_mode" == "false" ]; then From c207b057cddadeb02a6a10beb8a07286ce0bd63c Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 18:41:02 +0000 Subject: [PATCH 27/40] chore: replace jq with sed to avoid external dependency --- bin/ctrlhelper.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 5273ab848..bcf749b41 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -95,7 +95,11 @@ get_version() { PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") readonly PANEL_VER # Latest CtrlPanel version - PANEL_LATEST_VER=$(curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | jq -r '.[0].name') + PANEL_LATEST_VER=$( + curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | + sed -n 's/.*"name": "\([^"]*\)".*/\1/p' | + head -n 1 + ) readonly PANEL_LATEST_VER } From eac5ee6ac522295c01124d84c75f9918e05d8087 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Thu, 6 Jun 2024 21:07:48 +0000 Subject: [PATCH 28/40] refactor: optimized directory validation --- bin/ctrlhelper.sh | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index bcf749b41..8f5e19c88 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -62,20 +62,14 @@ set_cpgg_dir() { is_cpgg_root="" # Validation of the entered directory - if [ "$cpgg_dir" != "" ]; then - if [ ! -d "$cpgg_dir" ]; then - is_null="" - is_exists="false" - else - if [ ! -f "$cpgg_dir/config/app.php" ]; then - is_null="" - is_cpgg_root="false" - else - break - fi - fi - else + if [ "$cpgg_dir" == "" ]; then is_null="true" + elif [ ! -d "$cpgg_dir" ]; then + is_exists="false" + elif [ ! -f "$cpgg_dir/config/app.php" ]; then + is_cpgg_root="false" + else + break fi done @@ -177,25 +171,21 @@ while [[ $# -gt 0 ]]; do shift # Validation of specified directory - if [ "$cpgg_dir" != "" ]; then - if [ ! -d "$cpgg_dir" ]; then - echo " ERROR: Directory $cpgg_dir doesn't exist." - exit 1 - else - if [ ! -f "$cpgg_dir/config/app.php" ]; then - echo " ERROR: $cpgg_dir is not a root CtrlPanel directory." - exit 1 - else - continue - fi - fi - else + if [ "$cpgg_dir" == "" ]; then echo " ERROR: Argument --cpgg-dir can't be empty!" exit 1 + elif [ ! -d "$cpgg_dir" ]; then + echo " ERROR: Directory $cpgg_dir doesn't exist." + exit 1 + elif [ ! -f "$cpgg_dir/config/app.php" ]; then + echo " ERROR: $cpgg_dir is not a root CtrlPanel directory." + exit 1 + else + continue fi ;; *) - echo "ERROR: Argument $1 not exists. Use --help to display help menu" + echo " ERROR: Argument $1 not exists. Use --help to display help menu" exit 1 ;; esac From 99c1ec4a89e28fdb29533bc2ee38b52f02c6f461 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Mon, 10 Jun 2024 21:06:14 +0000 Subject: [PATCH 29/40] fix: :bug: checking the existence of a default directory This commit returns a check for the existence of a standard directory removed in one of the previous commits to display an error about the absence of the --cpgg-dir parameter --- bin/ctrlhelper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 8f5e19c88..5325b70b8 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -75,7 +75,7 @@ set_cpgg_dir() { done fi else - if [ -z "$cpgg_dir" ]; then + if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then # If user uses --cli flag, default directory doesn't exists and user did not specify the directory using --cpgg-dir then return an error and stop script echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" exit 1 From bcc7880774da4592bb7adaee2b99ca2e3ebc78ef Mon Sep 17 00:00:00 2001 From: MrWeez Date: Mon, 24 Jun 2024 00:38:42 +0000 Subject: [PATCH 30/40] feat: :sparkles: Functions, Argument mode, and small code refactor In this commit, argument mode, dependency installation and update functions have been added, as well as a small refactoring of code has been carried out to make it better --- bin/ctrlhelper.sh | 266 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 234 insertions(+), 32 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 5325b70b8..6ffe7faeb 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -2,9 +2,9 @@ # System vars | DO NOT TOUCH!!! readonly SCRIPT_VER="0.0.1" -readonly DEFAULT_DIR="/var/www/controlpanel/" +readonly DEFAULT_DIR="/var/www/controlpanel" cpgg_dir="" -cli_mode="false" +force="false" # CtrlPanel logo logo() { @@ -39,7 +39,7 @@ set_cpgg_dir() { local is_cpgg_root="" local is_null="" - if [ "$cli_mode" == "false" ]; then + if [ -z "$cli_mode" ]; then if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then while true; do logo @@ -56,6 +56,9 @@ set_cpgg_dir() { # Reading specified directory read -rep " > " cpgg_dir + # Remove / at the end + cpgg_dir="${cpgg_dir%/}" + # Set all validation vars to null is_null="" is_exists="" @@ -77,7 +80,7 @@ set_cpgg_dir() { else if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then # If user uses --cli flag, default directory doesn't exists and user did not specify the directory using --cpgg-dir then return an error and stop script - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" + echo " ERROR: Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" exit 1 fi fi @@ -126,7 +129,7 @@ version_compare() { done echo "0" # Latest version is installed - return 0 + return 0 } # Checking if an update is needed @@ -159,6 +162,125 @@ logo_message() { fi } +# Confirm dialog menu in CLI mode +confirm_dialog() { + local message_line1="$1" + local message_line2="$2" + local function="$3" + + echo " $message_line1" + if [[ -n "$message_line2" ]]; then + echo " $message_line2" + fi + echo " Do you want to continue? (Y/n)" + read -rp " > " choice + + case "$choice" in + y | Y) + $function + ;; + n | N) + exit 0 + ;; + *) + echo " ERROR: Unknown choice $choice" + echo "" + confirm_dialog "$message_line1" "$message_line2" "$function" + ;; + esac +} + +# === ACTIONS SECTION START === + +# Install dependencies function +install_deps() { + local minimal="$1" + + if [[ -z "$cli_mode" ]]; then + logo + fi + + echo " Adding \"add-apt-repository\" command and additional dependencies" + sudo apt -y install software-properties-common curl apt-transport-https ca-certificates gnupg + + echo " Adding PHP repository" + LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/php + + if [[ ! -f "/usr/share/keyrings/redis-archive-keyring.gpg" ]]; then + echo " Adding Redis repository" + curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg + echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list + fi + + if [[ -z "$minimal" ]]; then + echo " Adding MariaDB repository" + curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash + elif [[ -n "$minimal" && "$minimal" != "true" ]]; then + echo " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" + exit 1 + fi + + echo " Running \"apt update\"" + sudo apt update + + echo " Installing dependencies" + if [[ "$minimal" ]]; then + sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} redis-server tar unzip git + elif [[ -z "$minimal" ]]; then + sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git + else + echo " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" + exit 1 + fi + + echo " Installing Composer" + curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer + + echo " Installing Composer dependencies to the CtrlPanel" + sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction +} + +# Update CtrlPanel function +update() { + if [ -z "$cli_mode" ]; then + logo + fi + + echo " Enabling maintenance mode" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan down + + if ! sudo git config --global --get-all safe.directory | grep -q -w "${cpgg_dir:-$DEFAULT_DIR}"; then + echo " Adding CtrlPanel directory to the git save.directory list" + sudo git config --global --add safe.directory "${cpgg_dir:-$DEFAULT_DIR}" + fi + + echo " Downloading file updates" + sudo git stash + sudo git pull + + echo " Installing Composer dependencies" + sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction + + echo " Migrating database updates" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan migrate --seed --force + + echo " Clearing the cache" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan view:clear + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan config:clear + + echo " Setting permissions" + sudo chown -R www-data:www-data "${cpgg_dir:-$DEFAULT_DIR}" + sudo chmod -R 755 "${cpgg_dir:-$DEFAULT_DIR}" + + echo " Restarting Queue Workers" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan queue:restart + + echo " Disabling maintenance mode" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan up +} + +# === ACTIONS SECTION END === + # Handling arguments while [[ $# -gt 0 ]]; do case "$1" in @@ -168,6 +290,7 @@ while [[ $# -gt 0 ]]; do ;; --cpgg-dir=*) cpgg_dir="${1#*=}" + cpgg_dir="${cpgg_dir%/}" shift # Validation of specified directory @@ -184,15 +307,45 @@ while [[ $# -gt 0 ]]; do continue fi ;; + --force) + force="true" + shift + ;; + --install=*) + if [[ -n "$update" ]]; then + echo " ERROR: You can't use --install with --update argument" + exit 1 + fi + + install="${1#*=}" + shift + + if [[ "$install" == "" ]]; then + echo " ERROR: Argument --install can't be empty!" + exit 1 + elif [[ "$install" != "full" && "$install" != "min" ]]; then + echo " ERROR: Invalid option $install for --install argument. Valid values are only full or min" + exit 1 + fi + ;; + --update) + if [[ -n "$install" ]]; then + echo " ERROR: You can't use --update with --install argument" + exit 1 + fi + + update="true" + shift + ;; *) - echo " ERROR: Argument $1 not exists. Use --help to display help menu" + echo " ERROR: Argument $1 not exists. Use --help to display all available arguments" exit 1 ;; esac done # Save terminal only if $cli_mode = false -if [ "$cli_mode" == "false" ]; then +if [ -z "$cli_mode" ]; then save_terminal fi @@ -200,29 +353,33 @@ fi set_cpgg_dir # Moving to the CtrlPanel directory -cd "${cpgg_dir:-$DEFAULT_DIR}" || { echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!"; exit 1; } +cd "${cpgg_dir:-$DEFAULT_DIR}" || { + echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!" + exit 1 +} # Main functions -if [ "$cli_mode" == "false" ]; then +if [ -z "$cli_mode" ]; then get_version update_needed_checker # Main menu main_menu() { + local choice="" + logo logo_version logo_message echo " Select an option:" echo " 1. Install dependencies" echo " 2. Update" - echo " 3. Uninstall" - echo " 4. Info & Help" + echo " 3. Info & Help" echo " 0. Exit" echo "" - read -rp " > " main_menu_choice + read -rp " > " choice - case $main_menu_choice in + case $choice in 1) menu_1 ;; @@ -232,9 +389,6 @@ if [ "$cli_mode" == "false" ]; then 3) menu_3 ;; - 4) - menu_4 - ;; 0) restore_terminal ;; @@ -246,30 +400,58 @@ if [ "$cli_mode" == "false" ]; then # Install dependencies menu menu_1() { + local choice="" + logo - echo " In dev" - sleep 3 - main_menu + echo " This action will install all the necessary dependencies such as PHP, Redis, MariaDB and others, as well as install composer files." + echo " You will still have to create MySQL user and configure nginx yourself." + echo "" + echo " Select the installation option:" + echo " 1. Full install" + echo " 2. Minimal install (Not include MariaDB and nginx)" + echo " 0. Exit to main menu" + read -rp " > " choice + + case "$choice" in + 1) + install_deps + ;; + 2) + install_deps "true" + ;; + 0) + main_menu + ;; + *) + menu_1 + ;; + esac } # Update menu menu_2() { - logo - echo " In dev" - sleep 3 - main_menu - } + local choice="" - # Uninstall menu - menu_3() { logo - echo " In dev" - sleep 3 - main_menu + echo " This action cannot be undone, create backup of the database before updating! It will also remove all installed themes and addons." + echo " Do you want to continue? (Y/n)" + read -rp " > " choice + + case "$choice" in + y | Y) + update + ;; + n | N) + main_menu + ;; + *) + menu_2 + ;; + esac } # Info & Help menu - menu_4() { + menu_3() { logo echo " In dev" sleep 3 @@ -282,6 +464,26 @@ if [ "$cli_mode" == "false" ]; then # Restoring terminal after script end restore_terminal else - # Temporary function. In the future, all necessary actions in CLI mode will be performed here - echo " CLI mode commands" + if [[ "$install" == "full" ]]; then + if [[ "$force" == "true" ]]; then + install_deps + else + confirm_dialog "This action will install all the necessary dependencies such as PHP, Redis, MariaDB and others, as well as install composer files." "You will still have to create MySQL user and configure nginx yourself." "install_deps" + fi + elif [[ "$install" == "min" ]]; then + if [[ "$force" == "true" ]]; then + install_deps "true" + else + confirm_dialog "This action will install all the necessary dependencies such as PHP, Redis, Composer and others, as well as install composer files." "" "install_deps \"true\"" + fi + elif [[ "$update" == "true" ]]; then + if [[ "$force" == "true" ]]; then + update + else + confirm_dialog "This action cannot be undone, create backup of the database before updating! It will also remove all installed themes and addons." "" "update" + fi + else + echo " ERROR: You have not specified the action you want to do! Use --help to display all available arguments" + exit 1 + fi fi From 228de672217c0a0c7534392ef91fa2d95a90b7c5 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Mon, 29 Jul 2024 16:00:19 +0000 Subject: [PATCH 31/40] refactor: :recycle: Code refactor to comply with Bash writing standards --- bin/ctrlhelper.sh | 656 ++++++++---------------- bin/ctrlhelper_sub_scripts/functions.sh | 199 +++++++ bin/ctrlhelper_sub_scripts/menus.sh | 169 ++++++ 3 files changed, 571 insertions(+), 453 deletions(-) create mode 100644 bin/ctrlhelper_sub_scripts/functions.sh create mode 100644 bin/ctrlhelper_sub_scripts/menus.sh diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 6ffe7faeb..7039976d1 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -1,489 +1,239 @@ #!/bin/bash - -# System vars | DO NOT TOUCH!!! -readonly SCRIPT_VER="0.0.1" -readonly DEFAULT_DIR="/var/www/controlpanel" -cpgg_dir="" -force="false" - -# CtrlPanel logo -logo() { - clear - echo " ________ ______ __ " - echo " / ____/ /______/ / __ \____ _____ ___ / /____ _____ _" - echo " / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/" - echo " / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / " - echo " \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / " - echo " /____//____/ " - echo "" -} - -# Saving terminal content -save_terminal() { - # Restoring terminal content after ^C - trap restore_terminal SIGINT - - # Save terminal - tput smcup -} - -# Restoring terminal content after the script is completed or closed -restore_terminal() { - tput rmcup - exit +# +# This script is designed to facilitate the tasks of +# installing dependencies and updating CtrlPanel via CLI +# +# Made with love by MrWeez +# Contact me +# GitHub: https://github.com/MrWeez +# Discord: @mrweez_ +# Email: contact@mrweez.dev + +# shellcheck disable=SC2034 +readonly SCRIPT_VER="0.5.12-dev" +readonly DEFAULT_DIR="/var/www/ctrlpanel" + +####################################### +# Return an error message in STDERR +# Arguments: +# Error message +####################################### +error_out() { + echo "$*" >&2 } -# Specifying CtrlPanel directory if it differs from the default one +####################################### +# Set the directory where CtrlPanel is installed +# Globals: +# DEFAULT_DIR +# cpgg_dir +####################################### set_cpgg_dir() { - local is_exists="" - local is_cpgg_root="" - local is_null="" - - if [ -z "$cli_mode" ]; then - if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then - while true; do - logo - - # Different message depending on the validation response - if [ -z "$is_exists" ] && [ -z "$is_cpgg_root" ] || [ "$is_null" == "true" ]; then - echo " Default directory wasn't found. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" - elif [[ $is_exists == false ]]; then - echo " Directory $cpgg_dir doesn't exist. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" - elif [[ $is_cpgg_root == false ]]; then - echo " $cpgg_dir is not a root CtrlPanel directory. Specify directory where your CtrlPanel is installed (e.g. /var/www/controlpanel)" - fi - - # Reading specified directory - read -rep " > " cpgg_dir - - # Remove / at the end - cpgg_dir="${cpgg_dir%/}" - - # Set all validation vars to null - is_null="" - is_exists="" - is_cpgg_root="" - - # Validation of the entered directory - if [ "$cpgg_dir" == "" ]; then - is_null="true" - elif [ ! -d "$cpgg_dir" ]; then - is_exists="false" - elif [ ! -f "$cpgg_dir/config/app.php" ]; then - is_cpgg_root="false" - else - break - fi - - done - fi - else - if [ ! -d "$DEFAULT_DIR" ] && [ -z "$cpgg_dir" ]; then - # If user uses --cli flag, default directory doesn't exists and user did not specify the directory using --cpgg-dir then return an error and stop script - echo " ERROR: Default directory wasn't found. Specify directory where your CtrlPanel is installed using --cpgg-dir argument" - exit 1 + local is_exists="" + local is_cpgg_root="" + local is_null="" + + if [[ -z "$cli_mode" ]]; then + if [[ ! -d "$DEFAULT_DIR" ]] && [[ -z "$cpgg_dir" ]]; then + while true; do + # Message that the user will see by default, if he specifies a + # non-existent directory or not root CtrlPanel directory + if [[ -z "$is_exists" ]] && [[ -z "$is_cpgg_root" ]] || [[ "$is_null" == "true" ]]; then + echo " Default directory wasn't found. Specify directory where your + CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + elif [[ $is_exists == false ]]; then + echo " Directory $cpgg_dir doesn't exist. Specify directory where + your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + elif [[ $is_cpgg_root == false ]]; then + echo " $cpgg_dir is not a root CtrlPanel directory. Specify + directory where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" fi - fi -} - -# Getting current and latest version of CtrlPanel -get_version() { - # Curent CtrlPanel version - PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") - readonly PANEL_VER - # Latest CtrlPanel version - PANEL_LATEST_VER=$( - curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags | - sed -n 's/.*"name": "\([^"]*\)".*/\1/p' | - head -n 1 - ) - readonly PANEL_LATEST_VER -} - -# Comparing current and latest versions -## 0 => Latest version is installed -## 1 => Update available -## 2 => Version above the latest one is installed -version_compare() { - local current_version="$1" - local latest_version="$2" - # Break down versions into components - IFS='.' read -r -a current_parts <<<"$current_version" - IFS='.' read -r -a latest_parts <<<"$latest_version" + read -rep " > " cpgg_dir - # Add zeros to the shorter version (e.g. 0.10 => 0.10.0) - while ((${#current_parts[@]} < ${#latest_parts[@]})); do - current_parts+=("0") - done + # Deleting / at the end of the specified directory + cpgg_dir="${cpgg_dir%/}" - # Compare components one by one - for ((i = 0; i < ${#current_parts[@]}; i++)); do - if ((current_parts[i] < latest_parts[i])); then - echo "1" # Update needed - return 0 - elif ((current_parts[i] > latest_parts[i])); then - echo "2" # A newer version is installed - return 0 + # Resetting values before validation + is_null="" + is_exists="" + is_cpgg_root="" + + # Validation of directory specified by user + if [[ "$cpgg_dir" == "" ]]; then + is_null="true" + elif [[ ! -d "$cpgg_dir" ]]; then + is_exists="false" + elif [[ ! -f "$cpgg_dir/config/app.php" ]]; then + is_cpgg_root="false" + else + break fi - done - - echo "0" # Latest version is installed - return 0 -} -# Checking if an update is needed -## 0 => Latest version is installed -## 1 => Update available -## 2 => Version above the latest one is installed -update_needed_checker() { - is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") - readonly is_update_needed -} - -# Version block for logo -logo_version() { - echo " Script version: $SCRIPT_VER" - echo " CtrlPanel version: $PANEL_VER" - echo "" -} - -# Message about update status for logo -logo_message() { - if [[ $is_update_needed == 0 ]]; then - echo " You are using the latest version! No update required." - echo "" - elif [[ $is_update_needed == 1 ]]; then - echo " New version available! You can update right now by selecting \"Update\" option." - echo "" - elif [[ $is_update_needed == 2 ]]; then - echo " You are using a newer version! Most likely you have a development branch installed." - echo "" + done fi -} - -# Confirm dialog menu in CLI mode -confirm_dialog() { - local message_line1="$1" - local message_line2="$2" - local function="$3" - - echo " $message_line1" - if [[ -n "$message_line2" ]]; then - echo " $message_line2" + else + # Error if default directory is not found and CtrlPanel root directory is + # not specified when using the CLI mode + if [[ ! -d "$DEFAULT_DIR" ]] && [[ -z "$cpgg_dir" ]]; then + error_out " ERROR: Default directory wasn't found. Specify directory + where your CtrlPanel is installed using --cpgg-dir argument" + exit 1 fi - echo " Do you want to continue? (Y/n)" - read -rp " > " choice - - case "$choice" in - y | Y) - $function - ;; - n | N) - exit 0 - ;; - *) - echo " ERROR: Unknown choice $choice" - echo "" - confirm_dialog "$message_line1" "$message_line2" "$function" - ;; - esac + fi } -# === ACTIONS SECTION START === - -# Install dependencies function -install_deps() { - local minimal="$1" - - if [[ -z "$cli_mode" ]]; then - logo - fi - - echo " Adding \"add-apt-repository\" command and additional dependencies" - sudo apt -y install software-properties-common curl apt-transport-https ca-certificates gnupg - - echo " Adding PHP repository" - LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/php - - if [[ ! -f "/usr/share/keyrings/redis-archive-keyring.gpg" ]]; then - echo " Adding Redis repository" - curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg - echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list - fi - - if [[ -z "$minimal" ]]; then - echo " Adding MariaDB repository" - curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash - elif [[ -n "$minimal" && "$minimal" != "true" ]]; then - echo " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" - exit 1 - fi - - echo " Running \"apt update\"" - sudo apt update - - echo " Installing dependencies" - if [[ "$minimal" ]]; then - sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} redis-server tar unzip git - elif [[ -z "$minimal" ]]; then - sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git +# Handling startup arguments +while [[ $# -gt 0 ]]; do + case "$1" in + --cli) + cli_mode="true" + shift + ;; + --cpgg-dir=*) + cpgg_dir="${1#*=}" + cpgg_dir="${cpgg_dir%/}" + shift + + if [[ "$cpgg_dir" == "" ]]; then + error_out " ERROR: Argument --cpgg-dir can't be empty!" + exit 1 + elif [[ ! -d "$cpgg_dir" ]]; then + error_out " ERROR: Directory $cpgg_dir doesn't exist." + exit 1 + elif [[ ! -f "$cpgg_dir/config/app.php" ]]; then + error_out " ERROR: $cpgg_dir is not a root CtrlPanel + directory." + exit 1 else - echo " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" - exit 1 + continue fi - - echo " Installing Composer" - curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer - - echo " Installing Composer dependencies to the CtrlPanel" - sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction -} - -# Update CtrlPanel function -update() { - if [ -z "$cli_mode" ]; then - logo + ;; + --force) + force="true" + shift + ;; + --install=*) + if [[ -n "$update" ]]; then + error_out " ERROR: You can't use --install with --update + argument" + exit 1 fi - echo " Enabling maintenance mode" - sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan down + install="${1#*=}" + shift - if ! sudo git config --global --get-all safe.directory | grep -q -w "${cpgg_dir:-$DEFAULT_DIR}"; then - echo " Adding CtrlPanel directory to the git save.directory list" - sudo git config --global --add safe.directory "${cpgg_dir:-$DEFAULT_DIR}" + if [[ "$install" == "" ]]; then + error_out " ERROR: Argument --install can't be empty!" + exit 1 + elif [[ "$install" != "full" && "$install" != "min" ]]; then + error_out " ERROR: Invalid option $install for --install + argument. Valid values are only full or min" + exit 1 + fi + ;; + --update) + if [[ -n "$install" ]]; then + error_out " ERROR: You can't use --update with --install + argument" + exit 1 fi - echo " Downloading file updates" - sudo git stash - sudo git pull - - echo " Installing Composer dependencies" - sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction - - echo " Migrating database updates" - sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan migrate --seed --force - - echo " Clearing the cache" - sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan view:clear - sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan config:clear - - echo " Setting permissions" - sudo chown -R www-data:www-data "${cpgg_dir:-$DEFAULT_DIR}" - sudo chmod -R 755 "${cpgg_dir:-$DEFAULT_DIR}" - - echo " Restarting Queue Workers" - sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan queue:restart - - echo " Disabling maintenance mode" - sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan up -} - -# === ACTIONS SECTION END === - -# Handling arguments -while [[ $# -gt 0 ]]; do - case "$1" in - --cli) - cli_mode="true" - shift - ;; - --cpgg-dir=*) - cpgg_dir="${1#*=}" - cpgg_dir="${cpgg_dir%/}" - shift - - # Validation of specified directory - if [ "$cpgg_dir" == "" ]; then - echo " ERROR: Argument --cpgg-dir can't be empty!" - exit 1 - elif [ ! -d "$cpgg_dir" ]; then - echo " ERROR: Directory $cpgg_dir doesn't exist." - exit 1 - elif [ ! -f "$cpgg_dir/config/app.php" ]; then - echo " ERROR: $cpgg_dir is not a root CtrlPanel directory." - exit 1 - else - continue - fi - ;; - --force) - force="true" - shift - ;; - --install=*) - if [[ -n "$update" ]]; then - echo " ERROR: You can't use --install with --update argument" - exit 1 - fi - - install="${1#*=}" - shift - - if [[ "$install" == "" ]]; then - echo " ERROR: Argument --install can't be empty!" - exit 1 - elif [[ "$install" != "full" && "$install" != "min" ]]; then - echo " ERROR: Invalid option $install for --install argument. Valid values are only full or min" - exit 1 - fi - ;; - --update) - if [[ -n "$install" ]]; then - echo " ERROR: You can't use --update with --install argument" - exit 1 - fi - - update="true" - shift - ;; - *) - echo " ERROR: Argument $1 not exists. Use --help to display all available arguments" - exit 1 - ;; - esac + update="true" + shift + ;; + --help) + echo " Usage: $0 [options]" + echo " Options:" + echo " --cli Use CLI mode. It does not have CLI-GUI + interface, and all actions are specified using action arguments" + echo " --cpgg-dir= Allows you to specify the root directory + of the CtrlPanel" + echo " --force Performs an action without confirmation + (applicable for --install and --update arguments)" + echo " --install= Perform installation. Valid values are + full or min" + echo " --update Perform an update" + echo " --help Display this help message" + exit 0 + ;; + *) + error_out " ERROR: Argument $1 not exists. + Use --help to display all available arguments" + exit 1 + ;; + esac done -# Save terminal only if $cli_mode = false -if [ -z "$cli_mode" ]; then - save_terminal -fi - -# Calling function to specify a directory set_cpgg_dir -# Moving to the CtrlPanel directory -cd "${cpgg_dir:-$DEFAULT_DIR}" || { - echo " ERROR: An error occurred while trying to switch to the working directory. Please try to run the script again, if the error persists, create support forum post on CtrlPanel's Discord server!" +# shellcheck source=/dev/null +source "${cpgg_dir:-$DEFAULT_DIR}/bin/ctrlhelper_sub_scripts/menus.sh" \ + || { + error_out " ERROR: Source files could not be added! Are you sure you are + using script for version 0.10 or above of CtrlPanel? Please try to run the + script again, if the error persists, create support forum post on + CtrlPanel's Discord server!" + exit 1 +} +# shellcheck source=/dev/null +source "${cpgg_dir:-$DEFAULT_DIR}/bin/ctrlhelper_sub_scripts/functions.sh" \ + || { + error_out " ERROR: Source files could not be added! Are you sure you are + using script for version 0.10 or above of CtrlPanel? Please try to run the + script again, if the error persists, create support forum post on + CtrlPanel's Discord server!" exit 1 } -# Main functions -if [ -z "$cli_mode" ]; then - - get_version - update_needed_checker - - # Main menu - main_menu() { - local choice="" - - logo - logo_version - logo_message - echo " Select an option:" - echo " 1. Install dependencies" - echo " 2. Update" - echo " 3. Info & Help" - echo " 0. Exit" - echo "" - read -rp " > " choice - - case $choice in - 1) - menu_1 - ;; - 2) - menu_2 - ;; - 3) - menu_3 - ;; - 0) - restore_terminal - ;; - *) - main_menu - ;; - esac - } - - # Install dependencies menu - menu_1() { - local choice="" - - logo - echo " This action will install all the necessary dependencies such as PHP, Redis, MariaDB and others, as well as install composer files." - echo " You will still have to create MySQL user and configure nginx yourself." - echo "" - echo " Select the installation option:" - echo " 1. Full install" - echo " 2. Minimal install (Not include MariaDB and nginx)" - echo " 0. Exit to main menu" - read -rp " > " choice - - case "$choice" in - 1) - install_deps - ;; - 2) - install_deps "true" - ;; - 0) - main_menu - ;; - *) - menu_1 - ;; - esac - } - - # Update menu - menu_2() { - local choice="" - - logo - echo " This action cannot be undone, create backup of the database before updating! It will also remove all installed themes and addons." - echo " Do you want to continue? (Y/n)" - read -rp " > " choice +cd "${cpgg_dir:-$DEFAULT_DIR}" \ + || { + error_out " ERROR: An error occurred while trying to switch to the working + directory. Please try to run the script again, if the error persists, + create support forum post on CtrlPanel's Discord server!" + exit 1 +} - case "$choice" in - y | Y) - update - ;; - n | N) - main_menu - ;; - *) - menu_2 - ;; - esac - } +if [[ -z "$cli_mode" ]]; then + save_terminal +fi - # Info & Help menu - menu_3() { - logo - echo " In dev" - sleep 3 - main_menu - } +if [[ -z "$cli_mode" ]]; then + get_version + update_needed_checker - # Launch main munu - main_menu + main_menu - # Restoring terminal after script end - restore_terminal + restore_terminal else - if [[ "$install" == "full" ]]; then - if [[ "$force" == "true" ]]; then - install_deps - else - confirm_dialog "This action will install all the necessary dependencies such as PHP, Redis, MariaDB and others, as well as install composer files." "You will still have to create MySQL user and configure nginx yourself." "install_deps" - fi - elif [[ "$install" == "min" ]]; then - if [[ "$force" == "true" ]]; then - install_deps "true" - else - confirm_dialog "This action will install all the necessary dependencies such as PHP, Redis, Composer and others, as well as install composer files." "" "install_deps \"true\"" - fi - elif [[ "$update" == "true" ]]; then - if [[ "$force" == "true" ]]; then - update - else - confirm_dialog "This action cannot be undone, create backup of the database before updating! It will also remove all installed themes and addons." "" "update" - fi + if [[ "$install" == "full" ]]; then + if [[ "$force" == "true" ]]; then + install_deps + else + confirm_dialog "This action will install all the necessary dependencies + such as PHP, Redis, MariaDB and others, as well as install composer + files." "You will still have to create MySQL user and configure nginx + yourself." "install_deps" + fi + elif [[ "$install" == "min" ]]; then + if [[ "$force" == "true" ]]; then + install_deps "true" else - echo " ERROR: You have not specified the action you want to do! Use --help to display all available arguments" - exit 1 + confirm_dialog "This action will install all the necessary dependencies + such as PHP, Redis, Composer and others, as well as install composer + files." "" "install_deps \"true\"" fi + elif [[ "$update" == "true" ]]; then + if [[ "$force" == "true" ]]; then + update + else + confirm_dialog "This action cannot be undone, create backup of the + database before updating! It will also remove all installed themes + and addons." "" "update" + fi + else + echo " ERROR: You have not specified the action you want to do! + Use --help to display all available arguments" + exit 1 + fi fi diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh new file mode 100644 index 000000000..bf8706a68 --- /dev/null +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -0,0 +1,199 @@ +#!/bin/bash +# +# The auxiliary file for CtrlHelper script. +# Contains important functions for the main script to work +# +# Made with love by MrWeez +# Contact me +# GitHub: https://github.com/MrWeez +# Discord: @mrweez_ +# Email: contact@mrweez.dev + +####################################### +# Terminal save function, for recovery after exiting +####################################### +save_terminal() { + trap restore_terminal SIGINT + tput smcup +} + +####################################### +# Terminal recovery function, after execution +####################################### +restore_terminal() { + tput rmcup + exit +} + +####################################### +# Getting current and latest version of CtrlPanel +# Globals: +# PANEL_VER +# PANEL_LATEST_VER +# DEFAULT_DIR +# cpgg_dir +####################################### +get_version() { + PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") + readonly PANEL_VER + PANEL_LATEST_VER=$( + curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags \ + | sed -n 's/.*"name": "\([^"]*\)".*/\1/p' \ + | head -n 1 + ) + readonly PANEL_LATEST_VER +} + +####################################### +# Comparing current and latest version of CtrlPanel +# Arguments: +# Current version +# Latest version +# Outputs: +# 0 if versions match +# 1 if latest version is newer than the current one +# 2 if current version is newer than the latest one +####################################### +version_compare() { + local current_version="$1" + local latest_version="$2" + + # Break down versions into components + IFS='.' read -r -a current_parts <<<"$current_version" + IFS='.' read -r -a latest_parts <<<"$latest_version" + + # Add zeros to the shorter version (e.g. 0.10 => 0.10.0) + while ((${#current_parts[@]} < ${#latest_parts[@]})); do + current_parts+=("0") + done + + # Compare components one by one + for ((i = 0; i < ${#current_parts[@]}; i++)); do + if ((current_parts[i] < latest_parts[i])); then + echo "1" + return 0 + elif ((current_parts[i] > latest_parts[i])); then + echo "2" + return 0 + fi + done + + echo "0" + return 0 +} + +####################################### +# Checking if the CtrlPanel needs to be updated +# Globals: +# PANEL_VER +# PANEL_LATEST_VER +# Outputs: +# 0 if versions match +# 1 if latest version is newer than the current one +# 2 if current version is newer than the latest one +####################################### +update_needed_checker() { + is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") + # shellcheck disable=SC2034 + readonly is_update_needed +} + +####################################### +# Installing dependencies +# Globals: +# cli_mode +# Arguments: +# NULL for full installation, true for minimal install +####################################### +install_deps() { + local minimal="$1" + + if [[ -z "$cli_mode" ]]; then + logo + fi + + echo " Adding \"add-apt-repository\" command and additional dependencies" + sudo apt -y install software-properties-common curl apt-transport-https ca-certificates gnupg + + echo " Adding PHP repository" + LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/php + + if [[ ! -f "/usr/share/keyrings/redis-archive-keyring.gpg" ]]; then + echo " Adding Redis repository" + curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg + echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" \ + | sudo tee /etc/apt/sources.list.d/redis.list + fi + + if [[ -z "$minimal" ]]; then + echo " Adding MariaDB repository" + curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash + elif [[ -n "$minimal" && "$minimal" != "true" ]]; then + error_out " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" + exit 1 + fi + + echo " Running \"apt update\"" + sudo apt update + + echo " Installing dependencies" + if [[ "$minimal" ]]; then + sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} redis-server tar unzip git + elif [[ -z "$minimal" ]]; then + sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git + else + error_out " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" + exit 1 + fi + + echo " Installing Composer" + curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer + + echo " Installing Composer dependencies to the CtrlPanel" + sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction +} + +####################################### +# Update CtrlPanel +# Globals: +# DEFAULT_DIR +# cpgg_dir +# cli_mode +####################################### +update() { + if [[ -z "$cli_mode" ]]; then + logo + fi + + echo " Enabling maintenance mode" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan down + + if ! sudo git config --global --get-all safe.directory | grep -q -w "${cpgg_dir:-$DEFAULT_DIR}"; then + echo " Adding CtrlPanel directory to the git save.directory list" + sudo git config --global --add safe.directory "${cpgg_dir:-$DEFAULT_DIR}" + fi + + echo " Downloading file updates" + sudo git stash + sudo git pull + + echo " Installing Composer dependencies" + sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction + + echo " Migrating database updates" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan migrate --seed --force + + echo " Clearing the cache" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan view:clear + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan config:clear + + echo " Setting permissions" + sudo chown -R www-data:www-data "${cpgg_dir:-$DEFAULT_DIR}" + sudo chmod -R 755 "${cpgg_dir:-$DEFAULT_DIR}" + + echo " Restarting Queue Workers" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan queue:restart + + echo " Disabling maintenance mode" + sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan up +} diff --git a/bin/ctrlhelper_sub_scripts/menus.sh b/bin/ctrlhelper_sub_scripts/menus.sh new file mode 100644 index 000000000..6c7edd9de --- /dev/null +++ b/bin/ctrlhelper_sub_scripts/menus.sh @@ -0,0 +1,169 @@ +#!/bin/bash +# +# The auxiliary file for CtrlHelper script. +# Contains the CLI-GUI parts of the interface +# +# Made with love by MrWeez +# Contact me +# GitHub: https://github.com/MrWeez +# Discord: @mrweez_ +# Email: contact@mrweez.dev + +####################################### +# Logo to display in the CLI-GUI +####################################### +logo() { + clear + echo " ________ ______ __ " + echo " / ____/ /______/ / __ \____ _____ ___ / /____ _____ _" + echo " / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/" + echo " / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / " + echo " \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / " + echo " /____//____/ " + echo "" +} + +####################################### +# Displaying the current version of the script and CtrlPanel under the logo +# Globals: +# SCRIPT_VER +# PANEL_VER +####################################### +logo_version() { + echo " Script version: $SCRIPT_VER" + echo " CtrlPanel version: $PANEL_VER" + echo "" +} + +####################################### +# Message to the user about whether they need to update, or if they are +# already using latest version +# Globals: +# is_update_needed +####################################### +logo_message() { + # shellcheck disable=SC2154 + if [[ $is_update_needed == 0 ]]; then + echo " You are using the latest version! No update required." + echo "" + elif [[ $is_update_needed == 1 ]]; then + echo " New version available! You can update right now by selecting + \"Update\" option." + echo "" + elif [[ $is_update_needed == 2 ]]; then + echo " You are using a newer version! Most likely you have a development + branch installed." + echo "" + fi +} + +####################################### +# Action confirmation dialog +# Arguments: +# First line of the message +# Second line of the message +# Action that will be performed upon confirmation +####################################### +confirm_dialog() { + local message_line1="$1" + local message_line2="$2" + local function="$3" + + echo " $message_line1" + if [[ -n "$message_line2" ]]; then + echo " $message_line2" + fi + echo " Do you want to continue? (Y/n)" + read -rp " > " choice + + case "$choice" in + y | Y) $function ;; + n | N) exit 0 ;; + *) + echo " ERROR: Unknown choice $choice" + echo "" + confirm_dialog "$message_line1" "$message_line2" "$function" + ;; + esac +} + +####################################### +# Main menu in CLI-GUI mode +####################################### +main_menu() { + local choice="" + + logo + logo_version + logo_message + echo " Select an option:" + echo " 1. Install dependencies" + echo " 2. Update" + echo " 3. Info & Help" + echo " 0. Exit" + echo "" + read -rp " > " choice + + case $choice in + 1) install_menu ;; + 2) update_menu ;; + 3) info_help_menu ;; + 0) restore_terminal ;; + *) main_menu ;; + esac +} + +####################################### +# Install dependencies menu in CLI-GUI mode +####################################### +install_menu() { + local choice="" + + logo + echo " This action will install all the necessary dependencies such as PHP, + Redis, MariaDB and others, as well as install composer files." + echo " You will still have to create MySQL user and configure nginx + yourself." + echo "" + echo " Select the installation option:" + echo " 1. Full install" + echo " 2. Minimal install (Not include MariaDB and nginx)" + echo " 0. Exit to main menu" + read -rp " > " choice + + case "$choice" in + 1) install_deps ;; + 2) install_deps "true" ;; + 0) main_menu ;; + *) install_menu ;; + esac +} + +####################################### +# Update menu in CLI-GUI mode +####################################### +update_menu() { + local choice="" + + logo + echo " This action cannot be undone, create backup of the database before + updating! It will also remove all installed themes and addons." + echo " Do you want to continue? (Y/n)" + read -rp " > " choice + + case "$choice" in + y | Y) update ;; + n | N) main_menu ;; + *) update_menu ;; + esac +} + +####################################### +# Info & Help menu in CLI-GUI mode +####################################### +info_help_menu() { + logo + echo " In dev" + sleep 3 + main_menu +} From e8ef9e7ad24fe0c34cdc8f685aa98ec5082f000e Mon Sep 17 00:00:00 2001 From: MrWeez Date: Mon, 29 Jul 2024 16:24:24 +0000 Subject: [PATCH 32/40] fix: :bug: Extra line breaks and spaces after refactor --- bin/ctrlhelper.sh | 98 ++++++++++++------------- bin/ctrlhelper_sub_scripts/functions.sh | 6 +- bin/ctrlhelper_sub_scripts/menus.sh | 20 ++--- 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 7039976d1..f00624eec 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -39,14 +39,14 @@ set_cpgg_dir() { # Message that the user will see by default, if he specifies a # non-existent directory or not root CtrlPanel directory if [[ -z "$is_exists" ]] && [[ -z "$is_cpgg_root" ]] || [[ "$is_null" == "true" ]]; then - echo " Default directory wasn't found. Specify directory where your - CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + echo " Default directory wasn't found. Specify directory where your \ +CtrlPanel is installed (e.g. /var/www/ctrlpanel)" elif [[ $is_exists == false ]]; then - echo " Directory $cpgg_dir doesn't exist. Specify directory where - your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + echo " Directory $cpgg_dir doesn't exist. Specify directory where \ +your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" elif [[ $is_cpgg_root == false ]]; then - echo " $cpgg_dir is not a root CtrlPanel directory. Specify - directory where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + echo " $cpgg_dir is not a root CtrlPanel directory. Specify \ +directory where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" fi read -rep " > " cpgg_dir @@ -76,8 +76,8 @@ set_cpgg_dir() { # Error if default directory is not found and CtrlPanel root directory is # not specified when using the CLI mode if [[ ! -d "$DEFAULT_DIR" ]] && [[ -z "$cpgg_dir" ]]; then - error_out " ERROR: Default directory wasn't found. Specify directory - where your CtrlPanel is installed using --cpgg-dir argument" + error_out " ERROR: Default directory wasn't found. Specify directory \ +where your CtrlPanel is installed using --cpgg-dir argument" exit 1 fi fi @@ -102,8 +102,8 @@ while [[ $# -gt 0 ]]; do error_out " ERROR: Directory $cpgg_dir doesn't exist." exit 1 elif [[ ! -f "$cpgg_dir/config/app.php" ]]; then - error_out " ERROR: $cpgg_dir is not a root CtrlPanel - directory." + error_out " ERROR: $cpgg_dir is not a root CtrlPanel \ +directory." exit 1 else continue @@ -115,8 +115,8 @@ while [[ $# -gt 0 ]]; do ;; --install=*) if [[ -n "$update" ]]; then - error_out " ERROR: You can't use --install with --update - argument" + error_out " ERROR: You can't use --install with --update \ +argument" exit 1 fi @@ -127,15 +127,15 @@ while [[ $# -gt 0 ]]; do error_out " ERROR: Argument --install can't be empty!" exit 1 elif [[ "$install" != "full" && "$install" != "min" ]]; then - error_out " ERROR: Invalid option $install for --install - argument. Valid values are only full or min" + error_out " ERROR: Invalid option $install for --install \ +argument. Valid values are only full or min" exit 1 fi ;; --update) if [[ -n "$install" ]]; then - error_out " ERROR: You can't use --update with --install - argument" + error_out " ERROR: You can't use --update with --install \ +argument" exit 1 fi @@ -145,21 +145,21 @@ while [[ $# -gt 0 ]]; do --help) echo " Usage: $0 [options]" echo " Options:" - echo " --cli Use CLI mode. It does not have CLI-GUI - interface, and all actions are specified using action arguments" - echo " --cpgg-dir= Allows you to specify the root directory - of the CtrlPanel" - echo " --force Performs an action without confirmation - (applicable for --install and --update arguments)" - echo " --install= Perform installation. Valid values are - full or min" + echo " --cli Use CLI mode. It does not have CLI-GUI \ +interface, and all actions are specified using action arguments" + echo " --cpgg-dir= Allows you to specify the root directory \ +of the CtrlPanel" + echo " --force Performs an action without confirmation \ +(applicable for --install and --update arguments)" + echo " --install= Perform installation. Valid values are \ +full or min" echo " --update Perform an update" echo " --help Display this help message" exit 0 ;; *) - error_out " ERROR: Argument $1 not exists. - Use --help to display all available arguments" + error_out " ERROR: Argument $1 not exists. \ +Use --help to display all available arguments" exit 1 ;; esac @@ -170,27 +170,27 @@ set_cpgg_dir # shellcheck source=/dev/null source "${cpgg_dir:-$DEFAULT_DIR}/bin/ctrlhelper_sub_scripts/menus.sh" \ || { - error_out " ERROR: Source files could not be added! Are you sure you are - using script for version 0.10 or above of CtrlPanel? Please try to run the - script again, if the error persists, create support forum post on - CtrlPanel's Discord server!" + error_out " ERROR: Source files could not be added! Are you sure you are \ +using script for version 0.10 or above of CtrlPanel? Please try to run the \ +script again, if the error persists, create support forum post on \ +CtrlPanel's Discord server!" exit 1 } # shellcheck source=/dev/null source "${cpgg_dir:-$DEFAULT_DIR}/bin/ctrlhelper_sub_scripts/functions.sh" \ || { - error_out " ERROR: Source files could not be added! Are you sure you are - using script for version 0.10 or above of CtrlPanel? Please try to run the - script again, if the error persists, create support forum post on - CtrlPanel's Discord server!" + error_out " ERROR: Source files could not be added! Are you sure you are \ +using script for version 0.10 or above of CtrlPanel? Please try to run the \ +script again, if the error persists, create support forum post on \ +CtrlPanel's Discord server!" exit 1 } cd "${cpgg_dir:-$DEFAULT_DIR}" \ || { - error_out " ERROR: An error occurred while trying to switch to the working - directory. Please try to run the script again, if the error persists, - create support forum post on CtrlPanel's Discord server!" + error_out " ERROR: An error occurred while trying to switch to the working \ +directory. Please try to run the script again, if the error persists, \ +create support forum post on CtrlPanel's Discord server!" exit 1 } @@ -210,30 +210,30 @@ else if [[ "$force" == "true" ]]; then install_deps else - confirm_dialog "This action will install all the necessary dependencies - such as PHP, Redis, MariaDB and others, as well as install composer - files." "You will still have to create MySQL user and configure nginx - yourself." "install_deps" + confirm_dialog "This action will install all the necessary dependencies \ +such as PHP, Redis, MariaDB and others, as well as install composer \ +files." "You will still have to create MySQL user and configure nginx \ +yourself." "install_deps" fi elif [[ "$install" == "min" ]]; then if [[ "$force" == "true" ]]; then install_deps "true" else - confirm_dialog "This action will install all the necessary dependencies - such as PHP, Redis, Composer and others, as well as install composer - files." "" "install_deps \"true\"" + confirm_dialog "This action will install all the necessary dependencies \ +such as PHP, Redis, Composer and others, as well as install composer \ +files." "" "install_deps \"true\"" fi elif [[ "$update" == "true" ]]; then if [[ "$force" == "true" ]]; then update else - confirm_dialog "This action cannot be undone, create backup of the - database before updating! It will also remove all installed themes - and addons." "" "update" + confirm_dialog "This action cannot be undone, create backup of the \ +database before updating! It will also remove all installed themes \ +and addons." "" "update" fi else - echo " ERROR: You have not specified the action you want to do! - Use --help to display all available arguments" + echo " ERROR: You have not specified the action you want to do! \ +Use --help to display all available arguments" exit 1 fi fi diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index bf8706a68..5d2692177 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -129,7 +129,8 @@ install_deps() { echo " Adding MariaDB repository" curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash elif [[ -n "$minimal" && "$minimal" != "true" ]]; then - error_out " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" + error_out " ERROR: Invalid argument $minimal for install_deps function. \ +Please, report to developers!" exit 1 fi @@ -142,7 +143,8 @@ install_deps() { elif [[ -z "$minimal" ]]; then sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git else - error_out " ERROR: Invalid argument $minimal for install_deps function. Please, report to developers!" + error_out " ERROR: Invalid argument $minimal for install_deps function. \ +Please, report to developers!" exit 1 fi diff --git a/bin/ctrlhelper_sub_scripts/menus.sh b/bin/ctrlhelper_sub_scripts/menus.sh index 6c7edd9de..9891b8d4d 100644 --- a/bin/ctrlhelper_sub_scripts/menus.sh +++ b/bin/ctrlhelper_sub_scripts/menus.sh @@ -47,12 +47,12 @@ logo_message() { echo " You are using the latest version! No update required." echo "" elif [[ $is_update_needed == 1 ]]; then - echo " New version available! You can update right now by selecting - \"Update\" option." + echo " New version available! You can update right now by selecting \ +\"Update\" option." echo "" elif [[ $is_update_needed == 2 ]]; then - echo " You are using a newer version! Most likely you have a development - branch installed." + echo " You are using a newer version! Most likely you have a development \ +branch installed." echo "" fi } @@ -120,10 +120,10 @@ install_menu() { local choice="" logo - echo " This action will install all the necessary dependencies such as PHP, - Redis, MariaDB and others, as well as install composer files." - echo " You will still have to create MySQL user and configure nginx - yourself." + echo " This action will install all the necessary dependencies such as PHP, \ +Redis, MariaDB and others, as well as install composer files." + echo " You will still have to create MySQL user and configure nginx \ +yourself." echo "" echo " Select the installation option:" echo " 1. Full install" @@ -146,8 +146,8 @@ update_menu() { local choice="" logo - echo " This action cannot be undone, create backup of the database before - updating! It will also remove all installed themes and addons." + echo " This action cannot be undone, create backup of the database before \ +updating! It will also remove all installed themes and addons." echo " Do you want to continue? (Y/n)" read -rp " > " choice From 1932477d978229ea81de7b068d77b7d8403963cf Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 00:25:53 +0000 Subject: [PATCH 33/40] refactor: :lipstick: Colors everywhere! Now the output looks colorful instead of default white --- bin/ctrlhelper.sh | 178 ++++++++++++++-------- bin/ctrlhelper_sub_scripts/functions.sh | 96 ++++++++---- bin/ctrlhelper_sub_scripts/menus.sh | 192 ++++++++++++++++-------- 3 files changed, 314 insertions(+), 152 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index f00624eec..17615a060 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -10,16 +10,47 @@ # Email: contact@mrweez.dev # shellcheck disable=SC2034 -readonly SCRIPT_VER="0.5.12-dev" +readonly SCRIPT_VER="0.6.3" readonly DEFAULT_DIR="/var/www/ctrlpanel" +####################################### +# Colors +####################################### +readonly T_BL="\033[30m" # Blue text +readonly T_RE="\033[31m" # Red text +readonly T_CY="\033[36m" # Cyan text +readonly T_WH="\033[37m" # White text + +readonly BT_RE="\033[91m" # Bright red text +readonly BT_GR="\033[92m" # Bright green text +readonly BT_YE="\033[93m" # Bright yellow text +readonly BT_BLU="\033[94m" # Bright blue text +readonly BT_CY="\033[96m" # Bright cyan text +readonly BT_WH="\033[97m" # Bright white text + +readonly B_RE="\033[41m" # Red background + +readonly BB_YE="\033[103m" # Yellow bright background +readonly BB_BLU="\033[104m" # Blue bright background +readonly BB_CY="\033[106m" # Cyan bright background + +readonly NC="\033[0m" # Reset +readonly TB="\033[1m" # Bold +readonly TU="\033[4m" # Underline + +####################################### +# Visual blocks +####################################### +readonly CHECK="${BT_YE}${TB}(${BT_GR}✓${BT_YE})${NC}" +readonly WARN="${BT_YE}${TB}(${BT_RE}!!${BT_YE})${NC}" + ####################################### # Return an error message in STDERR # Arguments: # Error message ####################################### error_out() { - echo "$*" >&2 + echo -e " ${B_RE}${BT_WH} ERROR ${NC} ${T_RE}$*${NC}" >&2 } ####################################### @@ -38,15 +69,18 @@ set_cpgg_dir() { while true; do # Message that the user will see by default, if he specifies a # non-existent directory or not root CtrlPanel directory + echo "" if [[ -z "$is_exists" ]] && [[ -z "$is_cpgg_root" ]] || [[ "$is_null" == "true" ]]; then - echo " Default directory wasn't found. Specify directory where your \ -CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + echo -e " ${T_CY}Default directory wasn't found. Specify directory \ +where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)${NC}" elif [[ $is_exists == false ]]; then - echo " Directory $cpgg_dir doesn't exist. Specify directory where \ -your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + echo -e " ${T_CY}Directory ${BT_YE}$cpgg_dir${T_CY} doesn't exist. \ +Specify directory where your CtrlPanel is installed \ +(e.g. /var/www/ctrlpanel)${NC}" elif [[ $is_cpgg_root == false ]]; then - echo " $cpgg_dir is not a root CtrlPanel directory. Specify \ -directory where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" + echo -e " ${BT_YE}$cpgg_dir${T_CY} is not a root CtrlPanel \ +directory. Specify directory where your CtrlPanel is installed \ +(e.g. /var/www/ctrlpanel)${NC}" fi read -rep " > " cpgg_dir @@ -54,7 +88,7 @@ directory where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" # Deleting / at the end of the specified directory cpgg_dir="${cpgg_dir%/}" - # Resetting values before validation + # Resetting validation values before validation is_null="" is_exists="" is_cpgg_root="" @@ -76,14 +110,16 @@ directory where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)" # Error if default directory is not found and CtrlPanel root directory is # not specified when using the CLI mode if [[ ! -d "$DEFAULT_DIR" ]] && [[ -z "$cpgg_dir" ]]; then - error_out " ERROR: Default directory wasn't found. Specify directory \ -where your CtrlPanel is installed using --cpgg-dir argument" + error_out "Default directory wasn't found. Specify directory where your \ +CtrlPanel is installed using ${BT_YE}--cpgg-dir${T_RE} argument" exit 1 fi fi } +####################################### # Handling startup arguments +####################################### while [[ $# -gt 0 ]]; do case "$1" in --cli) @@ -96,14 +132,13 @@ while [[ $# -gt 0 ]]; do shift if [[ "$cpgg_dir" == "" ]]; then - error_out " ERROR: Argument --cpgg-dir can't be empty!" + error_out "Argument ${BT_YE}--cpgg-dir${T_RE} can't be empty!" exit 1 elif [[ ! -d "$cpgg_dir" ]]; then - error_out " ERROR: Directory $cpgg_dir doesn't exist." + error_out "Directory ${BT_YE}$cpgg_dir${T_RE} doesn't exist." exit 1 elif [[ ! -f "$cpgg_dir/config/app.php" ]]; then - error_out " ERROR: $cpgg_dir is not a root CtrlPanel \ -directory." + error_out "${BT_YE}$cpgg_dir${T_RE} is not a root CtrlPanel directory." exit 1 else continue @@ -115,8 +150,8 @@ directory." ;; --install=*) if [[ -n "$update" ]]; then - error_out " ERROR: You can't use --install with --update \ -argument" + error_out "You can't use ${BT_YE}--install${T_RE} with \ +${BT_YE}--update${T_RE} argument" exit 1 fi @@ -124,18 +159,19 @@ argument" shift if [[ "$install" == "" ]]; then - error_out " ERROR: Argument --install can't be empty!" + error_out "Argument ${BT_YE}--install${T_RE} can't be empty!" exit 1 elif [[ "$install" != "full" && "$install" != "min" ]]; then - error_out " ERROR: Invalid option $install for --install \ -argument. Valid values are only full or min" + error_out "Invalid option ${BT_YE}$install${T_RE} for \ +${BT_YE}--install${T_RE} argument. Valid values are only \ +${BT_YE}${TU}full${NC}${T_RE} or ${BT_YE}${TU}min" exit 1 fi ;; --update) if [[ -n "$install" ]]; then - error_out " ERROR: You can't use --update with --install \ -argument" + error_out "You can't use ${BT_YE}--update${T_RE} with \ +${BT_YE}--install${T_RE} argument" exit 1 fi @@ -143,23 +179,27 @@ argument" shift ;; --help) - echo " Usage: $0 [options]" - echo " Options:" - echo " --cli Use CLI mode. It does not have CLI-GUI \ -interface, and all actions are specified using action arguments" - echo " --cpgg-dir= Allows you to specify the root directory \ -of the CtrlPanel" - echo " --force Performs an action without confirmation \ -(applicable for --install and --update arguments)" - echo " --install= Perform installation. Valid values are \ -full or min" - echo " --update Perform an update" - echo " --help Display this help message" + echo -e " ${BT_WH}Usage: ${T_CY}$0 ${BT_CY}[options]${NC}" + echo -e " ${BT_WH}Options:${NC}" + echo -e " ${BT_CY}--cli \ +${BT_WH}Use CLI mode. It does not have CLI-GUI interface, and all actions are \ +specified using action arguments${NC}" + echo -e " ${BT_CY}--cpgg-dir= \ +${BT_WH}Allows you to specify the root directory of the CtrlPanel${NC}" + echo -e " ${BT_CY}--force \ +${BT_WH}Performs an action without confirmation (applicable for --install and \ +--update arguments in CLI mode)${NC}" + echo -e " ${BT_CY}--install= \ +${BT_WH}Perform installation. Valid values are full or min${NC}" + echo -e " ${BT_CY}--update \ +${BT_WH}Perform an update${NC}" + echo -e " ${BT_CY}--help \ +${BT_WH}Display this help message${NC}" exit 0 ;; *) - error_out " ERROR: Argument $1 not exists. \ -Use --help to display all available arguments" + error_out "Argument ${BT_YE}$1${T_RE} not exists. Use \ +${BT_YE}--help${T_RE} to display all available arguments" exit 1 ;; esac @@ -170,27 +210,27 @@ set_cpgg_dir # shellcheck source=/dev/null source "${cpgg_dir:-$DEFAULT_DIR}/bin/ctrlhelper_sub_scripts/menus.sh" \ || { - error_out " ERROR: Source files could not be added! Are you sure you are \ -using script for version 0.10 or above of CtrlPanel? Please try to run the \ -script again, if the error persists, create support forum post on \ -CtrlPanel's Discord server!" + error_out "Source files could not be added! Are you sure you are using \ +script for version 0.10 or above of CtrlPanel? Please try to run the script \ +again, if the error persists, create support forum post on CtrlPanel's \ +Discord server!" exit 1 } # shellcheck source=/dev/null source "${cpgg_dir:-$DEFAULT_DIR}/bin/ctrlhelper_sub_scripts/functions.sh" \ || { - error_out " ERROR: Source files could not be added! Are you sure you are \ -using script for version 0.10 or above of CtrlPanel? Please try to run the \ -script again, if the error persists, create support forum post on \ -CtrlPanel's Discord server!" + error_out "Source files could not be added! Are you sure you are using \ +script for version 0.10 or above of CtrlPanel? Please try to run the script \ +again, if the error persists, create support forum post on CtrlPanel's \ +Discord server!" exit 1 } cd "${cpgg_dir:-$DEFAULT_DIR}" \ || { - error_out " ERROR: An error occurred while trying to switch to the working \ -directory. Please try to run the script again, if the error persists, \ -create support forum post on CtrlPanel's Discord server!" + error_out "An error occurred while trying to switch to the working \ +directory. Please try to run the script again, if the error persists, create \ +support forum post on CtrlPanel's Discord server!" exit 1 } @@ -210,30 +250,50 @@ else if [[ "$force" == "true" ]]; then install_deps else - confirm_dialog "This action will install all the necessary dependencies \ -such as PHP, Redis, MariaDB and others, as well as install composer \ -files." "You will still have to create MySQL user and configure nginx \ -yourself." "install_deps" + confirm_dialog \ + "${BT_YE}This action will install all the necessary dependencies such as \ +PHP, Redis, MariaDB and others, as well as install composer files. + You will still have to create MySQL user and configure nginx yourself. + + ${BT_GR}Below is a list of all packages that will be installed${NE}" \ + "${T_WH}software-properties-common curl apt-transport-https \ +ca-certificates gnupg lsb-release php8.3 \ +php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} \ +mariadb-server nginx redis-server tar unzip git${NC}" \ + "install_deps" \ + "exit 0" fi elif [[ "$install" == "min" ]]; then if [[ "$force" == "true" ]]; then install_deps "true" else - confirm_dialog "This action will install all the necessary dependencies \ -such as PHP, Redis, Composer and others, as well as install composer \ -files." "" "install_deps \"true\"" + confirm_dialog \ + "${BT_YE}This action will install all the necessary dependencies such as \ +PHP, Redis, Composer and others, as well as install composer files. + + ${BT_GR}Below is a list of all packages that will be installed${NE}" \ + "${T_WH}software-properties-common curl apt-transport-https \ +ca-certificates gnupg lsb-release php8.3 \ +php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} \ +redis-server tar unzip git${NC}" \ + "install_deps \"true\"" \ + "exit 0" fi elif [[ "$update" == "true" ]]; then if [[ "$force" == "true" ]]; then update else - confirm_dialog "This action cannot be undone, create backup of the \ -database before updating! It will also remove all installed themes \ -and addons." "" "update" + confirm_dialog \ + "${B_RE}${BT_WH} This action cannot be undone, create backup of the \ +database before updating! ${NC}" \ + "${B_RE}${BT_WH} It will also remove all installed themes and addons. ${NC}" \ + "update" \ + "exit 0" fi else - echo " ERROR: You have not specified the action you want to do! \ -Use --help to display all available arguments" + error_out \ + "You have not specified the action you want to do! Use \ +${BT_YE}--help${T_RE} to display all available arguments" exit 1 fi fi diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index 5d2692177..47dd9914d 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -25,6 +25,15 @@ restore_terminal() { exit } +####################################### +# Return an info message in STDOUT +# Arguments: +# Info message +####################################### +info_out() { + echo -e " ${BB_BLU}${BT_WH} INFO ${NC} ${BT_WH}$*${NC}" +} + ####################################### # Getting current and latest version of CtrlPanel # Globals: @@ -34,7 +43,9 @@ restore_terminal() { # cpgg_dir ####################################### get_version() { - PANEL_VER=$(grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php") + PANEL_VER=$( + grep -oP "'version' => '\K[^']+" "${cpgg_dir:-$DEFAULT_DIR}/config/app.php" + ) readonly PANEL_VER PANEL_LATEST_VER=$( curl -s https://api.github.com/repos/ctrlpanel-gg/panel/tags \ @@ -56,7 +67,9 @@ get_version() { ####################################### version_compare() { local current_version="$1" + readonly current_version local latest_version="$2" + readonly latest_version # Break down versions into components IFS='.' read -r -a current_parts <<<"$current_version" @@ -107,52 +120,65 @@ update_needed_checker() { ####################################### install_deps() { local minimal="$1" + # Removing double quotes at the beginning and end + minimal=${minimal#\"} + minimal=${minimal%\"} + readonly minimal - if [[ -z "$cli_mode" ]]; then - logo - fi + logo - echo " Adding \"add-apt-repository\" command and additional dependencies" - sudo apt -y install software-properties-common curl apt-transport-https ca-certificates gnupg + info_out "Adding \"add-apt-repository\" command and additional dependencies" + sudo apt -y -qq install software-properties-common curl apt-transport-https ca-certificates gnupg lsb-release - echo " Adding PHP repository" - LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/php + if [[ ! -f "/etc/apt/trusted.gpg.d/deb.sury.org-php.gpg" ]]; then + info_out "Adding PHP repository keyring" + sudo curl -sSLo /etc/apt/trusted.gpg.d/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg + fi + + if [[ ! -f "/etc/apt/sources.list.d/deb.sury.org-php.list" ]]; then + info_out "Adding PHP repository" + sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/deb.sury.org-php.list' + fi if [[ ! -f "/usr/share/keyrings/redis-archive-keyring.gpg" ]]; then - echo " Adding Redis repository" + info_out "Adding Redis repository" curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" \ | sudo tee /etc/apt/sources.list.d/redis.list fi if [[ -z "$minimal" ]]; then - echo " Adding MariaDB repository" + info_out "Adding MariaDB repository" curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash elif [[ -n "$minimal" && "$minimal" != "true" ]]; then - error_out " ERROR: Invalid argument $minimal for install_deps function. \ -Please, report to developers!" + error_out "Invalid argument $minimal for install_deps function. Please, report to developers!" exit 1 fi - echo " Running \"apt update\"" + info_out "Running \"apt update\"" sudo apt update - echo " Installing dependencies" + info_out "Installing dependencies" if [[ "$minimal" ]]; then - sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} redis-server tar unzip git + sudo apt -y -qq install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} redis-server tar unzip git elif [[ -z "$minimal" ]]; then - sudo apt -y install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git + sudo apt -y -qq install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git else - error_out " ERROR: Invalid argument $minimal for install_deps function. \ -Please, report to developers!" + error_out "Invalid argument $minimal for install_deps function. Please, report to developers!" exit 1 fi - echo " Installing Composer" + info_out "Installing Composer" curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer - echo " Installing Composer dependencies to the CtrlPanel" + info_out "Installing Composer dependencies to the CtrlPanel" sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction + + if [[ -z "$cli_mode" ]]; then + echo "" + echo " Installation finished. Press any key to exit" + read -rsn 1 -p " " + fi } ####################################### @@ -163,39 +189,43 @@ Please, report to developers!" # cli_mode ####################################### update() { - if [[ -z "$cli_mode" ]]; then - logo - fi + logo - echo " Enabling maintenance mode" + info_out "Enabling maintenance mode" sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan down if ! sudo git config --global --get-all safe.directory | grep -q -w "${cpgg_dir:-$DEFAULT_DIR}"; then - echo " Adding CtrlPanel directory to the git save.directory list" + info_out "Adding CtrlPanel directory to the git save.directory list" sudo git config --global --add safe.directory "${cpgg_dir:-$DEFAULT_DIR}" fi - echo " Downloading file updates" + info_out "Downloading file updates" sudo git stash sudo git pull - echo " Installing Composer dependencies" + info_out "Installing Composer dependencies" sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction - echo " Migrating database updates" + info_out "Migrating database updates" sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan migrate --seed --force - echo " Clearing the cache" + info_out "Clearing the cache" sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan view:clear sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan config:clear - echo " Setting permissions" + info_out "Setting permissions" sudo chown -R www-data:www-data "${cpgg_dir:-$DEFAULT_DIR}" sudo chmod -R 755 "${cpgg_dir:-$DEFAULT_DIR}" - echo " Restarting Queue Workers" + info_out "Restarting Queue Workers" sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan queue:restart - echo " Disabling maintenance mode" + info_out "Disabling maintenance mode" sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan up -} + + if [[ -z "$cli_mode" ]]; then + echo "" + echo " Update finished. Press any key to exit" + read -rsn 1 -p " " + fi +} \ No newline at end of file diff --git a/bin/ctrlhelper_sub_scripts/menus.sh b/bin/ctrlhelper_sub_scripts/menus.sh index 9891b8d4d..8c0e4f7ef 100644 --- a/bin/ctrlhelper_sub_scripts/menus.sh +++ b/bin/ctrlhelper_sub_scripts/menus.sh @@ -13,13 +13,15 @@ # Logo to display in the CLI-GUI ####################################### logo() { - clear - echo " ________ ______ __ " - echo " / ____/ /______/ / __ \____ _____ ___ / /____ _____ _" - echo " / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/" - echo " / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / " - echo " \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / " - echo " /____//____/ " + if [[ -z "$cli_mode" ]]; then + clear + fi + echo -e "${BT_CY} ________ ______ __ ${NC}" + echo -e "${BT_CY} / ____/ /______/ / __ \____ _____ ___ / /____ _____ _${NC}" + echo -e "${BT_CY} / / / __/ ___/ / /_/ / __ \`/ __ \/ _ \/ // __ \`/ __ \`/${NC}" + echo -e "${T_CY} / /___/ /_/ / / / ____/ /_/ / / / / __/ // /_/ / /_/ / ${NC}" + echo -e "${T_CY} \____/\__/_/ /_/_/ \__,_/_/ /_/\___/_(_)__, /\__, / ${NC}" + echo -e "${T_CY} /____//____/ ${NC}" echo "" } @@ -30,8 +32,8 @@ logo() { # PANEL_VER ####################################### logo_version() { - echo " Script version: $SCRIPT_VER" - echo " CtrlPanel version: $PANEL_VER" + echo -e " ${BT_YE}Script version:${NC}${BT_WH} $SCRIPT_VER${NC}" + echo -e " ${BT_YE}CtrlPanel version:${NC}${BT_WH} $PANEL_VER${NC}" echo "" } @@ -44,15 +46,16 @@ logo_version() { logo_message() { # shellcheck disable=SC2154 if [[ $is_update_needed == 0 ]]; then - echo " You are using the latest version! No update required." + echo -e " ${CHECK} ${BT_GR}You are using the latest version! \ +No update required.${NC}" echo "" elif [[ $is_update_needed == 1 ]]; then - echo " New version available! You can update right now by selecting \ -\"Update\" option." + echo -e " ${WARN} ${BT_RE}New version available! You can update right now \ +by selecting ${BT_YE}${TU}Update${NC} ${BT_RE}option.${NC}" echo "" elif [[ $is_update_needed == 2 ]]; then - echo " You are using a newer version! Most likely you have a development \ -branch installed." + echo -e " ${CHECK} ${BT_GR}You are using a newer version! Most likely you \ +have a development version installed.${NC}" echo "" fi } @@ -63,26 +66,42 @@ branch installed." # First line of the message # Second line of the message # Action that will be performed upon confirmation +# Exit action +# Unknown choice validation response +# Previous choice if $unknown_choice is true ####################################### confirm_dialog() { + local choice local message_line1="$1" local message_line2="$2" - local function="$3" + local action="$3" + local exit_action="$4" + local unknown_choice="$5" + local previous_choice="$6" - echo " $message_line1" + logo + echo -e " $message_line1" if [[ -n "$message_line2" ]]; then - echo " $message_line2" + echo -e " $message_line2" + fi + echo "" + echo -e " ${BT_WH}Continue? (Y/n)${NC}" + if [[ "$unknown_choice" == "true" ]]; then + echo -e " ${T_RE}Unknown choice ${BT_YE}${TU}$previous_choice${NC}" fi - echo " Do you want to continue? (Y/n)" read -rp " > " choice case "$choice" in - y | Y) $function ;; - n | N) exit 0 ;; + y | Y) $action ;; + n | N) $exit_action ;; *) - echo " ERROR: Unknown choice $choice" - echo "" - confirm_dialog "$message_line1" "$message_line2" "$function" + confirm_dialog \ + "$message_line1" \ + "$message_line2" \ + "$action" \ + "$exit_action" \ + "true" \ + "$choice" ;; esac } @@ -96,19 +115,25 @@ main_menu() { logo logo_version logo_message - echo " Select an option:" - echo " 1. Install dependencies" - echo " 2. Update" - echo " 3. Info & Help" - echo " 0. Exit" + echo -e " ${BB_CY} ${T_BL}Select an option: ${NC}" + echo -e " ${BT_WH}1. Install dependencies${NC}" + echo -e " ${BT_WH}2. Update${NC}" + echo -e " ${BT_WH}3. Info & Help${NC}" + echo -e " ${BT_WH}q. Quit${NC}" echo "" read -rp " > " choice case $choice in 1) install_menu ;; - 2) update_menu ;; + 2) + confirm_dialog "${B_RE}${BT_WH} This action cannot be undone, create \ +backup of the database before updating! ${NC}" \ + "${B_RE}${BT_WH} It will also remove all installed themes and addons. ${NC}" \ + "update" \ + "main_menu" + ;; 3) info_help_menu ;; - 0) restore_terminal ;; + q) restore_terminal ;; *) main_menu ;; esac } @@ -120,50 +145,97 @@ install_menu() { local choice="" logo - echo " This action will install all the necessary dependencies such as PHP, \ -Redis, MariaDB and others, as well as install composer files." - echo " You will still have to create MySQL user and configure nginx \ -yourself." + echo -e " ${BT_YE}This action will install all the necessary dependencies \ +such as PHP, Redis, MariaDB and others, as well as install composer files.${NC}" + echo -e " ${BT_YE}You will still need to create MySQL user/database and \ +configure nginx yourself.${NC}" + echo "" + echo -e " ${BB_CY} ${T_BL}Select an option: ${NC}" + echo -e " ${BT_WH}1. Full install${NC}" + echo -e " ${BT_WH}2. Minimal install (Not include MariaDB and nginx)${NC}" + echo -e " ${BT_WH}b. Back to main menu${NC}" echo "" - echo " Select the installation option:" - echo " 1. Full install" - echo " 2. Minimal install (Not include MariaDB and nginx)" - echo " 0. Exit to main menu" read -rp " > " choice case "$choice" in - 1) install_deps ;; - 2) install_deps "true" ;; - 0) main_menu ;; + 1) + confirm_dialog \ + "${BT_YE}You are going to install full set of dependencies, below is a \ +list of all packages that will be installed" \ + "${T_WH}software-properties-common curl apt-transport-https \ +ca-certificates gnupg lsb-release php8.3 \ +php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} \ +mariadb-server nginx redis-server tar unzip git${NC}" \ + "install_deps" \ + "install_menu" + ;; + 2) + confirm_dialog \ + "${BT_YE}You are going to install minimal set of dependencies, below is a \ +list of all packages that will be installed${NC}" \ + "${T_WH}software-properties-common curl apt-transport-https \ +ca-certificates gnupg lsb-release php8.3 \ +php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} \ +redis-server tar unzip git${NC}" \ + "install_deps \"true\"" \ + "install_menu" + ;; + b) main_menu ;; *) install_menu ;; esac } ####################################### -# Update menu in CLI-GUI mode +# Info & Help menu in CLI-GUI mode ####################################### -update_menu() { +info_help_menu() { local choice="" + local easter_egg="$1" logo - echo " This action cannot be undone, create backup of the database before \ -updating! It will also remove all installed themes and addons." - echo " Do you want to continue? (Y/n)" - read -rp " > " choice + logo_version + if [[ "$easter_egg" == "unlocked" ]]; then + echo -e " ${B_RE}${BT_WH} Easter egg unlocked!!! ${NC}" + echo -e " ${BT_WH}${TB}Congratulations, you found Easter egg. Here's a \ +well-deserved cake${NC}" + echo -e " ${BT_BLU}${TU}https://bit.ly/ctrlhelper_easter_egg_cake${NC}" + echo "" + fi + echo -e " ${BB_BLU}${BT_WH} Info ${NC}" + echo -e " ${BT_WH}This script is designed to simplify the installation of \ +dependencies and updating the CtrlPanel.${NC}" + echo -e " ${BT_WH}It can be executed both in CLI-GUI interface mode and in \ +CLI mode with an action argument specified${NC}" + echo "" + echo -e " ${BB_CY}${T_BL} Help ${NC}" + echo -e " ${BT_WH}Usage: ${T_CY}$0 ${BT_CY}[options]${NC}" + echo -e " ${BT_WH}Options:${NC}" + echo -e " ${BT_CY}--cli \ +${BT_WH}Use CLI mode. It does not have CLI-GUI interface, and all actions are \ +specified using action arguments${NC}" + echo -e " ${BT_CY}--cpgg-dir= \ +${BT_WH}Allows you to specify the root directory of the CtrlPanel${NC}" + echo -e " ${BT_CY}--force \ +${BT_WH}Performs an action without confirmation (applicable for --install and \ +--update arguments in CLI mode)${NC}" + echo -e " ${BT_CY}--install= \ +${BT_WH}Perform installation. Valid values are full or min${NC}" + echo -e " ${BT_CY}--update \ +${BT_WH}Perform an update${NC}" + echo -e " ${BT_CY}--help \ +${BT_WH}Display help message${NC}" + echo -e " ${BB_YE}${T_BL} Credits ${NC}" + echo -e " ${BT_WH}${TB}All code is written with \ +${NC}${BT_RE}♥${BT_WH}${TB} by MrWeez${NC}" + echo -e " ${BT_WH}GitHub: ${BT_BLU}${TU}https://github.com/MrWeez${NC}" + echo -e " ${BT_WH}Discord: ${T_CY}${TU}@mrweez_${NC}" + echo -e " ${BT_WH}E-mail: ${BT_YE}${TU}contact@mrweez.dev${NC}" + echo "" + echo " Press any key to return to the main menu" + read -rsn 1 -p " " choice case "$choice" in - y | Y) update ;; - n | N) main_menu ;; - *) update_menu ;; + ^) info_help_menu "unlocked" ;; + *) main_menu ;; esac } - -####################################### -# Info & Help menu in CLI-GUI mode -####################################### -info_help_menu() { - logo - echo " In dev" - sleep 3 - main_menu -} From 14450447f0e78f3988db6edd440934ddf465edd8 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 00:53:42 +0000 Subject: [PATCH 34/40] refactor: :art: Added quoting to variables --- bin/ctrlhelper.sh | 54 ++++++++++++------------- bin/ctrlhelper_sub_scripts/functions.sh | 22 +++++----- bin/ctrlhelper_sub_scripts/menus.sh | 46 ++++++++++----------- 3 files changed, 60 insertions(+), 62 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 17615a060..a0707ba53 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -64,21 +64,21 @@ set_cpgg_dir() { local is_cpgg_root="" local is_null="" - if [[ -z "$cli_mode" ]]; then - if [[ ! -d "$DEFAULT_DIR" ]] && [[ -z "$cpgg_dir" ]]; then + if [[ -z "${cli_mode}" ]]; then + if [[ ! -d "${DEFAULT_DIR}" ]] && [[ -z "${cpgg_dir}" ]]; then while true; do # Message that the user will see by default, if he specifies a # non-existent directory or not root CtrlPanel directory echo "" - if [[ -z "$is_exists" ]] && [[ -z "$is_cpgg_root" ]] || [[ "$is_null" == "true" ]]; then + if [[ -z "${is_exists}" ]] && [[ -z "${is_cpgg_root}" ]] || [[ "${is_null}" == "true" ]]; then echo -e " ${T_CY}Default directory wasn't found. Specify directory \ where your CtrlPanel is installed (e.g. /var/www/ctrlpanel)${NC}" - elif [[ $is_exists == false ]]; then - echo -e " ${T_CY}Directory ${BT_YE}$cpgg_dir${T_CY} doesn't exist. \ + elif [[ ${is_exists} == false ]]; then + echo -e " ${T_CY}Directory ${BT_YE}${cpgg_dir}${T_CY} doesn't exist. \ Specify directory where your CtrlPanel is installed \ (e.g. /var/www/ctrlpanel)${NC}" - elif [[ $is_cpgg_root == false ]]; then - echo -e " ${BT_YE}$cpgg_dir${T_CY} is not a root CtrlPanel \ + elif [[ ${is_cpgg_root} == false ]]; then + echo -e " ${BT_YE}${cpgg_dir}${T_CY} is not a root CtrlPanel \ directory. Specify directory where your CtrlPanel is installed \ (e.g. /var/www/ctrlpanel)${NC}" fi @@ -94,11 +94,11 @@ directory. Specify directory where your CtrlPanel is installed \ is_cpgg_root="" # Validation of directory specified by user - if [[ "$cpgg_dir" == "" ]]; then + if [[ "${cpgg_dir}" == "" ]]; then is_null="true" - elif [[ ! -d "$cpgg_dir" ]]; then + elif [[ ! -d "${cpgg_dir}" ]]; then is_exists="false" - elif [[ ! -f "$cpgg_dir/config/app.php" ]]; then + elif [[ ! -f "${cpgg_dir}/config/app.php" ]]; then is_cpgg_root="false" else break @@ -109,7 +109,7 @@ directory. Specify directory where your CtrlPanel is installed \ else # Error if default directory is not found and CtrlPanel root directory is # not specified when using the CLI mode - if [[ ! -d "$DEFAULT_DIR" ]] && [[ -z "$cpgg_dir" ]]; then + if [[ ! -d "${DEFAULT_DIR}" ]] && [[ -z "${cpgg_dir}" ]]; then error_out "Default directory wasn't found. Specify directory where your \ CtrlPanel is installed using ${BT_YE}--cpgg-dir${T_RE} argument" exit 1 @@ -135,10 +135,10 @@ while [[ $# -gt 0 ]]; do error_out "Argument ${BT_YE}--cpgg-dir${T_RE} can't be empty!" exit 1 elif [[ ! -d "$cpgg_dir" ]]; then - error_out "Directory ${BT_YE}$cpgg_dir${T_RE} doesn't exist." + error_out "Directory ${BT_YE}${cpgg_dir}${T_RE} doesn't exist." exit 1 - elif [[ ! -f "$cpgg_dir/config/app.php" ]]; then - error_out "${BT_YE}$cpgg_dir${T_RE} is not a root CtrlPanel directory." + elif [[ ! -f "${cpgg_dir}/config/app.php" ]]; then + error_out "${BT_YE}${cpgg_dir}${T_RE} is not a root CtrlPanel directory." exit 1 else continue @@ -149,7 +149,7 @@ while [[ $# -gt 0 ]]; do shift ;; --install=*) - if [[ -n "$update" ]]; then + if [[ -n "${update}" ]]; then error_out "You can't use ${BT_YE}--install${T_RE} with \ ${BT_YE}--update${T_RE} argument" exit 1 @@ -158,18 +158,18 @@ ${BT_YE}--update${T_RE} argument" install="${1#*=}" shift - if [[ "$install" == "" ]]; then + if [[ "${install}" == "" ]]; then error_out "Argument ${BT_YE}--install${T_RE} can't be empty!" exit 1 - elif [[ "$install" != "full" && "$install" != "min" ]]; then - error_out "Invalid option ${BT_YE}$install${T_RE} for \ + elif [[ "${install}" != "full" && "${install}" != "min" ]]; then + error_out "Invalid option ${BT_YE}${install}${T_RE} for \ ${BT_YE}--install${T_RE} argument. Valid values are only \ ${BT_YE}${TU}full${NC}${T_RE} or ${BT_YE}${TU}min" exit 1 fi ;; --update) - if [[ -n "$install" ]]; then + if [[ -n "${install}" ]]; then error_out "You can't use ${BT_YE}--update${T_RE} with \ ${BT_YE}--install${T_RE} argument" exit 1 @@ -234,11 +234,9 @@ support forum post on CtrlPanel's Discord server!" exit 1 } -if [[ -z "$cli_mode" ]]; then +if [[ -z "${cli_mode}" ]]; then save_terminal -fi -if [[ -z "$cli_mode" ]]; then get_version update_needed_checker @@ -246,8 +244,8 @@ if [[ -z "$cli_mode" ]]; then restore_terminal else - if [[ "$install" == "full" ]]; then - if [[ "$force" == "true" ]]; then + if [[ "${install}" == "full" ]]; then + if [[ "${force}" == "true" ]]; then install_deps else confirm_dialog \ @@ -263,8 +261,8 @@ mariadb-server nginx redis-server tar unzip git${NC}" \ "install_deps" \ "exit 0" fi - elif [[ "$install" == "min" ]]; then - if [[ "$force" == "true" ]]; then + elif [[ "${install}" == "min" ]]; then + if [[ "${force}" == "true" ]]; then install_deps "true" else confirm_dialog \ @@ -279,8 +277,8 @@ redis-server tar unzip git${NC}" \ "install_deps \"true\"" \ "exit 0" fi - elif [[ "$update" == "true" ]]; then - if [[ "$force" == "true" ]]; then + elif [[ "${update}" == "true" ]]; then + if [[ "${force}" == "true" ]]; then update else confirm_dialog \ diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index 47dd9914d..fadc75243 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -72,8 +72,8 @@ version_compare() { readonly latest_version # Break down versions into components - IFS='.' read -r -a current_parts <<<"$current_version" - IFS='.' read -r -a latest_parts <<<"$latest_version" + IFS='.' read -r -a current_parts <<<"${current_version}" + IFS='.' read -r -a latest_parts <<<"${latest_version}" # Add zeros to the shorter version (e.g. 0.10 => 0.10.0) while ((${#current_parts[@]} < ${#latest_parts[@]})); do @@ -106,7 +106,7 @@ version_compare() { # 2 if current version is newer than the latest one ####################################### update_needed_checker() { - is_update_needed=$(version_compare "$PANEL_VER" "$PANEL_LATEST_VER") + is_update_needed=$(version_compare "${PANEL_VER}" "${PANEL_LATEST_VER}") # shellcheck disable=SC2034 readonly is_update_needed } @@ -147,11 +147,11 @@ install_deps() { | sudo tee /etc/apt/sources.list.d/redis.list fi - if [[ -z "$minimal" ]]; then + if [[ -z "${minimal}" ]]; then info_out "Adding MariaDB repository" curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash - elif [[ -n "$minimal" && "$minimal" != "true" ]]; then - error_out "Invalid argument $minimal for install_deps function. Please, report to developers!" + elif [[ -n "${minimal}" && "${minimal}" != "true" ]]; then + error_out "Invalid argument ${minimal} for install_deps function. Please, report to developers!" exit 1 fi @@ -159,12 +159,12 @@ install_deps() { sudo apt update info_out "Installing dependencies" - if [[ "$minimal" ]]; then + if [[ "${minimal}" ]]; then sudo apt -y -qq install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} redis-server tar unzip git - elif [[ -z "$minimal" ]]; then + elif [[ -z "${minimal}" ]]; then sudo apt -y -qq install php8.3 php8.3-{cli,gd,mysql,pdo,mbstring,tokenizer,bcmath,xml,fpm,curl,zip,intl,redis} mariadb-server nginx redis-server tar unzip git else - error_out "Invalid argument $minimal for install_deps function. Please, report to developers!" + error_out "Invalid argument ${minimal} for install_deps function. Please, report to developers!" exit 1 fi @@ -174,7 +174,7 @@ install_deps() { info_out "Installing Composer dependencies to the CtrlPanel" sudo COMPOSER_ALLOW_SUPERUSER=1 composer install --no-dev --optimize-autoloader --no-interaction - if [[ -z "$cli_mode" ]]; then + if [[ -z "${cli_mode}" ]]; then echo "" echo " Installation finished. Press any key to exit" read -rsn 1 -p " " @@ -223,7 +223,7 @@ update() { info_out "Disabling maintenance mode" sudo php "${cpgg_dir:-$DEFAULT_DIR}"/artisan up - if [[ -z "$cli_mode" ]]; then + if [[ -z "${cli_mode}" ]]; then echo "" echo " Update finished. Press any key to exit" read -rsn 1 -p " " diff --git a/bin/ctrlhelper_sub_scripts/menus.sh b/bin/ctrlhelper_sub_scripts/menus.sh index 8c0e4f7ef..2c4cc23ff 100644 --- a/bin/ctrlhelper_sub_scripts/menus.sh +++ b/bin/ctrlhelper_sub_scripts/menus.sh @@ -13,7 +13,7 @@ # Logo to display in the CLI-GUI ####################################### logo() { - if [[ -z "$cli_mode" ]]; then + if [[ -z "${cli_mode}" ]]; then clear fi echo -e "${BT_CY} ________ ______ __ ${NC}" @@ -32,8 +32,8 @@ logo() { # PANEL_VER ####################################### logo_version() { - echo -e " ${BT_YE}Script version:${NC}${BT_WH} $SCRIPT_VER${NC}" - echo -e " ${BT_YE}CtrlPanel version:${NC}${BT_WH} $PANEL_VER${NC}" + echo -e " ${BT_YE}Script version:${NC}${BT_WH} ${SCRIPT_VER}${NC}" + echo -e " ${BT_YE}CtrlPanel version:${NC}${BT_WH} ${PANEL_VER}${NC}" echo "" } @@ -45,15 +45,15 @@ logo_version() { ####################################### logo_message() { # shellcheck disable=SC2154 - if [[ $is_update_needed == 0 ]]; then + if [[ ${is_update_needed} == 0 ]]; then echo -e " ${CHECK} ${BT_GR}You are using the latest version! \ No update required.${NC}" echo "" - elif [[ $is_update_needed == 1 ]]; then + elif [[ ${is_update_needed} == 1 ]]; then echo -e " ${WARN} ${BT_RE}New version available! You can update right now \ by selecting ${BT_YE}${TU}Update${NC} ${BT_RE}option.${NC}" echo "" - elif [[ $is_update_needed == 2 ]]; then + elif [[ ${is_update_needed} == 2 ]]; then echo -e " ${CHECK} ${BT_GR}You are using a newer version! Most likely you \ have a development version installed.${NC}" echo "" @@ -80,28 +80,28 @@ confirm_dialog() { local previous_choice="$6" logo - echo -e " $message_line1" - if [[ -n "$message_line2" ]]; then - echo -e " $message_line2" + echo -e " ${message_line1}" + if [[ -n "${message_line2}" ]]; then + echo -e " ${message_line2}" fi echo "" echo -e " ${BT_WH}Continue? (Y/n)${NC}" - if [[ "$unknown_choice" == "true" ]]; then - echo -e " ${T_RE}Unknown choice ${BT_YE}${TU}$previous_choice${NC}" + if [[ "${unknown_choice}" == "true" ]]; then + echo -e " ${T_RE}Unknown choice ${BT_YE}${TU}${previous_choice}${NC}" fi read -rp " > " choice - case "$choice" in - y | Y) $action ;; - n | N) $exit_action ;; + case "${choice}" in + y | Y) ${action} ;; + n | N) ${exit_action} ;; *) confirm_dialog \ - "$message_line1" \ - "$message_line2" \ - "$action" \ - "$exit_action" \ + "${message_line1}" \ + "${message_line2}" \ + "${action}" \ + "${exit_action}" \ "true" \ - "$choice" + "${choice}" ;; esac } @@ -123,7 +123,7 @@ main_menu() { echo "" read -rp " > " choice - case $choice in + case ${choice} in 1) install_menu ;; 2) confirm_dialog "${B_RE}${BT_WH} This action cannot be undone, create \ @@ -157,7 +157,7 @@ configure nginx yourself.${NC}" echo "" read -rp " > " choice - case "$choice" in + case "${choice}" in 1) confirm_dialog \ "${BT_YE}You are going to install full set of dependencies, below is a \ @@ -194,7 +194,7 @@ info_help_menu() { logo logo_version - if [[ "$easter_egg" == "unlocked" ]]; then + if [[ "${easter_egg}" == "unlocked" ]]; then echo -e " ${B_RE}${BT_WH} Easter egg unlocked!!! ${NC}" echo -e " ${BT_WH}${TB}Congratulations, you found Easter egg. Here's a \ well-deserved cake${NC}" @@ -234,7 +234,7 @@ ${NC}${BT_RE}♥${BT_WH}${TB} by MrWeez${NC}" echo " Press any key to return to the main menu" read -rsn 1 -p " " choice - case "$choice" in + case "${choice}" in ^) info_help_menu "unlocked" ;; *) main_menu ;; esac From 8b1a13aabc13503ae0c6426009e415ba94071879 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 12:28:19 +0000 Subject: [PATCH 35/40] style: :lipstick: Message about the end of the installation/update is now more noticeable --- bin/ctrlhelper.sh | 1 + bin/ctrlhelper_sub_scripts/functions.sh | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index a0707ba53..61fdb4f08 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -30,6 +30,7 @@ readonly BT_WH="\033[97m" # Bright white text readonly B_RE="\033[41m" # Red background +readonly BB_GR="\033[102m" # Green bright background readonly BB_YE="\033[103m" # Yellow bright background readonly BB_BLU="\033[104m" # Blue bright background readonly BB_CY="\033[106m" # Cyan bright background diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index fadc75243..a04a4d794 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -176,7 +176,7 @@ install_deps() { if [[ -z "${cli_mode}" ]]; then echo "" - echo " Installation finished. Press any key to exit" + echo -e " ${BB_GR}${T_BL} Done! ${NC} ${BT_GR}Installation finished. Press any key to exit${NC}" read -rsn 1 -p " " fi } @@ -225,7 +225,7 @@ update() { if [[ -z "${cli_mode}" ]]; then echo "" - echo " Update finished. Press any key to exit" + echo -e " ${BB_GR}${T_BL} Done! ${NC} ${BT_GR}Update finished. Press any key to exit${NC}" read -rsn 1 -p " " fi } \ No newline at end of file From 00b978d72b2bfb89a38b1f608af1cae2ecda2008 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 18:53:32 +0000 Subject: [PATCH 36/40] fix: :bug: The repository does not have a Release file Fixed "The repository 'https://packages.sury.org/php focal Release' does not have a Release file." on Ubuntu distros --- bin/ctrlhelper.sh | 8 ++-- bin/ctrlhelper_sub_scripts/functions.sh | 60 ++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 61fdb4f08..2d93aec25 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -44,6 +44,8 @@ readonly TU="\033[4m" # Underline ####################################### readonly CHECK="${BT_YE}${TB}(${BT_GR}✓${BT_YE})${NC}" readonly WARN="${BT_YE}${TB}(${BT_RE}!!${BT_YE})${NC}" +readonly INFO="${BB_BLU}${BT_WH} INFO ${NC}" +readonly ERROR="${B_RE}${BT_WH} ERROR ${NC}" ####################################### # Return an error message in STDERR @@ -51,7 +53,7 @@ readonly WARN="${BT_YE}${TB}(${BT_RE}!!${BT_YE})${NC}" # Error message ####################################### error_out() { - echo -e " ${B_RE}${BT_WH} ERROR ${NC} ${T_RE}$*${NC}" >&2 + echo -e " ${ERROR} ${T_RE}$*${NC}" >&2 } ####################################### @@ -132,10 +134,10 @@ while [[ $# -gt 0 ]]; do cpgg_dir="${cpgg_dir%/}" shift - if [[ "$cpgg_dir" == "" ]]; then + if [[ "${cpgg_dir}" == "" ]]; then error_out "Argument ${BT_YE}--cpgg-dir${T_RE} can't be empty!" exit 1 - elif [[ ! -d "$cpgg_dir" ]]; then + elif [[ ! -d "${cpgg_dir}" ]]; then error_out "Directory ${BT_YE}${cpgg_dir}${T_RE} doesn't exist." exit 1 elif [[ ! -f "${cpgg_dir}/config/app.php" ]]; then diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index a04a4d794..759be5872 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -31,7 +31,7 @@ restore_terminal() { # Info message ####################################### info_out() { - echo -e " ${BB_BLU}${BT_WH} INFO ${NC} ${BT_WH}$*${NC}" + echo -e " ${INFO} ${BT_WH}$*${NC}" } ####################################### @@ -111,6 +111,45 @@ update_needed_checker() { readonly is_update_needed } +####################################### +# +####################################### +check_distro() { + local choice + local unknown_choice="$1" + local previous_choice="$2" + # distro=$(lsb_release -is) + distro="Mint" + distro="${distro,,}" + + if [[ "${distro}" != "debian" && "${distro}" != "ubuntu" ]]; then + logo + + error_out "Your OS is not supported! You can continue the installation in compatibility mode, but in this case it is not guaranteed that all packages will be installed successfully" + echo -e "" + echo -e " So that we can add support for your OS, please let us know the information below" + echo -e " Detected OS: ${distro}" + echo -e " Detected OS (full): $(lsb_release -sd)" + echo "" + echo -e " ${BB_CY} ${T_BL}Select an option: ${NC}" + echo -e " ${BT_WH}1. Continue in Debian compatibility mode${NC}" + echo -e " ${BT_WH}2. Continue in Ubuntu compatibility mode${NC}" + echo -e " ${BT_WH}q. Quit${NC}" + echo "" + if [[ "${unknown_choice}" == "true" ]]; then + echo -e " ${T_RE}Unknown choice ${BT_YE}${TU}${previous_choice}${NC}" + fi + read -rp " > " choice + + case ${choice} in + 1) distro="debian" ;; + 2) distro="ubuntu" ;; + q) restore_terminal ;; + *) check_distro "true" "${choice}" ;; + esac + fi +} + ####################################### # Installing dependencies # Globals: @@ -130,14 +169,21 @@ install_deps() { info_out "Adding \"add-apt-repository\" command and additional dependencies" sudo apt -y -qq install software-properties-common curl apt-transport-https ca-certificates gnupg lsb-release - if [[ ! -f "/etc/apt/trusted.gpg.d/deb.sury.org-php.gpg" ]]; then - info_out "Adding PHP repository keyring" - sudo curl -sSLo /etc/apt/trusted.gpg.d/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg - fi + check_distro - if [[ ! -f "/etc/apt/sources.list.d/deb.sury.org-php.list" ]]; then + if [[ "$distro" == "debian" ]]; then + if [[ ! -f "/usr/share/keyrings/deb.sury.org-php.gpg" ]]; then + info_out "Adding PHP repository keyring" + sudo curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg + fi + + if [[ ! -f "/etc/apt/sources.list.d/deb.sury.org-php.list" ]]; then + info_out "Adding PHP repository" + sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/deb.sury.org-php.list' + fi + elif [[ "$distro" == "ubuntu" ]]; then info_out "Adding PHP repository" - sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/deb.sury.org-php.list' + LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/php fi if [[ ! -f "/usr/share/keyrings/redis-archive-keyring.gpg" ]]; then From 4611e3d1472f93f3af3c3733d08637bc77939fdf Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 18:59:14 +0000 Subject: [PATCH 37/40] fix: :pencil2: Forgotten test variable has been deleted --- bin/ctrlhelper_sub_scripts/functions.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index 759be5872..3a6b85bb5 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -118,8 +118,7 @@ check_distro() { local choice local unknown_choice="$1" local previous_choice="$2" - # distro=$(lsb_release -is) - distro="Mint" + distro=$(lsb_release -is) distro="${distro,,}" if [[ "${distro}" != "debian" && "${distro}" != "ubuntu" ]]; then From 1d12c1e06ebd3014f506617b6bf820c520911f88 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 20:48:54 +0000 Subject: [PATCH 38/40] fix: :bug: MariaDB repository will no longer be installed on Ubuntu 24.04 --- bin/ctrlhelper_sub_scripts/functions.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index 3a6b85bb5..038d473e0 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -192,12 +192,14 @@ install_deps() { | sudo tee /etc/apt/sources.list.d/redis.list fi - if [[ -z "${minimal}" ]]; then - info_out "Adding MariaDB repository" - curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash - elif [[ -n "${minimal}" && "${minimal}" != "true" ]]; then - error_out "Invalid argument ${minimal} for install_deps function. Please, report to developers!" - exit 1 + if [[ "$distro" == "ubuntu" && "$(grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | tr -d '"')" != "24.04" ]]; then + if [[ -z "${minimal}" ]]; then + info_out "Adding MariaDB repository" + curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash + elif [[ -n "${minimal}" && "${minimal}" != "true" ]]; then + error_out "Invalid argument ${minimal} for install_deps function. Please, report to developers!" + exit 1 + fi fi info_out "Running \"apt update\"" From b545ecd606687a523fbe8e7f8f872fc416d7adc4 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Sat, 3 Aug 2024 23:25:56 +0000 Subject: [PATCH 39/40] chore: :bookmark: CtrlHelper 1.0 release is here! --- bin/ctrlhelper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index 2d93aec25..b2399118c 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -10,7 +10,7 @@ # Email: contact@mrweez.dev # shellcheck disable=SC2034 -readonly SCRIPT_VER="0.6.3" +readonly SCRIPT_VER="1.0.0" readonly DEFAULT_DIR="/var/www/ctrlpanel" ####################################### From e065aa87c9949187c34ec34620d877c1983ae8e4 Mon Sep 17 00:00:00 2001 From: MrWeez Date: Wed, 7 Aug 2024 22:58:28 +0000 Subject: [PATCH 40/40] chore: :fire: removed credits sections --- bin/ctrlhelper.sh | 6 ------ bin/ctrlhelper_sub_scripts/functions.sh | 6 ------ bin/ctrlhelper_sub_scripts/menus.sh | 13 ++----------- 3 files changed, 2 insertions(+), 23 deletions(-) diff --git a/bin/ctrlhelper.sh b/bin/ctrlhelper.sh index b2399118c..d0169fae2 100644 --- a/bin/ctrlhelper.sh +++ b/bin/ctrlhelper.sh @@ -2,12 +2,6 @@ # # This script is designed to facilitate the tasks of # installing dependencies and updating CtrlPanel via CLI -# -# Made with love by MrWeez -# Contact me -# GitHub: https://github.com/MrWeez -# Discord: @mrweez_ -# Email: contact@mrweez.dev # shellcheck disable=SC2034 readonly SCRIPT_VER="1.0.0" diff --git a/bin/ctrlhelper_sub_scripts/functions.sh b/bin/ctrlhelper_sub_scripts/functions.sh index 038d473e0..b90a0ac2a 100644 --- a/bin/ctrlhelper_sub_scripts/functions.sh +++ b/bin/ctrlhelper_sub_scripts/functions.sh @@ -2,12 +2,6 @@ # # The auxiliary file for CtrlHelper script. # Contains important functions for the main script to work -# -# Made with love by MrWeez -# Contact me -# GitHub: https://github.com/MrWeez -# Discord: @mrweez_ -# Email: contact@mrweez.dev ####################################### # Terminal save function, for recovery after exiting diff --git a/bin/ctrlhelper_sub_scripts/menus.sh b/bin/ctrlhelper_sub_scripts/menus.sh index 2c4cc23ff..7000778e3 100644 --- a/bin/ctrlhelper_sub_scripts/menus.sh +++ b/bin/ctrlhelper_sub_scripts/menus.sh @@ -2,12 +2,6 @@ # # The auxiliary file for CtrlHelper script. # Contains the CLI-GUI parts of the interface -# -# Made with love by MrWeez -# Contact me -# GitHub: https://github.com/MrWeez -# Discord: @mrweez_ -# Email: contact@mrweez.dev ####################################### # Logo to display in the CLI-GUI @@ -225,11 +219,8 @@ ${BT_WH}Perform an update${NC}" echo -e " ${BT_CY}--help \ ${BT_WH}Display help message${NC}" echo -e " ${BB_YE}${T_BL} Credits ${NC}" - echo -e " ${BT_WH}${TB}All code is written with \ -${NC}${BT_RE}♥${BT_WH}${TB} by MrWeez${NC}" - echo -e " ${BT_WH}GitHub: ${BT_BLU}${TU}https://github.com/MrWeez${NC}" - echo -e " ${BT_WH}Discord: ${T_CY}${TU}@mrweez_${NC}" - echo -e " ${BT_WH}E-mail: ${BT_YE}${TU}contact@mrweez.dev${NC}" + echo -e " ${BT_WH}${TB}Made by MrWeez with \ +${NC}${BT_RE}♥${NC}" echo "" echo " Press any key to return to the main menu"