2020-11-09 10:35:36 -06:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import argparse
|
2020-10-14 17:15:24 -05:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import syslog
|
|
|
|
|
|
|
|
database_config_file = "/var/run/redis/sonic-db/database_config.json"
|
|
|
|
redis_chassis = 'redis_chassis'
|
2020-12-15 19:15:00 -06:00
|
|
|
chassis_db_list = ['CHASSIS_APP_DB', 'CHASSIS_STATE_DB']
|
2020-10-14 17:15:24 -05:00
|
|
|
|
2020-11-09 10:35:36 -06:00
|
|
|
|
2020-10-14 17:15:24 -05:00
|
|
|
def main():
|
2020-12-03 17:57:50 -06:00
|
|
|
parser = argparse.ArgumentParser(description="Update chassis_db config from database-config.json")
|
2020-10-14 17:15:24 -05:00
|
|
|
parser.add_argument("-j", "--json", help="databse-config json file", nargs='?',
|
2020-12-03 17:57:50 -06:00
|
|
|
const=database_config_file)
|
|
|
|
parser.add_argument("-p", "--port", help="update port number", nargs='?')
|
2020-10-14 17:15:24 -05:00
|
|
|
group = parser.add_mutually_exclusive_group()
|
2020-12-03 17:57:50 -06:00
|
|
|
group.add_argument("-k", "--keep", help="keep configuration", action='store_true')
|
|
|
|
group.add_argument("-d", "--delete", help="delete configuration", action='store_true')
|
2020-10-14 17:15:24 -05:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
jsonfile = ""
|
|
|
|
if args.json != None:
|
|
|
|
jsonfile = args.json
|
|
|
|
else:
|
2020-12-03 17:57:50 -06:00
|
|
|
return
|
2020-10-14 17:15:24 -05:00
|
|
|
if args.port != None:
|
|
|
|
port_number = args.port
|
|
|
|
else:
|
|
|
|
port_number = ""
|
|
|
|
if args.keep:
|
2020-12-03 17:57:50 -06:00
|
|
|
keep_config = True
|
2020-10-14 17:15:24 -05:00
|
|
|
else:
|
2020-12-03 17:57:50 -06:00
|
|
|
keep_config = False
|
2020-10-14 17:15:24 -05:00
|
|
|
if args.delete:
|
2020-12-03 17:57:50 -06:00
|
|
|
delete_config = True
|
2020-10-14 17:15:24 -05:00
|
|
|
else:
|
2020-12-03 17:57:50 -06:00
|
|
|
delete_config = False
|
2020-10-14 17:15:24 -05:00
|
|
|
data = {}
|
|
|
|
data_keep = {}
|
|
|
|
if os.path.isfile(jsonfile):
|
|
|
|
with open(jsonfile, "r") as read_file:
|
|
|
|
data = json.load(read_file)
|
|
|
|
else:
|
2020-12-03 17:57:50 -06:00
|
|
|
syslog.syslog(syslog.LOG_ERR,
|
|
|
|
'config file {} does notexist'.format(jsonfile))
|
|
|
|
return
|
2020-10-14 17:15:24 -05:00
|
|
|
if 'INSTANCES' in data and redis_chassis in data['INSTANCES']:
|
2020-12-15 19:15:00 -06:00
|
|
|
data_keep['INSTANCES'] = {}
|
|
|
|
data_keep['INSTANCES'][redis_chassis] = data['INSTANCES'][redis_chassis]
|
|
|
|
if delete_config:
|
|
|
|
del data['INSTANCES'][redis_chassis]
|
|
|
|
for chassis_db in chassis_db_list:
|
|
|
|
if 'DATABASES' in data and chassis_db in data['DATABASES']:
|
|
|
|
data_keep['DATABASES'] = {}
|
|
|
|
data_keep['DATABASES'][chassis_db] = data['DATABASES'][chassis_db]
|
|
|
|
if delete_config:
|
|
|
|
del data['DATABASES'][chassis_db]
|
|
|
|
|
2020-10-14 17:15:24 -05:00
|
|
|
with open(jsonfile, "w") as write_file:
|
2020-12-03 17:57:50 -06:00
|
|
|
data_publish = data_keep if keep_config else data
|
|
|
|
if port_number:
|
|
|
|
data_publish['INSTANCES']['redis_chassis']['port'] = int(port_number)
|
|
|
|
json.dump(data_publish, write_file, indent=4, separators=(',', ': '))
|
2020-10-14 17:15:24 -05:00
|
|
|
syslog.syslog(syslog.LOG_INFO,
|
2020-12-03 17:57:50 -06:00
|
|
|
'remove chassis_db from config file {}'.format(jsonfile))
|
2020-10-14 17:15:24 -05:00
|
|
|
|
2020-11-09 10:35:36 -06:00
|
|
|
|
2020-10-14 17:15:24 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|