From 39eefd9d8f233aeea3060af41eac188ba7bbcee9 Mon Sep 17 00:00:00 2001 From: Christopher <1289128+dragonfire1119@users.noreply.github.com> Date: Wed, 23 Oct 2024 13:35:34 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=F0=9F=94=8D=20feat(casaos-healthcheck?= =?UTF-8?q?):=20Improve=20root=20and=20sudo=20privileges=20checks=20(#14)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds two new functions, `check_root_privileges()` and `check_sudo_privileges()`, to handle the cases where the script is run with insufficient privileges. The script now provides clearer instructions and prompts the user to either continue with the limited functionality or exit and run the script with the required privileges. --- casaos-healthcheck/run.sh | 47 ++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/casaos-healthcheck/run.sh b/casaos-healthcheck/run.sh index 672f474..bc554e5 100644 --- a/casaos-healthcheck/run.sh +++ b/casaos-healthcheck/run.sh @@ -326,18 +326,43 @@ check_docker_ports() { echo " You can do this by running 'sudo ufw status verbose'" } -# Check for sudo privileges -if [ "$EUID" -ne 0 ]; then - print_color "0;33" "${WARNING_MARK} This script may require sudo privileges for full functionality." - echo "Some checks might fail or provide incomplete information without sudo." - echo "Consider running the script with sudo if you encounter permission-related issues." - echo - read -p "Do you want to continue without sudo? (y/N) " -n 1 -r - echo - if [[ ! $REPLY =~ ^[Yy]$ ]]; then - echo "Exiting. Please run the script again with sudo." - exit 1 +check_root_privileges() { + if [ "$EUID" -ne 0 ]; then + print_color "0;33" "${WARNING_MARK} This script requires root privileges for full functionality." + echo "Some checks might fail or provide incomplete information without root access." + echo "Consider running the script with sudo if you encounter permission-related issues." + echo + read -p "Do you want to continue without root privileges? (y/N) " -n 1 -r + echo + REPLY=${REPLY,,} # convert to lowercase + if [[ $REPLY != "y" ]]; then + echo "Exiting. Please run the script again with sudo." + exit 1 + fi fi +} + +check_sudo_privileges() { + if ! sudo -n true 2>/dev/null; then + print_color "0;33" "${WARNING_MARK} You are running as root, but might not have full sudo privileges." + echo "Some checks might still fail or provide incomplete information." + echo + read -p "Do you want to continue? (y/N) " -n 1 -r + echo + REPLY=${REPLY,,} # convert to lowercase + if [[ $REPLY != "y" ]]; then + echo "Exiting. Please ensure you have full sudo privileges and run the script again." + exit 1 + fi + fi +} + +# Main script flow +check_root_privileges + +# If running as root, optionally check for sudo +if [ "$EUID" -eq 0 ]; then + check_sudo_privileges fi # Main execution