Skip to content

Commit

Permalink
Fix on RHEL 7
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrosi committed Dec 7, 2023
1 parent 15d210a commit a9ef5b3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
9 changes: 6 additions & 3 deletions library/bootloader_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ def get_facts(stdout):
index_count += 1
kernels.append({})
search = re.search(r"(.*?)=(.*)", line)
key = search.group(1).strip('"')
value = search.group(2).strip('"')
kernels[index_count - 1].update({key: value})
if search:
key = search.group(1).strip('"')
value = search.group(2).strip('"')
kernels[index_count - 1].update({key: value})
else:
kernels[index_count - 1].update({"kernel": line})
return kernels


Expand Down
30 changes: 15 additions & 15 deletions library/bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ def get_kernels(bootloader_setting_kernel, kernels_keys):
def get_boot_args(kernel_info):
args = re.search(r'args="(.*)"', kernel_info)
if args is None:
return None
return args.group(1)
return ""
return args.group(1).strip()


def get_rm_boot_args_cmd(kernel_info, kernel):
bootloader_args = get_boot_args(kernel_info)
return (
"grubby --update-kernel="
+ kernel
+ " --remove-args="
+ escapeval(bootloader_args)
)
if bootloader_args:
return (
"grubby --update-kernel="
+ kernel
+ " --remove-args="
+ escapeval(bootloader_args)
)


def get_mod_boot_args_cmd(bootloader_setting_options, kernel, kernel_info):
Expand All @@ -114,14 +115,10 @@ def get_mod_boot_args_cmd(bootloader_setting_options, kernel, kernel_info):
else:
setting_name = kernel_setting["name"]
if "state" in kernel_setting and kernel_setting["state"] == "absent":
if bootloader_args and re.search(
r"(^|$| )" + setting_name + r"(^|$| )", bootloader_args
):
if re.search(r"(^|$| )" + setting_name + r"(^|$| )", bootloader_args):
boot_absent_args += setting_name + " "
else:
if bootloader_args and (
not re.search(r"(^|$| )" + setting_name + r"(^|$| )", bootloader_args)
):
if not re.search(r"(^|$| )" + setting_name + r"(^|$| )", bootloader_args):
boot_present_args += setting_name + " "
if boot_absent_args:
boot_mod_args = " --remove-args=" + escapeval(boot_absent_args.strip())
Expand All @@ -144,6 +141,7 @@ def run_module():
# for consumption, for example, in a subsequent task
result = dict(
changed=False,
msg=[]
)

# the AnsibleModule object will be our abstraction working with Ansible
Expand All @@ -167,8 +165,9 @@ def run_module():
rc, stdout, stderr = module.run_command("grubby --info=" + kernel)
rm_boot_args_cmd = get_rm_boot_args_cmd(stdout, kernel)
if rm_boot_args_cmd:
rc, stdout, stderr = module.run_command(rm_boot_args_cmd)
rc, stdout, stderr = module.run_command(str(rm_boot_args_cmd))
result["changed"] = True
result["msg"].append("Running this cmd to remove previous arguments: " + rm_boot_args_cmd)
rc, stdout, stderr = module.run_command("grubby --info=" + kernel)
# Configure boot settings
mod_boot_args_cmd = get_mod_boot_args_cmd(
Expand All @@ -177,6 +176,7 @@ def run_module():
if mod_boot_args_cmd:
rc, stdout, stderr = module.run_command(mod_boot_args_cmd)
result["changed"] = True
result["msg"].append("Running this cmd to modify arguments: " + mod_boot_args_cmd)
# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
Expand Down
7 changes: 3 additions & 4 deletions tests/tests_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
grubby --info=DEFAULT;
grubby --info=DEFAULT |
grep -P
'kernel="{{ default_bootloader }}"'
'kernel=("|){{ default_bootloader }}("|)'
changed_when: false

- name: Replace configuration with settings using kernel_index
Expand All @@ -43,7 +43,6 @@
- name: no_timer_check
state: present
- name: quiet
- name: debug
- previous: replaced
- kernel: ALL
options:
Expand All @@ -58,7 +57,7 @@

- name: Ensure bootloader_reboot_required is set to false
assert:
that: not bootloader_reboot_required
that: not (bootloader_reboot_required | d(false))

- name: Verify settings
assert:
Expand Down Expand Up @@ -96,7 +95,7 @@

- name: Ensure bootloader_reboot_required is set to false
assert:
that: not bootloader_reboot_required
that: not (bootloader_reboot_required | d(false))

- name: Verify settings
assert:
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_bootloader_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import bootloader_facts

# non linux entry: RHEL 7 might print such a message
INFO = """
index=0
kernel="/boot/vmlinuz-6.5.12-100.fc37.x86_64"
Expand Down Expand Up @@ -42,6 +43,8 @@
initrd="/boot/initramfs-0-rescue-c44543d15b2c4e898912c2497f734e67.img"
title="Fedora Linux (0-rescue-c44543d15b2c4e898912c2497f734e67) 36 (Workstation Edition)"
id="c44543d15b2c4e898912c2497f734e67-0-rescue"
index=4
non linux entry
"""

FACTS = [
Expand Down Expand Up @@ -81,6 +84,10 @@
"root": "UUID=65c70529-e9ad-4778-9001-18fe8c525285",
"title": "Fedora Linux (0-rescue-c44543d15b2c4e898912c2497f734e67) 36 (Workstation Edition)",
},
{
"index": "4",
"kernel": "non linux entry",
},
]


Expand Down
17 changes: 16 additions & 1 deletion tests/unit/test_bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
id="c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64"
"""

INFO_RHEL7 = """
index=0
kernel=/boot/vmlinuz-6.5.12-100.fc37.x86_64
args="arg_with_str_value_absent=test_value arg_with_int_value_absent=1 arg_without_val_absent"
root=UUID=65c70529-e9ad-4778-9001-18fe8c525285
initrd=/boot/initramfs-6.5.12-100.fc37.x86_64.img $tuned_initrd
title=Fedora Linux (6.5.12-100.fc37.x86_64) 37 (Workstation Edition)
id=c44543d15b2c4e898912c2497f734e67-6.5.12-100.fc37.x86_64
"""

kernels_keys = ["kernel_index", "kernel_path", "kernel_title", "DEFAULT", "ALL"]


Expand Down Expand Up @@ -94,8 +104,13 @@ def test_get_boot_args(self):
bootloader_args,
"arg_with_str_value_absent=test_value arg_with_int_value_absent=1 arg_without_val_absent",
)
bootloader_args = bootloader_settings.get_boot_args(INFO_RHEL7)
self.assertEqual(
bootloader_args,
"arg_with_str_value_absent=test_value arg_with_int_value_absent=1 arg_without_val_absent",
)
bootloader_args = bootloader_settings.get_boot_args("")
self.assertEqual(bootloader_args, None)
self.assertEqual(bootloader_args, "")

def test_get_rm_boot_args_cmd(self):
rm_boot_args_cmd = bootloader_settings.get_rm_boot_args_cmd(INFO, "0")
Expand Down

0 comments on commit a9ef5b3

Please sign in to comment.