Enhance .env file handling and validation in scripts
This commit is contained in:
@@ -4,11 +4,19 @@
|
|||||||
# This is called early so that DOTENV_* variables are available to all functions
|
# This is called early so that DOTENV_* variables are available to all functions
|
||||||
load_env_if_exists() {
|
load_env_if_exists() {
|
||||||
local env_file="${CONFIG_FILE:-}"
|
local env_file="${CONFIG_FILE:-}"
|
||||||
|
local config_explicit="${CONFIG_FILE_SET:-false}"
|
||||||
|
|
||||||
# If CONFIG_FILE is not set, check default location
|
# If CONFIG_FILE is not set, check default location
|
||||||
if [[ -z "$env_file" ]]; then
|
if [[ -z "$env_file" ]]; then
|
||||||
local script_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
local script_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
|
||||||
env_file="$script_dir/.env"
|
env_file="$script_dir/.env"
|
||||||
|
config_explicit="false"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Validate config file exists if explicitly specified
|
||||||
|
if [[ "$config_explicit" == "true" ]] && [[ ! -f "$env_file" ]]; then
|
||||||
|
echo "Error: Config file not found: $env_file"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -f "$env_file" ]]; then
|
if [[ -f "$env_file" ]]; then
|
||||||
|
|||||||
@@ -286,27 +286,8 @@ usage() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set default CONFIG_FILE
|
# Parse all arguments
|
||||||
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
|
CONFIG_FILE_SET=false
|
||||||
export CONFIG_FILE="$SCRIPT_DIR/.env"
|
|
||||||
|
|
||||||
# Parse --config argument first
|
|
||||||
i=1
|
|
||||||
while [[ $i -le $# ]]; do
|
|
||||||
arg="${!i}"
|
|
||||||
if [[ "$arg" == "--config" ]]; then
|
|
||||||
next_i=$((i+1))
|
|
||||||
CONFIG_FILE="${!next_i}"
|
|
||||||
export CONFIG_FILE
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
i=$((i+1))
|
|
||||||
done
|
|
||||||
|
|
||||||
# Source autodiscover.sh to load .env file
|
|
||||||
source "$(dirname "$0")/autodiscover.sh"
|
|
||||||
|
|
||||||
# Now parse all arguments normally
|
|
||||||
while [[ "$#" -gt 0 ]]; do
|
while [[ "$#" -gt 0 ]]; do
|
||||||
case $1 in
|
case $1 in
|
||||||
-t|--tag) IMAGE_TAG="$2"; shift ;;
|
-t|--tag) IMAGE_TAG="$2"; shift ;;
|
||||||
@@ -320,34 +301,6 @@ while [[ "$#" -gt 0 ]]; do
|
|||||||
add_copy_hosts "$1"
|
add_copy_hosts "$1"
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "${#COPY_HOSTS[@]}" -eq 0 ]; then
|
|
||||||
# Try to use COPY_HOSTS from .env first
|
|
||||||
if [[ -n "$DOTENV_COPY_HOSTS" ]]; then
|
|
||||||
echo "Using COPY_HOSTS from .env: $DOTENV_COPY_HOSTS"
|
|
||||||
IFS=',' read -ra HOSTS_FROM_ENV <<< "$DOTENV_COPY_HOSTS"
|
|
||||||
COPY_HOSTS=("${HOSTS_FROM_ENV[@]}")
|
|
||||||
else
|
|
||||||
echo "No hosts specified. Using autodiscovery..."
|
|
||||||
source "$(dirname "$0")/autodiscover.sh"
|
|
||||||
|
|
||||||
detect_nodes
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Autodiscovery failed."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ${#PEER_NODES[@]} -gt 0 ]; then
|
|
||||||
COPY_HOSTS=("${PEER_NODES[@]}")
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "${#COPY_HOSTS[@]}" -eq 0 ]; then
|
|
||||||
echo "Error: Autodiscovery found no other nodes."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "Autodiscovered hosts: ${COPY_HOSTS[*]}"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
continue
|
continue
|
||||||
;;
|
;;
|
||||||
-j|--build-jobs) BUILD_JOBS="$2"; shift ;;
|
-j|--build-jobs) BUILD_JOBS="$2"; shift ;;
|
||||||
@@ -380,17 +333,42 @@ while [[ "$#" -gt 0 ]]; do
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
--config) CONFIG_FILE="$2"; shift ;;
|
--config) CONFIG_FILE="$2"; CONFIG_FILE_SET=true; shift ;;
|
||||||
-h|--help) usage ;;
|
-h|--help) usage ;;
|
||||||
*) echo "Unknown parameter passed: $1"; usage ;;
|
*) echo "Unknown parameter passed: $1"; usage ;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
# Set CONFIG_FILE and source autodiscover.sh to load .env
|
# Source autodiscover.sh to load .env file
|
||||||
export CONFIG_FILE
|
|
||||||
source "$(dirname "$0")/autodiscover.sh"
|
source "$(dirname "$0")/autodiscover.sh"
|
||||||
|
|
||||||
|
# Handle COPY_HOSTS from .env or autodiscovery if not specified via arguments
|
||||||
|
if [ "${#COPY_HOSTS[@]}" -eq 0 ]; then
|
||||||
|
if [[ -n "$DOTENV_COPY_HOSTS" ]]; then
|
||||||
|
echo "Using COPY_HOSTS from .env: $DOTENV_COPY_HOSTS"
|
||||||
|
IFS=',' read -ra HOSTS_FROM_ENV <<< "$DOTENV_COPY_HOSTS"
|
||||||
|
COPY_HOSTS=("${HOSTS_FROM_ENV[@]}")
|
||||||
|
else
|
||||||
|
echo "No hosts specified. Using autodiscovery..."
|
||||||
|
detect_nodes
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Error: Autodiscovery failed."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ${#PEER_NODES[@]} -gt 0 ]; then
|
||||||
|
COPY_HOSTS=("${PEER_NODES[@]}")
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "${#COPY_HOSTS[@]}" -eq 0 ]; then
|
||||||
|
echo "Error: Autodiscovery found no other nodes."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Autodiscovered hosts: ${COPY_HOSTS[*]}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# Validate flag combinations
|
# Validate flag combinations
|
||||||
if [ -n "$VLLM_PRS" ]; then
|
if [ -n "$VLLM_PRS" ]; then
|
||||||
if [ "$EXP_MXFP4" = true ]; then echo "Error: --apply-vllm-pr is incompatible with --exp-mxfp4"; exit 1; fi
|
if [ "$EXP_MXFP4" = true ]; then echo "Error: --apply-vllm-pr is incompatible with --exp-mxfp4"; exit 1; fi
|
||||||
|
|||||||
@@ -159,9 +159,12 @@ done
|
|||||||
# Set .env file path (use default if not specified)
|
# Set .env file path (use default if not specified)
|
||||||
if [[ -z "$CONFIG_FILE" ]]; then
|
if [[ -z "$CONFIG_FILE" ]]; then
|
||||||
CONFIG_FILE="$SCRIPT_DIR/.env"
|
CONFIG_FILE="$SCRIPT_DIR/.env"
|
||||||
|
CONFIG_FILE_SET=false
|
||||||
|
else
|
||||||
|
CONFIG_FILE_SET=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Load .env file if exists
|
# Load .env file
|
||||||
if [[ -f "$CONFIG_FILE" ]]; then
|
if [[ -f "$CONFIG_FILE" ]]; then
|
||||||
echo "Loading configuration from .env file..."
|
echo "Loading configuration from .env file..."
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user