From 66e9dfb98f64f840a5525e932d556dac93c1d9b3 Mon Sep 17 00:00:00 2001 From: Dong Zhang <41927498+dzhangalibaba@users.noreply.github.com> Date: Fri, 28 Feb 2020 13:42:42 -0800 Subject: [PATCH] [MultiDB] sonic-utilities - replace redis-cli/redis-dump with sonic-db-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 --- crm/main.py | 5 +++++ scripts/aclshow | 1 - scripts/fast-reboot | 18 +++++++++--------- scripts/generate_dump | 18 ++++++++---------- scripts/route_check_test.sh | 12 ++++++------ 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/crm/main.py b/crm/main.py index 696d7fdedf8d..241362ed136d 100644 --- a/crm/main.py +++ b/crm/main.py @@ -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() diff --git a/scripts/aclshow b/scripts/aclshow index 5eb9cb0d8ccc..a574a2325fd6 100755 --- a/scripts/aclshow +++ b/scripts/aclshow @@ -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))) diff --git a/scripts/fast-reboot b/scripts/fast-reboot index ae9a1382b5da..66309fa85e7e 100755 --- a/scripts/fast-reboot +++ b/scripts/fast-reboot @@ -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 @@ -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() @@ -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 @@ -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|') \ @@ -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 diff --git a/scripts/generate_dump b/scripts/generate_dump index 3b3667e6dcdb..132d78f3c567 100755 --- a/scripts/generate_dump +++ b/scripts/generate_dump @@ -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" } ############################################################################### @@ -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" diff --git a/scripts/route_check_test.sh b/scripts/route_check_test.sh index eb6f9075603b..505253863ed0 100755 --- a/scripts/route_check_test.sh +++ b/scripts/route_check_test.sh @@ -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"