Skip to content

Commit

Permalink
[MultiDB] sonic-utilities - replace redis-cli/redis-dump with sonic-d…
Browse files Browse the repository at this point in the history
…b-cli/sonic-db-dump (#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
  • Loading branch information
dzhangalibaba authored Feb 28, 2020
1 parent 8aea564 commit 66e9dfb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
5 changes: 5 additions & 0 deletions crm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def show_acl_table_resources(self):
header = ("Table ID", "Resource Name", "Used Count", "Available Count")

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

Expand Down
1 change: 0 additions & 1 deletion scripts/aclshow
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class AclStat(object):
"""
Get ACL counters from the DB
"""
acl_counters_cmd = "docker exec -it database redis-cli --csv -n 2 hgetall COUNTERS:"
counters_cnt = len(self.acl_rules) # num of counters should be the same as rules
for table, rule in self.acl_rules.keys():
cnt_props = lowercase_keys(self.db.get_all(self.db.COUNTERS_DB, "COUNTERS:%s:%s" % (table, rule)))
Expand Down
18 changes: 9 additions & 9 deletions scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function init_warm_reboot_states()
# the current DB contents will likely mark warm reboot is done.
# Clear these states so that the next boot up image won't get confused.
if [[ "$REBOOT_TYPE" = "warm-reboot" || "$REBOOT_TYPE" = "fastfast-reboot" ]]; then
redis-cli -n 6 eval "
sonic-db-cli STATE_DB eval "
for _, key in ipairs(redis.call('keys', 'WARM_RESTART_TABLE|*')) do
redis.call('hdel', key, 'state')
end
Expand All @@ -132,11 +132,11 @@ function initialize_pre_shutdown()
{
debug "Initialize pre-shutdown ..."
TABLE="WARM_RESTART_TABLE|warm-shutdown"
RESTORE_COUNT=`/usr/bin/redis-cli -n 6 hget "${TABLE}" restore_count`
RESTORE_COUNT=`sonic-db-cli STATE_DB hget "${TABLE}" restore_count`
if [[ -z "$RESTORE_COUNT" ]]; then
/usr/bin/redis-cli -n 6 hset "${TABLE}" "restore_count" "0" > /dev/null
sonic-db-cli STATE_DB hset "${TABLE}" "restore_count" "0" > /dev/null
fi
/usr/bin/redis-cli -n 6 hset "${TABLE}" "state" "requesting" > /dev/null
sonic-db-cli STATE_DB hset "${TABLE}" "state" "requesting" > /dev/null
}
function request_pre_shutdown()
Expand All @@ -160,7 +160,7 @@ function wait_for_pre_shutdown_complete_or_fail()
while [[ ${waitcount} -lt 600 ]]; do
# timeout doesn't work with -i option of "docker exec". Therefore we have
# to invoke docker exec directly below.
STATE=`timeout 5s docker exec database redis-cli -n 6 hget "${TABLE}" state; if [[ $? == 124 ]]; then echo "timed out"; fi`
STATE=`timeout 5s sonic-db-cli STATE_DB hget "${TABLE}" state; if [[ $? == 124 ]]; then echo "timed out"; fi`
if [[ x"${STATE}" == x"timed out" ]]; then
waitcount+=50
Expand Down Expand Up @@ -190,7 +190,7 @@ function backup_database()
# Dump redis content to a file 'dump.rdb' in warmboot directory
mkdir -p $WARM_DIR
# Delete keys in stateDB except FDB_TABLE|*, MIRROR_SESSION_TABLE|*, WARM_RESTART_ENABLE_TABLE|*
redis-cli -n 6 eval "
sonic-db-cli STATE_DB eval "
for _, k in ipairs(redis.call('keys', '*')) do
if not string.match(k, 'FDB_TABLE|') and not string.match(k, 'WARM_RESTART_TABLE|') \
and not string.match(k, 'MIRROR_SESSION_TABLE|') \
Expand Down Expand Up @@ -481,9 +481,9 @@ if [[ "$REBOOT_TYPE" = "warm-reboot" || "$REBOOT_TYPE" = "fastfast-reboot" ]]; t
# Warm reboot: dump state to host disk
if [[ "$REBOOT_TYPE" = "fastfast-reboot" ]]; then
redis-cli -n 1 FLUSHDB > /dev/null
redis-cli -n 2 FLUSHDB > /dev/null
redis-cli -n 5 FLUSHDB > /dev/null
sonic-db-cli ASIC_DB FLUSHDB > /dev/null
sonic-db-cli COUNTERS_DB FLUSHDB > /dev/null
sonic-db-cli FLEX_COUNTER_DB FLUSHDB > /dev/null
fi
# TODO: backup_database preserves FDB_TABLE
Expand Down
18 changes: 8 additions & 10 deletions scripts/generate_dump
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,13 @@ save_proc() {
###############################################################################
# Dumps all fields and values from given Redis DB.
# Arguments:
# DB id: id of DB for redis-cli
# DB name: filename to which output will be saved
# Returns:
# None
###############################################################################
save_redis() {
local db=$1
local db_name=$2
save_cmd "redis-dump -d $db -s /var/run/redis/redis.sock -y" "$db_name.json"
local db_name=$1
save_cmd "sonic-db-dump -n '$db_name' -y" "$db_name.json"
}

###############################################################################
Expand Down Expand Up @@ -372,12 +370,12 @@ main() {

save_nat_info

save_redis "0" "APP_DB"
save_redis "1" "ASIC_DB"
save_redis "2" "COUNTERS_DB"
save_redis "4" "CONFIG_DB"
save_redis "5" "FLEX_COUNTER_DB"
save_redis "6" "STATE_DB"
save_redis "APPL_DB"
save_redis "ASIC_DB"
save_redis "COUNTERS_DB"
save_redis "CONFIG_DB"
save_redis "FLEX_COUNTER_DB"
save_redis "STATE_DB"

save_cmd "docker ps -a" "docker.ps"
save_cmd "docker top pmon" "docker.pmon"
Expand Down
12 changes: 6 additions & 6 deletions scripts/route_check_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

# add a route, interface & route-entry to simulate error
#
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"
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"


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"
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"

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

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

redis-cli -n 0 del "ROUTE_TABLE:20c0:d9b8:99:80::/64"
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\"}"
redis-cli -n 0 del "INTF_TABLE:PortChannel01:10.0.0.99/31"
sonic-db-cli APPL_DB del "ROUTE_TABLE:20c0:d9b8:99:80::/64"
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\"}"
sonic-db-cli APPL_DB del "INTF_TABLE:PortChannel01:10.0.0.99/31"


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

0 comments on commit 66e9dfb

Please sign in to comment.