From 0e3a648f40255f9c085448f4144b77686947b4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20B=C3=A9dard-Couture?= Date: Wed, 18 Dec 2024 11:00:22 -0500 Subject: [PATCH 1/2] Support for helper-scripts environment variables --- misc/build.func | 94 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/misc/build.func b/misc/build.func index bc15bd0b..7e8f8e7c 100644 --- a/misc/build.func +++ b/misc/build.func @@ -480,9 +480,11 @@ advanced_settings() { APT_CACHER="" APT_CACHER_IP="" else - if APT_CACHER_IP=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set APT-Cacher IP (leave blank for default)" 8 58 --title "APT-Cacher IP" 3>&1 1>&2 2>&3); then + if APT_CACHER_IP=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set APT-Cacher IP (leave blank for default)" 8 58 $APT_CACHER_IP --title "APT-Cacher IP" 3>&1 1>&2 2>&3); then APT_CACHER="${APT_CACHER_IP:+yes}" echo -e "${NETWORK}${BOLD}${DGN}APT-Cacher IP Address: ${BGN}${APT_CACHER_IP:-Default}${CL}" + add_proxmox_helper_scripts_env "APT_CACHER_IP" "${APT_CACHER_IP}" + #Or we could call phs_validate_required_config if it's a mandatory variable else exit_script fi @@ -591,6 +593,7 @@ install_script() { root_check arch_check ssh_check + read_proxmox_helper_scripts_env if systemctl is-active -q ping-instances.service; then systemctl -q stop ping-instances.service @@ -858,3 +861,92 @@ EOF systemctl start ping-instances.service fi } + + + +# Set a global variable for the PHS environment file +PVE_ENV="/etc/pve-helper-scripts.conf" + +# This function loads the environment file for common configuration in Proxmox-Helper-Scripts +function read_proxmox_helper_scripts_env(){ + #Check if file exists + if [ ! -f "$PVE_ENV" ]; then + msg_info "${BL}Creating Proxmox-Helper-Scripts configuration file.${CL}" + touch "$PVE_ENV" + chown root:root "$PVE_ENV" + chmod 0600 "$PVE_ENV" + msg_ok "${BL}Created Proxmox-Helper-Scripts configuration file.${CL}" + else + source "$PVE_ENV" + fi +} + +# This function adds a variable to the Proxmox-Helper-Scripts config file +function add_proxmox_helper_scripts_env(){ + #check if first parameter was passed and it's an integer + if [ $# -ge 1 ] && [ ! -z "$1" ]; then + PHS_VAR_NAME=$1 + DEFAULT_VALUE="" + if [ $# -ge 2 ] && [ ! -z "$2" ]; then + DEFAULT_VALUE=$2 + fi + if PHS_VAR_VALUE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set value for environment variable $PHS_VAR_NAME" 8 58 $DEFAULT_VALUE --title "VALUE" 3>&1 1>&2 2>&3); then + if [ -z "$PHS_VAR_VALUE" ]; then + msg_error "Value cannot be empty!" + exit-script + fi + echo -e "${DGN}Setting Proxmox-Helper-Scripts Envrionment Variable $PHS_VAR_NAME: ${BGN}${PHS_VAR_VALUE}${CL}" + if [ $# -ge 3 ] && [ ! -z "$3" ] && [ "$3" == "PASSWORD" ]; then + PHS_VAR_VALUE=$(openssl passwd -1 ${PHS_VAR_VALUE}) + fi + if grep -q "${PHS_VAR_NAME}=.*" "$PVE_ENV"; then + sed -i "s|${PHS_VAR_NAME}=.*|${PHS_VAR_NAME}='${PHS_VAR_VALUE}'|g" "$PVE_ENV" + else + echo "${PHS_VAR_NAME}='${PHS_VAR_VALUE}'" >> "$PVE_ENV" + fi + else + exit-script + fi + else + msg_error "You need to pass the variable name to set as the first parameter" + exit-script + fi + read_proxmox_helper_scripts_env +} + +# This function adds an encrypted variable to the Proxmox-Helper-Scripts config file by passing the right arguments to add_proxmox_helper_scripts_env() +function add_proxmox_helper_scripts_env_password(){ + if [ $# -ge 2 ] && [ ! -z "$1" ] && [ ! -z "$2" ]; then + add_proxmox_helper_scripts_env $1 $2 "PASSWORD" + elif [ $# -ge 1 ] && [ ! -z "$1" ]; then + add_proxmox_helper_scripts_env $1 "" "PASSWORD" + else + msg_error "You need to pass the variable name to set as the first parameter" + exit-script + fi +} + +# This function tests all enabled Proxmox-Helper-Scripts environment variable needed to build the container and prompts the user to create them if missing +# Useful when multiple environment variables are required to enable a configuration, like a username and a password +function phs_validate_required_config(){ + # These are example of potential required configs + # If the user specifies the use of APT_CACHER, then make sure to save the APT_CACHER_IP and reuse it + # We might not actually need this if nothing is required + if [ ! -z ${APT_CACHER+x} ] && [[ "${APT_CACHER}" == "yes" ]]; then + if [ -z ${APT_CACHER_IP+x} ]; then + msg_error "Missing proxmox-helper-scripts environment variables: APT_CACHER, APT_CACHER_IP" + add_proxmox_helper_scripts_env "APT_CACHER" "yes" + add_proxmox_helper_scripts_env "APT_CACHER_IP" + fi + fi + + # Another example with username/password (encrypted) + # These variables don't exist in the main repo, only in my fork, but it gives you an idea of how it could work + if [ ! -z ${ADD_SSH_USER+x} ] && [[ "${ADD_SSH_USER}" == "yes" ]]; then + if [ -z ${SSH_USER+x} ] || [ -z ${SSH_PASSWORD+x} ]; then + msg_error "Missing proxmox-helper-scripts environment variables: SSH_USER, SSH_PASSWORD" + add_proxmox_helper_scripts_env "SSH_USER" "admin" + add_proxmox_helper_scripts_env_password "SSH_PASSWORD" + fi + fi +} \ No newline at end of file From 610eff3d2adca299632466e2fb7efe961ff6002b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20B=C3=A9dard-Couture?= Date: Fri, 3 Jan 2025 17:07:04 -0500 Subject: [PATCH 2/2] Only read from the environment file. Users should manually edit the file. --- misc/build.func | 83 +++---------------------------------------------- 1 file changed, 5 insertions(+), 78 deletions(-) diff --git a/misc/build.func b/misc/build.func index 7e8f8e7c..1389cd73 100644 --- a/misc/build.func +++ b/misc/build.func @@ -460,7 +460,7 @@ advanced_settings() { if [ "$NET" != "dhcp" ]; then while true; do - GATE1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Enter gateway IP address" 8 58 --title "Gateway IP" 3>&1 1>&2 2>&3) + GATE1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Enter gateway IP address" 8 58 $GATE1 --title "Gateway IP" 3>&1 1>&2 2>&3) if [ -z "$GATE1" ]; then whiptail --backtitle "Proxmox VE Helper Scripts" --msgbox "Gateway IP address cannot be empty" 8 58 elif [[ ! "$GATE1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then @@ -483,8 +483,6 @@ advanced_settings() { if APT_CACHER_IP=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set APT-Cacher IP (leave blank for default)" 8 58 $APT_CACHER_IP --title "APT-Cacher IP" 3>&1 1>&2 2>&3); then APT_CACHER="${APT_CACHER_IP:+yes}" echo -e "${NETWORK}${BOLD}${DGN}APT-Cacher IP Address: ${BGN}${APT_CACHER_IP:-Default}${CL}" - add_proxmox_helper_scripts_env "APT_CACHER_IP" "${APT_CACHER_IP}" - #Or we could call phs_validate_required_config if it's a mandatory variable else exit_script fi @@ -497,7 +495,7 @@ advanced_settings() { fi echo -e "${DISABLEIPV6}${BOLD}${DGN}Disable IPv6: ${BGN}$DISABLEIP6${CL}" - if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" 3>&1 1>&2 2>&3); then + if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 $MTU1 --title "MTU SIZE" 3>&1 1>&2 2>&3); then if [ -z $MTU1 ]; then MTU1="Default" MTU="" @@ -509,7 +507,7 @@ advanced_settings() { exit_script fi - if SD=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a DNS Search Domain (leave blank for HOST)" 8 58 --title "DNS Search Domain" 3>&1 1>&2 2>&3); then + if SD=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a DNS Search Domain (leave blank for HOST)" 8 58 $SD --title "DNS Search Domain" 3>&1 1>&2 2>&3); then if [ -z $SD ]; then SX=Host SD="" @@ -522,7 +520,7 @@ advanced_settings() { exit_script fi - if NX=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a DNS Server IP (leave blank for HOST)" 8 58 --title "DNS SERVER IP" 3>&1 1>&2 2>&3); then + if NX=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a DNS Server IP (leave blank for HOST)" 8 58 $NX --title "DNS SERVER IP" 3>&1 1>&2 2>&3); then if [ -z $NX ]; then NX=Host NS="" @@ -546,7 +544,7 @@ advanced_settings() { exit_script fi - if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" 3>&1 1>&2 2>&3); then + if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 $VLAN1 --title "VLAN" 3>&1 1>&2 2>&3); then if [ -z $VLAN1 ]; then VLAN1="Default" VLAN="" @@ -863,7 +861,6 @@ EOF } - # Set a global variable for the PHS environment file PVE_ENV="/etc/pve-helper-scripts.conf" @@ -879,74 +876,4 @@ function read_proxmox_helper_scripts_env(){ else source "$PVE_ENV" fi -} - -# This function adds a variable to the Proxmox-Helper-Scripts config file -function add_proxmox_helper_scripts_env(){ - #check if first parameter was passed and it's an integer - if [ $# -ge 1 ] && [ ! -z "$1" ]; then - PHS_VAR_NAME=$1 - DEFAULT_VALUE="" - if [ $# -ge 2 ] && [ ! -z "$2" ]; then - DEFAULT_VALUE=$2 - fi - if PHS_VAR_VALUE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set value for environment variable $PHS_VAR_NAME" 8 58 $DEFAULT_VALUE --title "VALUE" 3>&1 1>&2 2>&3); then - if [ -z "$PHS_VAR_VALUE" ]; then - msg_error "Value cannot be empty!" - exit-script - fi - echo -e "${DGN}Setting Proxmox-Helper-Scripts Envrionment Variable $PHS_VAR_NAME: ${BGN}${PHS_VAR_VALUE}${CL}" - if [ $# -ge 3 ] && [ ! -z "$3" ] && [ "$3" == "PASSWORD" ]; then - PHS_VAR_VALUE=$(openssl passwd -1 ${PHS_VAR_VALUE}) - fi - if grep -q "${PHS_VAR_NAME}=.*" "$PVE_ENV"; then - sed -i "s|${PHS_VAR_NAME}=.*|${PHS_VAR_NAME}='${PHS_VAR_VALUE}'|g" "$PVE_ENV" - else - echo "${PHS_VAR_NAME}='${PHS_VAR_VALUE}'" >> "$PVE_ENV" - fi - else - exit-script - fi - else - msg_error "You need to pass the variable name to set as the first parameter" - exit-script - fi - read_proxmox_helper_scripts_env -} - -# This function adds an encrypted variable to the Proxmox-Helper-Scripts config file by passing the right arguments to add_proxmox_helper_scripts_env() -function add_proxmox_helper_scripts_env_password(){ - if [ $# -ge 2 ] && [ ! -z "$1" ] && [ ! -z "$2" ]; then - add_proxmox_helper_scripts_env $1 $2 "PASSWORD" - elif [ $# -ge 1 ] && [ ! -z "$1" ]; then - add_proxmox_helper_scripts_env $1 "" "PASSWORD" - else - msg_error "You need to pass the variable name to set as the first parameter" - exit-script - fi -} - -# This function tests all enabled Proxmox-Helper-Scripts environment variable needed to build the container and prompts the user to create them if missing -# Useful when multiple environment variables are required to enable a configuration, like a username and a password -function phs_validate_required_config(){ - # These are example of potential required configs - # If the user specifies the use of APT_CACHER, then make sure to save the APT_CACHER_IP and reuse it - # We might not actually need this if nothing is required - if [ ! -z ${APT_CACHER+x} ] && [[ "${APT_CACHER}" == "yes" ]]; then - if [ -z ${APT_CACHER_IP+x} ]; then - msg_error "Missing proxmox-helper-scripts environment variables: APT_CACHER, APT_CACHER_IP" - add_proxmox_helper_scripts_env "APT_CACHER" "yes" - add_proxmox_helper_scripts_env "APT_CACHER_IP" - fi - fi - - # Another example with username/password (encrypted) - # These variables don't exist in the main repo, only in my fork, but it gives you an idea of how it could work - if [ ! -z ${ADD_SSH_USER+x} ] && [[ "${ADD_SSH_USER}" == "yes" ]]; then - if [ -z ${SSH_USER+x} ] || [ -z ${SSH_PASSWORD+x} ]; then - msg_error "Missing proxmox-helper-scripts environment variables: SSH_USER, SSH_PASSWORD" - add_proxmox_helper_scripts_env "SSH_USER" "admin" - add_proxmox_helper_scripts_env_password "SSH_PASSWORD" - fi - fi } \ No newline at end of file