From a9e6c71f4e082be676280e01c280fe452d6ceb8f Mon Sep 17 00:00:00 2001 From: Judy Joseph Date: Mon, 15 Jun 2020 19:10:40 -0700 Subject: [PATCH 1/5] Support for connecting to DB in namespace via IP:port ( using docker bridge network ) for applications in multi-asic platform. --- dockers/docker-database/database_config.json.j2 | 2 +- dockers/docker-database/docker-database-init.sh | 17 ++++++++++++++++- dockers/docker-database/supervisord.conf.j2 | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dockers/docker-database/database_config.json.j2 b/dockers/docker-database/database_config.json.j2 index 3383ec161144..6d116b5e1ae0 100644 --- a/dockers/docker-database/database_config.json.j2 +++ b/dockers/docker-database/database_config.json.j2 @@ -1,7 +1,7 @@ { "INSTANCES": { "redis":{ - "hostname" : "127.0.0.1", + "hostname" : "{{HOST_IP}}", "port" : 6379, "unix_socket_path" : "/var/run/redis{{NAMESPACE_ID}}/redis.sock", "persistence_for_warm_boot" : "yes" diff --git a/dockers/docker-database/docker-database-init.sh b/dockers/docker-database/docker-database-init.sh index 5dae34d8f616..02938460182e 100755 --- a/dockers/docker-database/docker-database-init.sh +++ b/dockers/docker-database/docker-database-init.sh @@ -1,5 +1,17 @@ #!/usr/bin/env bash +# For linux host namespace, in both single and multi ASIC platform use the loopback interface +# For other namespaces, use eth0 interface which is connected to the docker0 bridge in the host. +if [[ $NAMESPACE_ID == "" ]] +then + INTFC=lo +else + INTFC=eth0 + LOOPBACK_IP=127.0.0.1 +fi +local_ip=$(ip -4 -o addr show $INTFC | awk '{print $4}' | cut -d'/' -f1) +export HOST_IP=$local_ip + REDIS_DIR=/var/run/redis$NAMESPACE_ID mkdir -p $REDIS_DIR/sonic-db @@ -22,6 +34,9 @@ then fi # generate all redis server supervisord configuration file -sonic-cfggen -j /var/run/redis/sonic-db/database_config.json -t /usr/share/sonic/templates/supervisord.conf.j2 > /etc/supervisor/conf.d/supervisord.conf +# Pass the Loopback IP explicitly in case of namespaces other than the linux host namespace. +# This is needed as there are applications who hardcoded host=loopback IP in Connector classes. +# Redis server will then bind on multiple IP addresses viz , +sonic-cfggen -j /var/run/redis/sonic-db/database_config.json -a "{\"LOOPBACK_IP\":\"$LOOPBACK_IP\"}" -t /usr/share/sonic/templates/supervisord.conf.j2 > /etc/supervisor/conf.d/supervisord.conf exec /usr/bin/supervisord diff --git a/dockers/docker-database/supervisord.conf.j2 b/dockers/docker-database/supervisord.conf.j2 index 44268274cdad..a28b408c2e40 100644 --- a/dockers/docker-database/supervisord.conf.j2 +++ b/dockers/docker-database/supervisord.conf.j2 @@ -20,7 +20,7 @@ stderr_logfile=syslog {% if INSTANCES %} {% for redis_inst, redis_items in INSTANCES.iteritems() %} [program: {{ redis_inst }}] -command=/bin/bash -c "{ [[ -s /var/lib/{{ redis_inst }}/dump.rdb ]] || rm -f /var/lib/{{ redis_inst }}/dump.rdb; } && mkdir -p /var/lib/{{ redis_inst }} && exec /usr/bin/redis-server /etc/redis/redis.conf --port {{ redis_items['port'] }} --unixsocket {{ redis_items['unix_socket_path'] }} --pidfile /var/run/redis/{{ redis_inst }}.pid --dir /var/lib/{{ redis_inst }}" +command=/bin/bash -c "{ [[ -s /var/lib/{{ redis_inst }}/dump.rdb ]] || rm -f /var/lib/{{ redis_inst }}/dump.rdb; } && mkdir -p /var/lib/{{ redis_inst }} && exec /usr/bin/redis-server /etc/redis/redis.conf --bind {{ LOOPBACK_IP }} {{ redis_items['hostname'] }} --port {{ redis_items['port'] }} --unixsocket {{ redis_items['unix_socket_path'] }} --pidfile /var/run/redis/{{ redis_inst }}.pid --dir /var/lib/{{ redis_inst }}" priority=2 autostart=true autorestart=false From 24a95e85f32baa32ec32a6dd04200e13196f1f80 Mon Sep 17 00:00:00 2001 From: Judy Joseph Date: Tue, 23 Jun 2020 11:22:13 -0700 Subject: [PATCH 2/5] Updates based on review comments. --- dockers/docker-database/docker-database-init.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dockers/docker-database/docker-database-init.sh b/dockers/docker-database/docker-database-init.sh index 02938460182e..cb057f423a09 100755 --- a/dockers/docker-database/docker-database-init.sh +++ b/dockers/docker-database/docker-database-init.sh @@ -9,7 +9,13 @@ else INTFC=eth0 LOOPBACK_IP=127.0.0.1 fi -local_ip=$(ip -4 -o addr show $INTFC | awk '{print $4}' | cut -d'/' -f1) +local_ip=$(ip -4 -o addr show $INTFC | awk '{print $4}' | cut -d'/' -f1 | head -1) + +# if the ip address was not retrieved correctly, put the loopbackIP as the default. +if [[ $local_ip == "" ]] +then + local_ip=127.0.0.1 +fi export HOST_IP=$local_ip REDIS_DIR=/var/run/redis$NAMESPACE_ID From 525a97c22de83dc4de131856c64bba06fc9612b6 Mon Sep 17 00:00:00 2001 From: Judy Joseph Date: Wed, 24 Jun 2020 12:37:11 -0700 Subject: [PATCH 3/5] Added the default IP as 127.0.0.1 if the IPaddress derivation from interface fails. Moved the localhost loopback IP binding logic into the supervisor.j2 file. --- .../docker-database/docker-database-init.sh | 20 +++++++++---------- dockers/docker-database/supervisord.conf.j2 | 5 +++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/dockers/docker-database/docker-database-init.sh b/dockers/docker-database/docker-database-init.sh index cb057f423a09..7620d3c6f4f0 100755 --- a/dockers/docker-database/docker-database-init.sh +++ b/dockers/docker-database/docker-database-init.sh @@ -7,16 +7,19 @@ then INTFC=lo else INTFC=eth0 - LOOPBACK_IP=127.0.0.1 fi -local_ip=$(ip -4 -o addr show $INTFC | awk '{print $4}' | cut -d'/' -f1 | head -1) -# if the ip address was not retrieved correctly, put the loopbackIP as the default. -if [[ $local_ip == "" ]] +# Get the ip address of the interface +# if the ip address was not retrieved correctly, put localhost(127.0.0.1) as the default. +host_ip=$(ip -4 -o addr show $INTFC | awk '{print $4}' | cut -d'/' -f1 | head -1) +if [[ $host_ip == "" ]] then - local_ip=127.0.0.1 + host_ip=127.0.0.1 fi -export HOST_IP=$local_ip + +# Export the host_ip as environment variable used in database_config.json.j2 to derive the hostname +# as defined "hostname" : "{{HOST_IP}}" +export HOST_IP=$host_ip REDIS_DIR=/var/run/redis$NAMESPACE_ID mkdir -p $REDIS_DIR/sonic-db @@ -40,9 +43,6 @@ then fi # generate all redis server supervisord configuration file -# Pass the Loopback IP explicitly in case of namespaces other than the linux host namespace. -# This is needed as there are applications who hardcoded host=loopback IP in Connector classes. -# Redis server will then bind on multiple IP addresses viz , -sonic-cfggen -j /var/run/redis/sonic-db/database_config.json -a "{\"LOOPBACK_IP\":\"$LOOPBACK_IP\"}" -t /usr/share/sonic/templates/supervisord.conf.j2 > /etc/supervisor/conf.d/supervisord.conf +sonic-cfggen -j /var/run/redis/sonic-db/database_config.json -t /usr/share/sonic/templates/supervisord.conf.j2 > /etc/supervisor/conf.d/supervisord.conf exec /usr/bin/supervisord diff --git a/dockers/docker-database/supervisord.conf.j2 b/dockers/docker-database/supervisord.conf.j2 index a28b408c2e40..3aede96cdb5a 100644 --- a/dockers/docker-database/supervisord.conf.j2 +++ b/dockers/docker-database/supervisord.conf.j2 @@ -20,6 +20,11 @@ stderr_logfile=syslog {% if INSTANCES %} {% for redis_inst, redis_items in INSTANCES.iteritems() %} [program: {{ redis_inst }}] +{%- if redis_items['hostname'] != '127.0.0.1'-%} +{%- set LOOPBACK_IP = '127.0.0.1'-%} +{%- else -%} +{%- set LOOPBACK_IP = '' -%} +{%- endif -%} command=/bin/bash -c "{ [[ -s /var/lib/{{ redis_inst }}/dump.rdb ]] || rm -f /var/lib/{{ redis_inst }}/dump.rdb; } && mkdir -p /var/lib/{{ redis_inst }} && exec /usr/bin/redis-server /etc/redis/redis.conf --bind {{ LOOPBACK_IP }} {{ redis_items['hostname'] }} --port {{ redis_items['port'] }} --unixsocket {{ redis_items['unix_socket_path'] }} --pidfile /var/run/redis/{{ redis_inst }}.pid --dir /var/lib/{{ redis_inst }}" priority=2 autostart=true From 13906496c064d913214f0ebed384c10c4b1f85f4 Mon Sep 17 00:00:00 2001 From: Judy Joseph Date: Thu, 25 Jun 2020 21:04:53 -0700 Subject: [PATCH 4/5] HOST_IP j2 template argument passed on command line, i.s.o exporting. --- dockers/docker-database/docker-database-init.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/dockers/docker-database/docker-database-init.sh b/dockers/docker-database/docker-database-init.sh index 7620d3c6f4f0..645a7d99272a 100755 --- a/dockers/docker-database/docker-database-init.sh +++ b/dockers/docker-database/docker-database-init.sh @@ -17,17 +17,13 @@ then host_ip=127.0.0.1 fi -# Export the host_ip as environment variable used in database_config.json.j2 to derive the hostname -# as defined "hostname" : "{{HOST_IP}}" -export HOST_IP=$host_ip - REDIS_DIR=/var/run/redis$NAMESPACE_ID mkdir -p $REDIS_DIR/sonic-db if [ -f /etc/sonic/database_config$NAMESPACE_ID.json ]; then cp /etc/sonic/database_config$NAMESPACE_ID.json $REDIS_DIR/sonic-db/database_config.json else - j2 /usr/share/sonic/templates/database_config.json.j2 > $REDIS_DIR/sonic-db/database_config.json + HOST_IP=$host_ip j2 /usr/share/sonic/templates/database_config.json.j2 > $REDIS_DIR/sonic-db/database_config.json fi mkdir -p /etc/supervisor/conf.d/ From 80e77b291d953f52b555fcc73931f232cf4352cc Mon Sep 17 00:00:00 2001 From: Judy Joseph Date: Sun, 28 Jun 2020 19:25:16 -0700 Subject: [PATCH 5/5] Fix to get a newline after program: This was causing redis server to not start up as command string was coming in same line as program: --- dockers/docker-database/supervisord.conf.j2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dockers/docker-database/supervisord.conf.j2 b/dockers/docker-database/supervisord.conf.j2 index 3aede96cdb5a..6d4557dab705 100644 --- a/dockers/docker-database/supervisord.conf.j2 +++ b/dockers/docker-database/supervisord.conf.j2 @@ -20,8 +20,8 @@ stderr_logfile=syslog {% if INSTANCES %} {% for redis_inst, redis_items in INSTANCES.iteritems() %} [program: {{ redis_inst }}] -{%- if redis_items['hostname'] != '127.0.0.1'-%} -{%- set LOOPBACK_IP = '127.0.0.1'-%} +{% if redis_items['hostname'] != '127.0.0.1' %} +{%- set LOOPBACK_IP = '127.0.0.1' -%} {%- else -%} {%- set LOOPBACK_IP = '' -%} {%- endif -%}