From 4df5e6bb81c42fadf87f027ff120b2f5c9607b12 Mon Sep 17 00:00:00 2001 From: Natan Junges Date: Fri, 2 Dec 2022 16:53:24 +0000 Subject: [PATCH] Reference count sources list files so packages that share repositories work together nicely (#643) * Count references when repository already exists * Unify repositories of packages * Use /etc/deb-get/aptrepos to count references * Remove deb-get prefix from APT_LIST_NAME of chronograf, influxdb, influxdb2, influxdb2-cli, kapacitor, nomad, sublime-merge, sublime-text, telegraf, terraform and vagrant * Properly fetch count, properly remove entry, silence tee in remove_repo and add_apt_repo * Add command line option --remove-repo for remove and purge --- 01-main/packages/chronograf | 1 + 01-main/packages/influxdb | 1 + 01-main/packages/influxdb2 | 1 + 01-main/packages/influxdb2-cli | 1 + 01-main/packages/kapacitor | 1 + 01-main/packages/nomad | 1 + 01-main/packages/sublime-merge | 1 + 01-main/packages/sublime-text | 1 + 01-main/packages/telegraf | 1 + 01-main/packages/terraform | 1 + 01-main/packages/vagrant | 1 + README.md | 9 ++-- deb-get | 95 +++++++++++++++++++++++++++------- deb-get_completion | 18 ++++--- docs/deb-get.1 | 7 ++- docs/deb-get.1.md | 7 +-- 16 files changed, 114 insertions(+), 33 deletions(-) diff --git a/01-main/packages/chronograf b/01-main/packages/chronograf index 24f95b19d..c3fab5363 100644 --- a/01-main/packages/chronograf +++ b/01-main/packages/chronograf @@ -1,6 +1,7 @@ DEFVER=1 ARCHS_SUPPORTED="amd64 arm64 armhf" ASC_KEY_URL="https://repos.influxdata.com/influxdb.key" +APT_LIST_NAME="influxdata" APT_REPO_URL="https://repos.influxdata.com/debian stable main" PRETTY_NAME="Chronograf" WEBSITE="https://www.influxdata.com/time-series-platform/chronograf/" diff --git a/01-main/packages/influxdb b/01-main/packages/influxdb index 7279f3d51..2e507b7e9 100644 --- a/01-main/packages/influxdb +++ b/01-main/packages/influxdb @@ -1,6 +1,7 @@ DEFVER=1 ARCHS_SUPPORTED="amd64 arm64 armhf" ASC_KEY_URL="https://repos.influxdata.com/influxdb.key" +APT_LIST_NAME="influxdata" APT_REPO_URL="https://repos.influxdata.com/debian stable main" PRETTY_NAME="InfluxDB" WEBSITE="https://www.influxdata.com/products/influxdb-overview/" diff --git a/01-main/packages/influxdb2 b/01-main/packages/influxdb2 index ee7e93ea4..35040b21a 100644 --- a/01-main/packages/influxdb2 +++ b/01-main/packages/influxdb2 @@ -1,6 +1,7 @@ DEFVER=1 ARCHS_SUPPORTED="amd64 arm64" ASC_KEY_URL="https://repos.influxdata.com/influxdb.key" +APT_LIST_NAME="influxdata" APT_REPO_URL="https://repos.influxdata.com/debian stable main" PRETTY_NAME="InfluxDB2" WEBSITE="https://www.influxdata.com/products/influxdb-overview/" diff --git a/01-main/packages/influxdb2-cli b/01-main/packages/influxdb2-cli index a70c05998..8519087b8 100644 --- a/01-main/packages/influxdb2-cli +++ b/01-main/packages/influxdb2-cli @@ -1,6 +1,7 @@ DEFVER=1 ARCHS_SUPPORTED="amd64 arm64" ASC_KEY_URL="https://repos.influxdata.com/influxdb.key" +APT_LIST_NAME="influxdata" APT_REPO_URL="https://repos.influxdata.com/debian stable main" PRETTY_NAME="InfluxDB2 CLI" WEBSITE="https://www.influxdata.com/products/influxdb-overview/" diff --git a/01-main/packages/kapacitor b/01-main/packages/kapacitor index 480c2bc9d..671e3b130 100644 --- a/01-main/packages/kapacitor +++ b/01-main/packages/kapacitor @@ -1,6 +1,7 @@ DEFVER=1 ARCHS_SUPPORTED="amd64 arm64" ASC_KEY_URL="https://repos.influxdata.com/influxdb.key" +APT_LIST_NAME="influxdata" APT_REPO_URL="https://repos.influxdata.com/debian stable main" PRETTY_NAME="Kapacitor" WEBSITE="https://github.com/influxdata/kapacitor" diff --git a/01-main/packages/nomad b/01-main/packages/nomad index 05d568f6e..20e8d1661 100644 --- a/01-main/packages/nomad +++ b/01-main/packages/nomad @@ -1,5 +1,6 @@ DEFVER=1 ASC_KEY_URL="https://apt.releases.hashicorp.com/gpg" +APT_LIST_NAME="hashicorp" APT_REPO_URL="https://apt.releases.hashicorp.com ${UPSTREAM_CODENAME} main" APT_REPO_OPTIONS="arch=${HOST_ARCH}" PRETTY_NAME="Nomad" diff --git a/01-main/packages/sublime-merge b/01-main/packages/sublime-merge index 68aef2d75..c851d5b95 100644 --- a/01-main/packages/sublime-merge +++ b/01-main/packages/sublime-merge @@ -1,5 +1,6 @@ DEFVER=1 ASC_KEY_URL="https://download.sublimetext.com/sublimehq-pub.gpg" +APT_LIST_NAME="sublime-text" APT_REPO_URL="https://download.sublimetext.com/ apt/stable/" PRETTY_NAME="Sublime Merge" WEBSITE="https://www.sublimemerge.com/" diff --git a/01-main/packages/sublime-text b/01-main/packages/sublime-text index d4f84ee52..a561aee3b 100644 --- a/01-main/packages/sublime-text +++ b/01-main/packages/sublime-text @@ -1,5 +1,6 @@ DEFVER=1 ASC_KEY_URL="https://download.sublimetext.com/sublimehq-pub.gpg" +APT_LIST_NAME="sublime-text" APT_REPO_URL="https://download.sublimetext.com/ apt/stable/" PRETTY_NAME="Sublime Text" WEBSITE="https://www.sublimetext.com/" diff --git a/01-main/packages/telegraf b/01-main/packages/telegraf index e67ad444e..7dec6e4c7 100644 --- a/01-main/packages/telegraf +++ b/01-main/packages/telegraf @@ -1,6 +1,7 @@ DEFVER=1 ARCHS_SUPPORTED="amd64 arm64 armhf" ASC_KEY_URL="https://repos.influxdata.com/influxdb.key" +APT_LIST_NAME="influxdata" APT_REPO_URL="https://repos.influxdata.com/debian stable main" PRETTY_NAME="Telegraf" WEBSITE="https://github.com/influxdata/telegraf" diff --git a/01-main/packages/terraform b/01-main/packages/terraform index 85aad4b33..26f087dc2 100644 --- a/01-main/packages/terraform +++ b/01-main/packages/terraform @@ -1,5 +1,6 @@ DEFVER=1 ASC_KEY_URL="https://apt.releases.hashicorp.com/gpg" +APT_LIST_NAME="hashicorp" APT_REPO_URL="https://apt.releases.hashicorp.com ${UPSTREAM_CODENAME} main" APT_REPO_OPTIONS="arch=${HOST_ARCH}" PRETTY_NAME="Terraform" diff --git a/01-main/packages/vagrant b/01-main/packages/vagrant index d5d32724e..a9c6c99ca 100644 --- a/01-main/packages/vagrant +++ b/01-main/packages/vagrant @@ -1,6 +1,7 @@ DEFVER=1 CODENAMES_SUPPORTED="buster bullseye focal jammy" ASC_KEY_URL="https://apt.releases.hashicorp.com/gpg" +APT_LIST_NAME="hashicorp" APT_REPO_URL="https://apt.releases.hashicorp.com ${UPSTREAM_CODENAME} main" PRETTY_NAME="Vagrant" WEBSITE="https://www.vagrantup.com/" diff --git a/README.md b/README.md index 17f618b5f..531a3d66f 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,8 @@ cog.out(f"```\n{help}\n```") ``` deb-get {update [--repos-only] | upgrade | show | install - | reinstall | remove | purge + | reinstall | remove [--remove-repo] + | purge [--remove-repo] | search [--include-unsupported] | cache | clean | list [--include-unsupported] [--raw|--installed|--not-installed] | prettylist [] | csvlist [] | fix-installed [--old-apps] @@ -90,11 +91,13 @@ reinstall remove remove is identical to install except that packages are removed instead of - installed. + installed. When --remove-repo is provided, also remove the apt repository + of apt/ppa packages. purge purge is identical to remove except that packages are removed and purged - (any configuration files are deleted too). + (any configuration files are deleted too). When --remove-repo is provided, + also remove the apt repository of apt/ppa packages. clean clean clears out the local repository (/var/cache/deb-get) of retrieved diff --git a/deb-get b/deb-get index 5b55e3578..edbf9315c 100755 --- a/deb-get +++ b/deb-get @@ -20,7 +20,8 @@ cat < | install - | reinstall | remove | purge + | reinstall | remove [--remove-repo] + | purge [--remove-repo] | search [--include-unsupported] | cache | clean | list [--include-unsupported] [--raw|--installed|--not-installed] | prettylist [] | csvlist [] | fix-installed [--old-apps] @@ -50,11 +51,13 @@ reinstall remove remove is identical to install except that packages are removed instead of - installed. + installed. When --remove-repo is provided, also remove the apt repository + of apt/ppa packages. purge purge is identical to remove except that packages are removed and purged - (any configuration files are deleted too). + (any configuration files are deleted too). When --remove-repo is provided, + also remove the apt repository of apt/ppa packages. clean clean clears out the local repository (/var/cache/deb-get) of retrieved @@ -257,17 +260,17 @@ function install_apt() { ((PACKAGE_INSTALLATION_TRIES++)) add_apt_repo if ! update_only_repo; then - remove_repo + remove_repo --remove-repo --quiet return fi if ! package_is_installed "${APP}"; then if ! eula; then - remove_repo + remove_repo --remove-repo --quiet return fi if ! ${ELEVATE} apt-get -q=2 -o Dpkg::Progress-Fancy="1" -y install "${APP}"; then - remove_repo + remove_repo --remove-repo --quiet return fi add_installed @@ -374,7 +377,7 @@ function remove_deb() { fi ;; apt|ppa) - remove_repo;; + remove_repo "${3}";; esac } @@ -1067,7 +1070,7 @@ function fix_installed() { if [ "${DEFVER}" != "${OLD_DEFVER}" ]; then remove_installed "${APP}" if [[ " apt ppa " =~ " ${OLD_METHOD} " ]]; then - remove_repo + remove_repo --remove-repo fi if [ "${METHOD}" = apt ]; then add_apt_repo @@ -1100,22 +1103,64 @@ function remove_old_ppa_repo() { } function remove_repo() { - fancy_message info "Removing /usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" - ${ELEVATE} rm -f "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" - fancy_message info "Removing /etc/apt/sources.list.d/${APT_LIST_NAME}.list" - ${ELEVATE} rm -f "/etc/apt/sources.list.d/${APT_LIST_NAME}.list" + local count="" + if [ -e "${ETC_DIR}/aptrepos" ]; then + count="$(grep -m 1 "^${APT_LIST_NAME} " "${ETC_DIR}/aptrepos" | cut -d " " -f 2)" + fi + if [ -z "${count}" ]; then + count=0 + fi + if [ "${count}" -gt 0 ]; then + ((count--)) + ${ELEVATE} sed -i -E "/^${APT_LIST_NAME} [0-9]+/d" "${ETC_DIR}/aptrepos" + echo "${APT_LIST_NAME} ${count}" | ${ELEVATE} tee -a "${ETC_DIR}/aptrepos" > /dev/null + fi + if [ "${1}" == --remove-repo ]; then + if [ "${count}" -eq 0 ]; then + if [ "${2}" != --quiet ]; then + fancy_message info "Removing /usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" + fi + ${ELEVATE} rm -f "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" + if [ "${2}" != --quiet ]; then + fancy_message info "Removing /etc/apt/sources.list.d/${APT_LIST_NAME}.list" + fi + ${ELEVATE} rm -f "/etc/apt/sources.list.d/${APT_LIST_NAME}.list" + if [ -e "${ETC_DIR}/aptrepos" ]; then + ${ELEVATE} sed -i -E "/^${APT_LIST_NAME} [0-9]+/d" "${ETC_DIR}/aptrepos" + fi + elif [ "${2}" != --quiet ]; then + fancy_message warn "/etc/apt/sources.list.d/${APT_LIST_NAME}.list was not removed because other packages depend on it." + fi + fi } function add_apt_repo() { + local count="" + if [ -e "${ETC_DIR}/aptrepos" ]; then + count="$(grep -m 1 "^${APT_LIST_NAME} " "${ETC_DIR}/aptrepos" | cut -d " " -f 2)" + fi + if [ -z "${count}" ]; then + count=0 + fi + if [ "${count}" -eq 0 ] && [ -e "/etc/apt/sources.list.d/${APT_LIST_NAME}.list" ]; then + ((count++)) + fi + ((count++)) + if [ -e "${ETC_DIR}/aptrepos" ]; then + ${ELEVATE} sed -i -E "/^${APT_LIST_NAME} [0-9]+/d" "${ETC_DIR}/aptrepos" + fi + echo "${APT_LIST_NAME} ${count}" | ${ELEVATE} tee -a "${ETC_DIR}/aptrepos" > /dev/null if [ ! -d /usr/share/keyrings ]; then ${ELEVATE} mkdir -p /usr/share/keyrings 2>/dev/null fi - if [ -n "${ASC_KEY_URL}" ]; then - ${ELEVATE} wget -q "${ASC_KEY_URL}" -O "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" - ${ELEVATE} gpg --yes --dearmor "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" - ${ELEVATE} rm "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" - else #GPG_KEY_URL - ${ELEVATE} wget -q "${GPG_KEY_URL}" -O "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" + if [ ! -e "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" ]; then + if [ -n "${ASC_KEY_URL}" ]; then + ${ELEVATE} wget -q "${ASC_KEY_URL}" -O "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" + ${ELEVATE} gpg --yes --dearmor "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" + ${ELEVATE} rm "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring" + else #GPG_KEY_URL + ${ELEVATE} wget -q "${GPG_KEY_URL}" -O "/usr/share/keyrings/${APT_LIST_NAME}-archive-keyring.gpg" + fi fi #TODO: https://superuser.com/questions/1641291/gpg-only-download-a-key-from-a-keyserver @@ -1395,6 +1440,11 @@ case "${ACTION}" in csvlist_debs "${1}";; purge) elevate_privs + opt_remove_repo="" + if [ "${1}" == --remove-repo ]; then + opt_remove_repo=--remove-repo + shift + fi for APP in "${@,,}"; do FULL_APP="$(IFS=$'\n'; echo "${APPS[*]}" | grep -m 1 "/${APP}$")" if [ -z "${FULL_APP}" ]; then @@ -1407,10 +1457,15 @@ case "${ACTION}" in exit 1 fi validate_deb "${FULL_APP}" - remove_deb "${APP}" purge + remove_deb "${APP}" purge "${opt_remove_repo}" done;; remove) elevate_privs + opt_remove_repo="" + if [ "${1}" == --remove-repo ]; then + opt_remove_repo=--remove-repo + shift + fi for APP in "${@,,}"; do FULL_APP="$(IFS=$'\n'; echo "${APPS[*]}" | grep -m 1 "/${APP}$")" if [ -z "${FULL_APP}" ]; then @@ -1423,7 +1478,7 @@ case "${ACTION}" in exit 1 fi validate_deb "${FULL_APP}" - remove_deb "${APP}" + remove_deb "${APP}" "" "${opt_remove_repo}" done;; search) if [ "${1}" == --include-unsupported ]; then diff --git a/deb-get_completion b/deb-get_completion index 544259216..098da839c 100644 --- a/deb-get_completion +++ b/deb-get_completion @@ -9,15 +9,21 @@ function _deb-get() { if [ "${COMP_CWORD}" = 2 ] && [ "${command}" = update ]; then COMPREPLY=($(compgen -W "--repos-only" "\\${COMP_WORDS[2]}")) elif [ "${command}" = show ]; then - COMPREPLY=($(compgen -W "$(./deb-get list --include-unsupported --raw | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) + COMPREPLY=($(compgen -W "$(deb-get list --include-unsupported --raw | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) elif [ "${COMP_CWORD}" = 2 ] && [ "${command}" = search ]; then - COMPREPLY=($(compgen -W "--include-unsupported $(./deb-get list --raw | tr "\n" " ")" "\\${COMP_WORDS[${COMP_CWORD}]}")) + COMPREPLY=($(compgen -W "--include-unsupported $(deb-get list --raw | tr "\n" " ")" "\\${COMP_WORDS[${COMP_CWORD}]}")) elif [ "${COMP_CWORD}" = 3 ] && [ "${command}" = search ] && [ "${COMP_WORDS[2]}" = --include-unsupported ]; then - COMPREPLY=($(compgen -W "$(./deb-get list --include-unsupported --raw | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) + COMPREPLY=($(compgen -W "$(deb-get list --include-unsupported --raw | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) elif [ "${command}" = install ]; then - COMPREPLY=($(compgen -W "$(./deb-get list --not-installed | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) - elif [[ " reinstall remove purge " =~ " ${command} " ]]; then - COMPREPLY=($(compgen -W "$(./deb-get list --installed | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) + COMPREPLY=($(compgen -W "$(deb-get list --not-installed | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) + elif [ "${command}" = reinstall ]; then + COMPREPLY=($(compgen -W "$(deb-get list --installed | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) + elif [[ " remove purge " =~ " ${command} " ]]; then + if [ "${COMP_CWORD}" = 2 ]; then + COMPREPLY=($(compgen -W "--remove-repo $(deb-get list --installed | tr "\n" " ")" "\\${COMP_WORDS[2]}")) + else + COMPREPLY=($(compgen -W "$(deb-get list --installed | tr "\n" " ")" "${COMP_WORDS[${COMP_CWORD}]}")) + fi elif [ "${command}" = list ]; then COMPREPLY=($(compgen -W "--include-unsupported --raw --installed --not-installed" "\\${COMP_WORDS[${COMP_CWORD}]}")) elif [ "${COMP_CWORD}" = 2 ] && [[ " pretty_list prettylist csv_list csvlist csv " =~ " ${command} " ]]; then diff --git a/docs/deb-get.1 b/docs/deb-get.1 index a86e11a12..2143784d8 100644 --- a/docs/deb-get.1 +++ b/docs/deb-get.1 @@ -26,7 +26,8 @@ deb-get - An installation manager for 3rd-Party deb packages .nf \f[C] deb-get {update [--repos-only] | upgrade | show | install - | reinstall | remove | purge + | reinstall | remove [--remove-repo] + | purge [--remove-repo] | search [--include-unsupported] | cache | clean | list [--include-unsupported] [--raw|--installed|--not-installed] | prettylist [] | csvlist [] | fix-installed [--old-apps] @@ -62,10 +63,14 @@ packages) desired for reinstallation. \f[B]remove\f[R] remove is identical to install except that packages are removed instead of installed. +When \[en]remove-repo is provided, also remove the apt repository of +apt/ppa packages. .TP \f[B]purge\f[R] purge is identical to remove except that packages are removed and purged (any configuration files are deleted too). +When \[en]remove-repo is provided, also remove the apt repository of +apt/ppa packages. .TP \f[B]clean\f[R] clean clears out the local repository (/var/cache/deb-get) of retrieved diff --git a/docs/deb-get.1.md b/docs/deb-get.1.md index 714bd1bdd..fce7c550b 100644 --- a/docs/deb-get.1.md +++ b/docs/deb-get.1.md @@ -17,7 +17,8 @@ deb-get - An installation manager for 3rd-Party deb packages ``` deb-get {update [--repos-only] | upgrade | show | install - | reinstall | remove | purge + | reinstall | remove [--remove-repo] + | purge [--remove-repo] | search [--include-unsupported] | cache | clean | list [--include-unsupported] [--raw|--installed|--not-installed] | prettylist [] | csvlist [] | fix-installed [--old-apps] @@ -45,10 +46,10 @@ repositories or via direct download. : reinstall is followed by one package (or a space-separated list of packages) desired for reinstallation. **remove** -: remove is identical to install except that packages are removed instead of installed. +: remove is identical to install except that packages are removed instead of installed. When --remove-repo is provided, also remove the apt repository of apt/ppa packages. **purge** -: purge is identical to remove except that packages are removed and purged (any configuration files are deleted too). +: purge is identical to remove except that packages are removed and purged (any configuration files are deleted too). When --remove-repo is provided, also remove the apt repository of apt/ppa packages. **clean** : clean clears out the local repository (/var/cache/deb-get) of retrieved package files.