Skip to content
71 changes: 71 additions & 0 deletions misc/admin-core.func
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
# Copyright (c) 2021-2026 community-scripts ORG
# Author: community-scripts ORG
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/branch/main/LICENSE

#function die() {
# msg_error "${1}"; exit 1
#}

# ==============================================================================
# OS DETECTION
# ==============================================================================
function detect_os() {
if grep -qi "alpine" /etc/os-release; then
#OS="Alpine"
PKG_UPDATE=""
PKG_INSTALL="apk add --no-cache"
PKG_UPGRADE="apk update"
PKG_UNINSTALL="apk del"
PKG_AUTOREMOVE=""
elif grep -qi "arch" /etc/os-release; then
#OS="Arch"
PKG_UPDATE=""
PKG_INSTALL="pacman -S"
PKG_UPGRADE="pacman -Syu"
PKG_UNINSTALL="pacman -Rs"
PKG_AUTOREMOVE=""
elif grep -qi "debian" /etc/os-release; then
#OS="Debian"
PKG_UPDATE="apt update"
PKG_INSTALL="apt -y install"
PKG_UPGRADE="apt -y upgrade"
PKG_UNINSTALL="apt -y --purge remove"
PKG_AUTOREMOVE="apt -y --purge autoremove"
if ! [[ -f /etc/apt/sources.list.d/smallstep.sources ]]; then
setup_deb822_repo \
"smallstep" \
"https://packages.smallstep.com/keys/apt/repo-signing-key.gpg" \
"https://packages.smallstep.com/stable/debian" \
"debs" \
"main"
fi
elif grep -qi "ubuntu" /etc/os-release; then
#OS="Ubuntu"
PKG_UPDATE="apt update"
PKG_INSTALL="apt -y install"
PKG_UPGRADE="apt -y upgrade"
PKG_UNINSTALL="apt -y --purge remove"
PKG_AUTOREMOVE="apt -y --purge autoremove"
if ! [[ -f /etc/apt/sources.list.d/smallstep.sources ]]; then
setup_deb822_repo \
"smallstep" \
"https://packages.smallstep.com/keys/apt/repo-signing-key.gpg" \
"https://packages.smallstep.com/stable/debian" \
"debs" \
"main"
fi
else
die "Unsupported OS. Exiting."
fi
}

# ==============================================================================
# HELPER FUNCTIONS
# ==============================================================================
function resolve_ip() {
local FQDN=$1
local IP
IP=$(dig +short "$FQDN")
[[ -z "$IP" ]] && exit 1 || echo "$IP"
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be part of this PR.

there is another PR open for this, and we'd rather like those changes be incorporated into our existing funcs, if even needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are partly wrong - I've opened a PR for pve-lxc-system-admin.sh
#1627

During the initial discussion on this PR I have only asked for a suitable possibility to add some new supporting functions ...

For the moment the refactoring of the step-ca LXC is much more important (separate PR is coming quickly) and for that reason this step-admin Add-On will be handled by me with priority.

If needed I can make separate PRs for the supporting functions, but for the moment from my perspection nothing is opened beside parts of this PR...

How to proceed???

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we won't just add new core things for a single script, and we will also not create even more scripts like admin-core.sh.

also those changes make not that much sense, as some of that code already exists in our functions and would be doubled code.

Yes seperate Pr for supporting funcs would be appreciated, but this would come with integrating them into our existing core and possible also directly applying to other scripts that may benefit from the new helper function

@heinemannj heinemannj Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detect_os function:

  • Copy&Paste from one of existing addon and define a funtion for better readability - I've seen this code fragments under the heading OS Detection) in most of actual existing Add-Ons under MAIN ...
  • If a core function for detect OS is already available please let me know

whitail is widely used in your framework - but every usage of whiptail is handmade ...

  • If whiptail functions are already available please let me know - I've found nothing

resolve_ip function:

  • peanuts - I can simply move back into the add-on

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whiptail is definitely something we have already talked about, but ultimately not yet tackled as basically every whiptail is a lot different.

So I think it would be nice if you could contribute the whiptail function as seperate PR.
Same for detect_os, but I think that can be incorporated into out core, as there are possibly already parts of that in core.

@MickLesk please correct me if I'm wrong. You know best about core.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CrazyWolf13 and @MickLesk :

  • PR for whiptail helper-scripts: whiptail helper functions for dialog boxes #1760
    • moved back to the step-admin add-on until above PR is validated
  • detect_os function - giving up - moved back to the step-admin add-on
  • resolve_ip function - giving up - moved back to the step-admin add-on

155 changes: 155 additions & 0 deletions misc/whiptail.func
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env bash

# Copyright (c) 2021-2026 community-scripts ORG
# Author: community-scripts ORG
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/branch/main/LICENSE

# ==============================================================================
# WHIPTAIL HELPER FUNCTIONS
# ==============================================================================
#
# This file contains several whiptail functions for displaying dialog boxes in the terminal.
#
# Whenever possible the dialog box
# - width is automatically calculated based on the length of the title, text, tag, item and status.
# - height and listheight are automatically calculated based on the number of options in the list or the number of lines in the text file.
#
# WHIPTAIL Box options:
# --msgbox <text> <height auto calculated> <width auto calculated>
# --yesno <text> <height> <width> (N/A)
# --infobox <text> <height> <width> (N/A)
# --inputbox <text> <height static> <width auto calculated> [init]
# --passwordbox <text> <height> <width> [init] (N/A)
# --textbox <file> <height auto calculated> <width static>
# --menu <text> <height auto calculated> <width auto calculated> <listheight auto calculated> [tag item]
# --checklist <text> <height auto calculated> <width auto calculated> <listheight auto calculated> [tag item status]
# --radiolist <text> <height auto calculated> <width auto calculated> <listheight auto calculated> [tag item status]
# --gauge <text> <height> <width> <percent> (N/A)
#

function whiptail_msgbox() {
local TITLE=$1
local TEXT=$2
local LEN
LEN=$(echo "$TEXT" | wc -l)
local HIGHT=$(( LEN + 7 ))
(( HIGHT > 30 )) && HIGHT=30
local WIDTH=$(( ${#TITLE} + 16 ))
local WIDTH_ARRAY=( "$WIDTH" $(( ${#TEXT} + 4 )) )
for i in "${WIDTH_ARRAY[@]}"; do
(( i > WIDTH )) && WIDTH=$i
done
(( WIDTH > 150 )) && WIDTH=150

whiptail --backtitle "$APP_BACKTITLE" --title "$TITLE" --scrolltext --msgbox "$TEXT" "$HIGHT" "$WIDTH" 3>&1 1>&2 2>&3
}

function whiptail_inputbox() {
local TITLE=$1
local TEXT=$2
local VALUE_INIT=$3
local VALUE_INPUT
local HIGHT=10
local WIDTH=$(( ${#TITLE} + 16 ))
local WIDTH_ARRAY=( "$WIDTH" $(( ${#TEXT} + 4 )) $(( ${#VALUE_INIT} + 8 )) )
for i in "${WIDTH_ARRAY[@]}"; do
(( i > WIDTH )) && WIDTH=$i
done

if VALUE_INPUT=$(whiptail --backtitle "$APP_BACKTITLE" --title "$TITLE" --inputbox "\n$TEXT" \
"$HIGHT" "$WIDTH" "$VALUE_INIT" 3>&1 1>&2 2>&3); then
echo "$VALUE_INPUT"
else
echo "$VALUE_INIT"
fi
}

function whiptail_textbox() {
local TITLE=$1
local FILE=$2
local LEN
LEN=$(wc -l < "$FILE")
local HIGHT=$(( LEN + 7 ))
(( HIGHT > 30 )) && HIGHT=30
local WIDTH=150

whiptail --backtitle "$APP_BACKTITLE" --title "$TITLE" --scrolltext --textbox "$FILE" "$HIGHT" "$WIDTH" 3>&1 1>&2 2>&3
}

function whiptail_menu() {
local TITLE=$1
local TEXT="\nSelect an option:"
local LEN=$(( ${#OPTIONS[@]} / 2 ))
local HIGHT=$(( LEN + 9 ))
local WIDTH=$(( ${#TITLE} + 16 ))
local WIDTH_OFFSET=5
local CHOICE
local MAX_LEFT=0
local MAX_RIGHT=0
for ((i=0; i<${#OPTIONS[@]}; i+=2)); do
(( ${#OPTIONS[i]} > MAX_LEFT )) && MAX_LEFT=${#OPTIONS[i]}
(( ${#OPTIONS[$(( i+1 ))]} > MAX_RIGHT )) && MAX_RIGHT=${#OPTIONS[$(( i+1 ))]}
done
(( MAX_LEFT + MAX_RIGHT + WIDTH_OFFSET > WIDTH )) && WIDTH=$(( MAX_LEFT + MAX_RIGHT + WIDTH_OFFSET ))

if CHOICE=$(whiptail --backtitle "$APP_BACKTITLE" --title "$TITLE" --menu "$TEXT" \
"$HIGHT" "$WIDTH" "$LEN" "${OPTIONS[@]}" 3>&1 1>&2 2>&3 || true); then
echo "$CHOICE"
fi
}

function whiptail_checklist() {
local TITLE=$1
local TEXT=$2
local -n LIST=$3
local CHOICE
local OPTIONS=()
local WIDTH=$(( ${#TITLE} + 16 ))
local WIDTH_OFFSET=15
local MAX_LEFT=0
local MAX_RIGHT=0

for ((i=0; i<${#LIST[@]}; i+=2)); do
local j=$(( i+1 ))
(( ${#LIST[i]} > MAX_LEFT )) && MAX_LEFT=${#LIST[i]}
(( ${#LIST[j]} > MAX_RIGHT )) && MAX_RIGHT=${#LIST[j]}
OPTIONS+=("${LIST[i]}" "${LIST[j]}" "OFF")
done
(( MAX_LEFT + MAX_RIGHT + WIDTH_OFFSET > WIDTH )) && WIDTH=$(( MAX_LEFT + MAX_RIGHT + WIDTH_OFFSET ))
local LEN=$(( ${#OPTIONS[@]} / 2 ))
(( LEN > 21 )) && LEN=21
local HIGHT=$(( LEN + 9 ))

if CHOICE=$(whiptail --backtitle "$APP_BACKTITLE" --title "$TITLE" --scrolltext --checklist "$TEXT" \
"$HIGHT" "$WIDTH" "$LEN" "${OPTIONS[@]}" 3>&1 1>&2 2>&3 | tr -d '"'); then
echo "$CHOICE"
fi
}

function whiptail_radiolist() {
local TITLE=$1
local TEXT=$2
local -n LIST=$3
local CHOICE
local OPTIONS=()
local WIDTH=$(( ${#TITLE} + 16 ))
local WIDTH_OFFSET=15
local MAX_LEFT=0
local MAX_RIGHT=0

for ((i=0; i<${#LIST[@]}; i+=2)); do
local j=$(( i+1 ))
(( ${#LIST[i]} > MAX_LEFT )) && MAX_LEFT=${#LIST[i]}
(( ${#LIST[j]} > MAX_RIGHT )) && MAX_RIGHT=${#LIST[j]}
OPTIONS+=("${LIST[i]}" "${LIST[j]}" "OFF")
done
(( MAX_LEFT + MAX_RIGHT + WIDTH_OFFSET > WIDTH )) && WIDTH=$(( MAX_LEFT + MAX_RIGHT + WIDTH_OFFSET ))
local LEN=$(( ${#OPTIONS[@]} / 2 ))
(( LEN > 21 )) && LEN=21
local HIGHT=$(( LEN + 9 ))

if CHOICE=$(whiptail --backtitle "$APP_BACKTITLE" --title "$TITLE" --scrolltext --radiolist "$TEXT" \
"$HIGHT" "$WIDTH" "$LEN" "${OPTIONS[@]}" 3>&1 1>&2 2>&3 | tr -d '"'); then
echo "$CHOICE"
fi
}
Comment thread
heinemannj marked this conversation as resolved.
Outdated
Loading
Loading