sonic-buildimage/dockers/docker-database/flush_unused_database
Myron Sosyak e7009513da [docker-database] Fix Python3 issue (#7700)
#### Why I did it
To avoid the following error
```
Traceback (most recent call last):
  File "/usr/local/bin/flush_unused_database", line 10, in <module>
    if 'PONG' in output:
TypeError: a bytes-like object is required, not 'str'
```
`communicate` method returns the strings if streams were opened in text mode; otherwise, bytes.
In our case text arg  in Popen is not true and that means that `communicate` return the bytes
#### How I did it
Set `text=True` to get strings instead of bytes
#### How to verify it
run `/usr/local/bin/flush_unused_database` inside database container
2021-06-02 02:39:31 +00:00

29 lines
815 B
Python
Executable File

#!/usr/bin/python3
import swsssdk
import redis
import subprocess
import time
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 = swsssdk.SonicDBConfig.get_instancelist()
for instname, v in instlists.items():
insthost = v['hostname']
instsocket = v['unix_socket_path']
dblists = swsssdk.SonicDBConfig.get_dblist()
for dbname in dblists:
dbid = swsssdk.SonicDBConfig.get_dbid(dbname)
dbinst = swsssdk.SonicDBConfig.get_instancename(dbname)
# this DB is on current instance, skip flush
if dbinst == instname:
continue
r = redis.Redis(host=insthost, unix_socket_path=instsocket, db=dbid)
r.flushdb()