Skip to content

Commit

Permalink
Merge pull request #956 from plexguide/configfix
Browse files Browse the repository at this point in the history
Configuration Fixes and Sudo Fixes
  • Loading branch information
Admin9705 authored Sep 17, 2024
2 parents a50137b + 4c1dedc commit 0192e0e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 63 deletions.
31 changes: 29 additions & 2 deletions mods/scripts/apps/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down
61 changes: 39 additions & 22 deletions mods/scripts/apps/stage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,53 @@ 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
}

# Function to list all available apps, excluding those already running in Docker
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
Expand All @@ -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
Expand All @@ -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..."
Expand All @@ -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}"
Expand All @@ -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
Expand Down
35 changes: 0 additions & 35 deletions mods/scripts/apps/support.sh

This file was deleted.

46 changes: 42 additions & 4 deletions mods/scripts/menu.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
#!/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"
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
Expand Down Expand Up @@ -130,4 +168,4 @@ main_menu() {

# Run the script
load_config
main_menu
main_menu

0 comments on commit 0192e0e

Please sign in to comment.