sonic-buildimage/dockers/docker-database/flush_unused_database
Hua Liu a9b7a1facd
Replace swsssdk with swsscommon (#11215)
#### Why I did it
Update scripts in sonic-buildimage from py-swsssdk to swsscommon


#### How I did it
Change code to use swsscommon.

#### How to verify it
Pass all E2E test case

#### Which release branch to backport (provide reason below if selected)

<!--
- Note we only backport fixes to a release branch, *not* features!
- Please also provide a reason for the backporting below.
- e.g.
- [x] 202006
-->

- [ ] 201811
- [ ] 201911
- [ ] 202006
- [ ] 202012
- [ ] 202106
- [ ] 202111
- [ ] 202205

#### Description for the changelog
Update scripts in sonic-buildimage from py-swsssdk to swsscommon

#### Link to config_db schema for YANG module changes
<!--
Provide a link to config_db schema for the table for which YANG model
is defined
Link should point to correct section on https://github.com/Azure/sonic-buildimage/blob/master/src/sonic-yang-models/doc/Configuration.md
-->

#### A picture of a cute animal (not mandatory but encouraged)
2022-07-11 10:01:10 +08:00

33 lines
1.0 KiB
Python
Executable File

#!/usr/bin/python3
from swsscommon import swsscommon
import redis
import subprocess
import time
import syslog
while(True):
output = subprocess.Popen(['sonic-db-cli', 'PING'], stdout=subprocess.PIPE, text=True).communicate()[0]
if 'PONG' in output:
break
time.sleep(1)
instlists = swsscommon.SonicDBConfig.get_instancelist()
for instname, v in instlists.items():
insthost = v['hostname']
instsocket = v['unix_socket_path']
dblists = swsscommon.SonicDBConfig.get_dblist()
for dbname in dblists:
dbid = swsscommon.SonicDBConfig.get_dbid(dbname)
dbinst = swsscommon.SonicDBConfig.get_instancename(dbname)
# this DB is on current instance, skip flush
if dbinst == instname:
continue
try:
r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
r.flushdb()
except (redis.exceptions.ConnectionError):
syslog.syslog(syslog.LOG_INFO,"flushdb:Redis Unix Socket connection error for path {} and dbaname {}".format(instsocket, dbname))