Skip to content

Commit 66e9dfb

Browse files
[MultiDB] sonic-utilities - replace redis-cli/redis-dump with sonic-db-cli/sonic-db-dump (sonic-net#810)
* [MultiDB] sonic-utilities - replace redis-cli/redis-dump with sonic-db-cli/sonic-db-dump * only accept upper and underscore to prevent injection * quotation on db_name
1 parent 8aea564 commit 66e9dfb

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

crm/main.py

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def show_acl_table_resources(self):
124124
header = ("Table ID", "Resource Name", "Used Count", "Available Count")
125125

126126
# Retrieve all ACL table keys from CRM:ACL_TABLE_STATS
127+
# TODO
128+
# Volodymyr is working on refactoring codes to access redis database via redis-py or swsssdk
129+
# we should avoid using 'keys' operation via redis-cli or sonic-db-cli
130+
# there would be an issue when KEY in database contains space or '\n'
131+
# for loop on the non-tty 'keys' output will take the space or `\n` as seperator when parsing the element
127132
proc = Popen("docker exec -i database redis-cli --raw -n 2 KEYS *CRM:ACL_TABLE_STATS*", stdout=PIPE, stderr=PIPE, shell=True)
128133
out, err = proc.communicate()
129134

scripts/aclshow

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ class AclStat(object):
131131
"""
132132
Get ACL counters from the DB
133133
"""
134-
acl_counters_cmd = "docker exec -it database redis-cli --csv -n 2 hgetall COUNTERS:"
135134
counters_cnt = len(self.acl_rules) # num of counters should be the same as rules
136135
for table, rule in self.acl_rules.keys():
137136
cnt_props = lowercase_keys(self.db.get_all(self.db.COUNTERS_DB, "COUNTERS:%s:%s" % (table, rule)))

scripts/fast-reboot

+9-9
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function init_warm_reboot_states()
120120
# the current DB contents will likely mark warm reboot is done.
121121
# Clear these states so that the next boot up image won't get confused.
122122
if [[ "$REBOOT_TYPE" = "warm-reboot" || "$REBOOT_TYPE" = "fastfast-reboot" ]]; then
123-
redis-cli -n 6 eval "
123+
sonic-db-cli STATE_DB eval "
124124
for _, key in ipairs(redis.call('keys', 'WARM_RESTART_TABLE|*')) do
125125
redis.call('hdel', key, 'state')
126126
end
@@ -132,11 +132,11 @@ function initialize_pre_shutdown()
132132
{
133133
debug "Initialize pre-shutdown ..."
134134
TABLE="WARM_RESTART_TABLE|warm-shutdown"
135-
RESTORE_COUNT=`/usr/bin/redis-cli -n 6 hget "${TABLE}" restore_count`
135+
RESTORE_COUNT=`sonic-db-cli STATE_DB hget "${TABLE}" restore_count`
136136
if [[ -z "$RESTORE_COUNT" ]]; then
137-
/usr/bin/redis-cli -n 6 hset "${TABLE}" "restore_count" "0" > /dev/null
137+
sonic-db-cli STATE_DB hset "${TABLE}" "restore_count" "0" > /dev/null
138138
fi
139-
/usr/bin/redis-cli -n 6 hset "${TABLE}" "state" "requesting" > /dev/null
139+
sonic-db-cli STATE_DB hset "${TABLE}" "state" "requesting" > /dev/null
140140
}
141141
142142
function request_pre_shutdown()
@@ -160,7 +160,7 @@ function wait_for_pre_shutdown_complete_or_fail()
160160
while [[ ${waitcount} -lt 600 ]]; do
161161
# timeout doesn't work with -i option of "docker exec". Therefore we have
162162
# to invoke docker exec directly below.
163-
STATE=`timeout 5s docker exec database redis-cli -n 6 hget "${TABLE}" state; if [[ $? == 124 ]]; then echo "timed out"; fi`
163+
STATE=`timeout 5s sonic-db-cli STATE_DB hget "${TABLE}" state; if [[ $? == 124 ]]; then echo "timed out"; fi`
164164
165165
if [[ x"${STATE}" == x"timed out" ]]; then
166166
waitcount+=50
@@ -190,7 +190,7 @@ function backup_database()
190190
# Dump redis content to a file 'dump.rdb' in warmboot directory
191191
mkdir -p $WARM_DIR
192192
# Delete keys in stateDB except FDB_TABLE|*, MIRROR_SESSION_TABLE|*, WARM_RESTART_ENABLE_TABLE|*
193-
redis-cli -n 6 eval "
193+
sonic-db-cli STATE_DB eval "
194194
for _, k in ipairs(redis.call('keys', '*')) do
195195
if not string.match(k, 'FDB_TABLE|') and not string.match(k, 'WARM_RESTART_TABLE|') \
196196
and not string.match(k, 'MIRROR_SESSION_TABLE|') \
@@ -481,9 +481,9 @@ if [[ "$REBOOT_TYPE" = "warm-reboot" || "$REBOOT_TYPE" = "fastfast-reboot" ]]; t
481481
482482
# Warm reboot: dump state to host disk
483483
if [[ "$REBOOT_TYPE" = "fastfast-reboot" ]]; then
484-
redis-cli -n 1 FLUSHDB > /dev/null
485-
redis-cli -n 2 FLUSHDB > /dev/null
486-
redis-cli -n 5 FLUSHDB > /dev/null
484+
sonic-db-cli ASIC_DB FLUSHDB > /dev/null
485+
sonic-db-cli COUNTERS_DB FLUSHDB > /dev/null
486+
sonic-db-cli FLEX_COUNTER_DB FLUSHDB > /dev/null
487487
fi
488488
489489
# TODO: backup_database preserves FDB_TABLE

scripts/generate_dump

+8-10
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,13 @@ save_proc() {
190190
###############################################################################
191191
# Dumps all fields and values from given Redis DB.
192192
# Arguments:
193-
# DB id: id of DB for redis-cli
194193
# DB name: filename to which output will be saved
195194
# Returns:
196195
# None
197196
###############################################################################
198197
save_redis() {
199-
local db=$1
200-
local db_name=$2
201-
save_cmd "redis-dump -d $db -s /var/run/redis/redis.sock -y" "$db_name.json"
198+
local db_name=$1
199+
save_cmd "sonic-db-dump -n '$db_name' -y" "$db_name.json"
202200
}
203201

204202
###############################################################################
@@ -372,12 +370,12 @@ main() {
372370

373371
save_nat_info
374372

375-
save_redis "0" "APP_DB"
376-
save_redis "1" "ASIC_DB"
377-
save_redis "2" "COUNTERS_DB"
378-
save_redis "4" "CONFIG_DB"
379-
save_redis "5" "FLEX_COUNTER_DB"
380-
save_redis "6" "STATE_DB"
373+
save_redis "APPL_DB"
374+
save_redis "ASIC_DB"
375+
save_redis "COUNTERS_DB"
376+
save_redis "CONFIG_DB"
377+
save_redis "FLEX_COUNTER_DB"
378+
save_redis "STATE_DB"
381379

382380
save_cmd "docker ps -a" "docker.ps"
383381
save_cmd "docker top pmon" "docker.pmon"

scripts/route_check_test.sh

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
# add a route, interface & route-entry to simulate error
44
#
5-
redis-cli -n 0 hmset "ROUTE_TABLE:20c0:d9b8:99:80::/64" "nexthop" "fc00::72,fc00::76,fc00::7a,fc00::7e" "ifname" "PortChannel01,PortChannel02,PortChannel03,PortChannel04"
5+
sonic-db-cli APPL_DB hmset "ROUTE_TABLE:20c0:d9b8:99:80::/64" "nexthop" "fc00::72,fc00::76,fc00::7a,fc00::7e" "ifname" "PortChannel01,PortChannel02,PortChannel03,PortChannel04"
66

77

8-
redis-cli -n 1 hmset "ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY:{\"dest\":\"192.193.120.255/25\",\"switch_id\":\"oid:0x21000000000000\",\"vr\":\"oid:0x3000000000022\"}" "SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID" "oid:0x5000000000614"
8+
sonic-db-cli ASIC_DB hmset "ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY:{\"dest\":\"192.193.120.255/25\",\"switch_id\":\"oid:0x21000000000000\",\"vr\":\"oid:0x3000000000022\"}" "SAI_ROUTE_ENTRY_ATTR_NEXT_HOP_ID" "oid:0x5000000000614"
99

10-
redis-cli -n 0 hmset "INTF_TABLE:PortChannel01:10.0.0.99/31" "scope" "global" "family" "IPv4"
10+
sonic-db-cli APPL_DB hmset "INTF_TABLE:PortChannel01:10.0.0.99/31" "scope" "global" "family" "IPv4"
1111

1212
echo "expect errors!\n------\nRunning Route Check...\n"
1313
./route_check.py
1414
echo "return value: $?"
1515

16-
redis-cli -n 0 del "ROUTE_TABLE:20c0:d9b8:99:80::/64"
17-
redis-cli -n 1 del "ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY:{\"dest\":\"192.193.120.255/25\",\"switch_id\":\"oid:0x21000000000000\",\"vr\":\"oid:0x3000000000022\"}"
18-
redis-cli -n 0 del "INTF_TABLE:PortChannel01:10.0.0.99/31"
16+
sonic-db-cli APPL_DB del "ROUTE_TABLE:20c0:d9b8:99:80::/64"
17+
sonic-db-cli ASIC_DB del "ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY:{\"dest\":\"192.193.120.255/25\",\"switch_id\":\"oid:0x21000000000000\",\"vr\":\"oid:0x3000000000022\"}"
18+
sonic-db-cli APPL_DB del "INTF_TABLE:PortChannel01:10.0.0.99/31"
1919

2020

2121
echo "expect success!\n------\nRunning Route Check...\n"

0 commit comments

Comments
 (0)