Skip to content

Commit

Permalink
api: switch to deb822 format for add_external_repo and `rm_external…
Browse files Browse the repository at this point in the history
…_repo`

also extend `remove_repofile_if_unused` to be able to read .sources files in addition to .list files
  • Loading branch information
theofficialgman committed Jan 1, 2024
1 parent a1367e5 commit cb85f59
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
79 changes: 55 additions & 24 deletions api
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,16 @@ remove_repofile_if_unused() { #Given a sources.list.d file, delete it if nothing
[ -z "$file" ] && error "remove_repo_if_unused: no sources.list.d file specified!"
#return now if the list file does not exist
[ -f "$file" ] || return 0

#determine what repo-urls are in the file
local urls="$(cat "$file" | grep -v '^#' | tr ' ' '\n' | grep '://')"


if [ "${file##*.}" == "list" ]; then
#determine what repo-urls are in the file
local urls="$(cat "$file" | grep -v '^#' | tr ' ' '\n' | grep '://')"
elif [ "${file##*.}" == "sources" ]; then
#determine what repo-urls are in the file
local urls="$(cat "$file" | grep -v '^#' | grep '^URIs: ' | sed 's/URIs: //g')"
else
error "$file was not of apt list or sources type"
fi
#there could be multiple urls in one file. Check each url and set the in_use variable to 1 if any packages are found
local IFS=$'\n'
local in_use=0
Expand All @@ -225,7 +231,7 @@ remove_repofile_if_unused() { #Given a sources.list.d file, delete it if nothing
if [ "$in_use" == 0 ] && [ "$testmode" == test ];then
echo "The given repository is not in use and can be deleted:"$'\n'"$file" 1>&2
elif [ "$in_use" == 0 ];then
status "Removing the $(basename "$file" | sed 's/.list$//g') repo as it is not being used"
status "Removing the $(basename "$file" | sed 's/.list$//g' | sed 's/.sources$//g') repo as it is not being used"
sudo rm -f "$file"
[ -f "$key" ] && sudo rm -f "$key" || true
fi
Expand Down Expand Up @@ -704,19 +710,25 @@ debian_ppa_installer() { #setup a PPA on a Debian distro. Arguments: ppa_name di
}

add_external_repo() { # add an external apt repo and its gpg key.
# follows https://wiki.debian.org/DebianRepository/UseThirdParty specification with one-line-style format https://repolib.readthedocs.io/en/latest/deb822-format.html#one-line-format
# follows https://wiki.debian.org/DebianRepository/UseThirdParty specification with deb822 format https://repolib.readthedocs.io/en/latest/deb822-format.html
# required inputs
local reponame="$1"
# "URI SUITE COMPONENT"
local repopath="$2"
local pubkeyurl="$3"

# check if all needed vars are set
[ -z "$repopath" ] && error "add_external_repo: repopath not set"
local pubkeyurl="$2"
local uris="$3"
local suites="$4"
# potentially optional inputs
# components is not used when suite is an absolute path
local components="$5"
# additional options can be specified as the 6th, 7th, 8th, etc argument (eg: "Architectures: arm64")

# check if all needed vars are set
[ -z "$reponame" ] && error "add_external_repo: reponame not set"
[ -z "$uris" ] && error "add_external_repo: uris not set"
[ -z "$suites" ] && error "add_external_repo: suites not set"
[ -z "$pubkeyurl" ] && error "add_external_repo: pubkeyurl not set"

# exit if reponame contains space, since apt doesn't accept .list files with spaces in filename or keyname.
if [[ $reponame = *" "* ]]; then
# exit if reponame or uri or suite contains space
if [[ $reponame = *" "* ]] || [[ $uris = *" "* ]] || [[ $suites = *" "* ]]; then
error "add_external_repo: provided reponame contains a space."
fi

Expand All @@ -733,23 +745,37 @@ add_external_repo() { # add an external apt repo and its gpg key.
sudo rm -f /etc/apt/sources.list.d/${reponame}.list || error "add_external_repo: failed to remove conflicting .list file."
fi

# check if .sources file already exists
if [ -f /etc/apt/sources.list.d/${reponame}.sources ]; then
sudo rm -f /etc/apt/sources.list.d/${reponame}.sources || error "add_external_repo: failed to remove conflicting .sources file."
fi

# download gpg key from specified url
if [ -f /usr/share/keyrings/${reponame}-archive-keyring.gpg ]; then
sudo rm -f /usr/share/keyrings/${reponame}-archive-keyring.gpg
fi
wget -qO- "$pubkeyurl" | sudo gpg --dearmor -o /usr/share/keyrings/${reponame}-archive-keyring.gpg

if [ $? != 0 ];then
sudo rm -f /etc/apt/sources.list.d/${reponame}.list
sudo rm -f /etc/apt/sources.list.d/${reponame}.sources
sudo rm -f /usr/share/keyrings/${reponame}-archive-keyring.gpg
error "add_external_repo: download from specified pubkeyurl failed."
fi

# create .list file
echo "deb [signed-by=/usr/share/keyrings/${reponame}-archive-keyring.gpg] $repopath" | sudo tee /etc/apt/sources.list.d/${reponame}.list >/dev/null
# create .sources file
echo "Types: deb
URIs: $uris
Suites: $suites" | sudo tee /etc/apt/sources.list.d/${reponame}.sources >/dev/null
if [ ! -z "$components" ]; then
echo "Components: $components" | sudo tee -a /etc/apt/sources.list.d/${reponame}.sources >/dev/null
fi
for input in "${@: 6}"; do
echo "$input" | sudo tee -a /etc/apt/sources.list.d/${reponame}.sources >/dev/null
done
echo "Signed-By: /usr/share/keyrings/${reponame}-archive-keyring.gpg" | sudo tee -a /etc/apt/sources.list.d/${reponame}.sources >/dev/null

if [ $? != 0 ];then
sudo rm -f /etc/apt/sources.list.d/${reponame}.list
sudo rm -f /etc/apt/sources.list.d/${reponame}.sources
sudo rm -f /usr/share/keyrings/${reponame}-archive-keyring.gpg
error "add_external_repo: failed to create ${reponame}.list file"
fi
Expand All @@ -767,27 +793,32 @@ rm_external_repo() { # remove an external apt repo and its gpg key, if the repo
error "rm_external_repo: provided reponame contains a space."
fi

# exit gracefully if .list file does not exist
[ -f "/etc/apt/sources.list.d/$reponame.list" ] || exit 0
# always remove deprecated .list file if present
if [ -f /etc/apt/sources.list.d/${reponame}.list ]; then
sudo rm -f /etc/apt/sources.list.d/${reponame}.list
fi

# exit gracefully if .sources file does not exist
[ -f "/etc/apt/sources.list.d/$reponame.sources" ] || exit 0

if [ "$force" == force ]; then
rm -f /usr/share/keyrings/${reponame}-archive-keyring.gpg || error "rm_external_repo: removal of ${reponame}-archive-keyring.gpg failed"
sudo rm -f /etc/apt/sources.list.d/${reponame}.list || error "rm_external_repo: removal of ${reponame}.list failed"
sudo rm -f /etc/apt/sources.list.d/${reponame}.sources || error "rm_external_repo: removal of ${reponame}.sources failed"
exit 0
fi

remove_repofile_if_unused /etc/apt/sources.list.d/${reponame}.list "" /usr/share/keyrings/${reponame}-archive-keyring.gpg
remove_repofile_if_unused /etc/apt/sources.list.d/${reponame}.sources "" /usr/share/keyrings/${reponame}-archive-keyring.gpg
}

adoptium_installer() {
case "$__os_codename" in
bionic | focal | jammy | buster | bullseye | bookworm)
add_external_repo "adoptium" "https://adoptium.jfrog.io/artifactory/deb $__os_codename main" "https://adoptium.jfrog.io/artifactory/api/security/keypair/default-gpg-key/public" || exit 1
add_external_repo "adoptium" "https://adoptium.jfrog.io/artifactory/api/security/keypair/default-gpg-key/public" "https://adoptium.jfrog.io/artifactory/deb" "$__os_codename" "main" || exit 1
;;
*)
# use bionic target name explicitly for any other OS as its the oldest LTS target adoptium continues to support
# all supported adoptium OSs use the same debs so the target specified does not actually matter
add_external_repo "adoptium" "https://adoptium.jfrog.io/artifactory/deb bionic main" "https://adoptium.jfrog.io/artifactory/api/security/keypair/default-gpg-key/public" || exit 1
add_external_repo "adoptium" "https://adoptium.jfrog.io/artifactory/api/security/keypair/default-gpg-key/public" "https://adoptium.jfrog.io/artifactory/deb" "bionic" "main" || exit 1
;;
esac
apt_update
Expand Down
2 changes: 1 addition & 1 deletion apps/Box64/install-64
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if dpkg -l box64 &>/dev/null ;then
sudo apt purge -y --allow-change-held-packages box64*
fi

add_external_repo "box64" "https://Pi-Apps-Coders.github.io/box64-debs/debian ./" "https://pi-apps-coders.github.io/box64-debs/KEY.gpg" || exit 1
add_external_repo "box64" "https://pi-apps-coders.github.io/box64-debs/KEY.gpg" "https://Pi-Apps-Coders.github.io/box64-debs/debian" "./" || exit 1

apt_update
if [ $? != 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion apps/Box86/install-32
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if dpkg -l box86 &>/dev/null ;then
sudo apt purge -y --allow-change-held-packages box86*
fi

add_external_repo "box86" "https://Pi-Apps-Coders.github.io/box86-debs/debian ./" "https://pi-apps-coders.github.io/box86-debs/KEY.gpg" || exit 1
add_external_repo "box86" "https://pi-apps-coders.github.io/box86-debs/KEY.gpg" "https://Pi-Apps-Coders.github.io/box86-debs/debian" "./" || exit 1

apt_update
if [ $? != 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion apps/Box86/install-64
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ install_packages libc6:armhf libstdc++6:armhf \
libgssapi-krb5-2:armhf libkrb5-3:armhf \
$rpi_arm_userspace $mesa_va_drivers libegl1:armhf libglx-mesa0:armhf libgl1:armhf libgles2:armhf

add_external_repo "box86" "https://Pi-Apps-Coders.github.io/box86-debs/debian ./" "https://pi-apps-coders.github.io/box86-debs/KEY.gpg" || exit 1
add_external_repo "box86" "https://pi-apps-coders.github.io/box86-debs/KEY.gpg" "https://Pi-Apps-Coders.github.io/box86-debs/debian" "./" || exit 1

apt_update
if [ $? != 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion apps/Pale Moon/install-64
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#remove deprecated files
sudo rm -f /etc/apt/sources.list.d/home:stevenpusser.list /etc/apt/trusted.gpg.d/home_stevenpusser.gpg

add_external_repo "stevenpusser" "http://download.opensuse.org/repositories/home:/stevenpusser/Debian_10/ /" "https://download.opensuse.org/repositories/home:stevenpusser/Debian_10/Release.key" || exit 1
add_external_repo "stevenpusser" "https://download.opensuse.org/repositories/home:stevenpusser/Debian_10/Release.key" "http://download.opensuse.org/repositories/home:/stevenpusser/Debian_10/" "/" || exit 1

apt_update
if [ $? != 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion apps/Sublime Text/install-64
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#remove deprecated files
sudo rm -f /etc/apt/trusted.gpg.d/sublimehq.asc /etc/apt/sources.list.d/sublime-text.list

add_external_repo "sublimehq" "https://download.sublimetext.com/ apt/stable/" "https://download.sublimetext.com/sublimehq-pub.gpg" || exit 1
add_external_repo "sublimehq" "https://download.sublimetext.com/sublimehq-pub.gpg" "https://download.sublimetext.com/" "apt/stable/" || exit 1

apt_update
if [ $? != 0 ]; then
Expand Down

0 comments on commit cb85f59

Please sign in to comment.