-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dnfdaemon: Add new API for offline transactions
Adds new `Offline` interface for interacting with offline transactions. The interface provides three methods: - check_pending() - check whether there is an offline update scheduled for the next reboot - clean() - cancel the scheduled offline update - set_finish_action() - to set whether after applying the offline transaction the system should be rebooted (default) or powered off.
- Loading branch information
Showing
6 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
dnf5daemon-server/dbus/interfaces/org.rpm.dnf.v0.Offline.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" | ||
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> | ||
|
||
<!-- | ||
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 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. | ||
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 Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with libdnf. If not, see <https://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<node> | ||
<!-- org.rpm.dnf.v0.Offline: | ||
@short_description: Interface to offline transactions | ||
--> | ||
<interface name="org.rpm.dnf.v0.Offline"> | ||
<!-- | ||
check_pending: | ||
@pending: boolean, true if there is a pending offline transaction | ||
Check whether there is an offline transaction configured for the next reboot (i.e. checks the /system-update symlink existence). | ||
--> | ||
<method name="check_pending"> | ||
<arg name="pending" type="b" direction="out" /> | ||
</method> | ||
|
||
|
||
<!-- | ||
clean: | ||
@options: an array of key/value pairs | ||
@transaction_cleaned: boolean, returns `false` if there was an error during the transaction cleanup. Returns `true` if the offline transaction was successfully cleaned or if no offline transaction was configured. | ||
@error_msg: string, contains error encountered while cleaning the transaction | ||
Clean the offline transaction configured for the next reboot by removing the /system-update symlink (see systemd.offline-updates(7)). | ||
Following @options are supported: | ||
- clean_datadir: boolean, default false | ||
If true, all stored offline transaction data, including downloaded packages, will be removed along with the system-update symlink. | ||
Unknown options are ignored. | ||
--> | ||
<method name="clean"> | ||
<arg name="options" type="a{sv}" direction="in" /> | ||
<arg name="transaction_cleaned" type="b" direction="out" /> | ||
<arg name="error_msg" type="s" direction="out" /> | ||
</method> | ||
|
||
<!-- | ||
set_finish_action: | ||
@action: string, if set to "poweroff", the system will be powered off after applying the offline transaction. Otherwise the system will reboot. | ||
@success: boolean, true if the action was successfully set | ||
@error_msg: string, contains error encountered while setting the action | ||
Set the action that should be performed after the offline transaction is applied. If the `action` is "poweroff", the system will be powered off, otherwise it will be rebooted (which is default). | ||
The call might fail in case there is no scheduled offline transaction, or the transaction was not scheduled using libdnf5. | ||
--> | ||
<method name="set_finish_action"> | ||
<arg name="action" type="s" direction="in" /> | ||
<arg name="success" type="b" direction="out" /> | ||
<arg name="error_msg" type="s" direction="out" /> | ||
</method> | ||
|
||
</interface> | ||
|
||
</node> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "offline.hpp" | ||
|
||
#include "dbus.hpp" | ||
|
||
#include <libdnf5/transaction/offline.hpp> | ||
#include <sdbus-c++/sdbus-c++.h> | ||
|
||
#include <exception> | ||
#include <filesystem> | ||
|
||
|
||
void Offline::dbus_register() { | ||
auto dbus_object = session.get_dbus_object(); | ||
dbus_object->registerMethod( | ||
dnfdaemon::INTERFACE_OFFLINE, | ||
"check_pending", | ||
{}, | ||
{}, | ||
"b", | ||
{"pending"}, | ||
[this](sdbus::MethodCall call) -> void { | ||
session.get_threads_manager().handle_method(*this, &Offline::check_pending, call, session.session_locale); | ||
}); | ||
dbus_object->registerMethod( | ||
dnfdaemon::INTERFACE_OFFLINE, | ||
"clean", | ||
"a{sv}", | ||
{"options"}, | ||
"bs", | ||
{"transaction_cleaned", "error_msg"}, | ||
[this](sdbus::MethodCall call) -> void { | ||
session.get_threads_manager().handle_method(*this, &Offline::clean, call, session.session_locale); | ||
}); | ||
dbus_object->registerMethod( | ||
dnfdaemon::INTERFACE_OFFLINE, | ||
"set_finish_action", | ||
"s", | ||
{"action"}, | ||
"bs", | ||
{"success", "error_msg"}, | ||
[this](sdbus::MethodCall call) -> void { | ||
session.get_threads_manager().handle_method( | ||
*this, &Offline::set_finish_action, call, session.session_locale); | ||
}); | ||
} | ||
|
||
sdbus::MethodReply Offline::check_pending(sdbus::MethodCall & call) { | ||
// check the presence of the magic symlink | ||
auto reply = call.createReply(); | ||
std::error_code ec; | ||
reply << std::filesystem::exists(libdnf5::offline::MAGIC_SYMLINK, ec); | ||
return reply; | ||
} | ||
|
||
sdbus::MethodReply Offline::clean(sdbus::MethodCall & call) { | ||
std::string error_msg; | ||
dnfdaemon::KeyValueMap options; | ||
call >> options; | ||
bool clean_datadir = dnfdaemon::key_value_map_get<bool>(options, "clean_datadir", false); | ||
bool cleaned = true; | ||
std::error_code ec; | ||
if (!std::filesystem::remove(libdnf5::offline::MAGIC_SYMLINK, ec) && ec) { | ||
cleaned = false; | ||
error_msg = ec.message(); | ||
} | ||
if (clean_datadir) { | ||
auto base = session.get_base(); | ||
const auto & installroot = base->get_config().get_installroot_option().get_value(); | ||
std::filesystem::path datadir = installroot / libdnf5::offline::DEFAULT_DATADIR.relative_path(); | ||
for (const auto & entry : std::filesystem::directory_iterator(datadir)) { | ||
std::filesystem::remove_all(entry.path(), ec); | ||
} | ||
} | ||
auto reply = call.createReply(); | ||
reply << cleaned; | ||
reply << error_msg; | ||
return reply; | ||
} | ||
|
||
sdbus::MethodReply Offline::set_finish_action(sdbus::MethodCall & call) { | ||
bool success{false}; | ||
std::string error_msg{}; | ||
// try load the offline transaction state | ||
const std::filesystem::path state_path{ | ||
libdnf5::offline::MAGIC_SYMLINK / libdnf5::offline::TRANSACTION_STATE_FILENAME}; | ||
libdnf5::offline::OfflineTransactionState state{state_path}; | ||
const auto & read_exception = state.get_read_exception(); | ||
if (read_exception == nullptr) { | ||
// set the poweroff_after item accordingly | ||
std::string finish_action; | ||
call >> finish_action; | ||
state.get_data().set_poweroff_after(finish_action == "poweroff"); | ||
// write the new state | ||
state.write(); | ||
success = true; | ||
} else { | ||
try { | ||
std::rethrow_exception(read_exception); | ||
} catch (const std::exception & ex) { | ||
error_msg = ex.what(); | ||
} | ||
} | ||
auto reply = call.createReply(); | ||
reply << success; | ||
reply << error_msg; | ||
return reply; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef DNF5DAEMON_SERVER_SERVICES_OFFLINE_OFFLINE_HPP | ||
#define DNF5DAEMON_SERVER_SERVICES_OFFLINE_OFFLINE_HPP | ||
|
||
#include "session.hpp" | ||
|
||
#include <sdbus-c++/sdbus-c++.h> | ||
|
||
class Offline : public IDbusSessionService { | ||
public: | ||
using IDbusSessionService::IDbusSessionService; | ||
~Offline() = default; | ||
void dbus_register(); | ||
void dbus_deregister(); | ||
|
||
private: | ||
sdbus::MethodReply check_pending(sdbus::MethodCall & call); | ||
sdbus::MethodReply set_finish_action(sdbus::MethodCall & call); | ||
sdbus::MethodReply clean(sdbus::MethodCall & call); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters