diff --git a/mods/scripts/apps_interface.sh b/mods/scripts/apps_interface.sh index f37e2e8b91..72a54206fc 100644 --- a/mods/scripts/apps_interface.sh +++ b/mods/scripts/apps_interface.sh @@ -17,10 +17,21 @@ config_type=$2 # 'personal' for personal configurations, 'official' for officia # Function: check_deployment_status check_deployment_status() { + # Load the configuration file to get the port_number + if [[ "$config_type" == "personal" ]]; then + config_file="/pg/personal_configs/${app_name}.cfg" + else + config_file="/pg/config/${app_name}.cfg" + fi + + if [[ -f "$config_file" ]]; then + source "$config_file" + fi + local container_status=$(docker ps --filter "name=^/${app_name}$" --format "{{.Names}}") if [[ "$container_status" == "$app_name" ]]; then - echo -e "${GREEN}[Deployed]${NC} $app_name" + echo -e "${GREEN}[Deployed]${NC} $app_name - Port: $port_number" else echo -e "${RED}[Not Deployed]${NC} $app_name" fi diff --git a/mods/scripts/cloud_hetzner.sh b/mods/scripts/cloud_hetzner.sh new file mode 100644 index 0000000000..47cb74f937 --- /dev/null +++ b/mods/scripts/cloud_hetzner.sh @@ -0,0 +1,256 @@ +#!/bin/bash + +# Title: PG Cloud Hetzner Manager Script +# Description: Script to manage Hetzner Cloud servers using the new hcloud CLI + +# Configuration file path for storing the token +TOKEN_FILE="/pg/hcloud/.token" + +# ANSI color codes +CYAN="\033[0;36m" +RED="\033[0;31m" +ORANGE="\033[0;33m" +WHITE="\033[1;37m" +BOLD="\033[1m" +NC="\033[0m" # No color + +# Function to install hcloud CLI if missing +install_hcloud() { + if ! command -v hcloud &> /dev/null; then + echo "Installing Hetzner CLI..." + curl -o /tmp/hcloud.tar.gz -L https://github.com/hetznercloud/cli/releases/latest/download/hcloud-linux-amd64.tar.gz + tar -xzf /tmp/hcloud.tar.gz -C /tmp + mv /tmp/hcloud /usr/local/bin/hcloud + chmod +x /usr/local/bin/hcloud + rm -rf /tmp/hcloud.tar.gz + fi +} + +# Function to validate the Hetzner API token +validate_hcloud_token() { + if [[ -f "$TOKEN_FILE" ]]; then + export HCLOUD_TOKEN=$(cat "$TOKEN_FILE") + if ! hcloud server list &> /dev/null; then + echo -e "\n${RED}WARNING! The existing token is invalid.${NC}" + change_token + fi + else + change_token + fi +} + +# Function to change or set the hcloud API token +change_token() { + echo -e "\nPlease provide a valid Hetzner API token." + echo -e "\nFollow these steps:" + echo -e "1. Activate a Hetzner Cloud Account" + echo -e "2. Create a Project" + echo -e "3. Go to Security (left-hand side)" + echo -e "4. Select and Click API Tokens" + echo -e "5. Create a Token and Save It\n" + echo -e "Type 'exit' to cancel and return to the main menu.\n" + read -p 'Paste your API token here (or type exit): ' api_token "$TOKEN_FILE" + export HCLOUD_TOKEN="$api_token" + + # Verify the token + if ! hcloud server list &> /dev/null; then + echo -e "\n${RED}Invalid token provided. Please try again.${NC}\n" + rm -f "$TOKEN_FILE" + change_token + else + echo -e "\n${WHITE}Token successfully configured.${NC}" + fi +} + +# Function to ensure necessary directories exist +setup_directories() { + mkdir -p /pg/hcloud +} + +# Main menu function +main_menu() { + clear + echo -e "${CYAN}${BOLD}PG: Hetzner Cloud Manager${NC}\n" + echo -e "[1] Deploy a New Server" + echo -e "[2] Destroy a Server" + echo -e "[A] List Servers" + echo -e "[B] Show Initial Passwords" + echo -e "[T] Change API Token" + echo -e "[Z] Exit\n" + read -p 'Select an option: ' option "/pg/hcloud/$server_name.info" + echo -e "\nServer information:" + cat "/pg/hcloud/$server_name.info" + read -p 'Press [ENTER] to continue...' 1 {print $2}' + echo "" + read -p 'Press [ENTER] to continue...' 1 {print $2}' + echo "" + read -p 'Enter server name to destroy or [Z] to exit: ' server_name