Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@ sudo LANG=C cp $SCRIPTS_DIR/mgmt-framework.sh $FILESYSTEM_ROOT/usr/local/bin/mgm
sudo LANG=C cp $SCRIPTS_DIR/asic_status.sh $FILESYSTEM_ROOT/usr/local/bin/asic_status.sh
sudo LANG=C cp $SCRIPTS_DIR/asic_status.py $FILESYSTEM_ROOT/usr/local/bin/asic_status.py
sudo LANG=C cp $SCRIPTS_DIR/startup_tsa_tsb.py $FILESYSTEM_ROOT/usr/local/bin/startup_tsa_tsb.py
sudo LANG=C cp $SCRIPTS_DIR/sonic-db-aliases.py $FILESYSTEM_ROOT/usr/local/bin/sonic-db-aliases.py
sudo LANG=C cp $SCRIPTS_DIR/sonic-dpu-mgmt-traffic.sh $FILESYSTEM_ROOT/usr/local/bin/sonic-dpu-mgmt-traffic.sh
sudo LANG=C cp $SCRIPTS_DIR/dash-ha.sh $FILESYSTEM_ROOT/usr/local/bin/dash-ha.sh
# Copy sonic-netns-exec script
Expand Down
54 changes: 54 additions & 0 deletions files/image_config/bash/bash.bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,57 @@ if [ -n "$SSH_TARGET_CONSOLE_LINE" ]; then
exit
fi
fi

# Helper function to generate all redis-cli aliases at once
generate_sonic_redis_aliases() {
# Define DB names and alias suffixes (Single Source of Truth)
local -A SONIC_DBS=(
["APPL_DB"]="appdb"
["ASIC_DB"]="asicdb"
["COUNTERS_DB"]="counterdb"
["LOGLEVEL_DB"]="logleveldb"
["CONFIG_DB"]="configdb"
["FLEX_COUNTER_DB"]="flexcounterdb"
["STATE_DB"]="statedb"
["APPL_STATE_DB"]="appstatedb"
)

# Extract DB names (keys) from the associative array to pass to Python
local db_keys=("${!SONIC_DBS[@]}")

# Run the external Python script, passing DB names as arguments
local python_output
python_output=$(/usr/local/bin/sonic-db-aliases.py "${db_keys[@]}" 2>&1)
local python_exit_code=$?

# Check if Python command failed catastrophically (e.g., module not found)
if [ $python_exit_code -ne 0 ]; then
echo "Error generating Redis aliases: $python_output" >&2
return 1
fi

# Check if redis-cli exists
if ! command -v redis-cli &> /dev/null; then
echo "Error: redis-cli command not found" >&2
return 1
fi

# Parse output and create aliases
while IFS=: read -r db_name db_id db_port; do
# Skip lines that contain errors printed by the python script
if [[ "$db_name" == "ERROR" ]]; then
echo "$db_name:$db_id:$db_port" >&2
continue
fi

if [ -n "$db_name" ] && [ -n "$db_id" ] && [ -n "$db_port" ]; then
local alias_name="redis-${SONIC_DBS[$db_name]}"
if [ -n "$alias_name" ]; then
eval "alias $alias_name='redis-cli -n $db_id -p $db_port'"
fi
fi
done <<< "$python_output"
}

# Generate all aliases at shell startup
generate_sonic_redis_aliases
47 changes: 47 additions & 0 deletions files/scripts/sonic-db-aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
import sys

# Expect DB names as command-line arguments
db_names = sys.argv[1:]

if not db_names:
print('ERROR:No database names provided as arguments', file=sys.stderr)
sys.exit(1)

try:
import swsscommon.swsscommon

SonicDBConfig = None

if hasattr(swsscommon.swsscommon, 'SonicDBConfig'):
SonicDBConfig = swsscommon.swsscommon.SonicDBConfig

if SonicDBConfig is None:
print('ERROR:SonicDBConfig not found in swsscommon module', file=sys.stderr)
sys.exit(1)

# Initialize config (some versions require this)
try:
SonicDBConfig.loadSonicDBConfig()
except AttributeError:
pass # Some versions don't have this method
except Exception:
pass # Ignore if config file is missing

# Iterate over provided arguments
for db in db_names:
try:
db_id = SonicDBConfig.getDbId(db)
db_port = SonicDBConfig.getDbPort(db)
# Output format: DB_NAME:DB_ID:DB_PORT
print(f'{db}:{db_id}:{db_port}')
except Exception as e:
# Print error to stderr (will be captured by bash)
print(f'ERROR:Failed to get config for {db}: {e}', file=sys.stderr)

except ImportError as e:
print(f'ERROR:swsscommon module not found: {e}', file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f'ERROR:Unexpected error: {e}', file=sys.stderr)
sys.exit(1)
Loading