2021-05-20 17:47:46 -05:00
|
|
|
#!/usr/bin/python3
|
2022-07-10 21:01:10 -05:00
|
|
|
from swsscommon import swsscommon
|
2020-12-10 13:06:19 -06:00
|
|
|
import subprocess
|
|
|
|
import time
|
2022-05-24 18:54:12 -05:00
|
|
|
import syslog
|
2020-12-10 13:06:19 -06:00
|
|
|
|
|
|
|
while(True):
|
2021-05-31 07:36:24 -05:00
|
|
|
output = subprocess.Popen(['sonic-db-cli', 'PING'], stdout=subprocess.PIPE, text=True).communicate()[0]
|
2020-12-10 13:06:19 -06:00
|
|
|
if 'PONG' in output:
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-08-02 21:18:00 -05:00
|
|
|
instlists = swsscommon.SonicDBConfig.getInstanceList()
|
2020-12-10 13:06:19 -06:00
|
|
|
for instname, v in instlists.items():
|
2022-08-02 21:18:00 -05:00
|
|
|
insthost = v.hostname
|
|
|
|
instsocket = v.unixSocketPath
|
2020-12-10 13:06:19 -06:00
|
|
|
|
2022-08-02 21:18:00 -05:00
|
|
|
dblists = swsscommon.SonicDBConfig.getDbList()
|
2020-12-10 13:06:19 -06:00
|
|
|
for dbname in dblists:
|
2022-08-02 21:18:00 -05:00
|
|
|
dbid = swsscommon.SonicDBConfig.getDbId(dbname)
|
|
|
|
dbinst = swsscommon.SonicDBConfig.getDbInst(dbname)
|
2020-12-10 13:06:19 -06:00
|
|
|
|
|
|
|
# this DB is on current instance, skip flush
|
|
|
|
if dbinst == instname:
|
|
|
|
continue
|
|
|
|
|
2022-05-24 18:54:12 -05:00
|
|
|
try:
|
2023-06-29 17:08:54 -05:00
|
|
|
# Migrate code from py-redis to swsscommon, original code:
|
|
|
|
# r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
|
|
|
|
# py-redis will use TCP connection when unix_socket_path is None
|
|
|
|
# https://github.com/redis/redis-py/blob/d95d8a24ed2af3eae80b7b0f14cbccc9dbe86e96/redis/client.py#L1006
|
|
|
|
if instsocket is not None:
|
|
|
|
# connect with Unix socket
|
|
|
|
connector = swsscommon.DBConnector(dbid, instsocket, 0)
|
|
|
|
else:
|
|
|
|
# connect with TCP socket
|
|
|
|
port = swsscommon.SonicDBConfig.getDbPort(dbname);
|
|
|
|
connector = swsscommon.DBConnector(dbid, insthost, port, 0)
|
|
|
|
|
|
|
|
connector.flushdb()
|
|
|
|
except RuntimeError:
|
2022-05-24 18:54:12 -05:00
|
|
|
syslog.syslog(syslog.LOG_INFO,"flushdb:Redis Unix Socket connection error for path {} and dbaname {}".format(instsocket, dbname))
|