@@ -423,6 +423,66 @@ extract_package() {
423423 log_silent " Verified package structure (setup.sh, bitoarch, scripts/lib exist)"
424424}
425425
426+ # Merge new config keys from default env file into existing env file
427+ # This ensures new configuration options added in newer versions are available
428+ # with their default values while preserving user's existing configuration
429+ merge_new_env_configs () {
430+ local env_file=" $1 "
431+ local default_env_file=" ${NEW_DIR} /.env-bitoarch.default"
432+
433+ if [[ ! -f " $default_env_file " ]]; then
434+ log_silent " Skipping env config merge - default file not found: $default_env_file "
435+ return 0
436+ fi
437+
438+ if [[ ! -f " $env_file " ]]; then
439+ log_silent " Skipping env config merge - env file not found: $env_file "
440+ return 0
441+ fi
442+
443+ log_silent " Checking for new configuration options in newer version..."
444+
445+ local new_keys_added=0
446+ local temp_additions=$( mktemp)
447+
448+ # Read default env file line by line
449+ while IFS= read -r line || [[ -n " $line " ]]; do
450+ # Skip empty lines and comments
451+ [[ -z " $line " ]] && continue
452+ [[ " $line " =~ ^[[:space:]]* # ]] && continue
453+
454+ # Extract key from KEY=VALUE format
455+ if [[ " $line " =~ ^([A-Za-z_][A-Za-z0-9_]* )= ]]; then
456+ local key=" ${BASH_REMATCH[1]} "
457+
458+ # Check if this key already exists in the user's env file
459+ if ! grep -q " ^${key} =" " $env_file " 2> /dev/null; then
460+ # Key doesn't exist - add it with default value
461+ echo " $line " >> " $temp_additions "
462+ new_keys_added=$(( new_keys_added + 1 ))
463+ log_silent " New config key found: $key "
464+ fi
465+ fi
466+ done < " $default_env_file "
467+
468+ # If new keys were found, append them to the env file
469+ if [[ $new_keys_added -gt 0 ]]; then
470+ echo " " >> " $env_file "
471+ echo " # ============================================================================" >> " $env_file "
472+ echo " # NEW CONFIGURATION OPTIONS (added during upgrade from version ${CURRENT_VERSION:- unknown} )" >> " $env_file "
473+ echo " # Added on: $( date) " >> " $env_file "
474+ echo " # ============================================================================" >> " $env_file "
475+ cat " $temp_additions " >> " $env_file "
476+
477+ log_silent " Merged $new_keys_added new config keys from default to env file"
478+ else
479+ log_silent " No new config keys to merge - env file is up to date"
480+ fi
481+
482+ rm -f " $temp_additions "
483+ return 0
484+ }
485+
426486# Patch env file with IMAGE variables from new version
427487patch_env_with_images () {
428488 local env_file=" $1 "
508568 sed -i.bak " s/^CIS_TRACKER_VERSION=$/CIS_TRACKER_VERSION=${cis_tracker_version} /" " $env_file "
509569 fi
510570
571+ # Clean up sed backup files
572+ rm -f " ${env_file} .bak"
573+
511574 msg_success " Env file patched with IMAGE variables from new version"
512575 log_silent " Added IMAGE variables from new version: config=${cis_config_version} , manager=${cis_manager_version} , provider=${cis_provider_version} , tracker=${cis_tracker_version} , mysql=${mysql_version} "
513576}
@@ -535,6 +598,11 @@ migrate_config() {
535598 log_silent " Created .deployment-type with docker-compose for backward compatibility"
536599 fi
537600
601+ # Merge new config keys from default env file (adds missing keys with default values)
602+ if [[ -f " $NEW_ENV " ]]; then
603+ merge_new_env_configs " $NEW_ENV "
604+ fi
605+
538606 # Patch env file with IMAGE variables if missing (for old versions)
539607 if [[ -f " $NEW_ENV " ]]; then
540608 patch_env_with_images " $NEW_ENV "
@@ -577,8 +645,37 @@ migrate_config() {
577645 fi
578646 fi
579647 else
580- # Kubernetes: config is already in helm-bitoarch/services/cis-provider/config/default.json from package
581- msg_info " Kubernetes deployment - using packaged provider configuration"
648+ # Kubernetes: extract latest default.json from image to helm chart path for ConfigMap
649+ msg_info " Extracting latest provider configuration for Kubernetes deployment..."
650+ local k8s_config_path=" ${NEW_DIR} /helm-bitoarch/services/cis-provider/config/default.json"
651+
652+ if [[ -f " $NEW_ENV " ]]; then
653+ source " $NEW_ENV "
654+ local provider_image=" ${CIS_PROVIDER_IMAGE:- } "
655+
656+ if [[ -n " $provider_image " ]]; then
657+ if docker pull " $provider_image " >> " $LOG_FILE " 2>&1 ; then
658+ if docker create --name temp-provider-config-k8s-upgrade " $provider_image " > /dev/null 2>&1 ; then
659+ if docker cp temp-provider-config-k8s-upgrade:/opt/bito/xmcp/config/default.json " $k8s_config_path " 2>> " $LOG_FILE " ; then
660+ chmod 666 " $k8s_config_path " 2> /dev/null || true
661+ msg_success " Provider configuration extracted from new image for Kubernetes"
662+ log_silent " Extracted default.json to: $k8s_config_path "
663+ else
664+ msg_warn " Could not extract provider config from image, using packaged config"
665+ fi
666+ docker rm temp-provider-config-k8s-upgrade > /dev/null 2>&1 || true
667+ else
668+ msg_warn " Could not create temp container for config extraction"
669+ fi
670+ else
671+ msg_warn " Could not pull provider image for config extraction"
672+ fi
673+ else
674+ msg_warn " Provider image not set in env, using packaged config"
675+ fi
676+ else
677+ msg_warn " Env file not found, using packaged config"
678+ fi
582679 fi
583680
584681 msg_success " Configuration migrated"
0 commit comments