#!/usr/bin/env bash variables() { NSAPP=$(echo "$APP" | tr '[:upper:]' '[:lower:]' | tr -d ' ') var_install="${NSAPP}-install" INTEGER='^[0-9]+([.][0-9]+)?$' PVEHOST_NAME=$(hostname) METHOD="${METHOD:-}" WHIPTITLE="Dynasty Revolution Provisioning" if declare -f parse_dev_mode >/dev/null; then parse_dev_mode "$@"; fi if [[ "$OSTYPE" == "darwin"* ]]; then RANDOM_UUID="$(uuidgen | tr '[:upper:]' '[:lower:]')" INSTALL_LOG="/tmp/DYNR/INSTALL_LOG_${NSAPP}.log" mkdir -p /tmp/DYNR 2>/dev/null else RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" INSTALL_LOG="/opt/DYNR/INSTALL_LOG_${NSAPP}.log" mkdir -p /opt/DYNR 2>/dev/null fi EXECUTION_ID="${RANDOM_UUID}" SESSION_ID="${RANDOM_UUID:0:8}" CTTYPE="${CTTYPE:-${CT_TYPE:-1}}" if [[ "${DEV_MODE_LOGS:-false}" == "true" ]]; then if [[ "$OSTYPE" == "darwin"* ]]; then mkdir -p /tmp/DYNR 2>/dev/null BUILD_LOG="/tmp/DYNR/create-lxc-${SESSION_ID}-$(date +%Y%m%d_%H%M%S).log" else mkdir -p /var/log/DYNR 2>/dev/null BUILD_LOG="/var/log/DYNR/create-lxc-${SESSION_ID}-$(date +%Y%m%d_%H%M%S).log" fi fi if command -v pveversion >/dev/null 2>&1; then PVEVERSION="$(pveversion | awk -F'/' '{print $2}' | awk -F'-' '{print $1}')" else PVEVERSION="N/A" fi KERNEL_VERSION=$(uname -r) if [[ -n "${var_cpu:-}" ]] && [[ "${var_cpu}" =~ ^[0-9]+$ ]]; then export APP_DEFAULT_CPU="${var_cpu}" fi if [[ -n "${var_ram:-}" ]] && [[ "${var_ram}" =~ ^[0-9]+$ ]]; then export APP_DEFAULT_RAM="${var_ram}" fi if [[ -n "${var_disk:-}" ]] && [[ "${var_disk}" =~ ^[0-9]+$ ]]; then export APP_DEFAULT_DISK="${var_disk}" fi } DYNR_CORE="https://git.dynastyrevolution.com/DYNR/DynastyRevolution-Scripts/raw/branch/main/core" DYNR_REPO="https://git.dynastyrevolution.com/DYNR/DynastyRevolution-Scripts/raw/branch/main" eval "$(curl -fsSL ${DYNR_CORE}/api.func)" eval "$(curl -fsSL ${DYNR_CORE}/core.func)" eval "$(curl -fsSL ${DYNR_CORE}/error_handler.func)" fetch_wotd() { local wotd_html=$(curl -s --connect-timeout 3 https://www.merriam-webster.com/word-of-the-day) local word=$(echo "$wotd_html" | grep -oP '(?<=Word of the Day: ).*?(?= \|)' | head -n1) local def=$(echo "$wotd_html" | grep -A 1 "<h2>What It Means</h2>" | tail -n1 | sed 's/<[^>]*>//g' | xargs) if [ -n "$word" ]; then echo -e " ${GOLD}Word of the Day: ${BLUE}${word}${NC}" echo -e " ${def}\n" fi } network_check() { msg_info "Checking Network Connectivity (1.1.1.1 / 1.0.0.1)..." if ping -c 1 -W 2 1.1.1.1 &>/dev/null || ping -c 1 -W 2 1.0.0.1 &>/dev/null; then msg_ok "Network OK." else msg_error "Network Unreachable. Provision Aborted." exit 1 fi } network_verify() { local target_id=$1 local type=$2 local ATTEMPT=1 while [ $ATTEMPT -le 2 ]; do msg_info "⌚️ Checking Network Connectivity (Attempt $ATTEMPT)...." [[ $ATTEMPT -eq 1 ]] && fetch_wotd local IP_ADDRESSv4="localhost" local ITER=0 while [ $ITER -lt 15 ]; do if [[ "$type" == "vm" ]]; then if qm guest exec $target_id -- ping -c 1 -W 2 1.1.1.1 &>/dev/null; then IP_ADDRESSv4=$(qm guest network-get-interfaces $target_id --output-format json 2>/dev/null | jq -r '.[] | select(.name=="eth0") | .["ip-addresses"][] | select(.["ip-address-type"]=="ipv4") | .["ip-address"]' | cut -d/ -f1 | head -n 1) break fi else if pct exec $target_id -- ping -c 1 -W 2 1.1.1.1 &>/dev/null; then IP_ADDRESSv4=$(pct exec $target_id -- ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -n1) break fi fi sleep 5 ITER=$((ITER+1)) done if [[ "$IP_ADDRESSv4" != "localhost" ]]; then IP_ADDRESSv4_CAPTURED="$IP_ADDRESSv4" msg_ok "✅ Network Connection Succeeded" return 0 fi if [[ $ATTEMPT -eq 1 ]]; then msg_error "❌ Network Connection Failed, Rechecking...." ATTEMPT=$((ATTEMPT + 1)) else msg_error "❌ Network Connection Failed, removing $target_id. Please check network." if [[ "$type" == "vm" ]]; then qm stop $target_id >/dev/null 2>&1 qm destroy $target_id >/dev/null 2>&1 else pct stop $target_id >/dev/null 2>&1 pct destroy $target_id >/dev/null 2>&1 fi echo -e "\n${RED}Final Log Dump:${NC}" cat "$INSTALL_LOG" 2>/dev/null exit 1 fi done } github_api_check() { msg_info "Checking Github API Token..." local api_response=$(curl -s https://api.github.com/rate_limit) local remaining=$(echo "$api_response" | grep -A 2 '"core"' | grep '"remaining"' | tr -dc '0-9') if [[ -n "$remaining" ]]; then msg_ok "Github: Tokens Remaining: ${GOLD}${remaining}${NC}" else msg_warn "Github: API Limit Unreachable. Proceeding with caution." fi } check_manifest_updates() { local remote_script="${DYNR_REPO}/${MODULE_PATH}" local remote_ver=$(curl -fsSL --connect-timeout 2 "$remote_script" | grep -m 1 "VERSION=" | cut -d'"' -f2 || echo "0") if [[ "$remote_ver" > "$VERSION" ]]; then msg_warn "A New update has Dawned: v$remote_ver is available." echo -e "${GOLD}Recommendation: Re-sync with latest version.${NC}\n" sleep 2 fi } remote_provisioning_ui() { local title="Dynasty Remote Provisioning" local prompt="$1" local default="$2" if command -v osascript &>/dev/null && [[ "$OSTYPE" == "darwin"* ]]; then osascript -e "display dialog \"$prompt\" default answer \"$default\" with title \"$title\" buttons {\"Cancel\", \"Next\"} default button \"Next\"" -e "text returned of result" 2>/dev/null elif command -v whiptail &>/dev/null; then whiptail --backtitle "$title" --title "INPUT" --inputbox "$prompt" 10 58 "$default" 3>&1 1>&2 2>&3 else read -p "$prompt [$default]: " val </dev/tty echo "${val:-$default}" fi } Beginning_Remote_Provisioning() { local target_host=$(remote_provisioning_ui "Enter Proxmox IP/FQDN:" "") [ -z "$target_host" ] && exit_script local target_user=$(remote_provisioning_ui "Enter SSH User:" "root") [ -z "$target_user" ] && exit_script local deploy_mode if [[ "$OSTYPE" == "darwin"* ]]; then deploy_mode=$(osascript -e 'choose from list {"advanced", "default"} with title "Dynasty Remote Provisioning" with prompt "Select Deployment Mode:" default items {"advanced"}' 2>/dev/null) else deploy_mode=$(whiptail --backtitle "Dynasty Remote Provisioning" --title "STANCE" --radiolist "Select Deployment Mode:" 12 58 2 "advanced" "" ON "default" "" OFF 3>&1 1>&2 2>&3) fi deploy_mode="${deploy_mode:-default}" local script_home="${DYNR_REPO}/${MODULE_PATH}" clear echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" echo -e " ${GOLD}INITIATING REMOTE PROVISIONING${NC}" echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" ssh -tt "${target_user}@${target_host}" "TERM=xterm-256color bash -c 'METHOD=\"$deploy_mode\" bash -c \"\$(curl -fsSL $script_home)\"'" < /dev/tty exit 0 } maxkeys_check() { local per_user_maxkeys=$(cat /proc/sys/kernel/keys/maxkeys 2>/dev/null || echo 0) if [[ "$per_user_maxkeys" -eq 0 ]]; then msg_error "Kernel parameter read failure." exit 107 fi } validate_container_id() { local ctid="$1" if ! [[ "$ctid" =~ ^[0-9]+$ ]]; then return 1; fi if command -v pvesh &>/dev/null; then local cluster_ids=$(pvesh get /cluster/resources --type vm --output-format json 2>/dev/null | grep -oP '"vmid":\s*\K[0-9]+' 2>/dev/null || true) if [[ -n "$cluster_ids" ]] && echo "$cluster_ids" | grep -qw "$ctid"; then return 1; fi fi if [[ -f "/etc/pve/lxc/${ctid}.conf" ]] || [[ -f "/etc/pve/qemu-server/${ctid}.conf" ]]; then return 1; fi return 0 } get_valid_container_id() { local suggested_id="${1:-$(pvesh get /cluster/nextid 2>/dev/null || echo 100)}" while ! validate_container_id "$suggested_id"; do suggested_id=$((suggested_id + 1)); done echo "$suggested_id" } select_storage() { local CLASS=$1 local CONTENT='rootdir' [[ "$CLASS" == "template" ]] && CONTENT='vztmpl' declare -A STORAGE_MAP local -a MENU=() while read -r TAG TYPE _ TOTAL USED FREE _; do if [[ -z "$TAG" ]] || [[ -z "$TYPE" ]]; then continue; fi if [[ "$TYPE" == "pbs" ]] && [[ "$CLASS" == "container" ]]; then continue; fi local DISPLAY="${TAG} (${TYPE})" local FREE_FMT=$(numfmt --to=iec --from-unit=1024 --format %.1f <<<"$FREE") STORAGE_MAP["$DISPLAY"]="$TAG" MENU+=("$DISPLAY" "Free: ${FREE_FMT}B" "OFF") done < <(pvesm status -content "$CONTENT" | awk 'NR>1') if [[ $((${#MENU[@]} / 3)) -eq 0 ]]; then msg_error "No valid storage found. (Note: PBS is for backups only)"; exit 1; fi if [[ $((${#MENU[@]} / 3)) -eq 1 ]]; then STORAGE_RESULT="${STORAGE_MAP[${MENU[0]}]}"; return 0; fi local selected_display=$(whiptail --backtitle "$WHIPTITLE" --title "Assign Storage...." --radiolist "Select Storage Pool:" 16 60 6 "${MENU[@]}" 3>&1 1>&2 2>&3) STORAGE_RESULT="${STORAGE_MAP[$selected_display]}" } base_settings() { CT_TYPE=${var_unprivileged:-"1"} DISK_SIZE="${var_disk:-8}" CORE_COUNT="${var_cpu:-1}" RAM_SIZE="${var_ram:-1024}" CT_ID=$(get_valid_container_id "${var_ctid:-$NEXTID}") HN="${var_hostname:-$NSAPP}" BRG=${var_brg:-"vmbr0"} NET=${var_net:-"dhcp"} TAGS="dynasty-script,${var_tags:-}" VLAN_TAG="" ENABLE_TUN="0" ENABLE_SSH="0" SSH_KEY="" AUTOLOGIN="no" OS_CHOICE="${var_os:-debian}" OS_VERSION="${var_version:-12}" TZ="${var_tz:-host}" } default_settings() { header_info CT_ID=$(whiptail --backtitle "$WHIPTITLE" --title "Submit ID...." --inputbox "Set Container ID:" 10 58 "$CT_ID" 3>&1 1>&2 2>&3) BRG=$(whiptail --backtitle "$WHIPTITLE" --title "Network Bridge...." --inputbox "Network Bridge:" 10 58 "$BRG" 3>&1 1>&2 2>&3) VLAN_TAG=$(whiptail --backtitle "$WHIPTITLE" --title "VLAN Tag...." --inputbox "VLAN Tag (Leave blank for none):" 10 58 "" 3>&1 1>&2 2>&3) if whiptail --backtitle "$WHIPTITLE" --title "Enable SSH...." --yesno "Enable Root SSH Access?" 10 58; then ENABLE_SSH="1" SSH_KEY=$(whiptail --backtitle "$WHIPTITLE" --title "Enable SSH...." --inputbox "Paste Public Key (Leave blank to skip):" 10 58 "" 3>&1 1>&2 2>&3) fi if whiptail --backtitle "$WHIPTITLE" --title "Configure Autologin...." --yesno "Enable Console Auto-login?" 10 58; then AUTOLOGIN="yes" fi select_storage "container" CONTAINER_STORAGE="$STORAGE_RESULT" } advanced_settings() { tput smcup 2>/dev/null || true trap 'tput rmcup 2>/dev/null || true' RETURN local deb12_st="OFF" deb13_st="OFF" alp_st="OFF" arch_st="OFF" ubu_st="OFF" fed_st="OFF" cen_st="OFF" [[ "$var_os" == "debian" && "$var_version" == "12" ]] && deb12_st="ON" [[ "$var_os" == "debian" && "$var_version" == "13" ]] && deb13_st="ON" [[ "$var_os" == "alpine" ]] && alp_st="ON" [[ "$var_os" == "archlinux" ]] && arch_st="ON" local STEP=1 local MAX_STEP=15 while [[ $STEP -le $MAX_STEP ]]; do case $STEP in 1) if result=$(whiptail --backtitle "$WHIPTITLE" --title "PRIVILEGES" --radiolist "Select Container Type:" 12 58 2 "1" "LXC (Unprivileged)" ON "0" "LXC (Privileged)" OFF 3>&1 1>&2 2>&3); then CT_TYPE="$result"; STEP=$((STEP + 1)); else exit_script; fi ;; 2) if result=$(whiptail --backtitle "$WHIPTITLE" --title "OS SELECTION" --radiolist "Select OS:" 14 65 7 "debian" "Debian 12" $deb12_st "debian13" "Debian 13" $deb13_st "alpine" "Alpine 3.20" $alp_st "archlinux" "Arch Linux" $arch_st "ubuntu" "Ubuntu 24.04" OFF "fedora" "Fedora 40" OFF "centos" "CentOS Stream 9" OFF 3>&1 1>&2 2>&3); then OS_CHOICE="$result" [[ "$OS_CHOICE" == "debian13" ]] && { OS_CHOICE="debian"; OS_VERSION="13"; } [[ "$OS_CHOICE" == "debian" ]] && OS_VERSION="12" [[ "$OS_CHOICE" == "alpine" ]] && OS_VERSION="3.20" [[ "$OS_CHOICE" == "ubuntu" ]] && OS_VERSION="24.04" [[ "$OS_CHOICE" == "fedora" ]] && OS_VERSION="40" [[ "$OS_CHOICE" == "centos" ]] && OS_VERSION="9" [[ "$OS_CHOICE" == "archlinux" ]] && OS_VERSION="base" STEP=$((STEP + 1)) else STEP=$((STEP - 1)); fi ;; 3) if result=$(whiptail --backtitle "$WHIPTITLE" --title "PASSWORD" --passwordbox "Root Password:" 12 58 3>&1 1>&2 2>&3); then if [[ -n "$result" ]]; then PW="--password $result"; else PW=""; fi; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 4) if whiptail --backtitle "$WHIPTITLE" --title "Configure Autologin...." --yesno "Enable Console Auto-login?" 10 58; then AUTOLOGIN="yes"; else AUTOLOGIN="no"; fi; STEP=$((STEP + 1)) ;; 5) if whiptail --backtitle "$WHIPTITLE" --title "Enable SSH...." --yesno "Enable Root SSH Access?" 10 58; then ENABLE_SSH="1"; else ENABLE_SSH="0"; fi; STEP=$((STEP + 1)) ;; 6) if [[ "$ENABLE_SSH" == "1" ]]; then if result=$(whiptail --backtitle "$WHIPTITLE" --title "Enable SSH...." --inputbox "Paste Public Key:" 10 58 3>&1 1>&2 2>&3); then SSH_KEY="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi; else STEP=$((STEP + 1)); fi ;; 7) if result=$(whiptail --backtitle "$WHIPTITLE" --title "Submit ID...." --inputbox "Confirm ID:" 10 58 "$CT_ID" 3>&1 1>&2 2>&3); then CT_ID="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 8) if result=$(whiptail --backtitle "$WHIPTITLE" --title "HOSTNAME" --inputbox "Set Hostname:" 10 58 "$HN" 3>&1 1>&2 2>&3); then HN="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 9) if result=$(whiptail --backtitle "$WHIPTITLE" --title "CAPACITY" --inputbox "Storage (GB):" 10 58 "$DISK_SIZE" 3>&1 1>&2 2>&3); then DISK_SIZE="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 10) if result=$(whiptail --backtitle "$WHIPTITLE" --title "CORES" --inputbox "CPU Allocation:" 10 58 "$CORE_COUNT" 3>&1 1>&2 2>&3); then CORE_COUNT="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 11) if result=$(whiptail --backtitle "$WHIPTITLE" --title "MEMORY" --inputbox "RAM (MiB):" 10 58 "$RAM_SIZE" 3>&1 1>&2 2>&3); then RAM_SIZE="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 12) if result=$(whiptail --backtitle "$WHIPTITLE" --title "Network Bridge...." --inputbox "Network Bridge:" 10 58 "$BRG" 3>&1 1>&2 2>&3); then BRG="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 13) if select_storage "container"; then CONTAINER_STORAGE="$STORAGE_RESULT"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 14) if result=$(whiptail --backtitle "$WHIPTITLE" --title "TIMEZONE" --inputbox "Set Timezone:" 10 58 "$TZ" 3>&1 1>&2 2>&3); then TZ="$result"; STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; 15) if whiptail --backtitle "$WHIPTITLE" --title "CONFIRMATION" --yesno "Create $APP?" 10 58; then STEP=$((STEP + 1)); else STEP=$((STEP - 1)); fi ;; esac done tput rmcup 2>/dev/null || true trap - RETURN } build_container() { msg_info "Locating Files for $OS_CHOICE" local template_search="${OS_CHOICE}-${OS_VERSION}" [[ "$OS_CHOICE" == "archlinux" ]] && template_search="archlinux" local template=$(pveam available -section system | grep "$template_search" | awk '{print $2}' | sort -V | tail -n1) if [[ -z "$template" ]]; then if [[ "$OS_CHOICE" == "archlinux" ]]; then template="system/archlinux-base_20240911-1_amd64.tar.zst" else msg_error "Template not found in system storage."; exit 1; fi fi local template_filename="${template##*/}" local TEMPLATE_STORAGE="" local existing_storage="" while read -r st; do if pveam list "$st" 2>/dev/null | grep -q "$template_filename"; then existing_storage="$st"; break; fi done < <(pvesm status -content vztmpl | awk 'NR>1 {print $1}') if [[ -n "$existing_storage" ]]; then TEMPLATE_STORAGE="$existing_storage" else select_storage "template"; TEMPLATE_STORAGE="$STORAGE_RESULT"; pveam download "$TEMPLATE_STORAGE" "$template" $STD; fi msg_info "Welding OS into LXC $CT_ID...." local VLAN_OPT="" [[ -n "$VLAN_TAG" ]] && VLAN_OPT=",tag=$VLAN_TAG" if ! pct create "$CT_ID" "${TEMPLATE_STORAGE}:vztmpl/${template_filename}" -hostname "$HN" -net0 name=eth0,bridge="$BRG",ip="$NET"${VLAN_OPT} -cores "$CORE_COUNT" -memory "$RAM_SIZE" -rootfs "${CONTAINER_STORAGE:-local}":"$DISK_SIZE" -unprivileged "$CT_TYPE" -features nesting=1 -onboot 1 ${PW:-} $STD; then msg_error "Build Rejected by Proxmox."; exit 1 fi pct set "$CT_ID" -timezone "$TZ" pct start "$CT_ID" if [[ -n "$SSH_KEY" ]]; then msg_info "Spraying SSH Authorized Keys...." pct exec "$CT_ID" -- sh -c "mkdir -p /root/.ssh && chmod 700 /root/.ssh && echo '$SSH_KEY' >> /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys" fi network_verify "$CT_ID" "lxc" } echo_default() { msg_info "Provisioning with Defaults for ${GOLD}${APP}${NC}:" echo -e " ${BLUE}OS Version:${NC} ${GOLD}${OS_CHOICE} ${OS_VERSION}${NC}" echo -e " ${BLUE}Resources:${NC} ${GOLD}${CORE_COUNT} Cores, ${RAM_SIZE}MB RAM, ${DISK_SIZE}GB Storage${NC}" echo -e " ${BLUE}Network:${NC} ${GOLD}${NET} on ${BRG}${NC}" echo -e " ${BLUE}ID & Name:${NC} ${GOLD}LXC ${CT_ID} (${HN})${NC}" echo "" sleep 2 } description() { local portal="http://${IP_ADDRESSv4_CAPTURED}:${PORT:-0000}" local DESCRIPTION="<div align='center'><h2 style='color: #af8700;'>DYNASTY REVOLUTION</h2><p style='color: #1fd9f0;'>${APP} PROVISIONED</p></div>" pct set "$CT_ID" -description "$DESCRIPTION" >/dev/null 2>&1 echo -e "\n${GOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" echo -e " ${GOLD}${BOLD}PROVISION COMPLETE${NC}" echo -e " ${BLUE}Access: ${NC} ${GOLD}${portal}${NC}" [[ "$ENABLE_SSH" == "1" ]] && echo -e " ${BLUE}SSH :${NC} ${GOLD}ssh root@${IP_ADDRESSv4_CAPTURED}${NC}" echo -e " ${BLUE}ID & Name:${NC} ${GOLD}LXC ${CT_ID} (${HN})${NC}" echo -e " ${BLUE}Log Path:${NC} ${GOLD}${INSTALL_LOG}${NC}" echo -e "${GOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" } start() { variables if command -v pveversion >/dev/null 2>&1; then header_info; network_check; github_api_check; maxkeys_check; check_manifest_updates; NEXTID=$(pvesh get /cluster/nextid); base_settings if (whiptail --backtitle "$WHIPTITLE" --title "SETTINGS" --yesno "Provision with recommended resources for $APP?" --yes-button "Default" --no-button "Advanced" 10 58); then default_settings else advanced_settings fi build_container else Beginning_Remote_Provisioning fi }