diff --git a/mods/scripts/apps/deploy.sh b/mods/scripts/apps/deploy.sh index 84b15dbec..a99bbe4ff 100644 --- a/mods/scripts/apps/deploy.sh +++ b/mods/scripts/apps/deploy.sh @@ -32,6 +32,33 @@ check_and_create_network() { fi } +# Function to source configuration and functions for the app +appsourcing() { + if [[ "$script_type" == "personal" ]]; then + source "/pg/personal_configs/${app_name}.cfg" + source "/pg/p_apps/${app_name}/${app_name}.functions" 2>/dev/null + else + source "/pg/config/${app_name}.cfg" + source "/pg/apps/${app_name}/${app_name}.functions" 2>/dev/null + fi +} + +# Function to source configuration from the config file +#configsource() { +# if [[ "$config_type" == "personal" ]]; then +# config_path="/pg/personal_configs/${app_name}.cfg" +# else +# config_path="/pg/config/${app_name}.cfg" +# fi +# +# if [ -f "$config_path" ]; then +# source "$config_path" +# else +# echo "Config file for ${app_name} not found at ${config_path}." +# exit 1 +# fi +#} + # Function: Deploys / Redploys App redeploy_app() { # Check if lspci is installed; detect NVIDIA graphics cards @@ -48,10 +75,10 @@ redeploy_app() { # Determine which support script to source if [[ "$script_type" == "personal" ]]; then - source /pg/scripts/apps/support.sh "$app_name" "$script_type" && appsourcing + appsourcing source "/pg/p_apps/$app_name.app" elif [[ "$script_type" == "official" ]]; then - source /pg/scripts/apps/support.sh "$app_name" "$script_type" && appsourcing + appsourcing source "/pg/apps/$app_name.app" else echo -e "${RED}Invalid script type specified. Use 'personal' or 'official'.${NC}" diff --git a/mods/scripts/apps/stage.sh b/mods/scripts/apps/stage.sh index 200cffb56..4b4a47c6c 100644 --- a/mods/scripts/apps/stage.sh +++ b/mods/scripts/apps/stage.sh @@ -19,12 +19,30 @@ MAX_LINE_LENGTH=72 # Arguments deployment_type=$1 # 'personal' for personal deployment, 'official' for official deployment +# Base directory (adjust this to your actual base directory) +BASE_DIR="/pg" + +# Function to safely check if a directory exists +directory_exists() { + if [[ -d "$1" ]]; then + return 0 + else + return 1 + fi +} + # Function to create the appropriate apps directory if it doesn't exist create_apps_directory() { + local target_dir if [[ "$deployment_type" == "personal" ]]; then - [[ ! -d "/pg/p_apps" ]] && mkdir -p /pg/p_apps + target_dir="$BASE_DIR/p_apps" else - [[ ! -d "/pg/apps" ]] && mkdir -p /pg/apps + target_dir="$BASE_DIR/apps" + fi + + if ! directory_exists "$target_dir"; then + echo "Error: Directory $target_dir does not exist and cannot be created." + return 1 fi } @@ -32,18 +50,22 @@ create_apps_directory() { list_available_apps() { local app_dir if [[ "$deployment_type" == "personal" ]]; then - app_dir="/pg/p_apps" + app_dir="$BASE_DIR/p_apps" else - app_dir="/pg/apps" + app_dir="$BASE_DIR/apps" + fi + + if ! directory_exists "$app_dir"; then + echo "Error: App directory $app_dir does not exist." + return 1 fi - local all_apps=$(find "$app_dir" -maxdepth 1 -name "*.app" -type f -exec basename {} .app \; | sort) - local running_apps=$(docker ps --format '{{.Names}}' | sort) + local all_apps=$(find "$app_dir" -maxdepth 1 -name "*.app" -type f -exec basename {} .app \; 2>/dev/null | sort) + local running_apps=$(docker ps --format '{{.Names}}' 2>/dev/null | sort) local available_apps=() for app in $all_apps; do - # Only exclude those that are already running - if ! echo "$running_apps" | grep -i -w "$app" >/dev/null; then + if ! echo "$running_apps" | grep -q -i -w "$app"; then available_apps+=("$app") fi done @@ -59,20 +81,18 @@ display_available_apps() { for app in "${apps_list[@]}"; do local app_length=${#app} - local new_length=$((current_length + app_length + 1)) # +1 for the space + local new_length=$((current_length + app_length + 1)) - # If adding the app would exceed the maximum length, start a new line if [[ $new_length -gt $TERMINAL_WIDTH ]]; then echo "$current_line" current_line="$app " - current_length=$((app_length + 1)) # Reset with the new app and a space + current_length=$((app_length + 1)) else current_line+="$app " current_length=$new_length fi done - # Print the last line if it has content if [[ -n $current_line ]]; then echo "$current_line" fi @@ -81,13 +101,10 @@ display_available_apps() { # Function to deploy the selected app deploy_app() { local app_name=$1 - local app_script - app_script="/pg/scripts/apps/interface.sh" + local app_script="$BASE_DIR/scripts/apps/interface.sh" - # Ensure the app script exists before proceeding if [[ -f "$app_script" ]]; then - # Execute the apps_interface.sh script with the app name as an argument - bash /pg/scripts/apps/interface.sh "$app_name" "$deployment_type" + bash "$app_script" "$app_name" "$deployment_type" else echo "Error: Interface script $app_script not found!" read -p "Press Enter to continue..." @@ -99,13 +116,15 @@ deployment_function() { while true; do clear - create_apps_directory + if ! create_apps_directory; then + echo "Error: Unable to access or create necessary directories." + exit 1 + fi - # Get the list of available apps APP_LIST=($(list_available_apps)) echo -e "${RED}PG: Deployable Apps${NC}" - echo "" # Blank line for separation + echo "" if [[ ${#APP_LIST[@]} -eq 0 ]]; then echo -e "${ORANGE}No More Apps To Deploy${NC}" @@ -114,12 +133,10 @@ deployment_function() { fi echo "════════════════════════════════════════════════════════════════════════════════" - # Prompt the user to enter an app name or exit read -p "$(echo -e "Type [${RED}App${NC}] to Deploy or [${GREEN}Z${NC}] to Exit: ")" app_choice app_choice=$(echo "$app_choice" | tr '[:upper:]' '[:lower:]') - # Check if the user input is "z" if [[ "$app_choice" == "z" ]]; then exit 0 elif [[ " ${APP_LIST[@]} " =~ " $app_choice " ]]; then diff --git a/mods/scripts/apps/support.sh b/mods/scripts/apps/support.sh deleted file mode 100644 index c5cd3f57b..000000000 --- a/mods/scripts/apps/support.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -# Combined script for official and personal app support - -# Arguments -app_name=$1 -config_type=$2 # 'personal' for personal apps, 'official' for official apps - -# Function to source configuration and functions for the app -appsourcing() { - if [[ "$config_type" == "personal" ]]; then - source "/pg/personal_configs/${app_name}.cfg" - source "/pg/p_apps/${app_name}/${app_name}.functions" 2>/dev/null - else - source "/pg/config/${app_name}.cfg" - source "/pg/apps/${app_name}/${app_name}.functions" 2>/dev/null - fi -} - -# Function to source configuration from the config file -configsource() { - local app_name="$1" - if [[ "$config_type" == "personal" ]]; then - config_path="/pg/personal_configs/${app_name}.cfg" - else - config_path="/pg/config/${app_name}.cfg" - fi - - if [ -f "$config_path" ]; then - source "$config_path" - else - echo "Config file for ${app_name} not found at ${config_path}." - exit 1 - fi -} \ No newline at end of file diff --git a/mods/scripts/menu.sh b/mods/scripts/menu.sh index 392e3b213..53d76e52b 100644 --- a/mods/scripts/menu.sh +++ b/mods/scripts/menu.sh @@ -1,10 +1,49 @@ #!/bin/bash +# ANSI color codes +RED="\033[0;31m" +NC="\033[0m" # No color + +# Get the username of the user with UID 1000 +REQUIRED_USER=$(getent passwd 1000 | cut -d: -f1) + +# Function to check if the script is being run with sudo +is_sudo() { + if [ -n "$SUDO_USER" ]; then + return 0 # True, it's being run with sudo + else + return 1 # False, it's not being run with sudo + fi +} + +# Enhanced security check +if [[ -z "$SUDO_USER" ]]; then + echo -e "${RED}WARNING: This script must be run with sudo.${NC}" + echo -e "${RED}Please run it as 'sudo -u $REQUIRED_USER $0 $@'${NC}" + read -p "Press [ENTER] to acknowledge" + bash /pg/installer/menu_exit.sh + exit 1 +elif [[ $SUDO_UID -ne 1000 ]] || [[ $SUDO_GID -ne 1000 ]]; then + echo -e "${RED}WARNING: This script can only be run by the user '$REQUIRED_USER' (UID 1000 and GID 1000) using sudo.${NC}" + echo -e "${RED}Please run it as 'sudo -u $REQUIRED_USER $0 $@'${NC}" + read -p "Press [ENTER] to acknowledge" + bash /pg/installer/menu_exit.sh + exit 1 +elif [[ $EUID -ne 0 ]]; then + echo -e "${RED}WARNING: This script must be run with sudo privileges.${NC}" + echo -e "${RED}Please run it as 'sudo -u $REQUIRED_USER $0 $@'${NC}" + read -p "Press [ENTER] to acknowledge" + bash /pg/installer/menu_exit.sh + exit 1 +fi + +# If we've made it here, the user is either UID 1000 or is UID 1000 using sudo +echo "Security check passed. Proceeding with the script..." + # Configuration file path CONFIG_FILE="/pg/config/config.cfg" -# ANSI color codes -RED="\033[0;31m" +# Additional ANSI color codes ORANGE="\033[0;33m" YELLOW="\033[1;33m" GREEN="\033[0;32m" @@ -12,7 +51,6 @@ CYAN="\033[0;36m" BLUE="\033[0;34m" PURPLE="\033[0;35m" BOLD="\033[1m" -NC="\033[0m" # No color # Clear the screen at the start clear @@ -130,4 +168,4 @@ main_menu() { # Run the script load_config -main_menu +main_menu \ No newline at end of file