diff --git a/files/scripts/remove_chassisdb_config b/files/scripts/remove_chassisdb_config new file mode 100755 index 0000000000..1583042ce5 --- /dev/null +++ b/files/scripts/remove_chassisdb_config @@ -0,0 +1,40 @@ +#!/usr/bin/python +import json +import os +import syslog +import argparse + +database_config_file = "/var/run/redis/sonic-db/database_config.json" +redis_chassis = 'redis_chassis' +chassis_db = 'CHASSIS_DB' + +def main(): + parser=argparse.ArgumentParser(description= + "Remove chassis_db config from database-config.json") + parser.add_argument("-j", "--json", help="databse-config json file", nargs='?', + const=database_config_file) + args = parser.parse_args() + jsonfile = "" + if args.json != None: + jsonfile = args.json + else: + return + data = {} + if os.path.isfile(jsonfile): + with open(jsonfile, "r") as read_file: + data = json.load(read_file) + else: + syslog.syslog(syslog.LOG_ERR, + 'config file {} does notexist'.format(jsonfile)) + return + if 'INSTANCES' in data and redis_chassis in data['INSTANCES']: + del data['INSTANCES'][redis_chassis] + if 'DATABASES' in data and chassis_db in data['DATABASES']: + del data['DATABASES'][chassis_db] + with open(jsonfile, "w") as write_file: + json.dump(data, write_file, indent=4, separators=(',', ': ')) + syslog.syslog(syslog.LOG_INFO, + 'remove chassis_db from config file {}'.format(jsonfile)) + +if __name__ == "__main__": + main() diff --git a/platform/vs/docker-sonic-vs.mk b/platform/vs/docker-sonic-vs.mk index e9ebf4d8ae..2e0ead9157 100644 --- a/platform/vs/docker-sonic-vs.mk +++ b/platform/vs/docker-sonic-vs.mk @@ -37,7 +37,8 @@ $(DOCKER_SONIC_VS)_FILES += $(CONFIGDB_LOAD_SCRIPT) \ $(ARP_UPDATE_SCRIPT) \ $(BUFFERS_CONFIG_TEMPLATE) \ $(QOS_CONFIG_TEMPLATE) \ - $(SONIC_VERSION) + $(SONIC_VERSION) \ + $(RM_CHASSISDB_CONFIG_SCRIPT) $(DOCKER_SONIC_VS)_LOAD_DOCKERS += $(DOCKER_CONFIG_ENGINE_BUSTER) SONIC_DOCKER_IMAGES += $(DOCKER_SONIC_VS) diff --git a/platform/vs/docker-sonic-vs/Dockerfile.j2 b/platform/vs/docker-sonic-vs/Dockerfile.j2 index 73a5d154bc..0947fdd47b 100644 --- a/platform/vs/docker-sonic-vs/Dockerfile.j2 +++ b/platform/vs/docker-sonic-vs/Dockerfile.j2 @@ -120,13 +120,16 @@ RUN sed -ri 's/^(save .*$)/# \1/g; ' /etc/redis/redis.conf COPY ["50-default.conf", "/etc/rsyslog.d/"] -COPY ["start.sh", "orchagent.sh", "/usr/bin/"] +COPY ["start.sh", "orchagent.sh", "files/remove_chassisdb_config", "/usr/bin/"] COPY ["supervisord.conf", "/etc/supervisor/conf.d/"] COPY ["files/configdb-load.sh", "/usr/bin/"] COPY ["files/arp_update", "/usr/bin/"] COPY ["files/buffers_config.j2", "files/qos_config.j2", "/usr/share/sonic/templates/"] COPY ["files/sonic_version.yml", "/etc/sonic/"] COPY ["database_config.json", "/etc/default/sonic-db/"] +COPY ["hostname.j2", "/usr/share/sonic/templates/"] +COPY ["default_chassis_cfg.json", "/etc/default/sonic-db/"] +COPY ["chassis_db.py", "/usr/bin/"] # Workaround the tcpdump issue RUN mv /usr/sbin/tcpdump /usr/bin/tcpdump diff --git a/platform/vs/docker-sonic-vs/chassis_db.py b/platform/vs/docker-sonic-vs/chassis_db.py new file mode 100644 index 0000000000..a51a339145 --- /dev/null +++ b/platform/vs/docker-sonic-vs/chassis_db.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +import swsssdk +import json +chassisdb = swsssdk.SonicV2Connector(host='10.0.0.200', port='6380') +chassisdb.connect(chassisdb.CHASSIS_DB) +fname='/usr/share/sonic/virtual_chassis/chassis_db.json' +with open(fname) as f: + js = json.load(f) + client=chassisdb.get_redis_client(chassisdb.CHASSIS_DB) + for h, table in js.items(): + for k, v in table.items(): + client.hset(h, k, v) diff --git a/platform/vs/docker-sonic-vs/database_config.json b/platform/vs/docker-sonic-vs/database_config.json index b86ae11bba..f3ed663a40 100644 --- a/platform/vs/docker-sonic-vs/database_config.json +++ b/platform/vs/docker-sonic-vs/database_config.json @@ -4,6 +4,12 @@ "hostname" : "127.0.0.1", "port" : 6379, "unix_socket_path" : "/var/run/redis/redis.sock" + }, + + "redis_chassis":{ + "hostname" : "redis_chassis.server", + "port": 6380, + "unix_socket_path": "/var/run/redis/redis_chassis.sock" } }, "DATABASES" : { @@ -51,6 +57,11 @@ "id" : 7, "separator": "|", "instance" : "redis" + }, + "CHASSIS_DB" : { + "id" : 8, + "separator": "|", + "instance" : "redis_chassis" } }, "VERSION" : "1.0" diff --git a/platform/vs/docker-sonic-vs/default_chassis_cfg.json b/platform/vs/docker-sonic-vs/default_chassis_cfg.json new file mode 100644 index 0000000000..673a4791fa --- /dev/null +++ b/platform/vs/docker-sonic-vs/default_chassis_cfg.json @@ -0,0 +1,7 @@ +{ + "DEVICE_METADATA": { + "localhost": { + "chassis_db_address" : "10.8.1.200" + } + } +} diff --git a/platform/vs/docker-sonic-vs/hostname.j2 b/platform/vs/docker-sonic-vs/hostname.j2 new file mode 100644 index 0000000000..91d7cbaaeb --- /dev/null +++ b/platform/vs/docker-sonic-vs/hostname.j2 @@ -0,0 +1,3 @@ +{% if DEVICE_METADATA.localhost.chassis_db_address %} +{{ DEVICE_METADATA.localhost.chassis_db_address }} redis_chassis.server +{% endif %} diff --git a/platform/vs/docker-sonic-vs/start.sh b/platform/vs/docker-sonic-vs/start.sh index 54d585f29e..5d576c18b1 100755 --- a/platform/vs/docker-sonic-vs/start.sh +++ b/platform/vs/docker-sonic-vs/start.sh @@ -26,8 +26,12 @@ popd [ -d /etc/sonic ] || mkdir -p /etc/sonic +if [[ -f /usr/share/sonic/virtual_chassis/default_config.json ]]; then + CHASS_CFG="-j /usr/share/sonic/virtual_chassis/default_config.json" +fi + SYSTEM_MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}') -sonic-cfggen -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' --print-data > /etc/sonic/init_cfg.json +sonic-cfggen -a '{"DEVICE_METADATA":{"localhost": {"mac": "'$SYSTEM_MAC_ADDRESS'"}}}' $CHASS_CFG --print-data > /etc/sonic/init_cfg.json if [ -f /etc/sonic/config_db.json ]; then sonic-cfggen -j /etc/sonic/init_cfg.json -j /etc/sonic/config_db.json --print-data > /tmp/config_db.json @@ -46,11 +50,39 @@ rm -f /var/run/rsyslogd.pid supervisorctl start rsyslogd +supervisord_cfg="/etc/supervisor/conf.d/supervisord.conf" +chassis_cfg_file="/usr/share/sonic/virtual_chassis/default_config.json" +chassis_cfg_file_default="/etc/default/sonic-db/default_chassis_cfg.json" +host_template="/usr/share/sonic/templates/hostname.j2" +db_cfg_file="/var/run/redis/sonic-db/database_config.json" +db_cfg_file_tmp="/var/run/redis/sonic-db/database_config.json.tmp" + +if [ -r "$chassis_cfg_file" ]; then + echo $(sonic-cfggen -j $chassis_cfg_file -t $host_template) >> /etc/hosts +else + chassis_cfg_file="$chassis_cfg_file_default" + echo "10.8.1.200 redis_chassis.server" >> /etc/hosts +fi + mkdir -p /var/run/redis/sonic-db cp /etc/default/sonic-db/database_config.json /var/run/redis/sonic-db/ supervisorctl start redis-server +start_chassis_db=`sonic-cfggen -v DEVICE_METADATA.localhost.start_chassis_db -y $chassis_cfg_file` +if [[ "$HOSTNAME" == *"supervisor"* ]] || [ "$start_chassis_db" == "1" ]; then + supervisorctl start redis-chassis + python /usr/bin/chassis_db.py +fi + +conn_chassis_db=`sonic-cfggen -v DEVICE_METADATA.localhost.connect_to_chassis_db -y $chassis_cfg_file` +if [ "$start_chassis_db" != "1" ] && [ "$conn_chassis_db" != "1" ]; then + cp $db_cfg_file $db_cfg_file_tmp + remove_chassisdb_config -j $db_cfg_file_tmp + cp $db_cfg_file_tmp $db_cfg_file +fi + + /usr/bin/configdb-load.sh supervisorctl start syncd diff --git a/platform/vs/docker-sonic-vs/supervisord.conf b/platform/vs/docker-sonic-vs/supervisord.conf index 3a7acfd20b..abf09574db 100644 --- a/platform/vs/docker-sonic-vs/supervisord.conf +++ b/platform/vs/docker-sonic-vs/supervisord.conf @@ -27,6 +27,14 @@ autorestart=false stdout_logfile=syslog stderr_logfile=syslog +[program:redis-chassis] +command=/bin/bash -c "{ [[ -s /var/lib/redis_chassis/dump.rdb ]] || rm -f /var/lib/redis_chassis/dump.rdb; } && mkdir -p /var/lib/redis_chassis && exec /usr/bin/redis-server /etc/redis/redis.conf --bind redis_chassis.server --port 6380 --unixsocket /var/run/redis/redis_chassis.sock --pidfile /var/run/redis/redis_chassis.pid --dir /var/lib/redis_chassis" +priority=3 +autostart=false +autorestart=false +stdout_logfile=syslog +stderr_logfile=syslog + [program:syncd] command=/usr/bin/syncd_start.sh priority=4 diff --git a/rules/scripts.mk b/rules/scripts.mk index b3ed0cab95..bae3b92538 100644 --- a/rules/scripts.mk +++ b/rules/scripts.mk @@ -17,9 +17,13 @@ $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT)_PATH = files/scripts SYSCTL_NET_CONFIG = sysctl-net.conf $(SYSCTL_NET_CONFIG)_PATH = files/image_config/sysctl +RM_CHASSISDB_CONFIG_SCRIPT = remove_chassisdb_config +$(RM_CHASSISDB_CONFIG_SCRIPT)_PATH = files/scripts + SONIC_COPY_FILES += $(CONFIGDB_LOAD_SCRIPT) \ $(ARP_UPDATE_SCRIPT) \ $(BUFFERS_CONFIG_TEMPLATE) \ $(QOS_CONFIG_TEMPLATE) \ $(SUPERVISOR_PROC_EXIT_LISTENER_SCRIPT) \ - $(SYSCTL_NET_CONFIG) + $(SYSCTL_NET_CONFIG) \ + $(RM_CHASSISDB_CONFIG_SCRIPT)