Skip to content

Commit

Permalink
Fix comments
Browse files Browse the repository at this point in the history
Signed-off-by: Myron Sosyak <[email protected]>
  • Loading branch information
Myron Sosyak committed May 31, 2019
1 parent 7393f80 commit 574f237
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
12 changes: 8 additions & 4 deletions files/build_templates/docker_image_ctl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function updateHostName()

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"
Expand All @@ -23,8 +26,9 @@ function updateHostName()
$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 "hostname '$NEW_HOSTNAME'"
$EXEC "cat $HOSTS_TMP > $HOSTS"
$EXEC "rm -f $HOSTS_TMP"
}

function getBootType()
Expand Down Expand Up @@ -128,7 +132,7 @@ start() {
HWSKU=`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`
{%- endif %}
HOSTNAME=`sonic-cfggen -m -v 'DEVICE_METADATA["localhost"]["hostname"]'`
if [ -z "$HOSTNAME" ]; then
if [ -z "$HOSTNAME" ] || ! [[ $HOSTNAME =~ ^[a-zA-Z0-9.\-]*$ ]]; then
HOSTNAME=`hostname`
fi

Expand All @@ -147,7 +151,7 @@ start() {
{%- endif %}
preStartAction
docker start {{docker_container_name}}
updateHostName $HOSTNAME
updateHostName "$HOSTNAME"
postStartAction
exit $?
fi
Expand Down Expand Up @@ -185,7 +189,7 @@ start() {
{%- endif %}
--tmpfs /tmp \
--tmpfs /var/tmp \
--hostname $HOSTNAME \
--hostname "$HOSTNAME" \
--name={{docker_container_name}} {{docker_image_name}}:latest || {
echo "Failed to docker run" >&1
exit 4
Expand Down
31 changes: 25 additions & 6 deletions files/image_config/hostcfgd/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import os
import re
import sys
import subprocess
import syslog
Expand All @@ -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}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))


def is_true(val):
if val == 'True' or val == 'true':
return True
Expand Down Expand Up @@ -168,7 +178,16 @@ class HostConfigDaemon:
syslog.syslog(syslog.LOG_INFO, 'value of {} changed to {}'.format(key, log_data))

def hostname_handler(self, key, data):
hostname = data["hostname"]
if key != "localhost":
return

hostname = data.get("hostname")

if not hostname:
syslog.syslog(syslog.LOG_WARNING, "hostname key is missing")
return
if not is_valid_hostname(hostname):
return
if hostname == self.hostname_cache:
return

Expand All @@ -177,22 +196,22 @@ class HostConfigDaemon:
try:
containers = subprocess.check_output(cmd, shell=True).split("\n")[:-1]
except subprocess.CalledProcessError as err:
syslog.syslog(syslog.LOG_WARNING, "{} - failed: return code - {}, output:\n{}"
.format(err.cmd, err.returncode, err.output))
syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}"
.format(err.cmd, err.returncode, err.output))

for name in containers:
script = '/usr/bin/{}.sh'.format(name)
exists = os.path.isfile(script)
if not exists:
syslog.syslog(syslog.LOG_INFO, "Can't find controll script for {}".format(name))
syslog.syslog(syslog.LOG_ERR, "Can't find control script for {}".format(name))
continue

cmd = "{} updateHostName {}".format(script, hostname)
try:
subprocess.check_call(cmd, shell=True)
except subprocess.CalledProcessError as err:
syslog.syslog(syslog.LOG_WARNING, "{} - failed: return code - {}, output:\n{}"
.format(err.cmd, err.returncode, err.output))
syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}"
.format(err.cmd, err.returncode, err.output))

self.hostname_cache = hostname

Expand Down

0 comments on commit 574f237

Please sign in to comment.