Skip to content

Commit

Permalink
api: update/correct add_external_repo and rm_external_repo to fol…
Browse files Browse the repository at this point in the history
…low debian spec and pi-apps api use
  • Loading branch information
theofficialgman committed Dec 28, 2023
1 parent a96c478 commit 8b08d49
Showing 1 changed file with 21 additions and 38 deletions.
59 changes: 21 additions & 38 deletions api
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,28 @@ app_to_pkgname() { #given an app-name, convert it to a unique, valid package-nam
echo "pi-apps-$(echo "$app" | md5sum | cut -c1-8 | awk '{print $1}')"
}

add_external_repo() { # add an external apt repo and its gpg key
repopath="$1"
reponame="$2"
pubkeyurl="$3"
add_external_repo() { # add an external apt repo and its gpg key. follows https://wiki.debian.org/DebianRepository/UseThirdParty specification
local reponame="$1"
# "URI SUITE COMPONENT"
local repopath="$2"
local pubkeyurl="$5"

# check if all needed vars are set
[ -z "$repopath" ] && error "add_external_packages: repopath not set"
[ -z "$reponame" ] && error "add_external_packages: reponame not set"
[ -z "$pubkeyurl" ] && error "add_external_packages: pubkeyurl not set"
[ -z "$repopath" ] && error "add_external_repo: repopath not set"
[ -z "$reponame" ] && error "add_external_repo: reponame 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
error "add_external_repo: provided reponame contains a space."
fi

# check if links are valid
wget -q --spider "$pubkeyurl" || error "add_external_packages: pubkeyurl isn't a valid link"
wget -q --spider "$pubkeyurl" || error "add_external_repo: pubkeyurl isn't a valid link"

# make pi-apps key storage dir if it doesn't exist
# make apt keyring directory if it doesn't exist
if [ ! -d /usr/share/keyrings ]; then
sudo mkdir -p /usr/share/keyrings || error "add_external_repo: failed to create pi-apps key storage dir."
sudo mkdir -p /usr/share/keyrings || error "add_external_repo: failed to create apt keyring directory."
fi

# check if .list file already exists
Expand All @@ -398,32 +399,31 @@ add_external_repo() { # add an external apt repo and its gpg key
fi

# download gpg key from specified url
if [ -f /usr/share/keyrings/${reponame-keyring.gpg} ]; then
sudo rm -f /usr/share/keyrings/${reponame}-keyring.gpg
if [ -f /usr/share/keyrings/${reponame}-archive-keyring.gpg ]; then
sudo rm -f /usr/share/keyrings/${reponame}-archive-keyring.gpg
fi
curl -fsSL "$pubkeyurl" | sudo gpg --dearmor -o /usr/share/keyrings/${reponame}-keyring.gpg
curl -fsSL "$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 /usr/share/keyrings/${reponame}-keyring.gpg
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 $repopath" | sudo tee /etc/apt/sources.list.d/${reponame}.list >/dev/null
echo "deb [signed-by=/usr/share/keyrings/${reponame}-archive-keyring.gpg] $repopath" | sudo tee /etc/apt/sources.list.d/${reponame}.list >/dev/null

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

rm_external_repo() { # remove an external apt repo and its gpg key, if the repo is no longer in use. (force-remove the repo with force argument)
# part of this function is modified from the`remove_repofile_if_unused` function.

reponame="$1"
force="$2"
local reponame="$1"
local force="$2"

[ -z "$reponame" ] && error "rm_external_repo: reponame not provided"

Expand All @@ -436,29 +436,12 @@ rm_external_repo() { # remove an external apt repo and its gpg key, if the repo
[ -f "/etc/apt/sources.list.d/$reponame.list" ] || exit 0

if [ "$force" == force ]; then
rm -f /usr/share/keyrings/${reponame}-keyring.gpg || error "rm_external_repo: removal of ${reponame}-keyring.gpg failed"
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"
exit 0
fi

# determine what repo-urls are in the file
local urls="$(cat "/etc/apt/sources.list.d/$reponame.list" | grep -v '^#' | tr ' ' '\n' | grep '://')"

# 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
local url
for url in $urls ; do
if anything_installed_from_repo "$url" >/dev/null; then
in_use=1
break
fi
done

if [ "$in_use" == 0 ]; then
status "Removing the $reponame repo as it is not being used"
sudo rm -f "/etc/apt/sources.list.d/$reponame.list"
fi
remove_repofile_if_unused /etc/apt/sources.list.d/${reponame}.list "" /usr/share/keyrings/${reponame}-archive-keyring.gpg
}

install_packages() { #Make some packages dependencies of the $app app. Package-names, regex, filenames, and urls are supported.
Expand Down

0 comments on commit 8b08d49

Please sign in to comment.