diff --git a/misc/build.func b/misc/build.func index bc15bd0b7..7e8f8e7cb 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