Skip to content

Instantly share code, notes, and snippets.

@bohanyang

bohanyang/repo.sh

Created Apr 1, 2021
Embed
What would you like to do?
#!/bin/sh
# shellcheck shell=dash
set -eu
os_release=
add() {
local repo
repo=$1
shift
if [ -z "$os_release" ]; then
os_release=$(. /etc/os-release; echo "$ID $VERSION_CODENAME $VERSION_ID")
fi
# shellcheck disable=SC2086
"_$repo" $os_release "$@"
apt update
}
fetch() {
wget -O "/etc/apt/trusted.gpg.d/$1" "$2"
}
save() {
echo "$2" > "/etc/apt/sources.list.d/$1.list"
}
_docker() {
fetch docker.asc "https://download.docker.com/linux/$1/gpg"
save docker "deb https://download.docker.com/linux/$1 $2 stable"
}
_sury_dpa() {
fetch sury.gpg https://packages.sury.org/php/apt.gpg
save "sury-$4" "deb https://packages.sury.org/$4 $2 main"
cat > /etc/apt/preferences.d/50-sury << 'EOF'
Package: openssl
Pin: origin "packages.sury.org"
Pin-Priority: 100
Package: libzip4
Pin: origin "packages.sury.org"
Pin-Priority: 500
Package: lib*
Pin: origin "packages.sury.org"
Pin-Priority: 100
EOF
}
_sury_ppa() {
fetch sury_ppa.asc 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x4f4ea0aae5267a6c'
save "sury-$4" "deb http://ppa.launchpad.net/ondrej/$4/ubuntu $2 main"
cat > "/etc/apt/preferences.d/50-sury-$4" << EOF
Package: openssl
Pin: release o=LP-PPA-ondrej-$4
Pin-Priority: 100
Package: libzip4
Pin: release o=LP-PPA-ondrej-$4
Pin-Priority: 500
Package: lib*
Pin: release o=LP-PPA-ondrej-$4
Pin-Priority: 100
EOF
}
_sury() {
if [ "$1" = ubuntu ]; then
_sury_ppa "$@"
elif [ "$1" = debian ]; then
_sury_dpa "$@"
fi
}
_mysql() {
fetch mysql.asc https://repo.mysql.com/RPM-GPG-KEY-mysql
save "mysql-$4" "deb https://repo.mysql.com/apt/$1 $2 mysql-$4"
}
_mariadb() {
fetch mariadb.asc https://mariadb.org/mariadb_release_signing_key.asc
save "mariadb-$4" "deb https://ftp.osuosl.org/pub/mariadb/repo/$4/$1 $2 main"
}
_postgresql() {
fetch postgresql.asc https://www.postgresql.org/media/keys/ACCC4CF8.asc
save postgresql "deb https://apt.postgresql.org/pub/repos/apt $2-pgdg main"
}
_microsoft() {
fetch microsoft.asc https://packages.microsoft.com/keys/microsoft.asc
save microsoft "deb https://packages.microsoft.com/$1/$3/prod $2 main"
}
_azure_cli() {
fetch microsoft.asc https://packages.microsoft.com/keys/microsoft.asc
save azure-cli "deb https://packages.microsoft.com/repos/azure-cli $2 main"
}
_mongodb() {
fetch "mongodb-$4.asc" "https://www.mongodb.org/static/pgp/server-$4.asc"
save "mongodb-$4" "deb https://repo.mongodb.org/apt/$1 $2/mongodb-org/$4 main"
}
_nginx() {
fetch nginx.asc https://nginx.org/keys/nginx_signing.key
save nginx "deb https://nginx.org/packages/mainline/$1 $2 nginx"
}
_sb_nginx() {
fetch sb_nginx.asc https://mirrors.xtom.com/sb/nginx/public.key
save sb-nginx "deb https://mirrors.xtom.com/sb/nginx $2 main"
}
_haproxy() {
fetch haproxy.asc https://haproxy.debian.net/bernat.debian.org.gpg
save "haproxy-$4" "deb https://haproxy.debian.net $2-backports-$4 main"
cat > /etc/apt/preferences.d/50-haproxy << 'EOF'
Package: haproxy haproxy-doc vim-haproxy
Pin: release o=PPA-haproxy
Pin-Priority: 500
EOF
}
_caddy() {
fetch caddy.asc https://dl.cloudsmith.io/public/caddy/stable/gpg.key
save caddy "deb https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main"
}
if [ "$(id -u)" -ne 0 ]; then
echo 'This script needs to be run as root.' 1>&2
exit 1
fi
if [ $# -gt 0 ]; then
add "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment