Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic_config_updater: Filename changed & VLAN validator added #1919

Merged
merged 20 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions generic_config_updater/change_applier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
from .gu_common import genericUpdaterLogging

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
UPDATER_CONF_FILE = f"{SCRIPT_DIR}/generic_updater_config.conf.json"
UPDATER_CONF_FILE = f"{SCRIPT_DIR}/generic_config_updater.conf.json"
logger = genericUpdaterLogging.get_logger(title="Change Applier")

print_to_console = False
print_to_stdout = False

def set_print_options(to_console=False, to_stdout=False):
global print_to_console, print_to_stdout
def set_verbose(verbose=False):
global print_to_console, logger

print_to_console = to_console
print_to_stdout = to_stdout
print_to_console = verbose
if verbose:
logger.set_min_log_priority_debug()
else:
logger.set_min_log_priority_notice()


def log_debug(m):
logger.log_debug(m, print_to_console)
if print_to_stdout:
print(m)
logger.log(logger.LOG_PRIORITY_DEBUG, m, print_to_console)


def log_error(m):
logger.log_error(m, print_to_console)
if print_to_stdout:
print(m)
logger.log(logger.LOG_PRIORITY_ERROR, m, print_to_console)


def get_config_db():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
},
"DHCP_SERVER": {
"services_to_validate": [ "dhcp-relay" ]
},
"VLAN": {
"services_to_validate": [ "vlan-service" ]
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

services_to_validate

According to the design doc https://github.com/Azure/SONiC/blob/master/doc/config-generic-update-rollback/Json_Change_Application_Design.md#3212-metadata. It fits better as restart-command than services_to_validate #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not blindly restart.
For example, in case of VLAN table update, we call vlan_validator, which may or may not restart dhcp_sevice.
Again a validator, could potentially restart more than one and possibly none too, as in vlan case.

So I prefer this name validator, which is table specific.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the intelligence of restarting dhcp_service for vlan-service. However, the validator is design as read-only operations for the purpose of validation, including validating services are completely restarted and healthy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get your last comments. But Design doc updated.

}
},
"services": {
Expand All @@ -49,10 +52,13 @@
"validate_commands": [ ]
},
"rsyslog": {
"validate_commands": [ "generic_config_updater.services_validator.ryslog_validator" ]
"validate_commands": [ "generic_config_updater.services_validator.rsyslog_validator" ]
},
"dhcp-relay": {
"validate_commands": [ "generic_config_updater.services_validator.dhcp_validator" ]
},
"vlan-service": {
"validate_commands": [ "generic_config_updater.services_validator.vlan_validator" ]
}
}
}
Expand Down
36 changes: 31 additions & 5 deletions generic_config_updater/services_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,41 @@

logger = genericUpdaterLogging.get_logger(title="Service Validator")

print_to_console = False

def set_verbose(verbose=False):
global print_to_console, logger

print_to_console = verbose
if verbose:
logger.set_min_log_priority_debug()
else:
logger.set_min_log_priority_notice()


def _service_restart(svc_name):
os.system(f"systemctl restart {svc_name}")
logger.log_notice(f"Restarted {svc_name}")
rc = os.system(f"systemctl restart {svc_name}")
logger.log(logger.LOG_PRIORITY_NOTICE,
f"Restarted {svc_name}", print_to_console)
return rc == 0


def ryslog_validator(old_config, upd_config, keys):
_service_restart("rsyslog-config")
def rsyslog_validator(old_config, upd_config, keys):
return _service_restart("rsyslog-config")


def dhcp_validator(old_config, upd_config, keys):
_service_restart("dhcp_relay")
return _service_restart("dhcp_relay")


def vlan_validator(old_config, upd_config, keys):
old_vlan = old_config.get("VLAN", {})
upd_vlan = upd_config.get("VLAN", {})

for key in set(old_vlan.keys()).union(set(upd_vlan.keys())):
if (old_vlan.get(key, {}).get("dhcp_servers", []) !=
upd_vlan.get(key, {}).get("dhcp_servers", [])):
return _service_restart("dhcp_relay")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE! We just restart dhcp at the first occurrence of mismatch between dhcp_servers in old vs updated.

# No update to DHCP servers.
return True

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
'watchdogutil',
],
package_data={
'generic_config_updater': ['generic_updater_config.conf.json'],
'generic_config_updater': ['generic_config_updater.conf.json'],
renukamanavalan marked this conversation as resolved.
Show resolved Hide resolved
'show': ['aliases.ini'],
'sonic_installer': ['aliases.ini'],
'tests': ['acl_input/*',
Expand Down
4 changes: 3 additions & 1 deletion tests/generic_config_updater/change_applier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest.mock import patch

import generic_config_updater.change_applier
import generic_config_updater.services_validator
import generic_config_updater.gu_common

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -227,7 +228,8 @@ def test_change_apply(self, mock_set, mock_db, mock_os_sys):
json_changes = copy.deepcopy(read_data["json_changes"])

generic_config_updater.change_applier.UPDATER_CONF_FILE = CONF_FILE
generic_config_updater.change_applier.set_print_options(to_stdout=True)
generic_config_updater.change_applier.set_verbose(True)
generic_config_updater.services_validator.set_verbose(True)

applier = generic_config_updater.change_applier.ChangeApplier()
debug_print("invoked applier")
Expand Down
71 changes: 71 additions & 0 deletions tests/generic_config_updater/service_validator_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import copy
import json
import jsondiff
import os
import unittest
from collections import defaultdict
from unittest.mock import patch

from generic_config_updater.services_validator import vlan_validator
import generic_config_updater.gu_common


# Mimics os.system call
#
os_system_expected_cmd = ""
msg = ""

def os_system_cfggen(cmd):
assert cmd == os_system_expected_cmd, msg
return 0


test_data = [
{ "old": {}, "upd": {}, "cmd": "" },
{
"old": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.10" ] } } },
"upd": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.10" ] } } },
"cmd": ""
},
{
"old": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.10" ] } } },
"upd": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.11" ] } } },
"cmd": "systemctl restart dhcp_relay"
},
{
"old": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.10" ] } } },
"upd": { "VLAN": {
"XXX": { "dhcp_servers": [ "10.10.10.10" ] },
"YYY": { "dhcp_servers": [ ] } } },
"cmd": ""
},
{
"old": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.10" ] } } },
"upd": { "VLAN": {
"XXX": { "dhcp_servers": [ "10.10.10.10" ] },
"YYY": { "dhcp_servers": [ "10.12.12.1" ] } } },
"cmd": "systemctl restart dhcp_relay"
},
{
"old": { "VLAN": { "XXX": { "dhcp_servers": [ "10.10.10.10" ] } } },
"upd": {},
"cmd": "systemctl restart dhcp_relay"
}
]

class TestServiceValidator(unittest.TestCase):

@patch("generic_config_updater.change_applier.os.system")
def test_change_apply(self, mock_os_sys):
global os_system_expected_cmd

mock_os_sys.side_effect = os_system_cfggen

i = 0
for entry in test_data:
os_system_expected_cmd = entry["cmd"]
msg = "case failed: {}".format(str(entry))

vlan_validator(entry["old"], entry["upd"], None)