From e20c9276455276c9957c5326c61d88a34ca262d4 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Tue, 20 Oct 2020 02:28:21 +0000 Subject: [PATCH 1/6] [hostcfgd] Make compatible with Python 3 --- files/image_config/hostcfgd/hostcfgd | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/files/image_config/hostcfgd/hostcfgd b/files/image_config/hostcfgd/hostcfgd index 75086b45fe1a..b5341065e4ec 100755 --- a/files/image_config/hostcfgd/hostcfgd +++ b/files/image_config/hostcfgd/hostcfgd @@ -1,16 +1,16 @@ -#!/usr/bin/python -u -# -*- coding: utf-8 -*- +#!/usr/bin/env python3 import ast +import copy +import ipaddress import os -import sys import subprocess +import sys import syslog -import copy + import jinja2 -import ipaddr as ipaddress -from swsssdk import ConfigDBConnector from sonic_py_common import device_info +from swsssdk import ConfigDBConnector # FILE PAM_AUTH_CONF = "/etc/pam.d/common-auth-sonic" @@ -80,7 +80,7 @@ class Iptables(object): iface, ip = key ip_str = ip.split("/")[0] - ip_addr = ipaddress.IPAddress(ip_str) + ip_addr = ipaddress.ip_address(ip_str) if isinstance(ip_addr, ipaddress.IPv6Address): ver = '6' else: @@ -313,7 +313,7 @@ class HostConfigDaemon: def update_all_feature_states(self): feature_table = self.config_db.get_table('FEATURE') - for feature_name in feature_table.keys(): + for feature_name in list(feature_table.keys()): if not feature_name: syslog.syslog(syslog.LOG_WARNING, "Feature is None") continue @@ -359,7 +359,7 @@ class HostConfigDaemon: def feature_state_handler(self, key, data): feature_name = key feature_table = self.config_db.get_table('FEATURE') - if feature_name not in feature_table.keys(): + if feature_name not in list(feature_table.keys()): syslog.syslog(syslog.LOG_WARNING, "Feature '{}' not in FEATURE table".format(feature_name)) return From 7e63693c00d0acf356e7267c1e23e55934406213 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 24 Oct 2020 00:30:10 +0000 Subject: [PATCH 2/6] [hostcfgd] Add to sonic-host-services package --- files/build_templates/sonic_debian_extension.j2 | 6 ------ src/sonic-host-services-data/MAINTAINERS | 2 +- src/sonic-host-services-data/debian/install | 1 + src/sonic-host-services-data/debian/rules | 1 + .../debian/sonic-host-services-data.hostcfgd.service | 2 +- .../templates}/common-auth-sonic.j2 | 0 .../sonic-host-services-data/templates}/tacplus_nss.conf.j2 | 0 src/sonic-host-services/.gitignore | 1 + .../hostcfgd => src/sonic-host-services/scripts}/hostcfgd | 0 src/sonic-host-services/setup.py | 2 ++ 10 files changed, 7 insertions(+), 8 deletions(-) rename files/image_config/hostcfgd/hostcfgd.service => src/sonic-host-services-data/debian/sonic-host-services-data.hostcfgd.service (82%) rename {files/image_config/hostcfgd => src/sonic-host-services-data/templates}/common-auth-sonic.j2 (100%) rename {files/image_config/hostcfgd => src/sonic-host-services-data/templates}/tacplus_nss.conf.j2 (100%) rename {files/image_config/hostcfgd => src/sonic-host-services/scripts}/hostcfgd (100%) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 52b6205e1487..ad37bb312911 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -324,12 +324,6 @@ sudo cp files/dhcp/90-dhcp6-systcl.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMP sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network/interfaces sudo mkdir -p $FILESYSTEM_ROOT/etc/network/interfaces.d -# Copy hostcfgd files -sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM -echo "hostcfgd.service" | sudo tee -a $GENERATED_SERVICE_FILE -sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd $FILESYSTEM_ROOT/usr/bin/ -sudo cp $IMAGE_CONFIGS/hostcfgd/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ - # copy core file uploader files sudo cp $IMAGE_CONFIGS/corefile_uploader/core_uploader.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM sudo LANG=C chroot $FILESYSTEM_ROOT systemctl disable core_uploader.service diff --git a/src/sonic-host-services-data/MAINTAINERS b/src/sonic-host-services-data/MAINTAINERS index fde1eede3214..09c497897e62 100644 --- a/src/sonic-host-services-data/MAINTAINERS +++ b/src/sonic-host-services-data/MAINTAINERS @@ -1,4 +1,4 @@ -# This file describes the maintainers for sonic-device-data +# This file describes the maintainers for sonic-host-services-data # See the SONiC project governance document for more information Name = "Joe LeVeque" diff --git a/src/sonic-host-services-data/debian/install b/src/sonic-host-services-data/debian/install index e69de29bb2d1..839eb8d0e127 100644 --- a/src/sonic-host-services-data/debian/install +++ b/src/sonic-host-services-data/debian/install @@ -0,0 +1 @@ +templates/*.j2 /usr/share/sonic/templates/ diff --git a/src/sonic-host-services-data/debian/rules b/src/sonic-host-services-data/debian/rules index 65ed966382c4..9de6b974f599 100755 --- a/src/sonic-host-services-data/debian/rules +++ b/src/sonic-host-services-data/debian/rules @@ -7,4 +7,5 @@ build: override_dh_installsystemd: dh_installsystemd --no-start --name=caclmgrd + dh_installsystemd --no-start --name=hostcfgd dh_installsystemd --no-start --name=procdockerstatsd diff --git a/files/image_config/hostcfgd/hostcfgd.service b/src/sonic-host-services-data/debian/sonic-host-services-data.hostcfgd.service similarity index 82% rename from files/image_config/hostcfgd/hostcfgd.service rename to src/sonic-host-services-data/debian/sonic-host-services-data.hostcfgd.service index 762786ad830e..ea0c3ba60fb7 100644 --- a/files/image_config/hostcfgd/hostcfgd.service +++ b/src/sonic-host-services-data/debian/sonic-host-services-data.hostcfgd.service @@ -5,7 +5,7 @@ After=updategraph.service [Service] Type=simple -ExecStart=/usr/bin/hostcfgd +ExecStart=/usr/local/bin/hostcfgd [Install] WantedBy=multi-user.target diff --git a/files/image_config/hostcfgd/common-auth-sonic.j2 b/src/sonic-host-services-data/templates/common-auth-sonic.j2 similarity index 100% rename from files/image_config/hostcfgd/common-auth-sonic.j2 rename to src/sonic-host-services-data/templates/common-auth-sonic.j2 diff --git a/files/image_config/hostcfgd/tacplus_nss.conf.j2 b/src/sonic-host-services-data/templates/tacplus_nss.conf.j2 similarity index 100% rename from files/image_config/hostcfgd/tacplus_nss.conf.j2 rename to src/sonic-host-services-data/templates/tacplus_nss.conf.j2 diff --git a/src/sonic-host-services/.gitignore b/src/sonic-host-services/.gitignore index 730e4244bebd..70be1ce98629 100644 --- a/src/sonic-host-services/.gitignore +++ b/src/sonic-host-services/.gitignore @@ -1,6 +1,7 @@ # Compiled Python files *.pyc scripts/caclmgrdc +scripts/hostcfgdc scripts/procdockerstatsdc # Generated by packaging diff --git a/files/image_config/hostcfgd/hostcfgd b/src/sonic-host-services/scripts/hostcfgd similarity index 100% rename from files/image_config/hostcfgd/hostcfgd rename to src/sonic-host-services/scripts/hostcfgd diff --git a/src/sonic-host-services/setup.py b/src/sonic-host-services/setup.py index 3fab561da867..d0f7bd055683 100644 --- a/src/sonic-host-services/setup.py +++ b/src/sonic-host-services/setup.py @@ -12,9 +12,11 @@ maintainer_email = 'jolevequ@microsoft.com', scripts = [ 'scripts/caclmgrd', + 'scripts/hostcfgd', 'scripts/procdockerstatsd', ], install_requires = [ + 'Jinja2>=2.10', 'sonic-py-common', 'swsssdk>=2.0.1', ], From ea1b63a773ab7715c0582a7cb041265b91379feb Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 24 Oct 2020 07:57:22 +0000 Subject: [PATCH 3/6] Remove unused import --- src/sonic-host-services/scripts/hostcfgd | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index b5341065e4ec..01039d5336e5 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -5,7 +5,6 @@ import copy import ipaddress import os import subprocess -import sys import syslog import jinja2 From d3ecaea9e77bb5cdf2660a038e485a0f5829bf83 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Thu, 29 Oct 2020 22:25:46 +0000 Subject: [PATCH 4/6] Pass 'decode_responses=True' to ConfigDBConnector constructor --- src/sonic-host-services/scripts/hostcfgd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index 01039d5336e5..3145b819d356 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -229,7 +229,7 @@ class AaaCfg(object): class HostConfigDaemon: def __init__(self): - self.config_db = ConfigDBConnector() + self.config_db = ConfigDBConnector(decode_responses=True) self.config_db.connect(wait_for_init=True, retry_on=True) syslog.syslog(syslog.LOG_INFO, 'ConfigDB connect success') From fd8f88769a7bba59faaee423f0de2f12c603c8e2 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Sat, 31 Oct 2020 00:00:37 +0000 Subject: [PATCH 5/6] Iterate dict directly rather than list(dict.keys()) --- src/sonic-host-services/scripts/hostcfgd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index 3145b819d356..51579430398f 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -312,7 +312,7 @@ class HostConfigDaemon: def update_all_feature_states(self): feature_table = self.config_db.get_table('FEATURE') - for feature_name in list(feature_table.keys()): + for feature_name in feature_table: if not feature_name: syslog.syslog(syslog.LOG_WARNING, "Feature is None") continue @@ -358,7 +358,7 @@ class HostConfigDaemon: def feature_state_handler(self, key, data): feature_name = key feature_table = self.config_db.get_table('FEATURE') - if feature_name not in list(feature_table.keys()): + if feature_name not in feature_table: syslog.syslog(syslog.LOG_WARNING, "Feature '{}' not in FEATURE table".format(feature_name)) return From 76358674f662fc5d16c95f875e7eb96e0fdeed30 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Fri, 6 Nov 2020 00:31:43 +0000 Subject: [PATCH 6/6] Don't pass 'decode_responses=True' to ConfigDBConnector constructor, as it is no longer necessary for Python 3 --- src/sonic-host-services/scripts/hostcfgd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sonic-host-services/scripts/hostcfgd b/src/sonic-host-services/scripts/hostcfgd index 51579430398f..b97d800db3eb 100755 --- a/src/sonic-host-services/scripts/hostcfgd +++ b/src/sonic-host-services/scripts/hostcfgd @@ -229,7 +229,7 @@ class AaaCfg(object): class HostConfigDaemon: def __init__(self): - self.config_db = ConfigDBConnector(decode_responses=True) + self.config_db = ConfigDBConnector() self.config_db.connect(wait_for_init=True, retry_on=True) syslog.syslog(syslog.LOG_INFO, 'ConfigDB connect success')