Skip to content

Commit

Permalink
Check the conflicting commands automatically
Browse files Browse the repository at this point in the history
If the command, that is going to be parsed, defines any conflicting commands,
the kickstart handler will check them first and raise an exception if there
is a conflict. This happens automatically now.

(cherry picked from commit cfcc525)
Related: RHEL-17662
  • Loading branch information
poncovka authored and bcl committed Nov 29, 2023
1 parent 3a68baf commit 084a5e0
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 72 deletions.
29 changes: 21 additions & 8 deletions pykickstart/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,6 @@ def _setToObj(self, namespace, obj):
warnings.warn("_setToObj has been renamed to set_to_obj. The old name will be removed in a future release.", PendingDeprecationWarning, stacklevel=2)
self.set_to_obj(namespace, obj)

# Check for conflicting commands and raise an error
def _checkConflictingCommands(self, msg):
for cmd in self.conflictingCommands:
if not hasattr(self.handler, cmd) or not getattr(self.handler, cmd).seen:
continue

raise KickstartParseError(msg % cmd, lineno=self.lineno)


class DeprecatedCommand(KickstartCommand):
"""Specify that a command is deprecated and no longer has any function.
Expand Down Expand Up @@ -429,6 +421,10 @@ def dispatcher(self, args, lineno):
self.commands[cmd].lineno = lineno
self.commands[cmd].seen = True

# Check for conflicting commands.
conflicting_cmds = self.commands[cmd].conflictingCommands
self._checkConflictingCommands(cmd, conflicting_cmds, lineno=lineno)

# The parser returns the data object that was modified. This is either
# the command handler object itself (a KickstartCommand object), or it's
# a BaseData subclass instance that should be put into the command's
Expand All @@ -444,6 +440,23 @@ def dispatcher(self, args, lineno):

return obj

def _checkConflictingCommands(self, cmd, conflicting_cmds, lineno=None):
"""Check for conflicting commands and raise an error."""
for conflicting_cmd in conflicting_cmds:
if conflicting_cmd not in self.commands:
continue

if self.commands[conflicting_cmd] is None:
continue

if not self.commands[conflicting_cmd].seen:
continue

raise KickstartParseError(
_("The %s and %s commands can't be used at the same time.")
% (cmd, conflicting_cmd), lineno=lineno
)


class BaseHandler(KickstartHandler):
"""A base kickstart handler.
Expand Down
21 changes: 0 additions & 21 deletions pykickstart/commands/autopart.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,6 @@ def _getParser(self):
filesystem.""")
return op

def parse(self, args):
# call the overriden command to do its job first
retval = F12_AutoPart.parse(self, args)

# Using autopart together with other partitioning command such as
# part/partition, raid, logvol or volgroup can lead to hard to debug
# behavior that might among other result into an unbootable system.
self._checkConflictingCommands(_("The %s and autopart commands can't be used at the same time"))
return retval


class F16_AutoPart(F12_AutoPart):
removedKeywords = F12_AutoPart.removedKeywords
removedAttrs = F12_AutoPart.removedAttrs
Expand Down Expand Up @@ -317,16 +306,6 @@ def __init__(self, writePriority=100, *args, **kwargs):
F18_AutoPart.__init__(self, writePriority=writePriority, *args, **kwargs)
self.typeMap["thinp"] = AUTOPART_TYPE_LVM_THINP

def parse(self, args):
# call the overriden command to do its job first
retval = F18_AutoPart.parse(self, args)

# Using autopart together with other partitioning command such as
# part/partition, raid, logvol or volgroup can lead to hard to debug
# behavior that might among other result into an unbootable system.
self._checkConflictingCommands(_("The %s and autopart commands can't be used at the same time"))
return retval

def _getParser(self):
"Only necessary for the type change documentation"
op = F18_AutoPart._getParser(self)
Expand Down
7 changes: 0 additions & 7 deletions pykickstart/commands/logvol.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,6 @@ def _getParser(self):
def parse(self, args):
# call the overriden method
retval = F12_LogVol.parse(self, args)
# the logvol command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The logvol and %s commands can't be used at the same time"))

if retval.thin_volume and retval.thin_pool:
errorMsg = _("--thin and --thinpool cannot both be specified for "
Expand Down Expand Up @@ -748,10 +745,6 @@ def parse(self, args):
err = _("--chunksize and --metadatasize are for thin pools only")
raise KickstartParseError(err, lineno=self.lineno)

# the logvol command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The logvol and %s commands can't be used at the same time"))

if not retval.preexist and not retval.percent and not retval.size and not retval.recommended and not retval.hibernation:
errorMsg = _("No size given for logical volume. Use one of --useexisting, --noformat, --size, --percent, or --hibernation.")
raise KickstartParseError(errorMsg, lineno=self.lineno)
Expand Down
1 change: 0 additions & 1 deletion pykickstart/commands/mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def _getParser(self):
return op

def parse(self, args):
self._checkConflictingCommands(_("The mount and %s commands can't be used at the same time"))
(ns, extra) = self.op.parse_known_args(args=args, lineno=self.lineno)

if extra:
Expand Down
11 changes: 0 additions & 11 deletions pykickstart/commands/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,6 @@ def _getParser(self):
""")
return op

def parse(self, args):
# first call the overriden command
retval = F12_Partition.parse(self, args)
# the part command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The part/partition and %s commands can't be used at the same time"))

return retval

class F14_Partition(F12_Partition):
removedKeywords = F12_Partition.removedKeywords
Expand Down Expand Up @@ -640,9 +632,6 @@ class F20_Partition(F18_Partition):
def parse(self, args):
# first call the overriden command
retval = F18_Partition.parse(self, args)
# the part command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The part/partition and %s commands can't be used at the same time"))

# when using tmpfs, grow is not suported
if retval.fstype == "tmpfs":
Expand Down
17 changes: 0 additions & 17 deletions pykickstart/commands/raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,6 @@ def _getParser(self):
filesystem.""")
return op

def parse(self, args):
# first call the overriden method
retval = F13_Raid.parse(self, args)
# the raid command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The raid and %s commands can't be used at the same time"))
return retval

class F14_Raid(F13_Raid):
removedKeywords = F13_Raid.removedKeywords
removedAttrs = F13_Raid.removedAttrs
Expand Down Expand Up @@ -685,15 +677,6 @@ class F20_Raid(F19_Raid):
removedAttrs = F19_Raid.removedAttrs
conflictingCommands = ["autopart", "mount"]

def parse(self, args):
# first call the overriden method
retval = F19_Raid.parse(self, args)
# the raid command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The raid and %s commands can't be used at the same time"))

return retval

class F23_Raid(F20_Raid):
removedKeywords = F20_Raid.removedKeywords
removedAttrs = F20_Raid.removedAttrs
Expand Down
4 changes: 0 additions & 4 deletions pykickstart/commands/reqpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
from pykickstart.base import KickstartCommand
from pykickstart.options import KSOptionParser

from pykickstart.i18n import _

class F23_ReqPart(KickstartCommand):
removedKeywords = KickstartCommand.removedKeywords
removedAttrs = KickstartCommand.removedAttrs
Expand Down Expand Up @@ -73,8 +71,6 @@ def _getParser(self):
return op

def parse(self, args):
self._checkConflictingCommands(_("The reqpart and %s commands can't be used at the same time"))

ns = self.op.parse_args(args=args, lineno=self.lineno)
self.set_to_self(ns)
self.reqpart = True
Expand Down
3 changes: 0 additions & 3 deletions pykickstart/commands/volgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ def parse(self, args):
if retval.reserved_percent is not None and not 0 < retval.reserved_percent < 100:
raise KickstartParseError("Volume group reserved space percentage must be between 1 and 99.", lineno=self.lineno)

# the volgroup command can't be used together with the autopart command
# due to the hard to debug behavior their combination introduces
self._checkConflictingCommands(_("The volgroup and %s commands can't be used at the same time"))
return retval

class F21_VolGroup(F16_VolGroup):
Expand Down

0 comments on commit 084a5e0

Please sign in to comment.