Skip to content

Commit

Permalink
Handle the %pre and %pre-install sections in the runtime module
Browse files Browse the repository at this point in the history
Implement the support for the %pre and %pre-install sections in the runtime module.
  • Loading branch information
KKoukiou committed Mar 4, 2024
1 parent 9d6cd87 commit 29cf11b
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 44 deletions.
2 changes: 1 addition & 1 deletion anaconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def _earlyExceptionHandler(ty, value, traceback):
log.info("Found a kickstart file: %s", kspath)

# Run %pre scripts.
startup_utils.run_pre_scripts(kspath)
startup_utils.run_pre_scripts()

# Parse the kickstart file.
ksdata = startup_utils.parse_kickstart(kspath, strict_mode=opts.ksstrict)
Expand Down
2 changes: 1 addition & 1 deletion pyanaconda/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def _prepare_installation(self, payload, ksdata):
)
pre_install_scripts.append(Task(
"Run %pre-install scripts",
runPreInstallScripts, (ksdata.scripts,)
runPreInstallScripts, ()
))
installation_queue.append(pre_install_scripts)

Expand Down
52 changes: 18 additions & 34 deletions pyanaconda/kickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
from pyanaconda.errors import ScriptError, errorHandler
from pyanaconda.flags import flags
from pyanaconda.core.i18n import _
from pyanaconda.modules.common.constants.services import BOSS
from pyanaconda.modules.common.constants.objects import SCRIPTS
from pyanaconda.modules.common.constants.services import BOSS, RUNTIME
from pyanaconda.modules.common.structures.kickstart import KickstartReport

from pykickstart.base import KickstartCommand, RemovedCommand
from pykickstart.constants import KS_SCRIPT_POST, KS_SCRIPT_PRE, KS_SCRIPT_TRACEBACK, KS_SCRIPT_PREINSTALL
from pykickstart.constants import KS_SCRIPT_POST, KS_SCRIPT_TRACEBACK
from pykickstart.errors import KickstartError, KickstartParseWarning
from pykickstart.ko import KickstartObject
from pykickstart.parser import KickstartParser
from pykickstart.parser import Script as KSScript
from pykickstart.sections import NullSection, PostScriptSection, PreScriptSection, \
PreInstallScriptSection, OnErrorScriptSection, TracebackScriptSection, Section
from pykickstart.sections import NullSection, PreScriptSection, PostScriptSection, OnErrorScriptSection, TracebackScriptSection, Section
from pykickstart.version import returnClassForVersion

log = get_module_logger(__name__)
Expand Down Expand Up @@ -259,6 +259,9 @@ def __init__(self, commandUpdates=None, dataUpdates=None):
# The %packages section is handled by the DBus module.
self.packages = UselessObject()

# The %pre, %pre-install sections are handled by the DBus module.
self.scripts = UselessObject()

def __str__(self):
proxy = BOSS.get_proxy()
modules = proxy.GenerateKickstart().strip()
Expand Down Expand Up @@ -296,11 +299,11 @@ def handleCommand(self, lineno, args):
return KickstartParser.handleCommand(self, lineno, args)

def setupSections(self):
self.registerSection(PreScriptSection(self.handler, dataObj=self.scriptClass))
self.registerSection(PreInstallScriptSection(self.handler, dataObj=self.scriptClass))
self.registerSection(PostScriptSection(self.handler, dataObj=self.scriptClass))
self.registerSection(TracebackScriptSection(self.handler, dataObj=self.scriptClass))
self.registerSection(OnErrorScriptSection(self.handler, dataObj=self.scriptClass))
self.registerSection(UselessSection(self.handler, sectionOpen="%pre"))
self.registerSection(UselessSection(self.handler, sectionOpen="%pre-install"))
self.registerSection(UselessSection(self.handler, sectionOpen="%packages"))
self.registerSection(UselessSection(self.handler, sectionOpen="%addon"))

Expand All @@ -309,13 +312,9 @@ def preScriptPass(f):
# The first pass through kickstart file processing - look for %pre scripts
# and run them. This must come in a separate pass in case a script
# generates an included file that has commands for later.
ksparser = AnacondaPreParser(AnacondaKSHandler())

with check_kickstart_error():
ksparser.readKickstart(f)

# run %pre scripts
runPreScripts(ksparser.handler.scripts)
runPreScripts()


def parseKickstart(handler, f, strict_mode=False):
Expand Down Expand Up @@ -406,33 +405,18 @@ def runPostScripts(scripts):
script_log.info("All kickstart %%post script(s) have been run")


def runPreScripts(scripts):
preScripts = [s for s in scripts if s.type == KS_SCRIPT_PRE]

if len(preScripts) == 0:
return

script_log.info("Running kickstart %%pre script(s)")
stdoutLog.info(_("Running pre-installation scripts"))

for script in preScripts:
script.run("/")

script_log.info("All kickstart %%pre script(s) have been run")

def runPreScripts():
runtime_proxy = RUNTIME.get_proxy(SCRIPTS)
task = runtime_proxy.RunPreScriptsWithTask()

def runPreInstallScripts(scripts):
preInstallScripts = [s for s in scripts if s.type == KS_SCRIPT_PREINSTALL]
return task

if len(preInstallScripts) == 0:
return

script_log.info("Running kickstart %%pre-install script(s)")

for script in preInstallScripts:
script.run("/")
def runPreInstallScripts():
runtime_proxy = RUNTIME.get_proxy(SCRIPTS)
task = runtime_proxy.RunPreInstallScriptsWithTask()

script_log.info("All kickstart %%pre-install script(s) have been run")
return task


def runTracebackScripts(scripts):
Expand Down
5 changes: 5 additions & 0 deletions pyanaconda/modules/common/constants/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
basename="UserInterface"
)

SCRIPTS = DBusObjectIdentifier(
namespace=RUNTIME_NAMESPACE,
basename="ScriptsInterface"
)

# Storage objects.

BOOTLOADER = DBusObjectIdentifier(
Expand Down
58 changes: 58 additions & 0 deletions pyanaconda/modules/common/structures/scripts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# DBus structures for the packages data.
#
# Copyright (C) 2024 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
from dasbus.structure import DBusData
from dasbus.typing import * # pylint: disable=wildcard-import

class Script(DBusData):
""" Structure for the script data. """

def __init__(self, _type: Int, script: Str, interp: Str, logfile: Str, errorOnFail: Bool, lineno: Int):
self.type = _type
self.script = script
self.interp = interp
self.logfile = logfile
self.errorOnFail = errorOnFail
self.lineno = lineno

@property
def type(self) -> Int:
""" The type of the script.
:return: The type of the script.
:rtype: Int
"""
return self.type

@type.setter
def type(self, value: Int) -> None:
self.type = value

@property
def script(self) -> Str:
""" The script.
:return: The script.
:rtype: Str
"""
return self.script

@script.setter
def script(self, value: Str) -> None:
self.script = value
5 changes: 5 additions & 0 deletions pyanaconda/modules/runtime/kickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Red Hat, Inc.
#
from pyanaconda.core.kickstart import KickstartSpecification, commands as COMMANDS
from pykickstart.sections import PreScriptSection


class RuntimeKickstartSpecification(KickstartSpecification):
Expand All @@ -34,3 +35,7 @@ class RuntimeKickstartSpecification(KickstartSpecification):
"DriverDiskData": COMMANDS.DriverDiskData,
"SshPwData": COMMANDS.SshPwData,
}

sections = {
"pre": PreScriptSection,
}
4 changes: 4 additions & 0 deletions pyanaconda/modules/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pyanaconda.modules.runtime.kickstart import RuntimeKickstartSpecification
from pyanaconda.modules.runtime.dracut_commands import DracutCommandsModule
from pyanaconda.modules.runtime.user_interface import UIModule
from pyanaconda.modules.runtime.scripts import ScriptsModule
from pyanaconda.modules.common.base import KickstartService
from pyanaconda.modules.common.constants.services import RUNTIME
from pyanaconda.modules.common.containers import TaskContainer
Expand Down Expand Up @@ -51,6 +52,9 @@ def __init__(self):
self._ui_module = UIModule()
self._modules.add_module(self._ui_module)

self._scripts_module = ScriptsModule()
self._modules.add_module(self._scripts_module)

def publish(self):
"""Publish the module."""
TaskContainer.set_namespace(RUNTIME.namespace)
Expand Down
21 changes: 21 additions & 0 deletions pyanaconda/modules/runtime/scripts/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Copyright (C) 2024 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

pkgpyexecdir = $(pyexecdir)/py$(PACKAGE_NAME)
scriptsdir = $(pkgpyexecdir)/modules/runtime/scripts
scripts_PYTHON = $(srcdir)/*.py

MAINTAINERCLEANFILES = Makefile.in
20 changes: 20 additions & 0 deletions pyanaconda/modules/runtime/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright (C) 2024 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
from pyanaconda.modules.runtime.scripts.scripts import ScriptsModule

__all__ = ["ScriptsModule"]

0 comments on commit 29cf11b

Please sign in to comment.