Merge ~afreiberger/charm-sysconfig:lp1854135 into charm-sysconfig:master

Proposed by Drew Freiberger
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: ~afreiberger/charm-sysconfig:lp1854135
Merge into: charm-sysconfig:master
Diff against target: 119 lines (+77/-0)
2 files modified
src/lib/lib_sysconfig.py (+31/-0)
src/tests/unit/test_lib.py (+46/-0)
Reviewer Review Type Date Requested Status
Alvaro Uria (community) Needs Fixing
Peter Sabaini (community) Approve
BootStack Reviewers Pending
BootStack Reviewers mr tracking; do not claim Pending
Canonical IS Reviewers Pending
Review via email: mp+377584@code.launchpad.net

Commit message

Verify grub/systemd parameters and affinity

    * Verify that grub parameters are set before running update_grub_file
    function. If it is not verified, this function will run and set the
    default options through the grub template which is undesirable.
    * Verify that systemd parameters are set before running
    update_systemd_system_file function.

To post a comment you must log in.
11b15a8... by Drew Freiberger

Verify grub/systemd parameters and affinity

* Verify that grub parameters are set before running update_grub_file
function. If it is not verified, this function will run and set the
default options through the grub template which is undesirable.
* Verify that systemd parameters are set before running
update_systemd_system_file function.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

Revision history for this message
Peter Sabaini (peter-sabaini) wrote :

LGTM, cheers

review: Approve
Revision history for this message
Alvaro Uria (aluria) wrote :

I think this change will conflict with Paul's, which uses pytest for the unit testing.

Revision history for this message
Alvaro Uria (aluria) wrote :

This change needs to be rebased. OTOH, I have added a couple of comments about the verifications added. If the issue is that checksums don't exist, shouldn't we call any_file_changed (without any action) in the entry function (install_sysconfig) so checksums are initialized? By doing so, we would only need to check that a file has changed (instead of validating every possible config parameter that affects the config file(s)).

review: Needs Fixing
Revision history for this message
Drew Freiberger (afreiberger) wrote :

I think you may be onto something with using any_file_changed. While the intention is not to render a grub config if no change was detected from the charm, we could accomplish that with a data_changed query. The reason for this would be that kernel upgrades would change contents of grub files between config-changed runs, so we'd need to know if there were changes effected since last install/upgrade-charm/config-changed that would affect a change in config files before re-running a grub config.

I see your point about the if statements not catching all exceptions and need to update the logic there. Maybe incorporating a dict of those values and assess the impact via data_changed could make this a bit more readable and consistent. Likely evaluating all the configs except for reservation, and then evaluating the setting/changing of reservation with the exclusion of off/isolcpus.

If I get time, I'll add this as a stretch goal for addressing in 20.01 release.

Unmerged commits

11b15a8... by Drew Freiberger

Verify grub/systemd parameters and affinity

* Verify that grub parameters are set before running update_grub_file
function. If it is not verified, this function will run and set the
default options through the grub template which is undesirable.
* Verify that systemd parameters are set before running
update_systemd_system_file function.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/lib_sysconfig.py b/src/lib/lib_sysconfig.py
2index 51bf049..b938f35 100644
3--- a/src/lib/lib_sysconfig.py
4+++ b/src/lib/lib_sysconfig.py
5@@ -223,11 +223,30 @@ class SysConfigHelper:
6
7 return valid
8
9+ def is_update_grub_needed(self):
10+ if any(self.charm_config[flag] for flag in (
11+ 'reservation',
12+ 'hugepages',
13+ 'hugepagesz',
14+ 'raid-autodetection',
15+ 'enable-pti',
16+ 'enable-iommu',
17+ 'grub-config-flags',
18+ 'kernel-version',
19+ 'update-grub',
20+ 'config-flags',
21+ 'cpu-range',
22+ )) and self.reservation not in ('off', 'affinity'):
23+ return True
24+ return False
25+
26 def update_grub_file(self):
27 """Update /etc/default/grub.d/90-sysconfig.cfg according to charm configuration.
28
29 Will call update-grub if update-grub config is set to True.
30 """
31+ if not self.is_update_grub_needed():
32+ return
33 context = {}
34 if self.reservation == 'isolcpus':
35 context['cpu_range'] = self.cpu_range
36@@ -256,8 +275,20 @@ class SysConfigHelper:
37 hookenv.log('grub configuration updated')
38 self._update_grub()
39
40+ def is_update_systemd_needed(self):
41+ if any(self.charm_config[flag] for flag in (
42+ 'reservation',
43+ 'systemd-config-flags',
44+ 'cpu-range',
45+ 'config-flags',
46+ )) and self.reservation not in ('off', 'isolcpus'):
47+ return True
48+ return False
49+
50 def update_systemd_system_file(self):
51 """Update /etc/systemd/system.conf according to charm configuration."""
52+ if not self.is_update_systemd_needed():
53+ return
54 context = {}
55 if self.reservation == 'affinity':
56 context['cpu_range'] = self.cpu_range
57diff --git a/src/tests/unit/test_lib.py b/src/tests/unit/test_lib.py
58index b7ac651..3534ca9 100644
59--- a/src/tests/unit/test_lib.py
60+++ b/src/tests/unit/test_lib.py
61@@ -198,6 +198,33 @@ class TestLib:
62 @mock.patch("lib_sysconfig.hookenv.config")
63 @mock.patch("lib_sysconfig.hookenv.log")
64 @mock.patch("lib_sysconfig.render")
65+ def test_update_grub_file_false(self, render, log, config, check_call):
66+ """Do not update /etc/default/grub.d/90-sysconfig.cfg and update-grub false.
67+
68+ Expect file is not rendered and updated-grub is not called.
69+ """
70+ config.return_value = {
71+ "reservation": "off",
72+ "cpu-range": "",
73+ "hugepages": "",
74+ "hugepagesz": "",
75+ "raid-autodetection": "",
76+ "enable-pti": False,
77+ "enable-iommu": False,
78+ "grub-config-flags": "",
79+ "kernel-version": "",
80+ "update-grub": False,
81+ }
82+
83+ sysh = lib_sysconfig.SysConfigHelper()
84+ sysh.update_grub_file()
85+ render.assert_not_called()
86+ check_call.assert_not_called()
87+
88+ @mock.patch("lib_sysconfig.subprocess.check_call")
89+ @mock.patch("lib_sysconfig.hookenv.config")
90+ @mock.patch("lib_sysconfig.hookenv.log")
91+ @mock.patch("lib_sysconfig.render")
92 def test_legacy_grub_config_flags(self, render, log, config, check_call):
93 """Update /etc/default/grub.d/90-sysconfig.cfg and update-grub true.
94
95@@ -307,6 +334,25 @@ class TestLib:
96 @mock.patch("lib_sysconfig.hookenv.config")
97 @mock.patch("lib_sysconfig.hookenv.log")
98 @mock.patch("lib_sysconfig.render")
99+ def test_update_systemd_system_file_false(self, render, log, config):
100+ """Do not update /etc/default/grub.d/90-sysconfig.cfg
101+
102+ Expect file is not rendered given blank configs for file
103+ """
104+ config.return_value = {
105+ "reservation": "off",
106+ "cpu-range": "",
107+ "systemd-config-flags": "",
108+ "config-flags": "",
109+ }
110+
111+ sysh = lib_sysconfig.SysConfigHelper()
112+ sysh.update_systemd_system_file()
113+ render.assert_not_called()
114+
115+ @mock.patch("lib_sysconfig.hookenv.config")
116+ @mock.patch("lib_sysconfig.hookenv.log")
117+ @mock.patch("lib_sysconfig.render")
118 def test_legacy_systemd_config_flags(self, render, log, config):
119 """Update /etc/default/grub.d/90-sysconfig.cfg and update-grub false.
120

Subscribers

People subscribed via source and target branches

to all changes: