From bf67d1914db5be25435cc89d62e8533761aa00a6 Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 9 Nov 2023 13:48:57 +0100 Subject: [PATCH] Allow using `repo-add`/`repo-remove` from container in repo management actions --- librepomgr/buildactions/buildactionmeta.cpp | 15 ++++- librepomgr/buildactions/buildactionmeta.h | 11 +++- librepomgr/buildactions/buildactionprivate.h | 2 + librepomgr/buildactions/repomanagement.cpp | 58 +++++++++++++++----- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/librepomgr/buildactions/buildactionmeta.cpp b/librepomgr/buildactions/buildactionmeta.cpp index c605865..28453f2 100644 --- a/librepomgr/buildactions/buildactionmeta.cpp +++ b/librepomgr/buildactions/buildactionmeta.cpp @@ -35,7 +35,14 @@ BuildActionMetaInfo::BuildActionMetaInfo() .category = "Repo management", .name = "Remove packages", .type = "remove-packages", - .flags = {}, + .flags = { + BuildActionFlagMetaInfo{ + .id = static_cast(PackageMovementFlags::UseContainer), + .name = "Use container", + .desc = "Uses `makecontainerpkg` to invoke `repo-remove`; eliminates the need to having pacman on the host by using docker/podman instead", + .param = "use-container", + }, + }, .settings = {}, .directory = true, .sourceDb = false, @@ -48,6 +55,12 @@ BuildActionMetaInfo::BuildActionMetaInfo() .name = "Move packages", .type = "move-packages", .flags = { + BuildActionFlagMetaInfo{ + .id = static_cast(PackageMovementFlags::UseContainer), + .name = "Use container", + .desc = "Uses `makecontainerpkg` to invoke `repo-add`/`repo-remove`; eliminates the need to having pacman on the host by using docker/podman instead", + .param = "use-container", + }, BuildActionFlagMetaInfo{ .id = static_cast(MovePackagesFlags::IgnoreExistingFiles), .name = "Ignore existing files", diff --git a/librepomgr/buildactions/buildactionmeta.h b/librepomgr/buildactions/buildactionmeta.h index 0289cc2..0e5ef34 100644 --- a/librepomgr/buildactions/buildactionmeta.h +++ b/librepomgr/buildactions/buildactionmeta.h @@ -53,9 +53,17 @@ enum class BuildActionType : std::uint64_t { using BuildActionFlagType = std::uint64_t; constexpr BuildActionFlagType noBuildActionFlags = 0; +enum class PackageMovementFlags : BuildActionFlagType { + None, + UseContainer = (1 << 0), +}; +constexpr auto lastPackageMovementFlag = BuildActionFlagType(0); +enum class RemovePackagesFlags : BuildActionFlagType { + None, +}; enum class MovePackagesFlags : BuildActionFlagType { None, - IgnoreExistingFiles = (1 << 0), + IgnoreExistingFiles = (1 << (lastPackageMovementFlag + 1)), }; enum class CheckForUpdatesFlags : BuildActionFlagType { None, @@ -198,6 +206,7 @@ inline const BuildActionTypeMetaMapping &BuildActionMetaInfo::mappingForId(Build } // namespace LibRepoMgr +CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(LibRepoMgr, LibRepoMgr::PackageMovementFlags) CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(LibRepoMgr, LibRepoMgr::MovePackagesFlags) CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(LibRepoMgr, LibRepoMgr::ReloadDatabaseFlags) CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(LibRepoMgr, LibRepoMgr::ReloadLibraryDependenciesFlags) diff --git a/librepomgr/buildactions/buildactionprivate.h b/librepomgr/buildactions/buildactionprivate.h index 717703c..c73b38c 100644 --- a/librepomgr/buildactions/buildactionprivate.h +++ b/librepomgr/buildactions/buildactionprivate.h @@ -341,6 +341,8 @@ protected: std::vector> m_packageLocations; boost::filesystem::path m_repoRemovePath; boost::filesystem::path m_repoAddPath; + boost::filesystem::path m_makeContainerPkgPath; + bool m_useContainer; }; struct LIBREPOMGR_EXPORT RemovePackages : public PackageMovementAction { diff --git a/librepomgr/buildactions/repomanagement.cpp b/librepomgr/buildactions/repomanagement.cpp index fb94fdd..cf9b559 100644 --- a/librepomgr/buildactions/repomanagement.cpp +++ b/librepomgr/buildactions/repomanagement.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -33,26 +34,37 @@ PackageMovementAction::PackageMovementAction(ServiceSetup &setup, const std::sha bool PackageMovementAction::prepareRepoAction(RequiredDatabases requiredDatabases) { - // initialize build action + // validate and read parameter/settings auto configReadLock = init(BuildActionAccess::ReadConfig, requiredDatabases | RequiredDatabases::OneDestination, RequiredParameters::Packages); if (std::holds_alternative(configReadLock)) { return false; } - + const auto flags = static_cast(m_buildAction->flags); + m_useContainer = flags & PackageMovementFlags::UseContainer; auto setupLock = m_setup.lockToRead(); m_repoRemovePath = findExecutable(m_setup.building.repoRemovePath); if (requiredDatabases & RequiredDatabases::OneSource) { m_repoAddPath = findExecutable(m_setup.building.repoAddPath); } + if (m_useContainer) { + m_makeContainerPkgPath = findExecutable(m_setup.building.makeContainerPkgPath); + } // check executables - if (!checkExecutable(m_repoRemovePath)) { - reportError("Unable to find repo-remove executable \"" % m_setup.building.repoRemovePath + "\" in PATH."); - return false; - } - if (requiredDatabases & RequiredDatabases::OneSource && !checkExecutable(m_repoAddPath)) { - reportError("Unable to find repo-add executable \"" % m_setup.building.repoAddPath + "\" in PATH."); - return false; + if (m_useContainer) { + if (!checkExecutable(m_makeContainerPkgPath)) { + reportError("Unable to find makecontainerpkg executable \"" % m_setup.building.makeContainerPkgPath + "\" in PATH."); + return false; + } + } else { + if (!checkExecutable(m_repoRemovePath)) { + reportError("Unable to find repo-remove executable \"" % m_setup.building.repoRemovePath + "\" in PATH."); + return false; + } + if (requiredDatabases & RequiredDatabases::OneSource && !checkExecutable(m_repoAddPath)) { + reportError("Unable to find repo-add executable \"" % m_setup.building.repoAddPath + "\" in PATH."); + return false; + } } setupLock.unlock(); @@ -158,8 +170,14 @@ void RemovePackages::run() repoRemoveProcess = m_buildAction->makeBuildProcess("repo-remove", m_workingDirectory + "/repo-remove.log", std::bind(&RemovePackages::handleRepoRemoveResult, this, std::placeholders::_1, std::placeholders::_2))](UniqueLoggingLock &&lock) { repoRemoveProcess->locks().emplace_back(std::move(lock)); - repoRemoveProcess->launch( - boost::process::start_dir(m_destinationRepoDirectory), m_repoRemovePath, m_destinationDatabaseFile, m_result.processedPackages); + if (m_useContainer) { + repoRemoveProcess->launch(boost::process::start_dir(m_destinationRepoDirectory), + boost::process::env["PKGNAME"] = argsToString(m_buildAction->id), boost::process::env["TOOL"] = "repo-remove", + m_makeContainerPkgPath, "--", m_destinationDatabaseFile, m_result.processedPackages); + } else { + repoRemoveProcess->launch( + boost::process::start_dir(m_destinationRepoDirectory), m_repoRemovePath, m_destinationDatabaseFile, m_result.processedPackages); + } buildAction->log()(Phrases::InfoMessage, "Invoking repo-remove within \"", m_destinationRepoDirectory, "\" for \"", m_destinationDatabaseFile, "\", see logfile for details\n"); }); @@ -307,7 +325,13 @@ void MovePackages::run() std::bind(&MovePackages::handleRepoAddResult, this, processSession, std::placeholders::_1, std::placeholders::_2))]( UniqueLoggingLock &&lock) { repoAddProcess->locks().emplace_back(std::move(lock)); - repoAddProcess->launch(boost::process::start_dir(m_destinationRepoDirectory), m_repoAddPath, m_destinationDatabaseFile, m_fileNames); + if (m_useContainer) { + repoAddProcess->launch(boost::process::start_dir(m_destinationRepoDirectory), + boost::process::env["PKGNAME"] = argsToString(m_buildAction->id), boost::process::env["TOOL"] = "repo-add", + m_makeContainerPkgPath, "--", m_destinationDatabaseFile, m_fileNames); + } else { + repoAddProcess->launch(boost::process::start_dir(m_destinationRepoDirectory), m_repoAddPath, m_destinationDatabaseFile, m_fileNames); + } m_buildAction->log()(ps(Phrases::InfoMessage), "Invoking repo-add within \"", m_destinationRepoDirectory, "\" for \"", m_destinationDatabaseFile, "\", see logfile for details\n"); }); @@ -319,8 +343,14 @@ void MovePackages::run() std::bind(&MovePackages::handleRepoRemoveResult, this, processSession, std::placeholders::_1, std::placeholders::_2))]( UniqueLoggingLock &&lock) { repoRemoveProcess->locks().emplace_back(std::move(lock)); - repoRemoveProcess->launch( - boost::process::start_dir(m_sourceRepoDirectory), m_repoRemovePath, m_sourceDatabaseFile, m_result.processedPackages); + if (m_useContainer) { + repoRemoveProcess->launch(boost::process::start_dir(m_sourceRepoDirectory), + boost::process::env["PKGNAME"] = argsToString(m_buildAction->id), boost::process::env["TOOL"] = "repo-remove", + m_makeContainerPkgPath, "--", m_sourceDatabaseFile, m_result.processedPackages); + } else { + repoRemoveProcess->launch( + boost::process::start_dir(m_sourceRepoDirectory), m_repoRemovePath, m_sourceDatabaseFile, m_result.processedPackages); + } m_buildAction->log()(ps(Phrases::InfoMessage), "Invoking repo-remove within \"", m_sourceRepoDirectory, "\" for \"", m_sourceDatabaseFile, "\", see logfile for details\n"); });