helpers 2.1: simplify the apt extra repo clusterfuck / unused args etc

This commit is contained in:
Alexandre Aubin 2024-05-27 21:47:32 +02:00
parent 422e02244d
commit 1b03058858

View file

@ -339,13 +339,13 @@ EOF
#
# [packagingv1]
#
# usage: ynh_add_app_dependencies --package=phpversion [--replace]
# usage: ynh_add_app_dependencies --package=phpversion
# | arg: -p, --package= - Packages to add as dependencies for the app.
#
# Requires YunoHost version 3.8.1 or higher.
ynh_add_app_dependencies() {
# ============ Argument parsing =============
local -A args_array=([p]=package= [r]=replace)
local -A args_array=([p]=package=)
local package
ynh_handle_getopts_args "$@"
# ===========================================
@ -392,31 +392,23 @@ ynh_remove_app_dependencies() {
#
# [packagingv1]
#
# usage: ynh_install_extra_app_dependencies --repo="repo" --package="dep1 dep2" [--key=key_url] [--name=name]
# usage: ynh_install_extra_app_dependencies --repo="repo" --package="dep1 dep2" --key=key_url
# | arg: -r, --repo= - Complete url of the extra repository.
# | arg: -p, --package= - The packages to install from this extra repository
# | arg: -k, --key= - url to get the public key.
# | arg: -n, --name= - Name for the files for this repo, $app as default value.
#
# Requires YunoHost version 3.8.1 or higher.
ynh_install_extra_app_dependencies() {
# ============ Argument parsing =============
local -A args_array=([r]=repo= [p]=package= [k]=key= [n]=name=)
local -A args_array=([r]=repo= [p]=package= [k]=key=)
local repo
local package
local key
local name
ynh_handle_getopts_args "$@"
name="${name:-$app}"
key=${key:-}
# ===========================================
# Set a key only if asked
if [ -n "$key" ]; then
key="--key=$key"
fi
# Add an extra repository for those packages
ynh_install_extra_repo --repo="$repo" $key --priority=995 --name=$name
ynh_install_extra_repo --repo="$repo" --key=$key
# Install requested dependencies from this extra repository.
ynh_install_app_dependencies "$package"
@ -428,76 +420,53 @@ ynh_install_extra_app_dependencies() {
[ -z "$apps_auto_installed" ] || apt-mark auto $apps_auto_installed
# Remove this extra repository after packages are installed
ynh_remove_extra_repo --name=$name
ynh_remove_extra_repo
}
# Add an extra repository correctly, pin it and get the key.
#
# [internal]
#
# usage: ynh_install_extra_repo --repo="repo" [--key=key_url] [--priority=priority_value] [--name=name] [--append]
# usage: ynh_install_extra_repo --repo="repo" [--key=key_url]
# | arg: -r, --repo= - Complete url of the extra repository.
# | arg: -k, --key= - url to get the public key.
# | arg: -p, --priority= - Priority for the pin
# | arg: -n, --name= - Name for the files for this repo, $app as default value.
# | arg: -a, --append - Do not overwrite existing files.
#
# Requires YunoHost version 3.8.1 or higher.
ynh_install_extra_repo() {
# ============ Argument parsing =============
local -A args_array=([r]=repo= [k]=key= [p]=priority= [n]=name= [a]=append)
local -A args_array=([r]=repo= [k]=key=)
local repo
local key
local priority
local name
local append
ynh_handle_getopts_args "$@"
name="${name:-$app}"
append=${append:-0}
key=${key:-}
priority=${priority:-}
# ===========================================
if [ $append -eq 1 ]; then
append="--append"
wget_append="tee --append"
else
append=""
wget_append="tee"
fi
# Split the repository into uri, suite and components.
# Remove "deb " at the beginning of the repo.
repo="${repo#deb }"
# Get the uri
local uri="$(echo "$repo" | awk '{ print $1 }')"
# Get the suite
local suite="$(echo "$repo" | awk '{ print $2 }')"
# Get the components
local component="${repo##$uri $suite }"
# Add the repository into sources.list.d
ynh_add_repo --uri="$uri" --suite="$suite" --component="$component" --name="$name" $append
# Add the new repo in sources.list.d
mkdir --parents "/etc/apt/sources.list.d"
echo "deb $uri $suite $component" > "/etc/apt/sources.list.d/$app.list"
# Pin the new repo with the default priority, so it won't be used for upgrades.
# Build $pin from the uri without http and any sub path
local pin="${uri#*://}"
pin="${pin%%/*}"
# Set a priority only if asked
if [ -n "$priority" ]; then
priority="--priority=$priority"
fi
ynh_pin_repo --package="*" --pin="origin \"$pin\"" $priority --name="$name" $append
# Pin repository
mkdir --parents "/etc/apt/preferences.d"
cat << EOF > "/etc/apt/preferences.d/$app"
Package: *
Pin: origin $pin
Pin-Priority: 995
EOF
# Get the public key for the repo
if [ -n "$key" ]; then
mkdir --parents "/etc/apt/trusted.gpg.d"
# Timeout option is here to enforce the timeout on dns query and tcp connect (c.f. man wget)
wget --timeout 900 --quiet "$key" --output-document=- | gpg --dearmor | $wget_append /etc/apt/trusted.gpg.d/$name.gpg >/dev/null
fi
mkdir --parents "/etc/apt/trusted.gpg.d"
# Timeout option is here to enforce the timeout on dns query and tcp connect (c.f. man wget)
wget --timeout 900 --quiet "$key" --output-document=- | gpg --dearmor | tee /etc/apt/trusted.gpg.d/$name.gpg >/dev/null
# Update the list of package with the new repo
ynh_package_update
@ -507,117 +476,15 @@ ynh_install_extra_repo() {
#
# [internal]
#
# usage: ynh_remove_extra_repo [--name=name]
# | arg: -n, --name= - Name for the files for this repo, $app as default value.
# usage: ynh_remove_extra_repo
#
# Requires YunoHost version 3.8.1 or higher.
ynh_remove_extra_repo() {
# ============ Argument parsing =============
local -A args_array=([n]=name=)
local name
ynh_handle_getopts_args "$@"
name="${name:-$app}"
# ===========================================
ynh_secure_remove --file="/etc/apt/sources.list.d/$name.list"
# Sury pinning is managed by the regenconf in the core...
[[ "$name" == "extra_php_version" ]] || ynh_secure_remove --file="/etc/apt/preferences.d/$name"
if [ -e /etc/apt/trusted.gpg.d/$name.gpg ]; then
ynh_secure_remove --file="/etc/apt/trusted.gpg.d/$name.gpg"
ynh_secure_remove --file="/etc/apt/sources.list.d/$app.list"
ynh_secure_remove --file="/etc/apt/preferences.d/$app"
if [ -e /etc/apt/trusted.gpg.d/$app.gpg ]; then
ynh_secure_remove --file="/etc/apt/trusted.gpg.d/$app.gpg"
fi
# (Do we even create a .asc file anywhere ...?)
if [ -e /etc/apt/trusted.gpg.d/$name.asc ]; then
ynh_secure_remove --file="/etc/apt/trusted.gpg.d/$name.asc"
fi
# Update the list of package to exclude the old repo
ynh_package_update
}
# Add a repository.
#
# [internal]
#
# usage: ynh_add_repo --uri=uri --suite=suite --component=component [--name=name] [--append]
# | arg: -u, --uri= - Uri of the repository.
# | arg: -s, --suite= - Suite of the repository.
# | arg: -c, --component= - Component of the repository.
# | arg: -n, --name= - Name for the files for this repo, $app as default value.
# | arg: -a, --append - Do not overwrite existing files.
#
# Example for a repo like deb http://forge.yunohost.org/debian/ stretch stable
# uri suite component
# ynh_add_repo --uri=http://forge.yunohost.org/debian/ --suite=stretch --component=stable
#
# Requires YunoHost version 3.8.1 or higher.
ynh_add_repo() {
# ============ Argument parsing =============
local -A args_array=([u]=uri= [s]=suite= [c]=component= [n]=name= [a]=append)
local uri
local suite
local component
local name
local append
ynh_handle_getopts_args "$@"
name="${name:-$app}"
append=${append:-0}
# ===========================================
if [ $append -eq 1 ]; then
append="tee --append"
else
append="tee"
fi
mkdir --parents "/etc/apt/sources.list.d"
# Add the new repo in sources.list.d
echo "deb $uri $suite $component" \
| $append "/etc/apt/sources.list.d/$name.list"
}
# Pin a repository.
#
# [internal]
#
# usage: ynh_pin_repo --package=packages --pin=pin_filter [--priority=priority_value] [--name=name] [--append]
# | arg: -p, --package= - Packages concerned by the pin. Or all, *.
# | arg: -i, --pin= - Filter for the pin.
# | arg: -p, --priority= - Priority for the pin
# | arg: -n, --name= - Name for the files for this repo, $app as default value.
# | arg: -a, --append - Do not overwrite existing files.
#
# See https://manpages.debian.org/stretch/apt/apt_preferences.5.en.html#How_APT_Interprets_Priorities for information about pinning.
#
# Requires YunoHost version 3.8.1 or higher.
ynh_pin_repo() {
# ============ Argument parsing =============
local -A args_array=([p]=package= [i]=pin= [r]=priority= [n]=name= [a]=append)
local package
local pin
local priority
local name
local append
ynh_handle_getopts_args "$@"
package="${package:-*}"
priority=${priority:-50}
name="${name:-$app}"
append=${append:-0}
# ===========================================
if [ $append -eq 1 ]; then
append="tee --append"
else
append="tee"
fi
# Sury pinning is managed by the regenconf in the core...
[[ "$name" != "extra_php_version" ]] || return 0
mkdir --parents "/etc/apt/preferences.d"
echo "Package: $package
Pin: $pin
Pin-Priority: $priority
" \
| $append "/etc/apt/preferences.d/$name"
}