From 3ec95e17c87bdcf933194f1b521073805044fc91 Mon Sep 17 00:00:00 2001 From: Myron Sosyak <49795530+msosyak@users.noreply.github.com> Date: Thu, 6 Jun 2019 00:41:30 -0700 Subject: [PATCH] [build_templates] [hostcfgd] Keep containers hostname up to date (#2924) * Add updateHostName function to docker_image_ctl.j2 * Add hostname specification on container creating step * Add listener for hostname changes in hostcfgd Signed-off-by: Myron Sosyak --- files/build_templates/docker_image_ctl.j2 | 40 ++++++++++++++++-- files/image_config/hostcfgd/hostcfgd | 50 +++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index aab3789332..9b11ea5175 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -5,6 +5,32 @@ function getMountPoint() echo $1 | python -c "import sys, json, os; mnts = [x for x in json.load(sys.stdin)[0]['Mounts'] if x['Destination'] == '/usr/share/sonic/hwsku']; print '' if len(mnts) == 0 else os.path.basename(mnts[0]['Source'])" 2>/dev/null } +function updateHostName() +{ + HOSTS=/etc/hosts + HOSTS_TMP=/etc/hosts.tmp + + EXEC="docker exec -i {{docker_container_name}} bash -c" + + NEW_HOSTNAME="$1" + HOSTNAME=`$EXEC "hostname"` + if ! [[ $HOSTNAME =~ ^[a-zA-Z0-9.\-]*$ ]]; then + HOSTNAME=`hostname` + fi + + # copy HOSTS to HOSTS_TMP + $EXEC "cp $HOSTS $HOSTS_TMP" + # remove entry with hostname + $EXEC "sed -i \"/$HOSTNAME$/d\" $HOSTS_TMP" + # add entry with new hostname + $EXEC "echo -e \"127.0.0.1\t$NEW_HOSTNAME\" >> $HOSTS_TMP" + + echo "Set hostname in {{docker_container_name}} container" + $EXEC "hostname '$NEW_HOSTNAME'" + $EXEC "cat $HOSTS_TMP > $HOSTS" + $EXEC "rm -f $HOSTS_TMP" +} + function getBootType() { local BOOT_TYPE @@ -105,6 +131,10 @@ start() { # Obtain our HWSKU as we will mount directories with these names in each docker HWSKU=`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'` {%- endif %} + HOSTNAME=`sonic-cfggen -m -v 'DEVICE_METADATA["localhost"]["hostname"]'` + if [ -z "$HOSTNAME" ] || ! [[ $HOSTNAME =~ ^[a-zA-Z0-9.\-]*$ ]]; then + HOSTNAME=`hostname` + fi DOCKERCHECK=`docker inspect --type container {{docker_container_name}} 2>/dev/null` if [ "$?" -eq "0" ]; then @@ -121,6 +151,7 @@ start() { {%- endif %} preStartAction docker start {{docker_container_name}} + updateHostName "$HOSTNAME" postStartAction exit $? fi @@ -167,6 +198,7 @@ start() { --tmpfs /tmp \ {%- endif %} --tmpfs /var/tmp \ + --hostname "$HOSTNAME" \ --name={{docker_container_name}} {{docker_image_name}}:latest || { echo "Failed to docker run" >&1 exit 4 @@ -186,11 +218,13 @@ stop() { } case "$1" in - start|wait|stop) - $1 + start|wait|stop|updateHostName) + cmd=$1 + shift + $cmd $@ ;; *) - echo "Usage: $0 {start|wait|stop}" + echo "Usage: $0 {start|wait|stop|updateHostName new_hostname}" exit 1 ;; esac diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index ae51af5cc8..739c0baa4d 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import os +import re import sys import subprocess import syslog @@ -22,6 +23,15 @@ TACPLUS_SERVER_TIMEOUT_DEFAULT = "5" TACPLUS_SERVER_AUTH_TYPE_DEFAULT = "pap" +def is_valid_hostname(name): + if hostname[-1] == ".": + hostname = hostname[:-1] # strip exactly one dot from the right, if present + if len(hostname) > 253: + return False + allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?