From e90f79e197492b4c7ad528cfe2e3a5da073f9890 Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Wed, 17 Jul 2024 11:48:49 +0200 Subject: [PATCH] doc: Add system-upgrade example using D-Bus API --- .../print_upgrades_with_severities.py | 18 +++++ doc/dnf_daemon/examples/system_upgrade.py | 69 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 doc/dnf_daemon/examples/system_upgrade.py diff --git a/doc/dnf_daemon/examples/print_upgrades_with_severities.py b/doc/dnf_daemon/examples/print_upgrades_with_severities.py index 47c018ca6..1bb8a539f 100644 --- a/doc/dnf_daemon/examples/print_upgrades_with_severities.py +++ b/doc/dnf_daemon/examples/print_upgrades_with_severities.py @@ -1,3 +1,21 @@ +#!/usr/bin/python3 +# Copyright Contributors to the libdnf project. +# +# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/ +# +# Libdnf is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Libdnf 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with libdnf. If not, see . + import dbus import os diff --git a/doc/dnf_daemon/examples/system_upgrade.py b/doc/dnf_daemon/examples/system_upgrade.py new file mode 100644 index 000000000..bb8daf702 --- /dev/null +++ b/doc/dnf_daemon/examples/system_upgrade.py @@ -0,0 +1,69 @@ +#!/usr/bin/python3 +# Copyright Contributors to the libdnf project. +# +# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/ +# +# Libdnf is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Libdnf 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with libdnf. If not, see . + +""" +This is an example how to perform a system-upgrade with dnf5daemon-server. +""" + +import dbus + +DNFDAEMON_BUS_NAME = 'org.rpm.dnf.v0' +DNFDAEMON_OBJECT_PATH = '/' + DNFDAEMON_BUS_NAME.replace('.', '/') + +IFACE_SESSION_MANAGER = '{}.SessionManager'.format(DNFDAEMON_BUS_NAME) +IFACE_RPM = '{}.rpm.Rpm'.format(DNFDAEMON_BUS_NAME) +IFACE_GOAL = '{}.Goal'.format(DNFDAEMON_BUS_NAME) + + +bus = dbus.SystemBus() +iface_session = dbus.Interface( + bus.get_object(DNFDAEMON_BUS_NAME, DNFDAEMON_OBJECT_PATH), + dbus_interface=IFACE_SESSION_MANAGER) + +# set the releasever to the new distribution release +session = iface_session.open_session( + dbus.Dictionary({"releasever": "40"}, signature=dbus.Signature('sv'))) + +iface_rpm = dbus.Interface( + bus.get_object(DNFDAEMON_BUS_NAME, session), + dbus_interface=IFACE_RPM) +iface_goal = dbus.Interface( + bus.get_object(DNFDAEMON_BUS_NAME, session), + dbus_interface=IFACE_GOAL) + +# Add system upgrade to the transaction +options = { + "mode": "distrosync", +} +iface_rpm.system_upgrade(options) + +# resolve the transaction +resolved, result = iface_goal.resolve({}) + +# now you can print the transaction table and ask the user for confirmation +print("Resolved.") + +if result == 0: + # execute the transaction offline (durint the next reboot) + iface_goal.do_transaction({"offline": True}, timeout=2000) + print("Reboot to continue with system upgrade...") +else: + errors = iface_goal.get_transaction_problems_string() + print("Errors while resolving the transaction:") + for error in errors: + print(error)