Skip to content

Commit

Permalink
FEAT: Add new parser ReaRLocalConf
Browse files Browse the repository at this point in the history
Signed-off-by: Xinting Li <[email protected]>
  • Loading branch information
Xinting Li committed Aug 22, 2024
1 parent 19a7722 commit c7c850e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/shared_parsers_catalog/rear_conf.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. automodule:: insights.parsers.rear_conf
:members:
:show-inheritance:
37 changes: 37 additions & 0 deletions insights/parsers/rear_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
ReaRLocalConf - File /etc/rear/local.conf
=========================================
"""

from insights.core import LegacyItemAccess, Parser
from insights.core.exceptions import SkipComponent
from insights.core.plugins import parser
from insights.parsers import get_active_lines
from insights.specs import Specs


@parser(Specs.rear_local_conf)
class ReaRLocalConf(Parser, LegacyItemAccess):
"""
Parses content of "/etc/rear/local.conf".
Typical content of "/etc/rear/local.conf" is::
BACKUP_RESTORE_MOVE_AWAY_FILES=( /boot/grub/grubenv /boot/grub2/grubenv )
Examples:
>>> type(local_conf)
<class 'insights.parsers.rear_conf.ReaRLocalConf'>
>>> local_conf[0] == 'BACKUP_RESTORE_MOVE_AWAY_FILES=( /boot/grub/grubenv /boot/grub2/grubenv )'
True
"""

def parse_content(self, content):
_content = get_active_lines(content)
self.data = []
for line in _content:
self.data.append(line)

if not self.data:
raise SkipComponent
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ class Specs(SpecSet):
readlink_e_etc_mtab = RegistryPoint(no_obfuscate=['hostname', 'ip'])
readlink_e_shift_cert_client = RegistryPoint(no_obfuscate=['hostname', 'ip'])
readlink_e_shift_cert_server = RegistryPoint(no_obfuscate=['hostname', 'ip'])
rear_local_conf = RegistryPoint()
recvq_socket_buffer = RegistryPoint()
redhat_release = RegistryPoint(no_obfuscate=['hostname', 'ip'])
repquota_agnpuv = RegistryPoint(no_obfuscate=['hostname', 'ip'])
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ class DefaultSpecs(Specs):
readlink_e_etc_mtab = simple_command("/usr/bin/readlink -e /etc/mtab")
readlink_e_shift_cert_client = simple_command("/usr/bin/readlink -e /etc/origin/node/certificates/kubelet-client-current.pem")
readlink_e_shift_cert_server = simple_command("/usr/bin/readlink -e /etc/origin/node/certificates/kubelet-server-current.pem")
rear_local_conf = simple_file("/etc/rear/local.conf")
redhat_release = simple_file("/etc/redhat-release")
repquota_agnpuv = simple_command("/usr/sbin/repquota -agnpuv")
resolv_conf = simple_file("/etc/resolv.conf")
Expand Down
51 changes: 51 additions & 0 deletions insights/tests/parsers/test_rear_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import doctest
import pytest

from insights.core.exceptions import SkipComponent
from insights.parsers import rear_conf
from insights.parsers.rear_conf import ReaRLocalConf
from insights.tests import context_wrap

RDMA_CONFIG = """
# This file etc/rear/local.conf is intended for the user's
# manual configuration of Relax-and-Recover (ReaR).
# For configuration through packages and other automated means
# we recommend a separated file named site.conf next to this file
# and leave local.conf as is (ReaR upstream will never ship a site.conf).
# The default OUTPUT=ISO creates the ReaR rescue medium as ISO image.
# You need to specify your particular backup and restore method for your data
# as the default BACKUP=REQUESTRESTORE does not really do that (see "man rear").
# Configuration variables are documented in /usr/share/rear/conf/default.conf
# and the examples in /usr/share/rear/conf/examples/ can be used as templates.
# ReaR reads the configuration files via the bash builtin command 'source'
# so bash syntax like VARIABLE="value" (no spaces at '=') is mandatory.
# Because 'source' executes the content as bash scripts you can run commands
# within your configuration files, in particular commands to set different
# configuration values depending on certain conditions as you need like
# CONDITION_COMMAND && VARIABLE="special_value" || VARIABLE="usual_value"
# but that means CONDITION_COMMAND gets always executed when 'rear' is run
# so ensure nothing can go wrong if you run commands in configuration files.
BACKUP_RESTORE_MOVE_AWAY_FILES=( /boot/grub/grubenv /boot/grub2/grubenv )
""".strip()

REAR_CONF_EMPTY = """
""".strip()


def test_rdma_config():
local_conf = ReaRLocalConf(context_wrap(RDMA_CONFIG))
assert len(local_conf.data) == 1
assert local_conf.data[0] == "BACKUP_RESTORE_MOVE_AWAY_FILES=( /boot/grub/grubenv /boot/grub2/grubenv )"


def test_rdma_config_empty():
with pytest.raises(SkipComponent):
ReaRLocalConf(context_wrap(REAR_CONF_EMPTY))


def test_rdma_config_doc():
env = {
'local_conf': ReaRLocalConf(context_wrap(RDMA_CONFIG)),
}
failed, total = doctest.testmod(rear_conf, globs=env)
assert failed == 0

0 comments on commit c7c850e

Please sign in to comment.