Skip to content

Commit c6a08f7

Browse files
theasianpianistyxieca
authored andcommitted
[scripts]: add support to db_migrator for non-default unix socket (#551)
* [scripts]: add support to db_migrator for non-default unix socket * add '-s' option to db_migrator Signed-off-by: Lawrence Lee <[email protected]> * [scripts]: make db_migrator follow python convention * change variable names Signed-off-by: Lawrence Lee <[email protected]>
1 parent 98e087f commit c6a08f7

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

scripts/db_migrator.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
import traceback
34
import sys
45
import argparse
56
import syslog
@@ -22,7 +23,7 @@ def log_error(msg):
2223

2324

2425
class DBMigrator():
25-
def __init__(self):
26+
def __init__(self, socket=None):
2627
"""
2728
Version string format:
2829
version_<major>_<minor>_<build>
@@ -39,7 +40,12 @@ def __init__(self):
3940
self.TABLE_NAME = 'VERSIONS'
4041
self.TABLE_KEY = 'DATABASE'
4142
self.TABLE_FIELD = 'VERSION'
42-
self.configDB = ConfigDBConnector()
43+
44+
db_kwargs = {}
45+
if socket:
46+
db_kwargs['unix_socket_path'] = socket
47+
48+
self.configDB = ConfigDBConnector(**db_kwargs)
4349
self.configDB.db_connect('CONFIG_DB')
4450

4551

@@ -120,16 +126,30 @@ def main():
120126
choices=['migrate', 'set_version', 'get_version'],
121127
help = 'operation to perform [default: get_version]',
122128
default='get_version')
129+
parser.add_argument('-s',
130+
dest='socket',
131+
metavar='unix socket',
132+
type = str,
133+
required = False,
134+
help = 'the unix socket that the desired database listens on',
135+
default = None )
123136
args = parser.parse_args()
124137
operation = args.operation
138+
socket_path = args.socket
139+
140+
if socket_path:
141+
dbmgtr = DBMigrator(socket=socket_path)
142+
else:
143+
dbmgtr = DBMigrator()
125144

126-
dbmgtr = DBMigrator()
127145
result = getattr(dbmgtr, operation)()
128146
if result:
129147
print(str(result))
130148

131149
except Exception as e:
132-
log_error('Caught excetion: ' + str(e))
150+
log_error('Caught exception: ' + str(e))
151+
traceback.print_exc()
152+
print(str(e))
133153
parser.print_help()
134154
sys.exit(1)
135155

0 commit comments

Comments
 (0)