Skip to content

Commit 13188ac

Browse files
dzhangalibabaabdosi
authored andcommitted
[multi-DB] Part 4: add sonic-db-cli to replace redis-cli (#54)
* [multi-DB] Part 4: add sonic-db-cli to replace redis-cli * use SonicV2Connector instead and fix previous if condition typo * remove unused import * use_unix_socket_path set to False by default to avoid unnecessary failed * update Usage message and add example under -h * remove logging and update usage msg
1 parent 677c32b commit 13188ac

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package_dir={'swsssdk': 'src/swsssdk'},
1515
packages=['swsssdk'],
1616
package_data={'swsssdk': ['config/*.json']},
17+
scripts=['src/swsssdk/scripts/sonic-db-cli'],
1718
license='Apache 2.0',
1819
author='SONiC Team',
1920
author_email='[email protected]',

src/swsssdk/dbconnector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ def get_separator(db_name):
103103
return SonicDBConfig._sonic_db_config["DATABASES"][db_name]["separator"]
104104

105105
class SonicV2Connector(DBInterface):
106-
def __init__(self, use_unix_socket_path=True, **kwargs):
106+
def __init__(self, use_unix_socket_path=False, **kwargs):
107107
super(SonicV2Connector, self).__init__(**kwargs)
108108
self.use_unix_socket_path = use_unix_socket_path
109109
for db_name in self.get_db_list():
110110
# set a database name as a constant value attribute.
111111
setattr(self, db_name, db_name)
112112

113113
def connect(self, db_name, retry_on=True):
114-
if self.use_unix_socket_path == False:
114+
if self.use_unix_socket_path:
115115
self.redis_kwargs["unix_socket_path"] = self.get_db_socket(db_name)
116116
self.redis_kwargs["host"] = None
117117
self.redis_kwargs["port"] = None

src/swsssdk/scripts/sonic-db-cli

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/python
2+
import sys
3+
import swsssdk
4+
5+
argc = len(sys.argv)
6+
if argc == 2 and sys.argv[1] == '-h':
7+
print "Example 1: sonic-db-cli CONFIG_DB keys *"
8+
print "Example 2: sonic-db-cli APPL_DB HGETALL VLAN_TABLE:Vlan10"
9+
print "Example 3: sonic-db-cli APPL_DB HGET VLAN_TABLE:Vlan10 mtu"
10+
elif argc < 3:
11+
msg = "'Usage: sonic-db-cli <db_name> <cmd> [arg [arg ...]]'. See 'sonic-db-cli -h' for detail examples."
12+
print >> sys.stderr, msg
13+
else:
14+
dbname = sys.argv[1]
15+
dbconn = swsssdk.SonicV2Connector(use_unix_socket_path=False)
16+
try:
17+
dbconn.connect(dbname)
18+
except RuntimeError:
19+
msg = "Invalid database name input : '{}'".format(sys.argv[1])
20+
print >> sys.stderr, msg
21+
else:
22+
client = dbconn.get_redis_client(dbname)
23+
print client.execute_command(" ".join(sys.argv[2:]))

0 commit comments

Comments
 (0)