From e3fa7ae9aebf78e012fd8c41b5846e8210ed3e97 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Sun, 2 Feb 2025 12:05:39 -0500 Subject: [PATCH] Start work on making this a nushell module --- modules/dnf/dnf.nu | 75 +++++++++++++++++++ modules/dnf/dnf.tsp | 171 ++++++++++++++++++++++++-------------------- 2 files changed, 170 insertions(+), 76 deletions(-) create mode 100644 modules/dnf/dnf.nu diff --git a/modules/dnf/dnf.nu b/modules/dnf/dnf.nu new file mode 100644 index 00000000..298dc431 --- /dev/null +++ b/modules/dnf/dnf.nu @@ -0,0 +1,75 @@ +#!/usr/bin/evn nu + +def add_repos [$repos: list]: nothing -> nothing { + if $repos | is-not-empty { + print $'(ansi green)Adding repositories(ansi reset)' + + for $repo in $repos { + # Substitute %OS_VERSION% & remove newlines/whitespaces from all repo entries + let repo = $repo | str replace '%OS_VERSION%' $env.OS_VERSION | str trim + + let repo = if ($repo | str starts-with 'https://') or ($repo | str starts-with 'http://') { + print $"Adding repository URL: (ansi cyan)'${repo}'(ansi reset)" + $repo + } else if ($repo | str ends-with '.repo') and ($'($envCONFIG_DIRECTORY)/dnf/($repo)' | path exists) { + print $"Adding repository file: (ansi cyan)'($repo)'(ansi reset)" + $'($env.CONFIG_DIRECTORY)/dnf/($repo)' + } else { + return (error make { + msg: $"(ansi red)Urecognized repo (ansi cyan)'($repo)'(ansi reset)" + label: { + span: (metadata $repo).span + text: 'Found in config' + } + }) + } + + try { + ^dnf -y config-manager addrepo --from-repofile $'($repo)' + } + } + } +} + +def add_coprs [$copr_repos: list]: nothing -> nothing { + if $copr_repos | is-not-empty { + print $'(ansi green)Adding COPR repositories(ansi reset)' + + for $copr in $copr_repos { + let is_copr = ($copr | split row | length) == 2 + + if not $is_copr { + return (error make { + msg: $"(ansi red)The string '(ansi cyan)($copr)(ansi red)' is not recognized as a COPR repo(ansi reset)" + label: { + span: (metadata $is_copr).span + text: 'Checks if string is a COPR repo' + } + }) + } + + print $"Adding COPR repository: (ansi cyan)'($copr)'(ansi reset)" + try { + ^dnf -y copr enable $copr + } + } + } +} + +def main [config: string]: nothing -> nothing { + let config = $config | from json + let has_dnf5 = ^rpm -q dnf5 | complete + + if has_dnf5.exit_code != 0 { + return (error make { + msg: $"(ansi red)ERROR: Main dependency '(ansi cyan)dnf5(ansi red)' is not installed. Install '(ansi cyan)dnf5(ansi red)' before using this module to solve this error.(ansi reset)" + label: { + span: (metadata $has_dn5).span + text: 'Checks for dnf5' + } + }) + } + + add_repos ($config | default [] repos | select repos) + add_coprs ($config | default [] copr | select copr) +} diff --git a/modules/dnf/dnf.tsp b/modules/dnf/dnf.tsp index 349cafcc..e372a2a3 100644 --- a/modules/dnf/dnf.tsp +++ b/modules/dnf/dnf.tsp @@ -1,80 +1,99 @@ import "@typespec/json-schema"; using TypeSpec.JsonSchema; -@jsonSchema("/modules/dnf.json") -model DnfModule { - /** The dnf module offers pseudo-declarative package and repository management using dnf. - * https://blue-build.org/reference/modules/dnf/ - */ - type: "dnf"; - - /** List of links to .repo files to download into /etc/yum.repos.d/. */ - repos?: Array; - - /** List of COPR project repos to download into /etc/yum.repos.d/. */ - copr?: Array; - - /** List of links to key files to import for installing from custom repositories. */ - keys?: Array; - - /** List of folder names under /opt/ to enable for installing into. */ - optfix?: Array; - - /** Configuration of RPM groups removal. */ - "group-remove"?: { - /** List of RPM groups to remove. */ - packages: Array; - }; - - /** Configuration of RPM groups install. */ - "group-install"?: { - /** List of RPM groups to install. */ - packages: Array, - /** Whether to install weak dependencies during the RPM group install or not. */ - "install-weak-dependencies"?: boolean = true, - /** Whether to continue with the RPM group install if there are no packages available in the repository. */ - "skip-unavailable-packages"?: boolean = false, - /** Whether to continue with the RPM group install if there are broken packages. */ - "skip-broken-packages"?: boolean = false, - /** Whether to allow erasing (removal) of packages in case of dependency problems during the RPM group install. */ - "allow-erasing-packages"?: boolean = false; - }; - - /** Configuration of RPM packages removal. */ - "remove"?: { - /** List of RPM packages to remove. */ - packages: Array, - /** Whether to remove unused dependencies during removal operation. */ - "remove-unused-dependencies"?: boolean = true; - }; - - /** Configuration of RPM packages install. */ - "install"?: { - /** List of RPM packages to install. */ - packages: Array, - /** Whether to install weak dependencies during the RPM package install or not. */ - "install-weak-dependencies"?: boolean = true, - /** Whether to continue with the RPM package install if there are no packages available in the repository. */ - "skip-unavailable-packages"?: boolean = false, - /** Whether to continue with the RPM package install if there are broken packages. */ - "skip-broken-packages"?: boolean = false, - /** Whether to allow erasing (removal) of packages in case of dependency problems during the RPM package install. */ - "allow-erasing-packages"?: boolean = false; - }; - - /** List of configurations for replacing packages from another repo. */ - replace?: Array<{ - /** URL to the source COPR repo for the new packages. */ - "from-repo": string, - /** List of packages to replace using packages from the defined repo. */ - packages: Array, - /** Whether to install weak dependencies during the replacement or not. */ - "install-weak-dependencies"?: boolean = true, - /** Whether to continue with the replacement if there are no packages available on the system to replace. */ - "skip-unavailable-packages"?: boolean = false, - /** Whether to continue with the replacement if there are broken packages in the system during the replacement. */ - "skip-broken-packages"?: boolean = false, - /** Whether to allow erasing (removal) of packages in case of dependency problems during the replacement. */ - "allow-erasing-packages"?: boolean = false; - }>; +@jsonSchema("/modules/dnf-latest.json") +model DnfModuleLatest { + ...DnfModuleV1; +} + +@jsonSchema("/modules/dnf-v1.json") +model DnfModuleV1 { + /** The dnf module offers pseudo-declarative package and repository management using dnf. + * https://blue-build.org/reference/modules/dnf/ + */ + type: "dnf"; + + /** List of links to .repo files to download into /etc/yum.repos.d/. */ + repos?: Array; + + /** List of COPR project repos to download into /etc/yum.repos.d/. */ + copr?: Array; + + /** List of links to key files to import for installing from custom repositories. */ + keys?: Array; + + /** List of folder names under /opt/ to enable for installing into. */ + optfix?: Array; + + /** Configuration of RPM groups removal. */ + `group-remove`?: { + /** List of RPM groups to remove. */ + packages: Array; + }; + + /** Configuration of RPM groups install. */ + `group-install`?: { + /** List of RPM groups to install. */ + packages: Array; + + /** Whether to install weak dependencies during the RPM group install or not. */ + `install-weak-dependencies`?: boolean = true; + + /** Whether to continue with the RPM group install if there are no packages available in the repository. */ + `skip-unavailable-packages`?: boolean = false; + + /** Whether to continue with the RPM group install if there are broken packages. */ + `skip-broken-packages`?: boolean = false; + + /** Whether to allow erasing (removal) of packages in case of dependency problems during the RPM group install. */ + `allow-erasing-packages`?: boolean = false; + }; + + /** Configuration of RPM packages removal. */ + remove?: { + /** List of RPM packages to remove. */ + packages: Array; + + /** Whether to remove unused dependencies during removal operation. */ + `remove-unused-dependencies`?: boolean = true; + }; + + /** Configuration of RPM packages install. */ + install?: { + /** List of RPM packages to install. */ + packages: Array; + + /** Whether to install weak dependencies during the RPM package install or not. */ + `install-weak-dependencies`?: boolean = true; + + /** Whether to continue with the RPM package install if there are no packages available in the repository. */ + `skip-unavailable-packages`?: boolean = false; + + /** Whether to continue with the RPM package install if there are broken packages. */ + `skip-broken-packages`?: boolean = false; + + /** Whether to allow erasing (removal) of packages in case of dependency problems during the RPM package install. */ + `allow-erasing-packages`?: boolean = false; + }; + + /** List of configurations for replacing packages from another repo. */ + replace?: Array<{ + /** URL to the source COPR repo for the new packages. */ + `from-repo`: string; + + /** List of packages to replace using packages from the defined repo. */ + packages: Array; + + /** Whether to install weak dependencies during the replacement or not. */ + `install-weak-dependencies`?: boolean = true; + + /** Whether to continue with the replacement if there are no packages available on the system to replace. */ + `skip-unavailable-packages`?: boolean = false; + + /** Whether to continue with the replacement if there are broken packages in the system during the replacement. */ + `skip-broken-packages`?: boolean = false; + + /** Whether to allow erasing (removal) of packages in case of dependency problems during the replacement. */ + `allow-erasing-packages`?: boolean = false; + }>; }